View previous topic :: View next topic |
Author |
Message |
picker
Joined: 07 Sep 2003 Posts: 19 Location: South Africa
|
Struggling with Timer1 - HELP!!! |
Posted: Fri Nov 28, 2003 8:44 am |
|
|
I am trying to time a 1Hz square wave using TIMER1. The program interrupts on all the interrupts, but I always get a reading of 0 or 1 on the RS232! The way I have it is that the TIMER1 will inc every 1.6uS and that is why I load it with 64535 which is a thousand ticks away from overflow. Then when it overflows I inc a counter which is the lenght of the period in 1.6mS's!. What am I missing?
Here is the code...
#device PIC18F452
#include "C:\Program Files\Picc\ClockCal\ClockCal.h"
long lTemp = 0;
long cnt =0;
long tempcnt=0;
short done=0;
#int_TIMER1
TIMER1_isr() //this will be called every 1000*1.6uS=1.6ms
{
set_timer1(64535); //1000 * 1.6uS
tempcnt++; //count no. of 1.6ms
}
#INT_EXT //L_TO_H
EXT_isr()
{
cnt=tempcnt;
tempcnt=0;
done=true;
}
#INT_EXT1 //H_TO_L
EXT1_isr()
{
tempcnt=0;
}
void main() {
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
port_b_pullups(FALSE);
ext_int_edge(0,L_TO_H);
ext_int_edge(1,H_TO_L);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_EXT);
enable_interrupts(INT_EXT1);
enable_interrupts(global);
set_timer1(64535);
while (true)
{
if (done)
{
printf("%lu \n",tempcnt);
done=false;
//cnt=0;
}
}
} |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Fri Nov 28, 2003 11:40 am |
|
|
Code: |
#device PIC18F452
#include "C:\Program Files\Picc\ClockCal\ClockCal.h"
long lTemp = 0;
long cnt =0;
long tempcnt=0;
short done=0;
int seconds;
|
Quote: |
Assuming you are using a 20Mhz Xtal you get:
T1 = 20000000/4=5000000Mhz >> 1/T1 = 0.0000002 s
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);0.0000002 * 8 = 0.0000016 s
1 s / 0.0000016 s = 625000 // times you needs T1 overflows to get 1 sec.
To get 0.1 s >>> 625000 / 10 = 62500
T1 max count = 65536
Loading T1 with 3036 = (65536 - 62500)
will give you an overflow every 0.100 s wich is more comfortable and adecuate to handle time count.
|
Code: |
#int_TIMER1
TIMER1_isr() // this will be called every 0.100 s
{
if(tempcnt < 10)
{
tempcnt++; // count no. of 100ms
}
else
{
done = true;
tempcnt = 0;
seconds++;
}
}
void main() {
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
port_b_pullups(FALSE);
ext_int_edge(0,L_TO_H);
ext_int_edge(1,H_TO_L);
enable_interrupts(INT_TIMER1);
// Just to test your timer1 ONLY
//enable_interrupts(INT_EXT);
//enable_interrupts(INT_EXT1);
enable_interrupts(global);
set_timer1(3036);
while (true)
{
if (done)
{
printf("%u \r\n", seconds);
done=false;
set_timer1(3036); // you must load the hardware timers outside the #INT
}
}
}
|
Hope this help
Humberto |
|
|
picker
Joined: 07 Sep 2003 Posts: 19 Location: South Africa
|
Thanks... |
Posted: Fri Nov 28, 2003 1:54 pm |
|
|
Thanks... |
|
|
|