|
|
View previous topic :: View next topic |
Author |
Message |
zipmmcs
Joined: 03 Feb 2008 Posts: 2
|
Measuring time |
Posted: Tue Jun 10, 2008 3:56 pm |
|
|
Hello,
I'm new in PIC programming, so please forgive me for the potential silly and obvious failures.
I'd like to measure frequency, especially RPM. I looked into the topics that are about this issue, but I could not implement them to my application, so I started to write my own one.
I'm using an 16F873 with 4MHz Crystal, but later I want to implement the code to an 16F628 (but the latter is not ICD compatible so I needed a more confortable one).
I'd like to measure the frequency on the RA4 pin, and I'm using 2pcs of TIL311 to display (that's why I'm cutting off the least 2 digits and translating the decimal numbers to dcb).
The only thing I know, that the frequency measuring is not working - the others are ok.
How should I modify the code? I think I declared everything fine, but...
Please give me soma advice!
Code: | #include <16F873.h>
#device *=16
#FUSES XT, NOPROTECT, BROWNOUT
#use delay(clock=4000000)
unsigned char x, pin[4], rpm_digit1, rpm_digit2;
unsigned int rpm_2dig, time, time1, time2, tick;
unsigned long rpm;
float freq;
#define IN_F PIN_A4
#define D1_1 PIN_A0
#define D1_2 PIN_A1
#define D1_3 PIN_A2
#define D1_4 PIN_A3
#define D2_1 PIN_B1
#define D2_2 PIN_B2
#define D2_3 PIN_B4
#define D2_4 PIN_B5
#define STRB PIN_B0
void initialize(){
freq=0; rpm=0; rpm_digit1=0; rpm_digit2=0; x=0; rpm_2dig=0;
for (x=1;x<4;x++)
pin[x]=0;
}
long count_rpm(){
while( input(IN_F));
set_timer1(0);
time1=get_timer1();
enable_interrupts(GLOBAL);
while(!input(IN_F));
while(tick < 10){
if (input(IN_F)>0) tick++;
}
disable_interrupts(INT_TIMER1);
time2=get_timer1();
time=time2-time1;
freq=tick/time; //OK, this is not the correct expression, I have to calculate the right frequency.
rpm=25*freq;
return rpm;
}
void display(long rpm){
//This function indicates the 1000th and 100th digits of the taken value - translates to BCD.
//The correspondence between the freq and the rpm is: 1Hz=25RPM - in case of 4 cyls engine.
//Easy round-up method is to increment the value with 50 then cut off the last 2 digit.
rpm_2dig=(rpm+50)/100;
rpm_digit1=(rpm_2dig-rpm_2dig%10)/10;
output_low(STRB);
for(x = 0; x < 4; x++){
pin[x] = bit_test(rpm_digit1,x);
if (pin[0] > 0) output_high(D1_1);
else output_low(D1_1);
if (pin[1] > 0) output_high(D1_2);
else output_low(D1_2);
if (pin[2] > 0) output_high(D1_3);
else output_low(D1_3);
if (pin[3] > 0) output_high(D1_4);
else output_low(D1_4);
}
rpm_digit2=rpm_2dig-rpm_digit1*10;
for(x = 0; x < 4; x++){
pin[x] = bit_test(rpm_digit2,x);
if (pin[0]) output_high(D2_1);
else output_low(D2_1);
if (pin[1]) output_high(D2_2);
else output_low(D2_2);
if (pin[2]) output_high(D2_3);
else output_low(D2_3);
if (pin[3]) output_high(D2_4);
else output_low(D2_4);
}
output_high(STRB);
}
void main(){
initialize();
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
enable_interrupts( GLOBAL );
enable_interrupts(INT_TIMER1);
while(1){
count_rpm();
//rpm=2485; //only for testing the display
display(rpm);
}
} |
Hope I told every important information belonging to this issue.
Thank you in advance, _________________ Regards,
zipmmcs |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Jun 10, 2008 4:52 pm |
|
|
You enable the timer1 interrupt, but where is the handler?
And some minor issues: Code: | for (x=1;x<4;x++)
pin[x]=0; | Why not start with x=0? Now you are only initializing 3 of the 4 elements.
In count_rpm() you return variable rpm but you never use the returned value, instead you have declared rpm as a global variable and access the value directly. Stick to one method of programming, use global variables or return values but mixing both is confusing and wasting memory. Personally I don't like global variables as they go against the practice of encapsulation and make it impossible for the compiler to optimize RAM memory usage. |
|
|
RArtz
Joined: 28 May 2008 Posts: 7
|
|
Posted: Wed Jun 11, 2008 10:09 am |
|
|
ya, you need to do:
rpm = count_rpm();
display(rpm); |
|
|
zipmmcs
Joined: 03 Feb 2008 Posts: 2
|
|
Posted: Mon Jun 16, 2008 12:07 pm |
|
|
Thank you for the answers.
Finally me and one of my friend carried out a particular solution using the CCP module. When the code is finished, I will post the whole program. _________________ Regards,
zipmmcs |
|
|
|
|
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
|