728x90

728x90

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

320x50

Search This Blog

tyro-728x90