pic16f877a timer0 c programming

Status
Not open for further replies.

omkardespande

Newbie level 5
Joined
Sep 29, 2010
Messages
9
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,355
Friends/sirs,

I am trying to get the frequency of external clock pulse to displayed on LCD using pic16f877a in Hi-Tech PICC 9.82 c compiler. Following is the program:-

********************************XXXXX************************************************

#include<pic16f877a.h>

#define ldata PORTB
#define rs PORTCbits.RC0
#define en PORTCbits.RC1


void Display_Freq(unsigned int freq2write) ;
void Lcd_Cmd(unsigned char value);
void Lcd_display(unsigned char value);
void delay_lcd(unsigned int time);
void delay();

const char txt1[10]="Frequency=\n";
const char txt2[10] = "Hz\n";
const char txt3[20] = "Out of Range\n";
//const char txt4[30] = "Wind speed>53Kn\n "
int i;
unsigned char freq[6] ;


void main()
{
PORTAbits.RA4 = 1;
TRISB = 0x00;
TRISC = 0x00;

Lcd_Cmd(0x38); //LCD initialize
delay();
Lcd_Cmd(0x0c);
delay();
Lcd_Cmd(0x01);
delay();
Lcd_Cmd(0x06);
delay();
Lcd_Cmd(0x80);
delay();
for(i=0;i<10;i++)
Lcd_display(txt1);

OPTION_REG = 0x00;
OPTION_REGbits.PS0=0;
OPTION_REGbits.PS1=0;
OPTION_REGbits.PS2=0;
OPTION_REGbits.PSA=1;
OPTION_REGbits.T0SE=0;
OPTION_REGbits.T0CS=1;
delay();
while(1)
{
TMR0=0;
INTCON = 0B00100000;
delay();
if(INTCONbits.TMR0IF)
{
Lcd_Cmd(0x80);
delay();
for(i=0;i<20;i++)
Lcd_display(txt3);
delay();
}

Display_Freq(TMR0);

}
}


void Display_Freq(unsigned int freq2write)
{
freq[0] = (freq2write/1000)+48 ;
freq[1] = (freq2write/100)%10+48 ;
freq[2] = (freq2write/10)%10+48;
freq[3] = freq2write%10+48 ;
Lcd_Cmd(0x86);

for(i=0;i<5;i++)
Lcd_display(freq);
Lcd_Cmd(0x8A);
delay();
for(i=0;i<10;i++)
Lcd_display(txt2);
}


void Lcd_Cmd(unsigned char value)
{
ldata = value;
rs = 0;
en = 1;
delay_lcd(30);
en = 0;
}


void Lcd_display(unsigned char value)
{
ldata = value;
rs = 1;
en = 1;
delay_lcd(30);
en = 0;
}


void delay_lcd(unsigned int time)
{
unsigned int i,j;
for(i=0;i<time;i++)
for(j=0;j<135;j++);
}



void delay()
{
unsigned i;
for(i=0;i<10000;i++);
}

********************************XXXXX************************************************

The problem is I am not getting the right value inside TMR0 register, when I apply signal to RA4 pin. Means if i give the clock pulse of 151Hz, it shows something 22Hz. But when I directly assign some value to TMR0 in while() loop, it displays correctly that value. I dont understand, whether my program is correct or not. Because when i do this program in mickro C, it works absolutely fine. I just copied that in mplab & added the functions. That it, but still unable to get it. Please help me out.

Thank You.
 

here is the attachment.
 

Attachments

  • Project files.zip
    130.2 KB · Views: 46

I thinks your delay function is not same.
This code is display number signals is received. You need calculate freq from that value.
 
I thinks your delay function is not same.
This code is display number signals is received. You need calculate freq from that value.

Oh....I got it. Now, its working.. Thanks a lot.!!
 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…