Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Timer0 problem

Status
Not open for further replies.
Hey there,

I've read thru the PIC 18F1320 datasheet for the Timer0 module, and I only understand the prescaler part.

However, let's say if I want to let it count around 1 second. I tried reading some examples about the Preload, but I couldn't really get the Preload concept. It isn't like those TTL counters where I can divide the frequency directly... :confused:
 
I've read thru the PIC 18F1320 datasheet for the Timer0 module, and I only understand the prescaler part.

However, let's say if I want to let it count around 1 second. I tried reading some examples about the Preload, but I couldn't really get the Preload concept.
Preloading just means that instead of letting the timer count from 0 to 255 (in 8-bit mode) or 0 to 65535 (in 16-bit mode), you preload the timer to start at some number higher than 0 each time it hits 255 or 65535 and triggers the interrupt. This shortens the time it takes to get to the rollover that triggers the interrupt. Good for fine tuning when your interrupt will occur.
 
Preloading just means that instead of letting the timer count from 0 to 255 (in 8-bit mode) or 0 to 65535 (in 16-bit mode), you preload the timer to start at some number higher than 0 each time it hits 255 or 65535 and triggers the interrupt. This shortens the time it takes to get to the rollover that triggers the interrupt. Good for fine tuning when your interrupt will occur.

Ok, got that part, and got it working finally in the program! :)

But what could be the difference between the TMR0L and TMR0H ? I know L is Low and H is High... and what's the significance? :)
 
Assuming your operating timer0 in 16 bit mode, TMR0L increments every clock cycle (or less if using the prescaler) and when it rolls over from 255 to 0, TMR0H gets incremented.

Note, when writing timer0 you have to write the low byte second. When reading you have to read the low byte first. This due to the high byte being cashed - see the data sheet for a full explanation.

Mike.
 
Just checking, I couldn't really make the software to detect when the Timer0 rolls over, since I want to make a 256Hz squarewave signal out of this.

Here's the code:

Code:
#include <p18F1320.inc>
    
    CONFIG WDT=OFF; disable watchdog timer
    CONFIG MCLRE = OFF; MCLEAR Pin off
    CONFIG DEBUG = OFF; Enable Debug Mode
    CONFIG LVP = OFF; Low-Voltage programming disabled (necessary for debugging)
    CONFIG OSC = INTIO2;Internal oscillator, port function on RA6 
    CONFIG PWRT = ON
    
    org 0; start code at 0

cblock 0x20        
endc


Start:
     movlw    b'01100000'
    movwf    OSCCON
    
    clrf    LATA
    clrf    LATB
    clrf    TRISA
    clrf    TRISB
    
    
    clrf    T0CON
    movlw    b'11000111' ; Prescaler: 256
    movwf    T0CON
    
  Loop1:
    movlw    b'00000010'
    movwf    LATA
    call    DelayFreq
    movlw    b'00000000'
    movwf    LATA
    call    DelayFreq
    goto    Loop1
       
DelayFreq:
    movlw    d'16'
    subwf    TMR0L,w
    goto    DelayFreq
    return
    
 TurnOff:
    clrf    LATB
    goto    TurnOff    

end

I made the program to substract 16 everytime the TMR0L reaches 16, and when it doesn't it goes back to the DelayFreq loop.

However there's no output - judging from the no sound from the speaker. :confused:
 
Your DelayFreq routine is stuck in an endless loop.

Try,
Code:
DelayFreq:	movlw	0x100-.16	;= -16
		movwf	TMR0L		;load timer0 with -16
		bcf	INTCON,TMR0IF	;clear interrupt flag
WaitTimeUp	btfss	INTCON,TMR0IF	;is the interrupt flag set
		goto	WaitTimeUp	;no so wait
		return			;yes, so done

This will however result in a frequency of 121Hz not 256. To make it 222 (your approximation of 256) either half the prescaler or load TMR0L with -8.

To get closer to 256 you could change your prescaler to 32 and load TMR0 with -61.

Mike.
 
Hey, i got that one correct, thanks, but another question.

I can't get nested timer loops to work. Let's say the OSC is 4MHz, and I wanna make it to do a 0.3sec delay.

Code:
DelayT:
    movlw    .0
    movwf    TMR0L
    bcf        INTCON, TMR0IF
    
    Loop1:
    btfss    INTCON,TMR0IF
    goto    Loop1
    
    decfsz    count1 ; count1 = 5
    goto    DelayT
    return

That's the fragment of the delay. the first Loop1 counts the timer until to the 256th time and then total time is 65536 microsecs. And then the second one, I used the "decfsz count1" to further extend the loop, and 65536x5 microsecs.

But it didn't! How to get the timing loop correct? :confused:
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top