Embedded Systems PIC16F
Embedded Systems PIC16F
Embedded Systems PIC16F
PIC Microcontroller
By Muluneh H. 18/05/2018 1
Outline of the Lecture
Introduction
PIC16F877A Architecture:
• The power supply
• The reset
• The clock sources
• Watchdog timers
• The configuration registers
• Ports
• Interrupts
• Timers
• Capture/compare/PWM modules (CCP)
• Analog-to-Digital Converter (A/D) Module
• Serial Communication
• Program memory organization
• Data memory organization
Summary
• Data width
8 / 16/ 32 bits
Wider integer ⇒ higher precision arithmetic
• Instruction width
12 / 14 / 16 / 32 bits
Wider instruction ⇒ more complex instructions + higher precision arithmetic
• The remaining pins deal with power/ground, the clock signal, and programmer I/O.
• 5 Power Pins
Power to Pin 1 (Vpp) – connected via 47k Resistor
Power to Pin 11 (Vdd)
Ground to Pin 12 (Vss)
Power to Pin 32 (Vdd)
Ground to Pin 31 (Vss)
• 2 External Clock Pins
Crystal Oscillator to Pins 13 & 14 (OSC1, OSC2), the polarity doesn’t matter.
Capacitor into power & ground
Output Output
CPU Data Bus
Data Current
Latch Driver
Write data bit
Input
Data
Read data bit Latch
Analogue input
multiplexer
• There are two ways for the processor to get information from the
external devices:
using polling
using interrupts
• Polling:
The microcontroller accesses the external device at the exact time interval , and gets
the required information.
The programmer is the one who determines the time intervals in which
microcontroller contacts the device.
In the Polling method, the PIC microcontroller must "access by itself" the device and
ask for the information it needs for processing.
The main drawback of using polling method for writing a program is
It waste the time of microcontroller, which needs to wait and check whether the new information has
arrived.
not suitable for time critical or real time system
}
• This function is a special function.
• The interrupt functions is called by the hardware itself and operates by
following steps:
If the interrupt function is loaded into the memory, the function will waits for a moment
for the interrupt to occur;
When the interrupt has occurred, the operating system stops the execution of the main
function and free itself to perform the interrupt function;
After the execution of the interrupt function, the operating system continues to run the
main function from the place it stopped before the interrupt has occurred.
void main()
{
TRISC=0x00; //COnfigure PORTD as output to blink the LEDs
OPTION_REG = (1<<SBIT_PS2); // Timer0 with external freq and 32
as prescalar
TMR0=100; // Load the time value for 1ms delay
TMR0IE=1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
while(1)
{
PORTC = value;
}
Hawassa University, Institute of Technology School of
} Electrical and Computer Engineering 18/05/2018 53
Timer 1:
• The timer TMR1 module is 16-bits timer/counter with the
following features:
16-bit timer/counter with two 8-Bit register TMR1H/TMR1L
Readable and writable
software programmable prescaler upto 1:8
Internal or external clock select
Interrupt on overflow from FFFFh to 00h
Edge select for external clock
• The below table shows the registers associated with PIC16f877A
Timer1 module.
} }
else
{
// Keep incrementing the count till it reaches 2000 to generate 1sec delay
count++;
}
}
}
Hawassa University, Institute of Technology School of
Electrical and Computer Engineering 18/05/2018 63
CAPTURE/COMPARE/PWM (CCP)MODULES
• Capture-Compare-Pulse-Width-Module (CCP) is a special module
designs for modulation and waveform generation applications.
Capture module causes the contents of an internal 16 bit timer, upon detecting an nth rising or
falling edge, to be written to on-board special function registers
Compare module generates an interrupt, or change on output pin, when Timer 1
matches a pre-set comparison value
PWM module generate a square waveform with a specified period and duty cycle.
• PIC16F877A microcontroller has two independent Capture/Compare/PWM
(CCP) modules, named as CCP1 and CCP2.
• Each CCP module has two 8-bit registers (CCPxH, CCPxL) that can be use as:
16 bit Capture Register
16 bit Compare Register
10-bit PWM Register
• Note: Where x will 1 for PWM module one and 2 for PWM module
two.
while (1) {
for (dutyCycle = 0; dutyCycle < 100; dutyCycle++) // Keep increasing the dutyCyle to increase the brightness of LED
{
CCPR1L = dutyCycle;
delay(1000);
}
for (dutyCycle = 100; dutyCycle > 0; dutyCycle--) // Keep reducing the dutyCyle to decrease the brightness of LED
{
CCPR1L = dutyCycle;
delay(1000);
}
}
}
Hawassa University, Institute of Technology School of
Electrical and Computer Engineering 18/05/2018 73
Analog-to-Digital Converter (A/D) Module
• The A/D module converts an analog input voltage into a digital
number so it can be processed by a microcontroller or any other
digital system.
• PIC16F877A has an inbuilt 10 bit Successive Approximation ADC
which is multiplexed among 8 input pins.
• The A/D module has high and low-voltage reference input that is
software selectable to some combination of VDD, VSS, RA2 or RA3.
• With 5v as the Vref the resolution of PIC16f877A ADC can be
determined as below:
Resolution of ADC = Vref/(210-1) = 5/1023 =0.004887 = 4.887mv
• Port A and Port E of ADC input pins multiplexed with other GPIO
pins.
• The ADC pin can be enabled by configuring the corresponding
ACON1 register.
• When the ADC function is selected for a pin ,then other Digital signals
are disconnected from the ADC input pins.
char UART_RxChar()
{
while(RCIF==0); // Wait till the data is received
RCIF=0; // Clear receiver flag
return(RCREG); // Return the received data to calling function
}