EmbeddedRelated.com
Forums

Timer0 interrupt on 18f452

Started by Wayne Fuchs January 16, 2005


Having a strange problem with the timer0 interrupt. I'm trying to
generate a square wave to send to data lines on a pair of servos (from
parallax). I tried to calculate the frequency using similar methods I
applied on the Motorolla 68hc11/12, but the frequency didn't quite work
out. I then used the trial-and-error method and was able to produce a
1kHz square wave. So far so good.

It was quite interesting that the frequency gain was not linear or
exponential as I tried different values for TMR0H and TMR0L.

My motors are configured in such a way that I need one to recieve a 285Hz
and the other a 350Hz square wave so my platform will go in a straight
line. My intent was to create the two waves from the 1kHz interrupt. I
added a few lines of code into my interrupt routine (see below) that I
expected to do just that. Instead, I ended up with two waves, one was
12.6Hz the other was 7.58Hz.

I wondered if my few instructions were using a lot of CPU time, so I
changed my main loop to be:
main: btg LATC,5
goto main
The frequency on this pin is 333kHz, so this theory seems unlikely.

I guess my question is: Am I doing anything fundamentally wrong with my
timer interrupt? (Code posted below) The fastest square wave I was able
to get from the timer interrupt was 1.887kHz from an 8MHz crystal. To
me, it seems that I should be able to get a much faster wave.

Sorry this is so long- and thanks in advance,

Wayne ----Code
TMR0_ISR
btg LATC,7 ; Creates a square wave on RC7
btg LATC,6 ; Creates a square wave on RC6

; dcfsnz TimC1
; call FIREM1
; dcfsnz TimC2
; call FIREM2

movff TIMER1H_C,TMR0H ; Resetting place to count from
movff TIMER1L_C,TMR0L ; for timer overflow (do i
; have to do this?)
bcf INTCON,T0IF ; Clear Overflow Bit
retfie ; Return From Interrupt

;FIREM1 btg LATC,6
; movff TimR1,TimC1
; return
;
;FIREM2 btg LATC,7
; movff TimR2,TimC2
; return
---- End Code

NOTE: The commented code is the code that I added to split the 1kHz square
wave into two square waves. TimR1, TimC1 are initially set to 285.
TimR2, TimC2 are initially set to 350.

---- If needed, timer init code
bcf INTCON,T0IF ; clear Timer0 interrupt flag
bsf INTCON,TMR0IE ; enable Timer0 interrupts
clrf T0CON ; Clear Timer Register
; This sets the timer:
; TMR0ON: OFF
; T08BIT: 16BIT Operating Mode
; T0CS: Set to Timer Mode
; T0SE: Set to do low-to-high edge
; PSA: Use Prescaler
; P0PS2-P0PS0: Set Prescaler 1:2

clrf TMR0H ; Clear Timer0 High bits
clrf TMR0L ; Clear Timer0 Low bits

bsf T0CON,TMR0ON ; Turn on the timer
bsf INTCON,GIE ; Turn on interrupts
---- End timer init code




Somehow this message took a few days to get posted. Anyhow, I solved
most of the problem I was having.. I was attempting to use timer1 as
an 8 bit timer. :( I switched to using timer2 instead.

Now, i'm still curious to know how to calculate the output frequency.
The formula i was using is:
DesiredFrequency = (1/(((256-PreLoadedVariable)*Pr
escaler)/(OscFreq/4))

Wayne

--- In , Wayne Fuchs <grok@x> wrote:
>
>
> Having a strange problem with the timer0 interrupt. I'm trying to
> generate a square wave to send to data lines on a pair of servos
(from
> parallax).........

> Sorry this is so long- and thanks in advance,
>
> Wayne



Take a closer look at the datasheet...
There are a special procedure for reloading the timer registers,
doing it the wrong way could cause a rollover error while reloading.

Wayne wrote:

 
Somehow this message took a few days to get posted.  Anyhow, I solved
most of the problem I was having..  I was attempting to use timer1 as
an 8 bit timer.  :(  I switched to using timer2 instead.

Now, i'm still curious to know how to calculate the output frequency.
The formula i was using is:
DesiredFrequency = (1/(((256-PreLoadedVariable)*Pr
escaler)/(OscFreq/4))

Wayne

--- In p...@yahoogroups.com, Wayne Fuchs <grok@x> wrote:
>
>
> Having a strange problem with the timer0 interrupt.  I'm trying to
> generate a square wave to send to data lines on a pair of servos
(from
> parallax).........

> Sorry this is so long- and thanks in advance,
>
> Wayne
 
 
 
 

to unsubscribe, go to http://www.yahoogroups.com and follow the instructions

--
*******************************************
VISIT MY HOME PAGE:
<http://home.online.no/~eikarlse/index.htm>
LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen