|
|
View previous topic :: View next topic |
Author |
Message |
scott0808
Joined: 04 Feb 2009 Posts: 4
|
12F683 pulse counter issue |
Posted: Wed Feb 04, 2009 7:00 am |
|
|
Hello. I am trying to count pulses on the CCP1 pin from a device that will hit about 5000 pulses/min max and I am reading that with MScomm1. I think the problem is that the PIC is just reading too fast? I have searched the forum but cannot for the life of me see how to slow this thing down (if that is even the problem).
Here is the code:
Code: |
#include <12F683.H>
#DEVICE ADC=10
#include <stdlib.h>
#fuses EC_IO,WDT,NOCPD,NOPROTECT,NOMCLR,NOPUT,NOBROWNOUT,NOIESO,NOFCMEN
#use delay(clock=12000000,restart_wdt)
#use rs232(baud=9600, xmit=PIN_A1, rcv=PIN_A3)
int8 DS_Data;
int16 finData = 0;
char buf[5];
int1 gotccp1=false;
#INT_CCP1
ccp1_isr()
{
gotCCP1 = TRUE;
}
//********************************************************************
void main()
{
int8 byte_read;
SETUP_ADC(ADC_OFF);
setup_adc_ports(NO_ANALOGS);
setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1(T1_DISABLED);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
while(1)
{
if(gotCCP1)
{
findata++;
gotCCP1 = FALSE;
itoa(findata,10,buf);
printf(buf);
buf[0] = "" ;
buf[1] = "" ;
buf[2] = "" ;
buf[3] = "" ;
buf[4] = "" ;
}
}
}
|
If I try changing
#use delay(clock=12000000,restart_wdt)
to
#use delay(clock=4000000,restart_wdt)
then it won't even work at all. Why wouldn't that slow it down? Should I be using setup_timer_1? |
|
|
Ttelmah Guest
|
|
Posted: Wed Feb 04, 2009 9:11 am |
|
|
Changing your clock statement, will just stop the serial from working.
The clock statement, _must_ match the real clock attached to the chip, or the serial baud rate will be wrong. To change the speed the chip is working, you have to change the physical oscillator used.
However, since the speed in this case, is determined by an external event, this won't slow things down.
There is no reason at all to use itoa. Just printf the output directly.
The main problem, is that you have nothing separating the characters. At present, if you have (say), 1234567, how can your receiving program tell this from 12 34 56?.
Assuming your count gets to a high value (10000 say), and you add a separator (normally line feed), you will send six characters for each count. if your rate is 5000/min, then this would correspond to 30000 chars/min. At 9600bps, you can send a maximum of 960/sec = 57600/min. So your data rate will support what you want to do, and the PC should handle this easily.
Your problem is almost certainly the lack of the separator. Try:
Code: |
while(1) {
if(gotCCP1) {
gotCCP1 = FALSE;
printf("%lu\n",++findata);
}
}
|
The only problem that would appear, is if with high counts (since these give more characters), a pulse arrived quicker than six character times (0.00625 seconds), when a count could be missed by the code as shown.
Best Wishes |
|
|
scott0808
Joined: 04 Feb 2009 Posts: 4
|
|
Posted: Wed Feb 04, 2009 9:46 am |
|
|
Hello. You are right, the output is a string of "1 2 3 4 5 6 7 8..." instead of each value individually. I used the above code but it still seems to be giving the numbers in a similar fashion. |
|
|
Ttelmah Guest
|
|
Posted: Wed Feb 04, 2009 11:01 am |
|
|
The code I posted, puts a _line feed_ character between each number (\n). Provided you are using a standard input routine on MSCOMM, this will be accepted as the end of number. How it behaves it going to depend on what code _you_ are using to read MSCOMM.
Best Wishes |
|
|
scott0808
Joined: 04 Feb 2009 Posts: 4
|
|
Posted: Wed Feb 04, 2009 1:41 pm |
|
|
OK, I just needed a mid to get the max value out of the string. Thank you so much! Your code works perfectly. |
|
|
scott0808
Joined: 04 Feb 2009 Posts: 4
|
|
Posted: Thu Feb 05, 2009 11:40 am |
|
|
Hello. I now have a new issue. The count will be going just fine "1 2 3 4 5 6 7 8 9 10..." but then for no reason it will reset and go back to 1 right in the middle of a count, so it is like "1 2 3 4 5 6 7 8 9 10 1 2 3 4 5". It happens whenever, sometimes around 10, sometimes around 400... whenever it wants... What would be causing that?
Code: |
#include <12F683.H>
#DEVICE ADC=10
#fuses EC_IO,WDT,NOCPD,NOPROTECT,NOMCLR,NOPUT,NOBROWNOUT,NOIESO,NOFCMEN
#use delay(clock=12000000,restart_wdt)
#use rs232(baud=9600, xmit=PIN_A1, rcv=PIN_A3)
int16 finData = 0;
int1 gotccp1=false;
#INT_CCP1
ccp1_isr()
{
gotCCP1 = TRUE;
}
//********************************************************************
void main()
{
SETUP_ADC(ADC_OFF);
setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1(T1_DISABLED);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
while(1) {
if(gotCCP1) {
gotCCP1 = FALSE;
printf("%lu\n",++findata);
}
}
}
|
|
|
|
|
|
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
|