Thursday, November 9, 2023

PIC16F84A SR-HC04 And 3-Digit Multiplexing Display Using XC8

In previous post, I use a character LCD to display distance measurement result. However we can use a 7-Segment display instead because this display has a large size. Seven-Segment display is very very easy to find in most of local electronics components store.

 

PIC16F84A SR-HC04 And 3-Digit Multiplexing Display Using XC8
Simulating Program

This program is similar to the previous example. It uses the same timer and pre-scaler. I use timer 0 interrupt timer tick to drive the display. The timer interrupt period is 1µs tick. The distance measurement process use timer 0 interrupt flag counting. The polling method is use for getting the duration of the echo high TTL signal from the distance sensor in µs. Even timer 0 is 8-bit wide we can get a longer duration by using the timer0 interrupt service routine.

  1.  
  2. #include <xc.h>
  3.  
  4. // PIC16F84A Configuration Bit Settings
  5. // CONFIG
  6. #pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
  7. #pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
  8. #pragma config PWRTE = OFF // Power-up Timer Enable bit (Power-up Timer is disabled)
  9. #pragma config CP = OFF // Code Protection bit (Code protection disabled)
  10.  
  11. #define _XTAL_FREQ 4000000UL
  12. #define TRIGGER RA3
  13. #define ECHO RA4
  14.  
  15. volatile uint8_t T0OV=0;
  16. volatile uint16_t myCounter=0,myDisplayTime=0;
  17.  
  18. void interrupt T0_ISR(void){
  19. if(T0IF){
  20. T0OV+=1;
  21. myCounter++;
  22. myDisplayTime++;
  23. T0IF=0;
  24. }
  25. }
  26.  
  27. uint16_t getDistance=0,temp=0;
  28.  
  29. void readDistance(void){
  30. TRIGGER=1;
  31. __delay_us(10);
  32. TRIGGER=0;
  33. __delay_us(30);
  34. while(ECHO==0){
  35. /*Waiting For High Pulse Response, Or Sensor Present*/
  36. temp++;
  37. __delay_us(10);
  38. if(temp>=20) break;
  39. }
  40. TMR0=0;
  41. T0OV=0;
  42. while(ECHO==1);
  43. temp=(T0OV<<8)+TMR0+20;
  44. getDistance=temp/58;
  45.  
  46. if(getDistance>=433){
  47. getDistance=0;
  48. }
  49.  
  50. temp=0;
  51. T0OV=0;
  52. TMR0=0;
  53. }
  54.  
  55. int main(void){
  56. uint8_t data7[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
  57. PORTA=0;
  58. TRISA=0x10;
  59.  
  60. PORTB=0;
  61. TRISB=0;
  62. T0CS=0;
  63. PSA=1;
  64. OPTION_REGbits.PS=0;
  65. T0IE=1;
  66. GIE=1;
  67. T0IF=0;
  68.  
  69.  
  70. TMR0=0;
  71.  
  72. while(1){
  73. if(myCounter>=2000){
  74. readDistance();
  75. myCounter=0;
  76. }
  77. if(myDisplayTime>=60) myDisplayTime=0;
  78. switch(myDisplayTime){
  79. case 0:
  80. PORTA=0;
  81. PORTB=data7[getDistance/100];
  82. RA0=1;
  83. break;
  84. case 20:
  85. PORTA=0;
  86. PORTB=data7[(getDistance%100)/10];
  87. RA1=1;
  88. break;
  89. case 40:
  90. PORTA=0;
  91. PORTB=data7[getDistance%10];
  92. RA2=1;
  93. break;
  94. }
  95. }
  96. return 0;
  97. }

 

Click here to download its 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)