Friday, November 10, 2023

PIC16F84A Simple Frequency Meter With LCD Using XC8

Frequency meter can be made using a conventional digital ICs with a big size, and complicated circuit connection. A small 8-bit micro-controller could do this job using a low level Assembly language or even a higher level C language.

In this example, I use an 8-bit PIC16F84A micro-controller to count external TTL input pulse. The total count will update for every seconds.

PIC16F84A Simple Frequency Meter With LCD Using XC8
Simulating Program


I use RA4/T0CKI (Timer0 External Clock Input) pin to count external TTL pulse. Since Timer0 is only 8-bit wide the maximum counting is only 255 counts. So I add Timer0 Interrupt to increase the maximum counts. I use XC8 built-in delay function to create a precise 1000 ms (1 second) delay before the summation of external pulse is evaluated. A 16x2 character LCD is suitable for this example.

  1. #include <stdio.h>
  2. #include <xc.h>
  3.  
  4. #define _XTAL_FREQ 4000000UL
  5.  
  6. #include "LCD4Bits.h"
  7.  
  8. void tmr0Init(void){
  9. PORTA=0;
  10. TRISA4=1;
  11. T0CS=1;
  12. T0SE=0;
  13. PSA=1;
  14. OPTION_REGbits.PS=0;
  15. T0IE=1;
  16. GIE=1;
  17. T0IF=0;
  18. }
  19.  
  20. long TMR0H=0;
  21.  
  22. void interrupt T0_ISR(void){
  23. if(T0IF){
  24. TMR0H+=1;
  25. T0IF=0;
  26. }
  27. }
  28.  
  29. int main(void){
  30. unsigned char freq[10];
  31. uint32_t temp=0;
  32. PORTB=0;
  33. TRISB=0;
  34. lcdInit();
  35. tmr0Init();
  36. lcdXY(4,1);
  37. lcdString("PIC16F84A");
  38. lcdXY(1,2);
  39. lcdString("Frequency Meter");
  40. __delay_ms(1000);
  41. lcdCommand(CLEAR_SCREEN);
  42. __delay_ms(5);
  43. lcdXY(4,1);
  44. lcdString("Frequency:");
  45. TMR0H=0;
  46. TMR0=0;
  47. while(1){
  48. __delay_ms(1000);
  49. temp=(TMR0H<<8)+TMR0;
  50. lcdXY(6,2);
  51. sprintf(freq,"%uHz ",temp);
  52. lcdString(freq);
  53. TMR0H=0;
  54. TMR0=0;
  55. }
  56. return 0;
  57. } 

I can not test it on bread-board because this chip was burn out. And I only have some newer PIC chips. So this program can only be tested using a simulator like Proteus. We can use another PIC chip like the PIC16F628A, and CCS PICC compiler

PIC16F84A Simple Frequency Meter With LCD Using XC8
Resource Usage

 

This program require 69.5% of program memory, and 89.7% of data memory. Click here to download this example.


 

Thursday, November 9, 2023

PIC16F84A EEPROM Reading And Writing Example Using XC8

PIC16F84A contains 68 bytes of EEPROM non-volatile memory. It's useful for storing some setting parameters when the MCU is powered off. EEPROM has a slower access time than SRAM and Flash memory. But it has a higher endurance compares to Flash memory. We can erase or write EEPROM up to 10 million times.

PIC16F84A EEPROM Reading And Writing Example Using XC8
Running Program


Using a low level Assembly language or C programming language without EEPROM library, we can access to this memory via these four SRFs,

  1. EEDATA - EEPROM Data Register
  2. EEADR   - EEPROM Address Register
  3. EECON1
  4. and EECON2.

EEPROM Control Regsiter 1 (EECON1) has some control bits to read, write, and status checking of EEPROM operations. 

PIC16F84A EEPROM Reading And Writing Example Using XC8
EECON1 Register

 

Using XC8 we can use all of these registers to read or write to EEPROM. However XC8 compiler has a built-in EEPROM library that's very easy to use. To read or write EEPROM, we only need these functions,

  1. EEPROM_READ(address)
  2. EEPROM_WRITE(address, data) .

We just include the "xc.h" header file to call these functions.

Example #1 : EEPROM Writing And Reading

