Hi there,
My code below is actually to capture pulse per second that generated from the motor's encoder and display the pulse per second and rpm on LCD module. However I couldn't hold the value and display on the LCD. In my code I created a 1sec interrupt using timer0 with 4Mhz frequency. I have no idea how to modify my code so it just interrupt for 1 sec and hold the value. I also found a problem here which is when I only display 1 line on the LCD the value for the pulse is getting around 33-39. However when I display 2 lines on LCD the value for the pulse is half of (33-39). How can I solve it? Could it better if I use 8Mhz?? When I on the motor the programming actually does the while loop, but i could not hold and clear the value, let say the value is 18 or 19, and it keeps update every 1 sec, when I stop the motor the value stop and without return or clear to 0. How can I clear it?
#include <htc.h>
#include <stdlib.h>
#include <string.h>
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & BORDIS & LVPDIS);
#define RS RE0
#define RW RE1
#define EN RE2
unsigned int reload=15;
unsigned int rpm;
unsigned int s, k, w, a, b, c, y;
//LCD instruction function
void instruction(char C)
{
PORTE = 0x04; //RS =1 , W/R = 0 , E = 0
PORTD = C;
PORTE = 0x00;
for (s=0;s<70;s++){}
}
// LCD display function
void lcd (char C)
{
PORTE = 0x05; //RS =1 , W/R = 0 , E = 1
PORTD = C;
PORTE = 0x01; //RS =0 , W/R = 0 , E = 1
PORTE = 0x01; //RS =0 , W/R = 0 , E = 1
PORTD = C;
PORTE = 0x04; //RS =1 , W/R = 0 , E = 0
for (s=0;s<1;s++){}
}
/* write a string of chars to the LCD */
// LCD lcdout function for character input
void lcdout (const char *C)
{
PORTE = 0x04;
while (*C)
{
lcd(*C++);
}
}
void out ()
{
unsigned long counter = y;
char output[12];
ltoa(output, counter, 10);
instruction(0x80);
lcdout("Pulse/sec:");
lcdout(output); //output
}
void out2 ()
{
unsigned long speed = rpm;
char r [12];
ltoa (r,speed,10);
instruction (0xC0);
lcdout("RPM:");
lcdout(r);
}
// Main Function
void main(void)
{
//OSCCON= 0x70;
TRISA = 0xFF; //set PORTA as Input
TRISD = 0x00; //Set PORTD as Output (Data)
TRISE = 0x00; //Set PORTE as Output (EN, R/W, RS)
TRISC = 0x00; //Set PORT C as Output
ANSEL = 0x00;
ANSELH = 0x00;
ADCON0 =0x00; //’disable ADC
CM1CON0 =0x00; //’disable COMPARATOR 1
CM2CON0 =0x00; //’disable COMPARATOR 2
T1CON =0x00; //’disable TIMER1
OPTION = 0x07;
T0IE = 1; // Enable Timer
GIE = 1; // Enable Global Interrupt
y=0; //Initial counter value=0
//LCD Function
instruction(0x0C); //Display on/off & cursor HEX 08-0F (1st)
instruction(0x38); //Function Set HEX 20-3F
instruction(0x01); //Clear Display HEX 01 (last)
while(1)
{
if (RA0==0){
if (RA1==1){
y++;
if (reload==0){
b=y;
a=0;
y=0;
for(s=0;s<10000;s++){}
}
}
}
rpm=((y*60)/(3*20)); //formula for rpm
out();
out2();
}
}
static void interrupt
isr(void)
{
if(reload == 0)
{
reload = 16;
}
reload--;
T0IF = 0; // Clear Timer Interrupt Flag
}