Thursday, 30 July 2015

RPM MEASUREMENT USING PIC18

SIMPLE RPM MEASUREMENT USING TIMER 0 OF PIC MICRO-CONTROLLER 



Description

PIC micro-controllers have most important future like Timers & Counter.

They can be used to perform a variety of time precision functions, such as generating events at specific times, measuring the duration of an event, keeping date and time record, counting events, etc.

The main component of a timer module is a free running binary counter that increments for each incoming pulse. Since it runs independently, it can count pulses concurrently with the main program execution.

A PIC18F4431 micro-controller has built-in hardware timer modules: Timer 0 , Timer 1 , Timer 2 And Timer 5 .

Theory

The Timer 0 module in a PIC18F4431 micro-controller is an 8-bit  or 16 - bit synchronous counter that stores the value for the counter in the special function register called T0CON.
First of all Crystel of PIC microcontroller  i have use external but also use internal to create accurate delay.
In  RPM measurement is most important is take sample and refresh LCD in 1 Second.
So , TIMER 0 count the input signal in 1 Second and and that's time timer 0 value is loaded in Any int variable to multiplies 60  given the RPM. below is the equation Of RPM.

              1 Hz =  60 RPM

Initialize Appropriate PORT of PIC18F4431 . Setting Parameter of Timer and Initialize LCD and Hardware. And Bingooooo. project Work . Line by Line Code and Circuit Diagram is Below.

Circuit Diagram
     Below Is the Circuit Diagram Of RPM meter . i have designed in Proteus V. 7.0. you have simply transform for you version.


.As in my Project I have a use Opto Senser to read input via on interrupt pin Of micro-controller. below is the opto-Sensor.




Now Every Thing Is About Coding. Simple Count the sample of  opto sensor in  1 second And display result on LCD.

Micro C Code

First of all Setting of Configuration Window Tab on Micro c Pro Editor.
Please  Press Ctrl + Shift + E to open Below Tab and appropriate Setting.




Now Below Is the Code.

unsigned long RPM_Value;

// Define LCD module connections.
 sbit LCD_RS at RB3_bit;
 sbit LCD_EN at RB2_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 TRISB3_bit;
 sbit LCD_EN_Direction at TRISB2_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 connection definition

// Define Messages
char message1[] = "RPM Meter";
char *RPM = "00000 RPM";

void Display_RPM(unsigned long num)
{
       RPM[0] = num/10000 + 48;
       RPM[1] = (num/1000)%10 + 48;
       RPM[2] = (num/100)%10 + 48;
       RPM[3] = (num/10)%10 + 48;
       RPM[4] = num%10 + 48;
       Lcd_Out(2,4,RPM);
}

void main()
 {
       int i = 1;
      
      TRISD = 0x00;                                     // PORTD as a output.
      PORTC = 0x00;
      TRISC = 0b00001000;                          // RC3 as a input of Sensor
      T0CON = 0b01101000;                         // TMR0 as 16-bit counter
      Lcd_Init();                                              // Initialize LCD
      Lcd_Cmd(_LCD_CLEAR);                  // CLEAR display
      Lcd_Cmd(_LCD_CURSOR_OFF);     // Cursor off
      Lcd_Out(1,4,message1);                       // Write message1 in 1st row
      while(1)
     {

        // Below Loop Rotate 4 time means 0.25 x 4 = 1 Second Delay loop
         for(i = 0 ; i < 4 ; i++)
        {
            T0CON.TMR0ON = 1;         // Start the timer
            TMR0L = 0;                           // Initialize Default load timer high and low byte to 0.   
            TMR0H = 0;
            Delay_ms(250);                      // Wait for 0.25 sec
            T0CON.TMR0ON = 0;         // Stop the timer
            RPM_Value += (256*TMR0H + TMR0L);
        }

       RPM_Value = RPM_Value * 60;    //  Multiply by 60 to get RPM
       Display_RPM(RPM_Value);           //  Display The RPM 
       RPM_Value = 0;                               //   RPM_Value to 0 to clear Variable.
     };             // Infinite Loop
}

You have also created 1 Sec Delay using Micro C Default library. like below code use

 while(1)
     {
            T0CON.TMR0ON = 1;    // Start the timer
            TMR0L = 0;                      // Initialize Default load timer high and low byte to 0.   
            TMR0H = 0;
            Delay_ms(1000);               // Wait for 1 sec
            T0CON.TMR0ON = 0;    // Stop the timer
            RPM_Value = (256*TMR0H + TMR0L);
        }

     Now Complete Source File and Schematic is Below Find Attachment and use your any pic and see what happened with your device.


Download File
https://sites.google.com/site/micronikunjp703/rpmmeter/pic18f4431rpmcounter.rar?attredirects=0&d=1