Professional Documents
Culture Documents
One More Way To Load Timer 0: 4 4 0.2 10 0.2 20 10 20 Ms 20 10 100 10 0x186 0.2 S 0.2 10
One More Way To Load Timer 0: 4 4 0.2 10 0.2 20 10 20 Ms 20 10 100 10 0x186 0.2 S 0.2 10
Branislav Vuksanovic
SOLUTION:
Td 20 ms 20 103
Clock ticks needed for 20 ms delay: N 100 103 0x186 A
Tic 0.2 s 0.2 10 6
Timer 0 register value = 0xFFFF + 1 – 0x186A = -0x86A1 i.e. not achievable without prescaler.
Td 20 ms 20 103
For the prescaler set to 64: N 1562.5 1562 0x061A
64 Tic 64 0.2 s 64 0.2 106
TMR0H = ~(1562/256);
TMR0L = -(1562%256);
Therefore
TMR0H = 0xF9; is identical to TMR0H = ~(1562/256);
TMR0L = 0xE6; is identical to TMR0L = -(1562%256);
University of Portsmouth, Department of Electronic and Computer Engineering
ENG531 – Microcontrollers and Programmable Logic - Lecture 10
Branislav Vuksanovic
EXAMPLE:
Use Timer 0 interrupt to generate pulse like signal from figure bellow. Your program should also make use
of external interrupt to stop and start generation of this signal.
20 us 20 ms 20 us
SOLUTION:
// Program to generate pulse width modulated signal on RB1 pin using Timer 0 interrupt.
// Short delay (20 us) is used for "pulse on" part, long delay (20 ms) for "pulse off" part.
// External interrupt (RB0 pin) is used to enable or disable pulse generation.
#include <p18f252.h>
Branislav Vuksanovic
void main(void)
{
TRISB = 0x01; // RB1 is output, but RB0 is input
TRISC = 0; // port C is output