Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Project 1

Tachometer
Digital Tachometer
A tachometer is a device that is commonly used to measure the speed
of an engine crankshaft in rotational speed in revolutions per minute
(rpm). The speed is indicated on a dashboard gauge and is used to
help drivers determine how fast they are driving.

12/5/2012
CNA1015
Photo Interrupters
Transmissive Photosensors
Microcontroller Programming by C++

#include <htc.h>

# include "pic.h"

#include "lcd.h"

#define IR_Tx RA0

#define _XTAL_FREQ 4000000

__CONFIG(XT & WDTDIS & PWRTDIS & BORDIS & LVPDIS & UNPROTECT & MCLREN);

unsigned short i, DD0, DD1, DD2, DD3;

unsigned int RPM;

char lookup(int numb);

void wait();
void main() {

TRISB = 0b00000000; // Set PORTB direction to be output

TRISA = 0b00110000; // Set PORTA direction to be output

PORTB = 0x00; // Turn OFF LEDs on PORTB

CMCON = 7 ; // Disable comparators

RPM = 0; // Initial Value of Counter

OPTION= 0b00111000; // TOCS=1 for Counter mode, PSA=1 for 1:1

char ch1, ch2, ch3, ch4;

IR_Tx = 0; // Turn OFF IR

lcd_init();

lcd_clear();

lcd_goto(0);

lcd_puts(" Speed: ");

while(1){

TMR0=0;

IR_Tx = 1;

wait();

RPM = TMR0*60;

IR_Tx = 0;

DD0 = RPM%10;

// DD0 = mask(DD0);

RPM = RPM/10;

DD1 = RPM%10;

//DD1 = mask(DD1);
RPM = RPM/10;

DD2 = RPM%10;

//DD2 = mask(DD2);

DD3 = RPM/10;

//DD3 = mask(DD3);

ch1 = lookup(DD3); // converting into char

ch2 = lookup(DD2);

ch3 = lookup(DD1);

ch4 = lookup(DD0);

lcd_goto(0x40); //2nd line

lcd_putch(ch1); // display the char on lcd

lcd_putch(ch2);

lcd_putch(ch3);

lcd_putch(ch4);

lcd_puts(" RPM");

void wait(){

for(int z=0; z<10; z++)

__delay_ms(100); // Delay 1 Sec

char lookup(int numb) {

char cha;

switch (numb) {
case 0: cha = '0';

break;

case 1: cha = '1';

break;

case 2: cha = '2';

break;

case 3: cha = '3';

break;

case 4: cha = '4';

break;

case 5: cha = '5';

break;

case 6: cha = '6';

break;

case 7: cha = '7';

break;

case 8: cha = '8';

break;

case 9: cha = '9';

break;

return cha;

You might also like