Embedded
Systems
Lecture (2)
PIC18F452
➢ Microchip Inc. has developed the PIC18 series of microcontrollers for use
in high pin count, high density, and complex applications.
➢ Basic features:
• 77 instructions.
• 32 KB Program Memory.
• 256 Bytes EEPROM for Fixed Data.
• 1536 Bytes RAM for Data.
• 40 pins.
PIC18F452
• 40 MHz Operating Frequency.
• 4 Timers (Timer0, Timer1, Timer2, Timer3).
• 2 Capture / Compare / PWM modules (CCPs).
• 8 input channels 10-bit Analog-to-Digital module (ADC).
• 18 sources of Interrupt.
• 16-bit Instruction word.
• 2 serial communication modules.
• RISC CPU.
Data Memory Organization
➢ The data memory consists of:
❖ General Purpose Registers (GPR):
❑ RAM used to execute the program and store data.
❖ Special Function Registers (SFR) :
❑ Control the operation of the device.
Ports of PIC18F452
➢ The PIC18F452 has five I/O ports named PORTA, PORTB, PORTC,
PORTD, and PORTE:
• PORTA has 6 pins (RA0 to RA5).
• PORTB has 8 pins (RB0 to RB7).
• PORTC has 8 pins (RC0 to RC7).
• PORTD has 8 pins (RD0 to RD7).
• PORTE has 3 pins (RE0 to RE2).
➢ Most port pins have multiple functions.
Ports of PIC18F452
➢ Port pins can be used as Input or Output.
➢ PORTA pins can be analog or digital:
• In analog mode, pins are input and built-in ADC is used.
• In digital mode, pins can be programmed as input or output.
➢ To deal with ports, use SFR.
➢ The most important SFR are TRIS and PORT registers.
TRIS Register
➢ Controls the port direction by defining which pins of the ports are input and
which pins are output.
➢ Each port has its own TRIS register.
➢ A logic 1 in a bit position defines the pin as an input pin.
➢ A logic 0 in a bit position defines the pin as an output pin.
PORT Register
➢ Stores the data that’s read or written from or to the port.
➢ Each port has its own PORT register.
➢ Each bit of the register can be logic 0 or logic 1.
Example (1)
➢ What are the contents of TRISB register to make all pins of PORTB output?
RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0
0 0 0 0 0 0 0 0
▪ TRISB = 0b00000000 //Binary
▪ TRISB = 0x00 //Hexadecimal
▪ TRISB = 0 //Decimal
Example (2)
➢ What are the contents of TRISB register to make all pins of PORTB input?
RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0
1 1 1 1 1 1 1 1
▪ TRISB = 0b11111111 //Binary
▪ TRISB = 0xFF //Hexadecimal
▪ TRISB = 255 //Decimal
Example (3)
➢ What are the contents of TRISB register to make the lower 4 bits of PORTB
as input and the upper 4 bits as output?
RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0
0 0 0 0 1 1 1 1
Output Input
▪ TRISB = 0b00001111 //Binary
▪ TRISB = 0x0F //Hexadecimal
▪ TRISB = 15 //Decimal
Minimum PIC Configurations
1. The Power Supply:
➢ Most microcontrollers are operated
with a single +5V DC supply derived
from a suitable voltage regulator.
Minimum PIC Configurations
2. RESET Input:
➢ The MCLR pin should be tied to the
supply voltage through a 10K resistor.
➢ MCLR is Master Clear pin which is
used to externally reset the
microcontroller by pressing a button.
Minimum PIC Configurations
2. RESET Input:
➢ Normally the MCLR input is at logic 1.
➢ When the RESET button is pressed, this
pin goes to logic 0 and resets the
microcontroller.
Minimum PIC Configurations
3. Oscillator Circuit:
➢ Used to provide clock pulses to the microcontroller.
➢ Microcontroller can be operated from an external
crystal or ceramic resonator connected to the
microcontroller’s OSC1 and OSC2 pins.
➢ For applications where accuracy of timing is
important, a crystal should be used.
➢ Resonators should be used in low-cost applications
where high accuracy in timing is not required.
Minimum PIC Configurations
3. Oscillator Circuit:
➢ There are several clock sources on the PIC18F452 microcontroller:
▪ LP: Low-power crystal.
▪ XT: Crystal or ceramic resonator.
▪ HS: High-speed crystal or ceramic resonator.
▪ HS-PLL: High-speed crystal or ceramic resonator with PLL.
▪ RC: External resistor/capacitor.
▪ Internal Clock.
Minimum PIC Configurations
3. Oscillator Circuit:
➢ Crystal Operation (LP, XT, HS):
▪ The LP oscillator mode is advised in applications
to up to 200KHz clock.
▪ The XT mode is advised to up to 4MHz.
▪ The HS mode is advised in applications where
the clock frequency is between 4MHz to 25MHz.
▪ C1 = C2 = 22pF.
Minimum PIC Configurations
3. Oscillator Circuit:
➢ Ceramic Resonator Operation (XT, HS):
▪ Resonators could be used where high accuracy
in timing is not required.
Minimum PIC Configurations
3. Oscillator Circuit:
➢ Crystal or Resonator with PLL:
▪ A Phase Locked Loop (PLL) circuit is provided to multiply the
clock frequency by 4.
▪ For example, for a crystal clock frequency of 10MHz, the internal
operation frequency will be multiplied to 40MHz.
▪ The PLL mode is enabled when the oscillator configuration bits are
programmed for HS mode.
Minimum PIC Configurations
3. Oscillator Circuit:
➢ Resistor/capacitor Operation (RC):
▪ A resistor and a capacitor can
provide clock pulses in
applications where accurate timing
is not required.
Minimum PIC Configurations
3. Oscillator Circuit:
➢ Internal Clock:
▪ Some devices in the PIC18F family have internal built-in clock
modes.
▪ The PIC18F452 does not have internal clock mode.
Minimum PIC Configurations
4. Input:
➢ Pull down resistor:
▪ For switch, use
10KΩ resistor.
Minimum PIC Configurations
4. Input:
➢ Pull up resistor:
▪ For switch, use
10KΩ resistor.
Minimum PIC Configurations
5. Output:
➢ LED:
▪ For LED, use
330Ω resistor.
Criteria for choosing the microcontroller
1. Maximum CPU speed.
2. Number of I/O pins.
3. Program and data memory size.
4. Internal peripherals (Timers, A/D, serial communication,…).
5. Number of operations CPU can perform (arithmetic and logic).
6. Cost.
Thank You