Professional Documents
Culture Documents
ch3 - Lec1 - Introduction To Microchip Microcontrollers (PIC 18F)
ch3 - Lec1 - Introduction To Microchip Microcontrollers (PIC 18F)
Interfacing
Ch3_lec1_
Introduction to
microchip micro-
controllers (PIC18FX)
Kassahun Tamir 1
Outline
Introduction
Architecture of 18F micro-controller
Memory Organization
Oscillator Configuration
I/O Ports
Interrupts
Timers
PWM
ADC
Comparators
Instruction sets Kassahun Tamir 2
Introduction
Micro-controllers (8051, PIC, ARM)
PIC – Peripheral Interface Controller
PIC families include 10xxx, 12xxx,
14xxx, 16xxx, 17xxx, and 18xxx
8-bit MCUs
✗ PIC10, PIC12, PIC16, PIC18
16-bit MCUs
✗ PIC24
32-bit MCUs
✗ PIC32 Kassahun Tamir 3
Introduction
PIC families come in 18 to 80 pin packages.
Kassahun Tamir 4
Architecture of 18F micro-controller
It is an 8-bit Micro-controller
It has 16-bits Instruction sets
256 byte of EPROM
2 KB SRAM
32 KB Flash Memory
It operates at 48 MHz Crystal Oscillator
It has a 10-bit A/D Converter
It supports Instruction Pipelining
It is implemented with nanoWatt Technology (low power
consumption) Kassahun Tamir 5
Architecture of 18F micro-controller
RISC architecture
On-chip program ROM, data RAM, data EEPROM, timers,
ADC, USART, and I/O Ports
✗ ROM, data RAM, data EEPROM, and I/O ports sizes
varies within PIC18 family
Priority levels for interrupts
Kassahun Tamir 6
Architecture of 18F
Kassahun Tamir 7
Architecture of 18F
Kassahun Tamir 8
Memory Organization
ROM – program or code ROM
–PIC18 can have up to 2 megabytes (2M) of ROM
UV-EPROM for program memory– must have special
eraser/programmer (burner ~20 minutes to erase)
Flash memory - PIC18F458 use for program
development, can be reprogrammed again and again
OTP Version - PIC16C452 use for final production
version, one time programmable (ROM)
EEPROM – electrically erasable PROM, additional
“permanent” memory Kassahun Tamir 9
Memory Organization
Data Memory and Program
Memory are separated
(Harvard Architecture)
Separation of Data memory
and Program memory makes possible the simultaneous
access of data and instruction
Data memory registers are used as general purpose
registers or special function registers (SFR)
Kassahun Tamir 10
Memory Organization- Data Memory
It consists of General Purpose Registers (GPR = RAM) and
Special Function Registers (SFRs). Both are referred to as
Data Registers or File Registers
Each location is 8-bit wide
PIC-18 has 4KB Data Memory
Data Memory is Divided into Banks and each Bank has
256 bytes (16 banks)
General purpose registers are used to hold dynamic data
Special function registers are used to control the
Operation of PeripheralKassahun
functions.
Tamir
Only one bank is active
11
at a time.
Oscillator Configuration
Primary clock source of the micro-controller can be from
An external oscillator or
Kassahun Tamir 12
Oscillator Configuration—Internal
Internal oscillator:
If we do not do anything about choosing oscillator, the
internal oscillator with 4MHz will be selected by default
We can select any frequency up to a maximum of 8 MHZ
Why would anyone want to use less than 8 MHZ, as we
prefer the fastest?
✗ Power consumption
✗ Synchronization
We can select any frequency up to 8 MHZ by configuring
the the 4,5,6 bits of OSCCON register
Kassahun Tamir 13
Oscillator Configuration
Example 1:
i. Create an 8 MHz frequency using the internal oscillator
Kassahun Tamir 14
Oscillator Configuration—Internal
Configure the OSCCON register as:
Kassahun Tamir 15
Oscillator Configuration
Example 1:
i. Choosing frequency from the internal oscillator (8MHZ)
OSCCONbits.IDLEN = 0; // sleep mode after SLEEP instn
OSCCONbits.IRCF = 0b111; // Internal RC Freq. 8MHZ
OSCCONbits.OCS = 0b10; // internal Oscillator
Note:
1. Other bits are status bits and are read-only.
2. If we do not configure the OSCCON register and do not
have external oscillator, 4MHz internal clock is generated
by default.
Kassahun Tamir 16
Oscillator Configuration—External
PIC18F2455/2550/4455/4550 devices can be operated in twelve
distinct oscillator modes, selected by the configuration register,
CONFIG1H. These are:
1. Crystal/Resonator Low Speed < 4MHZ
2. XTPLL Crystal/Resonator with PLL enabled
Kassahun Tamir 19
Oscillator Configuration
In basic programming (In HS, HSPLL, XT and XTPLL
Oscillator modes) a crystal or ceramic resonator is
connected to the OSC1 and OSC2 pins to establish
oscillation.
Kassahun Tamir 20
Oscillator Configuration—External
Configure the CONFIG1L and CONFIG1H registers to choose the
mode. The CONFIG1H configuration register is at address
300001H and is used to select the micro-controller clock sources.
Kassahun Tamir 21
Oscillator Configuration
Example 2:
i. Using an external HS Oscillator of 20MHz, we can create
a system clock of 5MHz.
Kassahun Tamir 22
Oscillator Configuration—External
Configure the CONFIG1L register:
Kassahun Tamir 23
Oscillator Configuration—External
Configure the CONFIG1H register:
Kassahun Tamir 24
Oscillator Configuration
Example 2:
i. Using an external HS oscillator of 20MHz, we can create a
system clock of 5MHz
CONFIG1Lbits.CPUDIV = 0b11// div. an HS input clock by 4
// the remaining bits of CONFIG1L register are set to their
default values, then CONFIG1L = 0b00011000
CONFIG1Hbits.IESO = 1; // internal external switch over
CONFIG1Hbits.FCMEN = 1; // if ext. src. fails switch to
// internal clock
CONFIG1Hbits.FOSC = 0b1100 // HS Oscillator (HS)
Kassahun Tamir 25
I/O Ports
Port Registers
✗ PORTA → 6/7 pins
✗ PORTB → 8 pins
✗ PORTC → 8 pins
✗ PORTD → 8 pins
TRISAbits.RA0 = 0 TRISAbits.RA0 = 1
Kassahun Tamir 28
I/O Ports – PORTx Registers
After we configure the ports, we write to(if it is an
output port) or read from(if it is an input port) ports.
✗ PORTx → Register used to write and read
✗ Example: Reading from and writing to all pins of
PORTA at once
PORTAbits.PORTA0 = 0 PORTAbits.PORTA0 = 1
Kassahun Tamir 30
I/O Ports – LATx Registers
Read-Modify-Write Problems: The actual physical state
(voltage level) of a pin may be different from the value written
to PORTx register in certain instances.
For example:
1.When the output pin is connected to high inductance or
capacitance loads.
2.When the output pin is connected to LEDs without current
limiting resistors.
3.When the output pin drives a transistor without base
resistor.
4.When the output pin is shorted to ground.
If a high value capacitor is connected to a pin, it will take some
time to charge the capacitorKassahun
when Tamir the pin is set to HIGH. 31
I/O Ports – LATx Registers
Example: - We’re going to run the following instructions
PORTB.B0 = 1;
PORTB.B1 = 1;
Assume that the PORTB is initially set to zero, and that all
pins are set to output. Let's say we connect a discharged
capacitor to RB0 pin.
Kassahun Tamir 32
I/O Ports – LATx Registers
PORTB.B0 = 1;
PORTB.B1 = 1;
Kassahun Tamir 33
I/O Ports – LATx Registers
PORTB.B0 = 1;
PORTB.B1 = 1;
Our intention was to read the value
as it was, 0b00000001, and change
it to 0b00000011. Alas, the read
value is 0b00000000. If we
proceed, the modified value will be
0b00000010.
To correct the problem in the code,
insert a delay after each PORTB.Bx
= 1 line, or modify the entire
PORTB register in a single line
PORTB = 0b00000011. Kassahun Tamir 34
I/O Ports – LATx Registers
Kassahun Tamir 35
I/O Ports – LATx Registers
Kassahun Tamir 37
Interrupts - definition
Kassahun Tamir 38
Interrupts – Polling vs Interrupt
Polling:
✗ PIC continuously monitors the status of each device
✗ Each device get the attention of the CPU as the same level of
priority
✗ Wastes MC time by polling devices that do not need service
Interrupt:
✗ Devices get the attention of the CPU only when it needs a
service
✗ Can service many devices with different level of priorities
Kassahun Tamir 39
Interrupts – ISR and IVT
Interrupt Service Routine (ISR):
✗ When an interrupt is invoked the MC runs the Interrupt
Service Routine (ISR)
Kassahun Tamir 40
Interrupts - Steps of Execution
Peripherals:
✗ Timers: Timer0, Timer1, Timer2
✗ ADC
✗ Serial Communication
Software Interrupts:
✗ Comes from a program that is being executed by a micro-
controller
Kassahun Tamir 42
Interrupts – Enabling and Disabling
When the PIC is powered on (resets):
✗ All interrupts are masked (disabled)
✗ The default ISR address is 0008h
✔ No interrupt priorities for interrupt
Kassahun Tamir 43
Interrupts – Registers
Configuration and functioning of interrupts are done with the
help of the following registers:
✗ RCON: Reset Control Register
✗ INTCON: Interrupt Control Register
✗ INTCON2: Interrupt Control Register 2
✗ INTCON3: Interrupt Control Register 3
✗ PIR1, PIR2: Peripheral Interrupt Request Registers
✗ PIE1, PIE2: Peripheral Interrupt Enable Registers
✗ IPR1, IPR2: Peripheral Interrupt Priority Registers
The main functionality of these registers is to store
configuration bits for each interrupt.
Kassahun Tamir 44
Interrupts – Bites Types in Registers
The previously stated registers have three types of bits within.
In general, interrupt sources have three bits to control their
operation. They are:
✗ Enable bit:
✔ That allows program execution to branch to the interrupt
vector address when the flag bit is set
✗ Priority bit:
✔ To select high priority or low priority
✗ Flag bit:
✔ To indicate that an interrupt event occurred
Kassahun Tamir 45
Interrupts – RCON: Reset Control Registers
Kassahun Tamir 46
Interrupts – Interrupt Control Registers
Kassahun Tamir 47
Interrupts – Peripheral Interrupt Request Registers
Kassahun Tamir 48
Interrupts – Peripheral Interrupt Enable Registers
Kassahun Tamir 49
Interrupts – Peripheral Interrupt Priority Registers
Kassahun Tamir 50
Interrupt - Example
Example 1:
✗ Whenever there is an interrupt at RB0
✗ Lit LEDs connected to RA0, RA1, and
RA2
Kassahun Tamir 51
Timers – what for
Kassahun Tamir 54
Timers - How
Kassahun Tamir 55
Timers - Registers
Steps we follow:
1.Calculate the Timer Count for the required delay
2.Set the Prescalar bits in TxCON as per the delay calculation
1.Clear the PSA bits for using the prescalar
2.Select the Clock Source Internal/External using TOCS bit
3.Load the timer value into TMR0 register
4.Enable the Timer0 Interrupt by setting TMR0IE bit
5.Enable the Global and Peripheral interrupts by setting GIE and
PIE bits.
Kassahun Tamir 56
Timers - Example
Kassahun Tamir 57
Timers - Example
Kassahun Tamir 58
Timers - Example
Kassahun Tamir 59
Timers - Registers
Timer Size Control Register Count Register
TIMER0 16-bit T0CON TMR0H,TMR0L
TIMER1 16-bit T1CON TMR1H,TMR1L
TIMER2 8-bit T2CON TMR2
Kassahun Tamir 60
PWM
If we need to take an analog input, we can get the real-time
analog data from a sensor, and then using an analog-to-digital
converter (ADC), convert it to digital data for a microcontroller.
But what if we need to control an analog device from our
microcontroller?
Some microcontrollers have an onboard digital-to-analog
converter (DAC) to output a true analog signal in order to
control analog devices, and we can even use an external DAC.
We achieve the functionality of a DAC using PWM
PWM or Pulse Width Modulation is a technique used to
Kassahun Tamir 61
control analog devices, using a digital signal.
PWM – Duty Cycle
When the signal is high, we call this "on time".
Kassahun Tamir 62
PWM – Duty Cycle
Kassahun Tamir 63
PWM - Application
DC motor speed control
We know that the fan will not instantly achieve the max speed
the moment we turn it on and neither will it immediately
come to a halt as we turnKassahun
it off.Tamir 64
PWM – The Module in PIC18F4550
Kassahun Tamir 65
PWM – The Module in PIC18F4550
CCP-Capture, Compare,
and PWM Module
Kassahun Tamir 66
PWM – Example
Example: Generate a pulse width frequency of 500Hz, using
fosc of 4MHz, prescalar 1:16, and duty cycle of 25%. (In
practice the duty cycle is the one we modify to increase or
decrease the “On Time”.In other words to increase or decrease
the speed of a DC motor if we’re working with a motor.)
Solution: - Configure all the requisites first
TRISCbits.RC2 = 0; // Make PORTC an o/p port, where
CCP1 is multiplexed with others.
T2CONbits.T2CKPS1 = 1; // We do not care about
T2CKPS0
T2CONbits.TMR2ON = 1; // Since we’re working on
TMR2
Kassahun Tamir 67
PWM – Example
Example: Generate a pulse width frequency of 500Hz, using
fosc of 4MHz, prescalar 1:16, and duty cycle of 25%. (In
practice the duty cycle is the one we modify to increase or
decrease the “On Time”.In other words to increase or decrease
the speed of a DC motor if we’re working with a motor.)
PR2 = fosc/(4*prescalar*fpwm)-1 = 4MHz/(4*16*500) -1 = 124
PR2 = 0b01111100;// or simply PR2 = 124; We do not
modify this once configured.
resolution = (0.25)*(PR2+1)*4 = 125 = 0b0001111101
DC1B0 = resolution&1;// store the 0th bit
DC1B1 = (resolution&2)>>1 // store the 1st bit
CCPR1L = resolution>>2; // store the remaining 8 bits
Kassahun Tamir 68
ADC
When we interface sensors to the micro-controller, the output
of the sensor many of the times is analog in nature. But
micro-controller processes digital signals.
Kassahun Tamir 69
ADC - Conversion
Digital value = [Analog value/ (Vref+ - Vref-)] * (2^resolution)
Kassahun Tamir 70
ADC - Conversion
For a 10 bit resolution ADC, it is possible to divide up to 1024
(2^10) voltages. So for a 5V reference voltage, the minimum
voltage will be 5/1024 = 4.88mV.
This means 4.88mV input in the analog pin will be detected as
1 (0b0000000001 in binary) and 9.6mV input will be 2
(0b0000000010 in binary).
It can’t detect the difference between 1mV and 3mV since
both will be detected as 0, as they are less than 4.88mV.
Accordingly:
for 0V -> Digital value = 0
for 2.49V -> Digital value = 511 (0b0111111111 in binary)
for 4.99V -> Digital value = 1023 (0b1111111111 in binary)
Kassahun Tamir 71
ADC – In PIC18F4550
PIC18f4550 has inbuilt 10-bit 13 channel ADC.
Kassahun Tamir 72
ADC – In PIC18F4550
ADCON0: A/D Control Register 0
When we do not use analog inputs, it
is recommended to disable the
ADCON0 register using ADON bit
because it consumes too much power
Kassahun Tamir 73
ADC – In PIC18F4550
ADCON1: A/D Control Register 1
Kassahun Tamir 74
ADC – In PIC18F4550
ADCON2: A/D Control Register 2
ADFM = 1: Right Justified: the lower bits in
ADRESL, and the remaining two higher bits
in ADRESSH.
ADFM = 0: Left Justified: the higher bits in
ADRESH, and the remaining two lower bits
in ADRESSL.
ADFM = 1 ADFM = 0
1010111001 = 10 bits 1010111001 = 10 bits
ADRESH = 00000010 ADRESH = 10101110
ADRESSL = 10111001 Kassahun Tamir
ADRESSL = 01000000
75
ADC – In PIC18F4550
ADRESH (High byte) and ADRESL (Low byte) Registers:
These registers are used in combination to store the
converted 10 bits data i.e. digital data.
Kassahun Tamir 76
ADC – In PIC18F4550
Example: Assume a potentiometer representing an analog
sensor with a varying signal of 0-5V is applied in the AN0 pin
of PIC18F4550, configure all the control registers.
ADON =1, Go/done = 1, and
CHS3:CHS0 = 1110 → AN0
ADCON0 = 0x03;
Ref vtg is VDD and configure
the pin as an analog pin
ADCON1 = 0x00; //and
Right Justified, 2Tad and
Fosc/16
ADCON2 =0x95; Kassahun Tamir 77
ADC – In PIC18F4550
Example: Assume a potentiometer representing an analog sensor
with a varying signal of 0-5V is applied in the AN0 pin of
PIC18F4550, configure all the control registers.
Kassahun Tamir 78
Comparators
Analog Comparator is a device which compares two
voltage signals and switches its output to indicate
which one is larger.
The input pins of comparator are multiplexed with I/O
port pins RA0 through RA3 while output pins are
multiplexed with RA4 and RA5.
Kassahun Tamir 79
Comparators
Kassahun Tamir 80
Comparators
Kassahun Tamir 81
Comparators
Kassahun Tamir 82
Instructions Sets
READING ASSIGNMENT
Kassahun Tamir 83
References
●
PIC18FXX Dasheet
●
https://electrosome.com/read-modify-write-problem-pic/
#google_vignette (access date: 05/05/2023)
●
https://download.mikroe.com/documents/compilers/mikroc/
pic/help/rmw.htm
(access date: 05/05/2023)
PWM:
●
https://www.circuitbread.com/ee-faq/what-is-a-pwm-signal
(access date: 05/05/2023)
●
https://circuitdigest.com/microcontroller-projects/pic-microc
ontroller-pic16f877a-pwm-tutorial
(access date: 15/05/2023) Kassahun Tamir 84
Questions?
Kassahun Tamir 85