|
|
View previous topic :: View next topic |
Author |
Message |
400fury
Joined: 30 Dec 2004 Posts: 1
|
rf PIC12F675 as Frequency counter |
Posted: Thu Dec 30, 2004 11:49 am |
|
|
Hello,
I have very recently been introduced to PIC programming and to this forum.I have a request to make.I am trying to program the PIC 12F675 to act as a frequency counter.Since I am using the CCS compiler,I basically just modified the EX_FREQC program and tried to verify that.I am using the PICKit Flash starter kit,and hence am trying to program the rfPIC12F675 transmitter module.The input frequency is given to pin GP5, and the PIC has to be programmed to calculate the frequency and write it to an EEPROM location, and display it over the RS-232.
Unfortunately,the modified program does not seem to be working rite.I do not see any output,and checking the EEPROM memory area also shows no change.
Any suggestions,pointers most welcome.
Code: |
// Pre-processor
#include <12F675F.h>
#include <stdlib.h>
#include <internal_eeprom.c>
#define ADC=10
#fuses RC,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_A2, ERRORS)
#bit t1_overflow=0x0C.0
#define GP0 PIN_A0
#define GP1 PIN_A1
#define GP2 PIN_A2
#define GP3 PIN_A3
#define GP4 PIN_A4
#define GP5 PIN_A5
void init()
{
set_tris_a( 0b00100000 ); // set all other outputs
setup_comparator( NC_NC_NC_NC ); // disable comparators
setup_adc_ports( NO_ANALOGS ); // disable analog inputs
setup_adc( ADC_OFF ); // disable A2D
}
// Main Program
main()
{
int cycles8, cycles;
int32 freq;
int32 Fin, Fcyc, Tcyc, Fout;
long freqc_high;
long freqc_low;
init();
write_eeprom(0x21, 4); // sample values for testing eeprom write
write_eeprom(0x22, 6);
write_eeprom(0x23, 5);
write_int32_eeprom(0x10,0x21);
while (TRUE) {
cycles8=0;
cycles=0;
freqc_high=0;
t1_overflow=0;
set_timer1(0);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
/* ___ wait one second ___ */
while (cycles!=0xFF) { //true=3, false=4
cycles8=0; //1 cycle
//start inner loop
while (cycles8!=0xFF) { //true=3, false=4
if (t1_overflow)//true=2,false=3 //----|
{t1_overflow=0;freqc_high++;}//6 cycles // |
else // |-- 8 cycles
{delay_cycles(5);} //----|
delay_cycles(1); //x
cycles8++; //1
///2 cycles to jump to top
//math: end inner loop
//math: total inner loop=((3+8+x+1+2)*255 + 4)*255
//math: if x=1.3630142252979623221837754709727 then inner loops takes 1mil instructions
//math: if x=1 then inner loop takes 976395, have to fill 23605 cycles
}
delay_cycles(85); //y
cycles++; ///1 cycle
///2 cylces to jump to top
//math: outer=(3+1+y+1+2)*255+4=23605
//math: y=(23605-4)/255)-(3+1+0+0+1+2)
//math: if y=85.552941176470588235294117647059 then outer loop cylces is 23605
//math: if y=85 then outer loop cycles is off by 141 cycles. z=141
}
delay_cycles(141); //z
/* ___ end waiting 1 second ___ */
setup_timer_1(T1_DISABLED); //turn of counter to prevent corruption while grabbing value
if (t1_overflow) //check one last time for overflow
freqc_high++;
freqc_low=get_timer1(); //get timer1 value as the least sign. 16bits of freq counter
freq=make32(freqc_high,freqc_low);//use new make32 function to join //lsb and msb
printf("%LU Hz\r\n",freq); //and print frequency
write_int32_eeprom(0x02,freq);
}
|
|
|
|
Guest
|
|
Posted: Thu Dec 30, 2004 4:14 pm |
|
|
Can anybody help me out...?
The program works when the Fuse is changed to INTRC, but the readings are all messed up.
For instance,the EEPROM value jumps all over the place.
Each time,the READ DEVICE is invoked..(from the PICkit Classic software)..i get different values..
For reference value,I am supplying 210000 Hz at Pin2..ie GP5,i get different values out at the EEPROM location.Ranging from
84 00 00 00
74 00 00 00
EC 00 00 00
6C 00 00 00...
any suggestions..
Thanks in Advance. |
|
|
|
|
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
|
Powered by phpBB © 2001, 2005 phpBB Group
|