C Program (using pic 16f877a) to
calculate no. of Pulses per second (rectangle waveform), which is given as
input to the in-built counter.
Application: Con tactless Tachometer
What the below code does ?
IT COUNTS THE NO. OF HIGH TO LOW PULSES
PER SECOND
I.E. IT CALCULATES REVOLUTIONS PER SECOND AS PER THE
ALGORITHM IMPLEMENTED (RPS).
SO, RPS* 60 = RPM.
RESOLUTION OF THIS TACHOMETER = 60 RPM
C program
#include<htc.h>
#include<stdio.h>
#define rs RC0
#define rw RC1
#define en RC2
#define lcd PORTD
void lcd_ini();
void lcd_cmd(unsigned char cmd);
void lcd_data(unsigned char data);
void lcd_str(unsigned char *str);
void delay();
void main()
{
unsigned int i,j;
char str1[5],str2[5];
unsigned int value,value1;
CLRWDT();
OPTION=0X38;
//TOCS=1 AND TOSE=1 AND PSA=0
TRISC=0X00;
TRISD=0X00;
TRISA=0Xff;
INTCON=0X20;
//TMR0IE=1
lcd_ini();
while(1)
{
lcd_cmd(0x80);
lcd_str("RPS");
TMR0=0;
//count=0
INTCON=0X20;
//enable timer/counter 0
for(i=0;i<=5000;i++)
//1 sec delay
{
for(j=0;j<=40;j++);
}
value=TMR0;
value1=TMR0*60;
lcd_cmd(0x88);
sprintf(str1,"%d",value);
lcd_str(str1);
lcd_str(" ");
lcd_cmd(0xC0);
lcd_str("RPM");
lcd_cmd(0xC8);
sprintf(str2,"%d",value1);
lcd_str(str2);
lcd_str(" ");
if(INTCON&&0X04)
INTCON=INTCON&0XFB; //clear
timer0 flag
}
}
void lcd_ini()
{
lcd_cmd(0x38);
delay();
lcd_cmd(0x0E);
delay();
lcd_cmd(0x01);
delay();
lcd_cmd(0x06);
delay();
}
void lcd_cmd(unsigned char cmd)
{
lcd=cmd;
rs=0;
rw=0;
en=1;
delay();
en=0;
}
void lcd_data(unsigned char data)
{
lcd=data;
rs=1;
rw=0;
en=1;
delay();
en=0;
}
void delay()
{
unsigned int i=5000;
while(i--);
}
void lcd_str(unsigned char *str)
{
unsigned char i=0;
while(str[i]!=0)
lcd_data(str[i++]);
}
Proteus Simulated design:
< click on the image for enlarged view >