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...
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.
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.
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.
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?