The General Instruments Microcontrollers created a PIC or peripheral interface microcontroller in 1993. This microcontroller was controlled by suitable software & programmed in such a way that it performed numerous tasks. Compared to other microcontrollers, these types are nearly functional, simple to discover & economical. Similarly, the PIC16F84A microcontroller manufactured by Microchip Technology Inc. is a convenient and amazing PIC microcontroller that helps you add some logic to your electronic projects. This article is about the PIC16F84A microcontroller, pinout, specifications, and its applications.
What is the PIC16F84A Microcontroller?
PIC16F84A is an 8-bit microcontroller from the PIC16F family from Microchip Technology. In 1998, the PIC16C84 microcontroller was presented as a successor to the first sequentially programmable PIC microcontroller. This microcontroller has a high-performance RISC CPU and, an 8-bit timer & is available with a serial programming interface ability that shows to be extremely useful for laying out serial communication through other devices. It operates at 20MHz CLK frequency.
PIC16F84A microcontroller has EEPROM -64 bytes, program memory 1 K, and Data memory -8 bytes which indicates the available memory for your program & data. This controller has 13-GPIO pins which help in controlling projects. This microcontroller can be used in DIY projects & industrial applications.
Pin Configuration:
The pin configuration of the PIC16F84A microcontroller is shown below. This microcontroller has 18 pins which are discussed below.
Pins 1,2,3,6,7,8,9,10,11,12,13,17&18 (GPIO pins):
These are 13 GPIO pins which are configured independently either as digital input (or) as digital output. In addition, every pin can either supply (or) absorb 25mA of maximum current for each pin. Thus, each pin drives an LED very easily however cannot drive any relays or DC motor due to the requirement of current for LED is normally < 10mA whereas a DC motor needs >25mA.
Pin-4 (MCLR): This is a memory clear pin which is also known as an active low pin which is utilized to reset the device. Once this pin is kept low when connected to the GND then it resets the device.
Pin-5 (GND): This is the GND pin of the microcontroller & should be connected to the battery negative terminal.
Pin-14 (VDD): This is a voltage supply pin, connected to the +ve terminal of the power supply. This microcontroller operates with 5 volts operating voltage.
Pin 15 & 16 (OSC1/OSC2): These are crystal oscillator pins & the highest frequency of these used is 20MHz. In addition, the power consumption is higher at a higher frequency. Connect both OSC1 & OSC2 pins across 20MHz crystal throughout two 22pF capacitors.
Features & Specifications:
The features & specifications of the PIC16F84A microcontroller are discussed below.
- This PIC microcontroller includes 13 I/O pins which can be individually configured either as i/p or o/p.
- This PIC microcontroller includes 64 bytes of EEPROM memory, used to store data & 1K program memory that specifies the code ability you can burn 68 bytes of RAM.
- This microcontroller includes two kinds of registers like; GPR & SFR. The general purpose registers are used for storing any arbitrary value where you can operate. The special function registers are utilized to execute a variety of functions that can assist in controlling the device.
- This microcontroller has different compilers like; MPLAB C18 & MikroC Pro. The code written within the compiler makes a hex record that is transmitted to the Microcontroller.
- The RAM of this microcontroller includes four banks. In the programming time (or) program writing before accessing any register, you should choose the particular bank that includes that register. If you write the code within assembly language, handling banks may be steep.
- This microcontroller has a USART module.
- The Flash memory of this microcontroller is based on 8-bit microcontroller packs. So a similar microcontroller device can be utilized for prototyping & production.
- Bus width is 8 Bits
- It includes – 18 pins available in PDIP, SOIC, or 20-pin SSOP packages.
- Its processor speed is 5 million instructions each second.
- Program memory is 1750 Bytes.
- RAM is 68 Bytes.
- EEPROM is 64 Bytes
- It has 8 bit – CPU.
- Its operating voltage ranges from 2 to 5.5 V.
- An internal oscillator is not available.
- The external oscillator is 20MHz.
Functions
This PIC microcontroller performs various functions that are fairly similar to the function within other PIC community controllers like; ICSP, Watchdog timer, timer, sleep mode, and power on reset.
It supports ICSP (In-circuit serial programming) which is used widely for in-circuit programming because it includes a USART module.
This microcontroller includes an in-built Watchdog timer which is an internal timer placed in the chip. This timer can enable (or) disable by programming which is used to rest the PIC microcontroller whenever the program goes wrong (or) it may go into to the infinite loop.
PIC16F84a microcontroller includes one 8-bit timer, used in both methods like; timer & counter. In addition, accompanies internal & external CLK select capability.
This IC includes sleep mode that generates a low current power down mode. This mode can be eliminated with an interrupt, watchdog timer & external RESET.
The power-on Reset feature is used in a variety of other PIC microcontrollers whenever it is powered ON. If there is any problem within the microcontroller, switching on the device will dismiss it from the malfunctioning loop within the device.
PIC16F84A Microcontroller Architecture
The PIC16F84A microcontroller architecture is shown below. This architecture includes different blocks which are discussed below.
Flash Program Memory
Flash program memory of this microcontroller is used for storing the program which is 14 bits long & 1024 words. When the power supply is even turned off the flash memory contents of this microcontroller will not be lost. Here, the Flash memory of this microcontroller can be written although the number of times it can be rewritten is restricted to 1000 times.
RAM File Registers
This RAM is accessed by bank switching where every bank includes 80 bytes of memory capacity (00h-4Fh). This memory is separated into two sections like the following.
The primary 12 bytes (00h-0Bh) of every bank is known as SFR, used for recording the operating conditions of the chip, the I/O port conditions & other conditions.
In the SFR, there are mainly 16 different registers where 11 registers are in bank-0 & 5 registers are in bank-1. So, the content of every register is managed through the microcontroller, but there are a total of 24 file registers, most of which are within both banks.
The remaining 68 bytes from 0Ch to 4Fh from byte 13 up known as; General Purpose Registers, are used for storing results & conditions temporally while the program is working.
The GPR contents are the same within both banks, thus the whole capacity is 68 bytes only even with bank switching. The GPR contents are lost whenever the power is off. So there is no boundary to the no. of times data can be rewritten.
EEPROM
This EEPROM is a nonvolatile memory, thus the memory contents are not lost whenever power is off and they can be rewritten through the program. The whole capacity is – 64 bytes & the no. of times it can be rewritten is about one million, so it is not suggested for data storage temporarily. This memory is used for storing data that will not vary frequently.
SFR Registers
This microcontroller has 16 different Special Function Registers which can be specified through the bank switching method. The memory capacity of these registers is 160 bytes only. Each SFR register has different functions.
Program Counter
This is a 13-bit counter which includes the next instruction address to be read from the flash memory. Generally, every time the count increments when an instruction is executed. However when a jump is performed then this counter contents can be rewritten to the jump address.
8 Level Stack
The 8-level stack is used to store the return address of the program whenever a jump is performed.
Instruction Register
The instruction next to the address can be specified through the PC which is read to this register. So this operation is known as a FETCH.
Instruction Decode & Control
The instruction within the IR can be analyzed & the operation is executed.
MUX & ALU
The calculations can be performed by the MUX & ALU.
W Register
The working register is used for storing the ALU contents temporarily. It is crucial for mainly calculation operations. This register contents can be sent to the variety of registers to be used by the program. This register can also be used for controlling the Input/Output ports.
STATUS Register
This status register is used to store the ALU result, register bank select & a time-out condition, etc.
FSR Register
FSR or File Select Register is used for specifying the RAM file register address while using the indirect addressing technique. The register address in the direct addressing technique is specified through the program instructions. In this condition, a 7-bit address from 0 to 127 can be specified which is for a single bank. To modify the bank, here the RP0 bit of the SR should also be specified. When the FSR is 8 bits, it is achievable to specify the address & bank together, saving instruction cycles. While using the file select register, it is suitable to make the file register data region continuous. FSR incrementing simplifies the processing in read & write operations.
Address Multiplexer
It distinguishes between the two direct & indirect addressing.
EDATA
This type of register is utilized whenever reading or writing from data to the EEPROM.
EEADR
EEADR register specifies the address of EEPROM because it is collected with eight bits addresses ranging from 0-255 can be specified. This microcontroller has 64 bytes of memory only & the EEPROM address can be started from 2100h. Whenever data is written throughout program execution, it is essential to write 55h sequentially then AAh toward the EECON2 register.
Timer
The PIC16F84A microcontroller includes one timer only which is 8-bit TMR0. Whenever the count achieves 256 & the INCON register’s TOIF bit of the SFR turns into “1” then it times out. It is achievable to make an interruption take place with a timed-out condition. To make the interrupt happen the TOIE & GIF bits of the INTCON register of the SFR have to be set (1)
I/O Ports
This microcontroller has 13 I/O pins totally including individual direction controls. Each pin mode can be set in the program. These pins are separated into two groups; Port A which includes five pins & Port B which has eight pins.
Timing Generation
This microcontroller generates the CLK pulses which decide the operation speed. It executes a single instruction for each four CLK pulses with pipeline architecture. When a JUMP is performed then two cycles are required. The execution time for an instruction is 200ns with a 20MHz CLK.
PIC16F84A Microcontroller Interfacing with HC-SR04 Ultrasonic Sensor
The PIC16F84A microcontroller interfacing with the HC-SR04 ultrasonic sensor is shown below. Here, the HC-SR04 ultrasonic sensor measures distances from 2 to 400 with a 3mm accuracy. This sensor includes four pins; 5V power supply (VCC), Trigger i/p pin (Trig), Echo o/p pin (Echo) & Ground (GND).
The required components to make this interfacing mainly include a PIC16F84A microcontroller, HC-SR04 ultrasonic sensor, and LCD. The circuit below is the main connection between the PIC16F84A microcontroller, the HC-SR04 sensor & LCD. Here, LCD is used for displaying the distance measured in cms. Connect the circuit as per the diagram shown below.
At first, we have to supply to trigger pin of the sensor with a 10µs pulse & this sensor will send 8 8-cycle bursts of ultrasound automatically at 40 kHz & raise its echo pin. The range can be calculated throughout the time interval in between transmitting the trigger signal & receiving the echo signal by using the formula; the range = high-level time x velocity (340M/S)/2.
Code
The required code for the PIC16F84A microcontroller interfacing with the HC-SR04 ultrasonic sensor is given below. In the below code, Timer0 is mainly configured to increase by 1 each 1 us with 8MHz mcu frequency. This timer helps measure the pulse widths from the Echo pin of the sensor. The pulse time of the Echo pin is equal to count * 256 + get_timer0().
#include <htc.h>
#include <stdio.h>
// Configuration bits
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF);
#define _XTAL_FREQ 8000000 // 8 MHz crystal oscillator
// Define pins for HC-SR04
#define TRIG RB0
#define ECHO RB1
// Define pins for LCD
#define RS RB2
#define EN RB3
#define D4 RB4
#define D5 RB5
#define D6 RB6
#define D7 RB7
// Function prototypes
void MCU_Init();
void send_trigger();
unsigned int measure_distance();
void LCD_Init();
void LCD_Command(unsigned char);
void LCD_Char(unsigned char);
void LCD_String(const char*);
void LCD_Set_Cursor(unsigned char, unsigned char);
// Function to initialize the MCU
void MCU_Init() {
TRISB = 0b00000010; // Set RB0, RB2-RB7 as outputs; RB1 as input
OPTION_REG = 0b00000000; // Configure prescaler for Timer0 (1:2)
T0CS = 0; // Timer0 clock source is the internal instruction cycle clock (FOSC/4)
PSA = 0; // Prescaler is assigned to Timer0
PS2 = 0; PS1 = 0; PS0 = 0; // Prescaler rate select bits (1:2), giving us 1 us per increment at 8 MHz
}
// Function to send trigger pulse
void send_trigger() {
TRIG = 1; // Set TRIG high
__delay_us(10); // Delay 10 microseconds
TRIG = 0; // Set TRIG low
}
// Function to measure distance
unsigned int measure_distance() {
unsigned int distance;
unsigned int timer_count = 0;
send_trigger(); // Send trigger pulse
// Wait for the ECHO pin to go high
while (!ECHO);
// Reset Timer0
TMR0 = 0;
T0IF = 0; // Clear Timer0 overflow flag
// Wait for the ECHO pin to go low
while (ECHO) {
if (T0IF) {
T0IF = 0;
timer_count += 256; // Timer0 overflows every 256 microseconds
}
}
timer_count += TMR0; // Add the final timer value to the count
// Calculate distance (speed of sound is 34300 cm/s)
// Distance = (time_us / 2) * (speed_of_sound / 1e6)
distance = (timer_count / 2) / 58;
return distance;
}
// Function to initialize the LCD
void LCD_Init() {
LCD_Command(0x02); // Initialize LCD in 4-bit mode
LCD_Command(0x28); // 2 lines, 5×7 matrix
LCD_Command(0x0C); // Display on, cursor off
LCD_Command(0x06); // Increment cursor
LCD_Command(0x01); // Clear display
__delay_ms(2);
}
// Function to send a command to the LCD
void LCD_Command(unsigned char cmd) {
RS = 0; // Command mode
EN = 1;
PORTB = (PORTB & 0x0F) | (cmd & 0xF0);
EN = 0;
__delay_us(200);
EN = 1;
PORTB = (PORTB & 0x0F) | ((cmd << 4) & 0xF0);
EN = 0;
__delay_ms(2);
}
// Function to send a character to the LCD
void LCD_Char(unsigned char data) {
RS = 1; // Data mode
EN = 1;
PORTB = (PORTB & 0x0F) | (data & 0xF0);
EN = 0;
__delay_us(200);
EN = 1;
PORTB = (PORTB & 0x0F) | ((data << 4) & 0xF0);
EN = 0;
__delay_ms(2);
}
// Function to send a string to the LCD
void LCD_String(const char* str) {
while (*str) {
LCD_Char(*str++);
}
}
// Function to set the cursor position
void LCD_Set_Cursor(unsigned char row, unsigned char column) {
unsigned char position;
if (row == 1) {
position = 0x80 + (column – 1);
} else if (row == 2) {
position = 0xC0 + (column – 1);
}
LCD_Command(position);
}
void main() {
unsigned int distance;
char buffer[16];
MCU_Init(); // Initialize the MCU
LCD_Init(); // Initialize the LCD
while (1) {
distance = measure_distance(); // Measure distance
sprintf(buffer, “Distance: %ucm”, distance); // Format the distance
LCD_Set_Cursor(1, 1); // Set cursor to the first line
LCD_String(buffer); // Display the distance
__delay_ms(500); // Delay 500 milliseconds before next measurement
}
}
The explanation of the code:
- MCU Initialization:
- The MCU_Init function sets up the necessary I/O pins and configures Timer0.
- Timer0 is set with a prescaler of 2, which results in a timer tick every 1 microsecond.
- Trigger Pulse:
- The send_trigger function sends a 10 microsecond pulse to the TRIG pin of the HC-SR04.
- Distance Measurement:
- The measure_distance function waits for the ECHO pin to go high, starts the timer, and then waits for it to go low, while counting the duration in microseconds.
- The distance is calculated based on the time the ECHO pin remains high, using the speed of sound.
- Main Loop:
- In the main loop, the distance is measured and can be used or displayed as needed.
- Ensure you have correctly configured the MPLAB project settings, including the correct frequency setting and inclusion of necessary header files for your setup.
Once the code is uploaded, give the supply to the circuit. After that, move the object in front of the sensor then the distance will be displayed on the LCD based on the object.
Applications
PIC16F84A microcontroller applications include the following.
- PIC16F84 microcontroller applications range from automotive industries & home appliances controlling to industrial instruments, electrical door locks, safety devices & remote sensors
- This microcontroller is also ideal for smart cards & battery-supplied devices due to its low power consumption.
- EEPROM memory of this microcontroller will make this IC used wherever various permanent parameters storage is needed.
- PIC16F84 microcontroller is even applicable in areas where microcontrollers had not been considered previously due to low consumption, less cost, flexibility, and easy handling.
- This microcontroller is used in student projects, central heating projects, gas sensor projects, etc.
This IC is used in temperature data logger production, in security systems, industrial automation, embedded systems, etc. - This microcontroller is used in different applications which include; automotive systems, consumer electronics, industrial automation, medical devices, home automation, etc.
- The PIC16F84A is frequently used as the CPU in a variety of embedded control systems like; Home automation & control systems, appliance control, Temperature & environmental monitoring as well and control.
- This is a popular choice for hobbyists & making DIY projects.
- These are used in security systems; they can work as a timer/counter within applications that need exact timing (or) event counting.
- This microcontroller can be used still for basic serial communication interfaces like; SPI, I2C, or UART to interface through other devices.
Please refer to this link for the PIC16F84A microcontroller datasheet.
Thus, this is an overview of the PIC16F84A Microcontroller, pin configuration, specifications, interfacing, and its applications. It is a very famous 8-bit microcontroller often utilized in different embedded systems & electronics projects due to its low cost, simple of use & versatility. What does PIC stand for?