Header Ads

Make Digital Speed Meter use PIC Microcontroller with code

Welcome to MINA TECHNOLOGY.

In this tutorial we will learn how to make Speed Meter use PIC Microcontroller. The Speed Meter measure the Wheel rotation and it convert to velocity. The display show the value of velocity. Here I am use PIC16F877A Microcontroller and Mikro C compiler for code editing. You can watch the video or read the written tutorial below.




The Speed Meter use measure the velocity of vehicles. It convert the velocity in Kilometer Per Hour(Km/h). 60Km/h it means the  vechicle beyond 60Km in per hour. The Speed Meter measure the Wheel rotation and calculation the velocity.

Calculation:
Assume,
              radius r = 2 cm
              circumference = 2rpai
                                      =  2*2*3.1416            // pai value = 3.1416
                                       =12.5664 cm


If the Wheel rotation is 1 than it beyond 12.57cm. When the Wheel one rotation per second than the velocity of Wheel is 12.57cm/s. If I conver the value in per hour the velocity is  (3600*12.57)/100000 =  0.4525Km/h.

Calculation value:

When ,
             The Wheel one rotation than IR sensor generate 4 pulses. The Timer0 module count those pulses. If Timer0 module count 8 pulses than the Wheel rotate 2(8/4)  rotation. The  MCU measure the pulse timer.

Time1 module generate 250ms delay interrupt. but we need 1second delay interrupt. Here I am occur 4interrrut at time. Total it generate 250*4 = 1second delay interrupt. The MCU measure Timer0 value at 1second and than clear the Timer0 module.

The IR Sensor count the rotation and it convert in pulses.

I am use here Timer Calculator for generate 250ms delay interrupt.



Circuit Diagram:
circuit diagram of Speed Meter



Mikro C Code:

// Lcd module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
//end lcd module connections
#define IR PORTD.RD7                             // RD7 pin is pulse input pin
int rotation=0;
int rpm = 0;
char text[15];
int i = 0;
int speed = 0;
//Timer1
//Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 250 ms

void Interrupt(){
  if (TMR1IF_bit){
    TMR1IF_bit = 0;
    TMR1H         = 0x0B;
    TMR1L         = 0xDC;
    i++;

  }
}

void main() {

     Lcd_Init();                                                              // Initialize the Lcd module
     Lcd_Cmd(_LCD_CLEAR);                                     // Clear Lcd display
     Lcd_Cmd(_LCD_CURSOR_OFF);                             // Lcd Cursor off
     Lcd_Out(1,3,"Speed Meter");
     delay_ms(2000);                                                          // 2 second delay
     Lcd_Cmd(_LCD_CLEAR);
     Lcd_Out(1,3,"DEVELOPED BY");
     Lcd_Out(2,1,"MINA TECHNOLOGY");
     delay_ms(2000);
     Lcd_Cmd(_LCD_CLEAR);
     T1CON         = 0x31;
     TMR1IF_bit         = 0;
     TMR1H         = 0x0B;                                                     // Reload the TMR1 Register
     TMR1L         = 0xDC;
     TMR1IE_bit         = 1;
     INTCON         = 0xC0;
     OPTION_REG.T0CS = 1 ;                                         // Timer0 use as a counter
     OPTION_REG.T0SE = 0;                           //Increment on low-to-high  transition on the TMR0 pin
     TMR0 = 0;                                                   // Clear the TMR0 Register
     Lcd_Out(1,5,"Speed:");
     while(1){
               TMR0 =0;                                                 // Clear the TMR0 Register
              i=0;                                                       // clear i
              while(i !=4);                                                    // 1s delay
              rotation = TMR0;
              rotation = rotation / 4 ;                         // 4 rotation 1 turn
              speed = rotation * 4.52 ;                        // caculation
              intTostr(speed,text);                                  //  rpm = (4/2)*60 = 120
              Lcd_Out(2,1,text);
              Lcd_Out(2,7,"Km/h");

              }
}


Click the download button for source code:
download








No comments

Theme images by 5ugarless. Powered by Blogger.