In this example, the program write a set of ASCII characters to EEPROM starting from address 0. Then the controller read them back, and they will be shown on virtual terminal. I use software UART (bit banging) to process serial data.

  1.  
  2. /*
  3.  for 9600 baud rate
  4.  * 1/9600 = 104uS per bit
  5.  * we can use 102.5uS per bit or above
  6.  */
  7.  
  8. #include <xc.h>
  9.  
  10. // PIC16F84A Configuration Bit Settings
  11.  
  12. // CONFIG
  13. #pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
  14. #pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
  15. #pragma config PWRTE = OFF // Power-up Timer Enable bit (Power-up Timer is disabled)
  16. #pragma config CP = OFF // Code Protection bit (Code protection disabled)
  17.  
  18. #define _XTAL_FREQ 4000000
  19. #define TX RA0
  20. #define RX RA1
  21.  
  22. void sendChar(char data){
  23. TX=0;
  24. __delay_us(100);
  25. for(int i=0;i<8;i++){
  26. TX=data&(1<<0);
  27. data>>=1;
  28. __delay_us(75);
  29. }
  30. TX=1;
  31. __delay_us(100);
  32. }
  33.  
  34. char myChar=0,temp=0;
  35. void receiveChar(void){
  36. if(RX==0){
  37. __delay_us(100);
  38.  
  39. for(char i=0;i<8;i++){
  40. __delay_us(60);
  41. if(RX==1) myChar|=(1<<i);
  42. else myChar&=~(1<<i);
  43. }
  44. __delay_us(100);
  45. }
  46. }
  47.  
  48. void sendText(char *text){
  49. while(*text) sendChar(*text++);
  50. }
  51.  
  52. void softUARTInit(void){
  53. PORTA=0x00;
  54. TRISA=0x02;
  55. PORTB=0;
  56. TRISB=0;
  57. TX=1;
  58. }
  59.  
  60. void main(){
  61.  
  62. softUARTInit();
  63. sendText("PIC16F84A Software UART Read EEPROM Write And Read\r\n");
  64. for(uint8_t i=0;i<43;i++) {EEPROM_WRITE(i,i+48);while(WR==1);}
  65. for(uint8_t i=0;i<43;i++) {sendChar(EEPROM_READ(i)); while(RD==1);}
  66. sendText("\r\n");
  67. while(1){
  68.  
  69. }
  70. }
  71.  

Click here to download this example.


 

Example #2: Initializing EEPROM Data

Without using the EEPROM_WRITE function, we can initialize the EEPROM space. We use the __EEPROM_DATA() macro. The input is an 8 parameters. Each parameters are the 1 byte EEPROM data. For example,

__EEPROM_DATA('H','E','L','L','O','S','I','R'); .

In this example, I initialize some ASCII characters on EEPROM space. Then the program read them back before they are sent over the software UART terminal.

  1. /*
  2.  for 9600 baud rate
  3.  * 1/9600 = 104uS per bit
  4.  * we can use 102.5uS per bit or above
  5.  */
  6.  
  7. #include <xc.h>
  8.  
  9. // PIC16F84A Configuration Bit Settings
  10.  
  11. // CONFIG
  12. #pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
  13. #pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
  14. #pragma config PWRTE = OFF // Power-up Timer Enable bit (Power-up Timer is disabled)
  15. #pragma config CP = OFF // Code Protection bit (Code protection disabled)
  16.  
  17. #define _XTAL_FREQ 4000000
  18. #define TX RA0
  19. #define RX RA1
  20.  
  21. __EEPROM_DATA('H','E','L','L','O','S','I','R');
  22. __EEPROM_DATA('P','I','C','1','6','F','8','4');
  23. __EEPROM_DATA(' ','S','i','m','p','l','e',' ');
  24. __EEPROM_DATA('U','A','R','T',' ','P','r','o');
  25. __EEPROM_DATA('g','r','a','m','m','i','n','g');
  26. __EEPROM_DATA(' ','W','i','t','h',' ','M','P');
  27. __EEPROM_DATA('L','A','B','X',' ','I','D','E');
  28. __EEPROM_DATA(' ','X','C','8',' ','C',' ','.');
  29.  
  30. void sendChar(char data){
  31. TX=0;
  32. __delay_us(100);
  33. for(int i=0;i<8;i++){
  34. TX=data&(1<<0);
  35. data>>=1;
  36. __delay_us(75);
  37. }
  38. TX=1;
  39. __delay_us(100);
  40. }
  41.  
  42. char myChar=0,temp=0;
  43. void receiveChar(void){
  44. if(RX==0){
  45. __delay_us(100);
  46.  
  47. for(char i=0;i<8;i++){
  48. __delay_us(60);
  49. if(RX==1) myChar|=(1<<i);
  50. else myChar&=~(1<<i);
  51. }
  52. __delay_us(100);
  53. }
  54. }
  55. void sendText(char *text){
  56. while(*text) sendChar(*text++);
  57. }
  58.  
  59. void softUARTInit(void){
  60. PORTA=0x00;
  61. TRISA=0x02;
  62. PORTB=0;
  63. TRISB=0;
  64. TX=1;
  65. }
  66.  
  67. void main(){
  68.  
  69. softUARTInit();
  70. sendText("PIC16F84A Software UART Read EEPROM Example 1\r\n");
  71. for(uint8_t i=0;i<8;i++) sendChar(EEPROM_READ(i));
  72. sendText("\r\n");
  73. for(uint8_t i=8;i<64;i++) sendChar(EEPROM_READ(i));
  74. while(1){
  75.  
  76. }
  77. }
  78.  

Click here to download its source file.

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.

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)