Книга: Advanced PIC Microcontroller Projects in C

Project Description

Project Description

This project is similar to Project 6 but here the microcontroller’s timer interrupt is used to refresh the displays. In Project 6 the microcontroller was busy updating the displays every 10ms and could not perform any other tasks. For example, the program given in Project 6 cannot be used to make a counter with a one-second delay between counts, as the displays cannot be updated while the program waits for one second.

In this project a counter is designed to count from 0 to 99, and the display is refreshed every 5ms inside the timer interrupt service routine. The main program can then perform other tasks, in this example incrementing the count and waiting for one second between counts.

In this project Timer 0 is operated in 8-bit mode. The time for an interrupt is given by:

 Time = (4 ? clock period) ? Prescaler ? (256 – TMR0L)

where Prescaler is the selected prescaler value, and TMR0L is the value loaded into timer register TMR0L to generate timer interrupts every Time period.

In our application the clock frequency is 4MHz, that is, clock period = 0.25?s, and Time = 5ms. Selecting a prescaler value of 32, the number to be loaded into TMR0L can be calculated as follows:

 

or

 

Thus, TMR0L should be loaded with 100. The value to be loaded into TMR0 control register T0CON can then be found as:

 

Thus, T0CON register should be loaded with hexadecimal 0xC4. The next register to be configured is the interrupt control register INTCON, where we will disable priority based interrupts and enable the global interrupts and TMR0 interrupts:

 

Taking the don’t-care entries (X) as 0, the hexadecimal value to be loaded into register INTCON is 0xA0.

When an interrupt occurs, the program automatically jumps to the interrupt service routine. Inside this routine we have to reload register TMR0L, reenable the TMR0 interrupts, and clear the TMR0 interrupt flag bit. Setting INTCON register to 0x20 reenables the TMR0 interrupts and at the same time clears the TMR0 interrupt flag.

The operations to be performed can thus be summarized as follows:

In the main program:

• Load TMR0L with 100

• Set T0CON to 0xC4

• Set INTCON to 0xA0

• Increment the counter with 1-second delays

In the interrupt service routine:

• Re-load TMR0L to 100

• Refresh displays

• Set INTCON to 0x20 (reenable TMR0 interrupts and clear timer interrupt flag)

Оглавление книги


Генерация: 0.678. Запросов К БД/Cache: 2 / 0
поделиться
Вверх Вниз