View previous topic :: View next topic |
Author |
Message |
leeyung
Joined: 23 Feb 2009 Posts: 4
|
Problem on using TIMER1 on PIC16F877A as counter |
Posted: Tue Feb 24, 2009 10:27 am |
|
|
hi all, i am very new in electronics, but i would like to do a very simple counter to count the pulse. I have an opto sensor which produce 5V when blocked and 0V when unblocked, this will create a train of 0-5-0-5-0-5 pulses, i wan to count the pulse using Timer1 (Pin RC0 as TIMER1). The following is my simple program to test if 3 pulses were counted, the LED will light up.. however, it seems like the counter doesnt count my input... anyone can guide me please?
#include<16f877a.h>
#use delay(clock=20000000)
#fuses hs,noprotect,nowdt,nolvp
#byte PORTC=7 //Call Port C
#byte PORTD=8 //Call Port D
void main() //Main Block
{
int count=0; //Receives timer count
set_tris_D(0b00000000); //Set Port D as I/O
setup_timer_1(T1_EXTERNAL);// The timer is set up for external input
do{
set_timer1(0); //Reset counter
count=get_timer1(); //Resulting count is read using get_timer()
if (count==3) //if 3 pulse were counted
{output_high(pin_D7);}// LED light up
else
{output_low(pin_D7);}//LED light off
}while(1);
} |
|
|
Ttelmah Guest
|
|
Posted: Tue Feb 24, 2009 11:37 am |
|
|
Hint use code buttons....
Code: |
set_timer1(0); //Reset counter
do{
count=get_timer1(); //Resulting count is read using get_timer()
if (count==3) //if 3 pulse were counted
{output_high(pin_D7);}// LED light up
else
{output_low(pin_D7);}//LED light off
}while(1);
|
Problem is that you are resetting the counter every time round the loop, so how can it ever reach 3?.....
Best Wishes |
|
|
leeyung
Joined: 23 Feb 2009 Posts: 4
|
|
Posted: Wed Feb 25, 2009 8:01 am |
|
|
Ttelmah wrote: | Hint use code buttons....
Code: |
set_timer1(0); //Reset counter
do{
count=get_timer1(); //Resulting count is read using get_timer()
if (count==3) //if 3 pulse were counted
{output_high(pin_D7);}// LED light up
else
{output_low(pin_D7);}//LED light off
}while(1);
|
Problem is that you are resetting the counter every time round the loop, so how can it ever reach 3?.....
Best Wishes |
opss... got it, sorry for the mistake, thx a lot |
|
|
|