Sunday, April 24, 2022

Using Timer0 Overflow Interrupt of PIC16F877A to Drive Multiplexing Display

I have showed an example of Timer 0 interrupt programming of PIC16F877A  in an earlier post. Here I use timer 0 interrupt to drive a multiplexing seven-segments display. This method is very effective in any application that contain many tasks polling.

In this example, timer 0 overflow interrupt will create a timer tick around 2.56 milliseconds. This timer tick regularly activate each digits of the multiplexing seven-segment display. 

Using Timer0 Overflow Interrupt of PIC16F877A to Drive Multiplexing Display
Sample program

In main program loop, the program just check whenever the button is press. It will need to wait until the button is released until it increase the press counter. 

The display shows the counting content up to 60 before it reset to 0. The display show the counting content with any flickering due to the long-time button pressing.

Source Code:

  1. /*
  2.  * PIC16F877A Timer0 Interrupt and multiplexing
  3.  * display example
  4.  */
  5. #include <xc.h>
  6. #include "config.h"
  7.  
  8. //RB0 input button
  9. #define SW1 RB0
  10.  
  11. unsigned int cnt=0,cntPress=0;
  12. //Common cathode display pattern
  13. char cCathode[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
  14.  
  15. int main(void){
  16. //Clear PortD
  17. PORTD=0x00;
  18. //Clear PortC
  19. PORTC=0x00;
  20. //Clear PortB
  21. PORTB=0x00;
  22. //PortD output
  23. TRISD=0x00;
  24. //PortC output
  25. TRISC=0x00;
  26. //RB0 is input
  27. TRISB0=1;
  28. //Turn on PortB Pullup resistors
  29. nRBPU=0;
  30. //select internal instruction cycle clock
  31. T0CS=0;
  32. //Select timer 0 prescaler
  33. PSA=0;
  34. //select 1:256 prescaler
  35. OPTION_REGbits.PS=0x07;
  36. //Enable timer0 interrupt
  37. T0IE=1;
  38. //Enable interrupt
  39. GIE=1;
  40. T0IF=0;
  41. //Clear Timer 0
  42. TMR0=0;
  43. while(1){
  44. //Switch pressing count
  45. if(SW1==0){
  46. //Wait until it's release
  47. while(SW1==0);
  48. cntPress+=1;
  49. }
  50. if(cntPress>=60) cntPress=0;
  51. }
  52. }
  53.  
  54. //Timer0 Interrupt Service Routine - ISR
  55. void interrupt _T0_ISR(void){
  56. if(T0IF){
  57. //increase counter
  58. cnt+=1;
  59. //make a 10 mS time
  60. TMR0=-50;
  61. //Clear Flag
  62. T0IF=0;
  63. }
  64.  
  65. switch(cnt){
  66. case 1:
  67. PORTC=0x00;
  68. PORTD=cCathode[cntPress/10];
  69. RC0=1;
  70. break;
  71.  
  72. case 2:
  73. PORTC=0x00;
  74. PORTD=cCathode[cntPress%10];
  75. RC1=1;
  76. break;
  77.  
  78. case 3:
  79. cnt=0;
  80. break;
  81. }
  82. }

Configuration bits:

  1.  
  2. // PIC16F877A Configuration Bit Settings
  3.  
  4. // CONFIG
  5. // Oscillator Selection bits (HS oscillator)
  6. #pragma config FOSC = HS
  7. // Watchdog Timer Enable bit (WDT disabled)
  8. #pragma config WDTE = OFF
  9. // Power-up Timer Enable bit (PWRT disabled)
  10. #pragma config PWRTE = OFF
  11. // Brown-out Reset Enable bit (BOR enabled)
  12. #pragma config BOREN = ON
  13. /*
  14.  * Low-Voltage (Single-Supply) In-Circuit Serial
  15.  * Programming Enable bit (RB3 is digital I/O,
  16.  * HV on MCLR must be used for programming)
  17.  */
  18. #pragma config LVP = OFF
  19. /*
  20.  * Data EEPROM Memory Code Protection bit
  21.  * (Data EEPROM code protection off)
  22.  */
  23. #pragma config CPD = OFF
  24. /*
  25.  * Flash Program Memory Write Enable bits
  26.  * (Write protection off; all program memory
  27.  * may be written to by EECON control)
  28.  */
  29. #pragma config WRT = OFF
  30. /*
  31.  * Flash Program Memory Code Protection bit
  32.  * (Code protection off)
  33.  */
  34. #pragma config CP = OFF
  35.  
  36.  
  37.  

Schematic:

Using Timer0 Overflow Interrupt of PIC16F877A to Drive Multiplexing Display
Using Timer0 Overflow Interrupt of PIC16F877A to Drive Multiplexing Display

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)