View previous topic :: View next topic |
Author |
Message |
AdamkT1
Joined: 21 Apr 2007 Posts: 44
|
Timer0 based interrupt |
Posted: Mon Jul 30, 2007 9:53 am |
|
|
I wish to use 16F84 with FOSC=8MHz for controlling a servo.
At 8 Mhz, Timer0 will run at 0.5 us / tick and with 1:1 prescaling, the 8 bit counter will rollover in:
0.5 us/tick x 256= 128us.
In order to create a pulse width of 20 ms, 156 rollovers shall be required because 128us x 156 = 19968 us = 20 ms.
The problem is that with this setting, I am getting a pulse width of 40 ms and not 20ms.
Desparately need help to understand...Pls.
Code:
Code: | #include "16F84a.H"
#fuses XT, NOWDT, NOPROTECT
#use delay(clock = 8000000)
#use fast_io(A)
#use fast_io(B)
#define PWM_PIN PIN_A0
#define LOOPCNT 78
void main(void)
{
SET_TRIS_A(0);
SET_TRIS_B(0);
PORTA=0;
PORTB=0;
delay_ms(100); //let the system stabilize
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1|RTCC_8_BIT);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
while(TRUE);
}
#INT_RTCC
void tick_interrupt(void)
{
if(--loop == 0)
{
loop = LOOPCNT;
pulse = width;
}
if(pulse)
{
output_high(PWM_PIN);
pulse--;
}
else
{
output_low(PWM_PIN);
}
} |
|
|
|
AdamkT1
Joined: 21 Apr 2007 Posts: 44
|
|
Posted: Mon Jul 30, 2007 9:55 am |
|
|
A correction Pls.
#define LOOPCNT 156 and not 78.
Sorry... |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Jul 30, 2007 1:06 pm |
|
|
Compiler version?
Declaration and init value for pulse and width variables?
Are you sure your processor is running at 8MHz? The XT fuse is only for external crystals up to 4MHz, change this into HS. |
|
|
AdamkT1
Joined: 21 Apr 2007 Posts: 44
|
|
Posted: Tue Jul 31, 2007 9:12 pm |
|
|
Thank you very much.
My problem is solved. |
|
|
|