make rpm and also get velocity use two timer one as counter and other as timer.

Thread Starter

huida__huida

Joined Apr 18, 2014
7
HI Ineed to make rpm and also get velocity use two timer one as counter and other as timer. iam trying but stuck here need help so as to repair this
C:
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
  int pulse=0;
int speed;
unsigned int RPM;
int puls;
//variables
unsigned int tmr1_Val=0; //stores timer1 value
char tmr1_txt[8];  //sotres timer1 value converted to string


//initiallizations
void _init()
{
  //pin B0 as output
  trisb.b0=0;
  portb.b0=0;

  //initiallize LCD
  lcd_init();
  Lcd_Cmd(_LCD_CURSOR_OFF);
  Lcd_Cmd(_LCD_CLEAR);
    void InitTimer0(){
  OPTION_REG         = 0x86;
  TMR0                 = 100;
  INTCON         = 0xA0;
}


  //TIMER1 CONTROL REGISTER
  //prescalar 1:1
  T1CKPS1_bit = 0;
  T1CKPS0_bit = 0;

  TMR1CS_bit = 1; //External clock from pin T1CKI (on the rising edge)

  T1SYNC_bit = 1; //Do not synchronize external clock input with internal phase clcok

  TMR1ON_bit = 1; //Enables Timer1

  TMR1IE_bit = 1; //Enables the TMR1 overflow interrupt

  //INTCON REGISTER
  PEIE_bit = 1; //Enables all unmasked peripheral interrupts
  GIE_bit = 1; //enable global interrupts

  //initial values to Timer1 Registers
  tmr1l = 0;
  tmr1h = 0;

}


//ISR
void interrupt()
{
  if(TMR1IF_bit == 1) //if Timer1 overflowed
  {
    TMR1IF_bit = 0; //reset timer1 interrupt flag
    rpm++
   
  }
  if(TMR0IF_bit==1){
  TMR0IF_bit=0:
  pulse++;
puls=52*pulse;
  }
}

#define SW PORTD.RD0

void main()
{
   _init(); //configurations

   for(;;)
   {
     tmr1_Val = (TMR1H << 8) + TMR1L; //get high and low bytes from timer1
     WordToStr(tmr1_Val, tmr1_txt); //convert timer1 value to string
     lcd_out(2,1, tmr1_txt);   //display timer1 value on LCD
   }
   TRISD.f0=1;
   TRISD=0x00;
  PORTD=0;
puls=pulse;
rpm=puls*60/1;
speed=52/60;
   while(1){
if(SW==1){
                        while(SW==1);
                        pulse++;

                        }


  if(pulse>=721) pulse  = 0;
              if(RD0_bit==1) pulse ++;
              if(pulse==pulse <=128){

                 portd.f5=0;

                 }
                 if (pulse>= 129 && pulse <=180){
                 portd.f2=1;

                 }
                 if (pulse>=181&& pulse <=308){
                 portd.f2=0;
                 portd.f3=0;
                 portd.f4=0 ;
                  }
                 if (pulse>=309 && pulse <=360){
                 portd.f2=0;
                  portd.f3=1 ;
                 portd.f4=0;
                 portd.f5=0;

                 }if (pulse>=361 && pulse <=488){
                 portd.f2=0;
                  portd.f3=0 ;
                 portd.f4=0;
                 portd.f5=0;

                 }
                if (pulse>=489 && pulse <=540){
                 portd.f2=0;
                  portd.f3=0 ;
                 portd.f4=1;
                 portd.f5=0;

                 }
               if (pulse>=541 && pulse <=668){
                 portd.f2=0;
                  portd.f3=0 ;
                 portd.f4=0;
                 portd.f5=0;

                 }
             if (pulse>=669 && pulse <=720){
                 portd.f2=0;
                  portd.f3=0;
                 portd.f4=0;
                 portd.f5=1;

                 }






}

}
Moderators note : Please use code tags for pieces of code
 

jpanhalt

Joined Jan 18, 2008
11,087
What microcontroller are you using? Why do you need two Timer1's? Your code, so far as I can tell, only references one Timer1.

Are the RPM and velocity related or independent. For example, is RPM wheel , transmission, or engine speed and velocity is vehicle speed?
What are your inputs for speed and rpm?

Most important, please tell us what problem are you trying to solve, not what your planned solution is.
 
Top