View previous topic :: View next topic |
Author |
Message |
Pisco Guest
|
PIC16F84 as frequency comparator |
Posted: Thu Nov 12, 2009 11:52 am |
|
|
Hello guys,
Surfing on the web I found this excellent forum about C language.
I´m really new on this area and I have a big problem with one step of my college graduation project.
I have a external clock from 7200hz to 6210Hz depending of sensor reading and I´m using EWB to simulate that clock generator and PIC16F84 to do this logic. The idea is when system have frequency = or less 6400hz, the PIC active a external port. It´s a eternal loop and for cost reduction i´m using PIC internal oscillator (RC)
So, I confess that I´m not expert about this and actually I have a lot of difficult to do that :(
Can anyone take a look on this draft code and help me to solve my issue?
Code: | #include <16F877.h>
#fuses RC,NOWDT,NOPROTECT
#use delay(clock=500000)
#use FAST_IO(B)
void main()
{
int timer0;
set_tris_b(0x00);
setup_timer_0(RTCC_INTERNAL);
timer0 = get_timer_0();
if( timer0 <= 800 ) output_high(B); // 6400hz / 8Bits
else
Loop? here?
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 12, 2009 1:27 pm |
|
|
Post the manufacturer and part number of the sensor, and post a link
to the webpage for it. |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Thu Nov 12, 2009 3:08 pm |
|
|
Sounds easy enough. Provided that the sensor really delivers an output in the range you say, you can use it to trigger an interrupt (wire the signal to port B.0). Every time the interrupt happens, compare the value of a timer to a threshold (I think you will find that 156 is the relevant number, but you might have to fudge that to allow for time taken between reading and setting the timer), and set that timer back to zero. Based on the comparison with the threshold, set or clear the output.
For extra credit, set up a second interrupt that occurs when the timer wraps around from 255 to 0. That indicates that the timer never got reset by the sensor input, and so it tells you that the sensor isn't connected, or has failed. You can have another output to show this. |
|
|
Psico
Joined: 12 Nov 2009 Posts: 1
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|