![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
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... | |
| |
| | #2 | |
| Quote:
__________________ ========================= Futz's Microcontrollers & Robotics ========================= | ||
| |
| | #3 | |
| Quote:
![]() 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? | ||
| |
| | #4 |
|
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. | |
| |
| | #5 |
|
Ok, thanks for the explaination. It's wonderful. Gotta read the datasheet... harder! | |
| |
| | #6 |
|
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
However there's no output - judging from the no sound from the speaker. | |
| |
| | #7 |
|
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 To get closer to 256 you could change your prescaler to 32 and load TMR0 with -61. Mike. | |
| |
| | #8 |
|
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
But it didn't! How to get the timing loop correct? | |
| |
| | #9 |
|
Have you remembered to set the prescaler to 1? Mike. | |
| |
|
| 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 |