Thursday, March 31, 2022

PIC16F877A Timer0 Interrupt Programming

In previous post, I introduced about Timer 0 Programming of PIC16F877A. Timer 0 can generate interrupt, whenever its 8-bit timer register TMR0 overflow from 0xFF to 0x00. Its interrupt flag T0IF (timer 0 interrupt flag) will be set.

However the programmer need to configure their relevant registers properly, before the interrupt can be used. There are three fundamental registers that relate to timer 0 operation, and interrupt programming.

PIC16F877A Timer0 Interrupt Programming
Running program on breadboard

PIC16F877A Timer0 Interrupt Programming
REGISTERS ASSOCIATED WITH TIMER0

Timer 0 prescaler must be configure to an appropriate value by the programmer, to obtain a prescribed result. Interrupt Control Register (INTCON) contains the interrupt setting of timer 0, and other interrupt sources.

PIC16F877A Timer0 Interrupt Programming
INTCON REGISTER (ADDRESS 0Bh, 8Bh, 10Bh, 18Bh)

Programmer will need to set the TMR0 Overflow Interrupt Flag bit first. Then the Global Interrupt Enable bit (GIE) must be set. TMR0 register must be clear (0x00) after finishing the timer 0 interrupt configuration settings.

In the interrupt service routine - ISR the programmer need to test the T0IF flag, and write the corresponding codes for what to do. Optionally, the TMR0 register must be pre-load with any value. Finally, the T0IF flag must be clear to make the interrupt triggers again.

In this example, the result is just like the one in previous post. An output LED toggle for every 500 mS, yielding an output frequency around 1 Hz. However the main program loop is just idle in "while" loop.

Source code:

  1. /*
  2.  * PIC16F877A Timer0 Interrupt Programming Example 1
  3.  */
  4. #include <xc.h>
  5. #include "config.h"
  6.  
  7. //LED connects to RB0
  8. #define LED RB0
  9.  
  10. unsigned int cnt=0;
  11.  
  12. int main(void){
  13.  
  14. //Clear PortB
  15. PORTB=0x00;
  16. //RB0 as output
  17. TRISB0=0;
  18. //select internal instruction cycle clock
  19. T0CS=0;
  20. //Select timer 0 prescaler
  21. PSA=0;
  22. //select 1:256 prescaler
  23. OPTION_REGbits.PS=0x07;
  24. //Enable timer0 interrupt
  25. T0IE=1;
  26. //Enable interrupt
  27. GIE=1;
  28. //Clear Timer 0
  29. TMR0=0;
  30. while(1);
  31. }
  32. //Timer0 Interrupt Service Routine - ISR
  33. void interrupt _T0_ISR(void){
  34. if(T0IF&&T0IE){
  35. //increase counter
  36. cnt+=1;
  37. //make a 10 mS time
  38. TMR0=-200;
  39. //Clear Flag
  40. T0IF=0;
  41. }
  42. if(cnt>=50){
  43. //toggle RB0
  44. LED^=1;
  45. //Clear Counter
  46. cnt=0;
  47. }
  48. }

Configuration settings:

  1. // PIC16F877A Configuration Bit Settings
  2.  
  3. // CONFIG
  4. #pragma config FOSC = HS
  5. #pragma config WDTE = OFF
  6. #pragma config PWRTE = OFF
  7. #pragma config BOREN = ON
  8. #pragma config LVP = OFF
  9. #pragma config CPD = OFF
  10. #pragma config WRT = OFF
  11. #pragma config CP = OFF
  12.  

Schematic Diagram:

PIC16F877A Timer0 Interrupt Programming
Schematic Diagram

Click here to download source file.

No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)