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.


 

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 (54) 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 (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) 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 (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)