Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 22nd November 2008, 07:24 AM   #1
Default Timer0 problem

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...
littletransistor is offline  
Old 22nd November 2008, 07:48 AM   #2
Default

Quote:
Originally Posted by littletransistor View Post
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.
__________________
=========================
Futz's Microcontrollers & Robotics
=========================
futz is offline  
Old 23rd November 2008, 03:34 AM   #3
Default

Quote:
Originally Posted by futz View Post
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?
littletransistor is offline  
Old 23rd November 2008, 04:05 AM   #4
Default

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.
Pommie is online now  
Old 23rd November 2008, 04:15 AM   #5
Default

Ok, thanks for the explaination. It's wonderful. Gotta read the datasheet... harder!
littletransistor is offline  
Old 24th November 2008, 07:03 AM   #6
Default

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.
littletransistor is offline  
Old 24th November 2008, 07:32 AM   #7
Default

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.
Pommie is online now  
Old 27th November 2008, 06:39 AM   #8
Default

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?
littletransistor is offline  
Old 27th November 2008, 06:55 AM   #9
Default

Have you remembered to set the prescaler to 1?

Mike.
Pommie is online now  
Reply

Tags
problem, timer0

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
timer0 sahanaashwin Micro Controllers 11 27th March 2009 01:47 PM
Using timer0 mrfunkyjay Micro Controllers 6 11th July 2008 10:43 AM
TIMER0 - Have I got this right? UTMonkey Micro Controllers 8 14th December 2007 11:08 PM
PIC Timer0 Hesam Kamalan Micro Controllers 7 29th October 2007 03:32 PM
Cant get timer0 to interrupt 2camjohn Micro Controllers 5 16th June 2004 01:06 PM



All times are GMT. The time now is 12:35 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker