728x90

728x90

Showing posts with label PIC16F877A. Show all posts
Showing posts with label PIC16F877A. Show all posts

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.


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.

320x50

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (57) AVR (58) CCS PICC (28) DAC (1) DHT11 (2) Display (106) Distance Sensor (3) DS18B20 (3) dsPIC (3) dsPIC30F1010 (3) dsPIC30F2010 (1) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (68) Interrupt (19) Keil (5) Keypad (10) LCD (48) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (73) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (10) PCD8544 (3) PCF8574 (5) PIC (108) PIC12F (3) PIC16F628A (3) PIC16F630 (2) PIC16F716 (4) PIC16F818 (11) PIC16F818/819 (3) PIC16F84A (16) PIC16F876A (2) PIC16F877A (9) PIC16F88 (2) PIC16F887 (60) PIC18 (19) PIC18F1220 (5) PIC18F2550 (5) PIC18F4550 (12) PICKit2 (1) PWM (11) RTC (9) Sensor (11) 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 (96)

tyro-728x90