hi every one
how to measure the time of input signal on pic16f84 pin ,I tried to write code but i think its not right
Code:
#include <16F84.h>
#use delay(clock=4000000)
void main() {
int n;
setup_timer_0(rtcc_internal|rtcc_DIV_1); // tick every 1us
n=get_timer0();
output_low(pin_b0); // turn off led
while (true)
{
if(input(pin_b1)) // high input start
{set_timer0(0); // start counting ...
if(!input(pin_b1)) // high input stop
{if (n<250) // if its high less than 250us
output_high(pin_b0);} // turn on led
}
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
Posted: Sat Jan 21, 2012 8:08 am
Your code will keep setting the timer to zero each time through the loop.
You need one loop to look for the bit to go high, which zeros the timer when it exits. Then you need another loop to look for the bit to go low, which reads the timer when it exits.
Or better yet use the hardware capture feature of a timer. _________________ The search for better is endless. Instead simply find very good and get the job done.
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
Posted: Mon Jan 23, 2012 12:10 pm
Quote:
hi guys
i have no much experience timers and i wonder if we can read the value of the input pulse as variable in microsecond ,for example
if we have 2 input frequency ,first high input on pin b0 last for 250us and second one last for 100us and we want to write a code like this:
If you get the timer to clock at 1us intervals then the readings will be in us. With a 4MHz crystal you need the timer to run at 1/4 crystal speed. Read the PIC datasheet section on timers. _________________ The search for better is endless. Instead simply find very good and get the job done.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum