|
|
View previous topic :: View next topic |
Author |
Message |
dear11235813
Joined: 08 Sep 2008 Posts: 4
|
pulse width?? |
Posted: Mon Sep 08, 2008 10:11 pm |
|
|
I want to do function km/h for electrical treadmill's LCD.
This my code. It doesn't work but I think for treadmill,
pwm low frequency.
Code: |
#include <18F8720.h>
#include "lcd_for_base8720.c"
#fuses H4,LVP,NOWDT,NOPROTECT,NOSTVREN,NOOSCSEN
#use delay (clock = 24000000)
//-------------------------------------------------------------------------
/***********************************************************************
* Global variables
***********************************************************************/
float time1,time2;
BOOLEAN hook_cpp1=TRUE, HookRise=TRUE;
/***********************************************************************
* FUNCTION: Capture Interrupt
* DESCRIPTION: CCP1 capture mode
* PARAMETERS: nothing
* RETURNED: nothing
***********************************************************************/
#int_ccp1
void capture_isr() {
if(HookRise) {
time1 = get_timer1();
HookRise = FALSE;
} else {
time2 = get_timer1();
HookRise = TRUE;
hook_cpp1 = FALSE;
}
}
void main() {
float tms;
int sec0=0;
int min0=0;
int hour0=0;
int round0=0;
lcd_init();
///////////////////////////////////////////////////////////////////////
while(1){
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // Setup timer 1
enable_interrupts(INT_CCP1); // Enable interrupt CCP1
enable_interrupts(GLOBAL); // All interrupts ON
set_timer1(0); // Start timer
while(hook_cpp1)
;
tms = (time2-time1)*8*166.666*0.000000001;
lcd_gotoxy(1,0);
printf(lcd_putc,"Time : %9.3e ms ", tms);
lcd_gotoxy(0,1);
printf(lcd_putc," Frequency: %f Hz ",1/tms);
delay_ms(1000);
}
|
_________________ 11235813 |
|
|
Ttelmah Guest
|
|
Posted: Tue Sep 09, 2008 3:14 am |
|
|
A series of comments.
Don't use 'float' values to hold the times. Problem here is that you are taking an integer value from the timer, and then converting this to a float inside the interrupt. This is unwanted extra time. Put the values into integers, and convert them to floats when you perform the arithmetic in the main.
You don't want to read the timer. You want to read the CCP latch. The timer is running all the time. The 'point' about the CCP, it it _latches_ the contents of the timer, when the event occurs. It is this value you want to read. It is available as 'CCP1'.
You are not handling overflows. Think for a moment. The timer could be sitting at (say) 65534, the first the first time it is latched. Then when the second reading occurs, have wrapped, and be reading 123. You need either to check if the second timer is lower than the first, and add 65536 to it if so, or have a separate 'overflow' counter, updated in a timer1 interrupt routine, and store this as well.
Best Wishes |
|
|
|
|
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
|