1 minute read
4.8.2 Program TIMER0 Interrupt for Real Time Applications
if (tmr_buz == 3) // time = 3 x 1s = 2s {
RB7=~RB7; //toggle buzzer tmr_buz =0;
Advertisement
//restore TMR0H & TMR0L value TMR0H=0x0B; TMR0L=0xDC; TMR0IF=0; //clear TMR0IF
4.8.2 Program Timer0 Interrupt for Real Time Applications
We can use timer interrupt programming to perform real time applications, for example, a
counter system.
Example 4.5
A fruit packaging factory wants to install an automatic packaging system. The system
used two DC motors to move two conveyor belts. The first conveyer belt is used to carry
and drop the fruits into boxes and the second conveyor belt is used to move a box. A
sensor is used to detect the fruits that fall into the box. When 50 of the fruits have fallen
into the box, the fruit conveyor belt will stop and the second conveyor will move for 2
seconds to place a new empty box.
Sketch the control circuit using PIC18 microcontroller, and write a program using Timer0
Interrupt as a counter to perform the operation. Use any pins as output.
Solution
Schematic diagram
Program :
#include <xc.h> #define Motor1 RB3 #define Motor2 RB5 void interrupt ISR (void); void main (void) {
ADCON1=0b00001111; TRISA4=0; TRISB3=0; //RA4 : input //RB3 : output
TRISB5=0; //RB5 : output
//configure timer as counter T0CON=0b01101000; // 8-bit mode, clock source from RA4, no prescaler TMR0L=206; //set count value to 50, (255 -50 +1) //Enable timer 0 Interupt GIE=1; TMR0IE=1; TMR0IF=0; TMR0ON=1; while(1) {
Motor1=1; //turn on motor 1
Motor2=0; // turn off motor 2