PIC18 Microcontrollers -1
Introduction to Microcontroller
PIC18F4550
ARCHITECTURE
PIC18F4550
BLOCK
ARCHITECTURE
Components in PIC18 Microcontroller
 Arithmetic and Logic Unit (ALU),
 Working Register in PIC Microcontrollers,
 Machine Cycles and Execution of Instructions,
 Pipelining for Instruction Execution,
 Oscillators,
 Configuration Bits,
 Reset Options,
 Low Power Consumption Mode,
 Watchdog Timer,
 Program Counter,
 RISC, CISC
 Compare Harvard architecture with von Neumann architecture.
Main Characteristics of PIC Microcontrollers
 The ALU executes the arithmetic and logic operations available in the instruction
set.
Arithmetic and Logic Unit (ALU)
(a) ALU in microprocessors (b) ALU in microcontrollers
 STATUS or FLAG Register:
 The ALU also has bits to indicate specific characteristics of the resulting value, such as if the result is
zero, the sign of the resulting value, or the existence of carryover. These bits are normally part of the
STATUS register.
 Carry flag (D0 bit of status reg) =1 or else 0.
 Digital Carry from the 4th bit (D3) to the 5th bit (D4) after an arithmetic or logical operation the DC flag
(D1 bit of status reg) =1 or as 0.
 Result of any arithmetic or logical operation is a zero it is the zero flag (D2 bit of status reg) = 1 or else
0.
 Signed number exceeds the range (00-7F for positive numbers and FF-80 for negative numbers) then
the overflow (D3 bit of status reg) = 1 or else 0.
 D4 bit of the status register is Negative flag . If D7 bit is 1 then the result is interpreted as –ve and if
D7 bit is 0 then the result is interpreted as +ve .
Arithmetic and Logic Unit (ALU)
 WREG:
 8-bit Register, Same as ACC in Microprocessor
 In PIC microcontrollers, an operation’s result can be placed in the W register or any data memory
register.
Arithmetic and Logic Unit (ALU)
Sample program: add two 8 bit numbers:
ORG 0X00
GOTO START
START
MOVLW 0XFE
MOVWF 0X30
MOVLW 0X06
MOVWF 0X31
MOVF 0X30,W
ADDWF 0X31,W
MOVWF 0X32
END
 ALU is associated with two registers- WREG and status.
 Result may be stored in WREG register.
 Status register gives the status of the current
arithmetic and logical operation.
 One of the input to the ALU has to be the WREG
 A machine cycle (MC) is defined as four pulses from the main oscillator (OSC1).
 These signals synchronize fetching, decode, and execute of instructions. TMC is the
duration of a machine cycle
Machine Cycles and Execution of Instructions
 In the fetching phase, the microcontroller reads the instruction in the program
memory and brings it to the CPU.
 During the decoding phase, the CPU determines the operation to carry out as
described by the instruction.
 Finally, the operation is executed during the execution phase.
 Instructions are executed in one machine cycle.
Machine Cycles and Execution of Instructions
 Overlap two or more instructions as they are being executed. This introduces some
parallelism in the execution of instructions, thus reducing the required execution
time.
 In an n-stage pipeline, each instruction spends a time equal to TMC for any stage,
with TMC being the time length of a machine cycle. Therefore, the time needed to
move through all the stages is n × TMC. The average time to execute any instruction
is TMC.
 Some instructions, such as control transfer instructions, require additional machine
cycles, the average instruction execution time for these instructions is slightly longer
than TMC.
Pipelining for Instruction Execution
Pipelining for Instruction Execution
 Overlap two or more instructions as they are being executed. This introduces some
parallelism in the execution of instructions, thus reducing the required execution
time.
Pipelining for Instruction Execution
 The main oscillator in PIC microcontrollers can be a crystal oscillator, an RC oscillator,
or an external clock. Some devices also have an internal RC oscillator at 4 MHz.
Oscillators
 Increasing the oscillator frequency shortens the length of the machine cycles and
therefore the time needed for executing instructions but also increases power
consumption.
 The type of oscillator can be selected by the configuration bits.
 These also select specific modes of operation for crystal or ceramic oscillators: LP,
XT, and HS. The LP mode selects oscillator frequencies between 32 kHz and 200 kHz,
and is used in very low power-consumption applications. The XT mode selects
oscillator frequencies between 100 kHz and 4 MHz. The HS mode selects oscillator
frequencies between 8 MHz and 20 MHz.
 The RC oscillator is the least expensive option for the main oscillator in the
microcontroller. used when frequency accuracy and stability are not critical.
medium-end PIC.
Oscillators
Oscillator type Frequency range
LP 32khz to 200khz
XT 100khz-4Mhz
HS 8Mhz -20 Mhz
Oscillator or Resonator Start UP
 All PIC microcontrollers have specific bits for their configuration.
 stored in non-volatile memory (EEPROM) when the device is being programmed.
 microcontroller to better fit the intended application.
 Configuration bits can modify
• Type of oscillator
• Watchdog timer on or off
• Program memory protection
• Protection of EEPROM data if available
• Specifications for reset and power supply
 Configuration bits in a medium-end PIC make up a word stored in address 2007h in
program memory. This address is only accessible during the programming of the
microcontroller. Once the program is executed, it cannot access this address.
Configuration Bits
 the microcontroller is temporarily paralyzed and not executing any program
instructions. Following this transient state, the device moves to a predetermined
state.
 In PIC microcontrollers, reset sets the program counter to zero. After the reset has
finished, the first instruction executed is the one located at this memory address.
 most PIC microcontrollers Reset Sources :
• External reset - An external reset occurs when the pin MCLR# is set to 0
• Power-on reset - MCLR# is connected to the microcontroller’s power supply.
detects the triggering of VDD it creates a reset signal.
• Watchdog reset - Program running in the microcontroller cannot clear the internal
counter of the watchdog time before reaching its maximum value. regular working
mode or when it is in the low-power consumption mode.
• Brown-out reset - transient or glitch in the voltage (VDD).
Reset Options
Fig: Reset signal due to brown-out in several situations. The timer PWRT
guarantees that VDD will be at its nominal value once it leaves the reset state.
 this circuit needs to guarantee that the microcontroller will only leave the reset
state if the voltage has reached a stable and high enough value. This is the task of
the blocks associated with the VDD signal.
Reset Options
 The OST/PWRT, oscillator start-up timer (OST), and power-up timer (PWRT). It takes
a certain amount of time for the main oscillator to reach stable values for its
frequency and amplitude after it has been turned on.
 power-up timer (PWRT): An internal RC oscillator, independent of the main
oscillator, introduces a 72 ms delay to the signal PWRT after the oscillator has been
powered. ]
 oscillator start-up timer (OST): The OST introduces an additional delay of 1024
pulses. This delay is long enough for the oscillator to reach stable amplitude and
frequency values. The OST starts its operation only after the PWRT has reached its
maximum value.
Reset Options
Figure: Time diagrams showing the sequence of signals
associated with the OST/PWRT block. (a)
Power-on reset—MCLR# pin connected to VDD
Reset Options
Figure: Reset signal due to brown-out in several situations. The
timer PWRT guarantees that VDD will be at its nominal value
once it leaves the reset state.
 Special function registers such as STATUS and PCON. register STATUS in which the bit
TO# is set to 0 when the watchdog timer produces a reset.
Reset Options
Figure: Two bits in the special function registry PCON in
PIC18F4550 are used to determine the origin of a reset
signal.
 Power consumed less than 1 μA, In low-power consumption mode (sleep mode) all
functions are stopped including oscillator.
 The instruction sleep, values stored in the data memory registers are not changed.
 The microcontroller will wake up leaving the low-consumption mode in any of these three
events:
• Reset
• Overflow of watchdog timer counter (if not disabled)
• Interrupt, either external or from its internal peripherals
 instruction after sleep has already entered the CPU, this same instruction will be executed
once the microcontroller wakes up.
 Reset - it will execute the instruction stored at address 0 in the program memory.
 Watchdog timer - it will continue running the program, executing the instruction after the
sleep instruction.
 External interrupt with the interrupt system enabled - it will execute the instruction
immediately after the sleep instruction, and then the program counter will jump to address
4 in the program memory searching for the interrupt routine.
Low-Power Consumption Mode
Low-Power Consumption Mode
• The interrupt system can be enabled or disabled by modifying the
global interrupt enable (GIE) bit.
• GIE = 1 the interrupt system is enabled;
• GIE = 0 it is disabled.
• GIE is bit number 7 in the special function register INTCON.
 Pulse counter, Timer, Internal Oscillator.
 If the pulse counter reaches its maximum value (overflows) -- the WDT times out --
generates a reset signal for the microcontroller.
Watchdog Timer
Figure: Block diagram of the circuits associated with a watchdog timer in medium-
end PICs. PSA and PS2:PS0 are bits from the special function registry OPTION. WDTE
is the configuration bit that enables the watchdog timer.
 Once the program is running normally, the WDT cannot be disabled.
 The WDT times out every 18 ms. to avoid the WDT timing out and generating the
reset signal for the microcontroller, the internal pulse counter needs to be set to 0
before the 18 ms have elapsed. The instruction clrwdt is used for this purpose. It is
possible to extend the 18 ms timeout up to 2.3 s by assigning an additional counter
to the WDT. This counter is called Prescaler.
Watchdog Timer
 The program counter is used by the CPU to point to the address in ROM of the next
instruction to be executed.
 As the CPU fetches the opcode from the program ROM, the program counter is
incremented automatically to point to the next instruction.
 The wider the program counter, more the memory locations.
14-bit program counter - (214 = 16K) of code - addresses 0000-3FFFH – PIC 16F
16-bit program counter - (216 = 64K) of code
21-bit program counter - (221 = 2M) of code in ROM
PIC 18 family can access program HEX addresses
000000 to IFFFFFH
0 0000 0000 0000 0000 B – Min Address
1 1111 1111 1111 1111 B – Max Address
 PCL is readable and writable while PCH and PCU are not directly readable or writable.
 Updates to PCH and PCU are provided by PCLATH and PCLATU registers resp.
Program counter in the PIC – 21 bit – 2 MB
Program ROM in the PIC
 Each location 1 byte or
8 bits
Program memory (ROM) for PIC18 family
[1] Fernando E. Valdes-Perez, Ramon Pallas-Areny, “Microcontrollers - Fundamentals and Applications with PIC”, CRC Press, 1st Edition, 2009
[2] Mazidi M. A., McKinlay R. D., Causey D., “PIC Microcontroller And Embedded Systems.”, Pearson Education International, 2008
[3] https://ww1.microchip.com/downloads/en/devicedoc/39632c.pdf

Lecture on PIC-1.pptx

  • 1.
  • 2.
  • 3.
  • 4.
    Components in PIC18Microcontroller
  • 5.
     Arithmetic andLogic Unit (ALU),  Working Register in PIC Microcontrollers,  Machine Cycles and Execution of Instructions,  Pipelining for Instruction Execution,  Oscillators,  Configuration Bits,  Reset Options,  Low Power Consumption Mode,  Watchdog Timer,  Program Counter,  RISC, CISC  Compare Harvard architecture with von Neumann architecture. Main Characteristics of PIC Microcontrollers
  • 6.
     The ALUexecutes the arithmetic and logic operations available in the instruction set. Arithmetic and Logic Unit (ALU) (a) ALU in microprocessors (b) ALU in microcontrollers
  • 7.
     STATUS orFLAG Register:  The ALU also has bits to indicate specific characteristics of the resulting value, such as if the result is zero, the sign of the resulting value, or the existence of carryover. These bits are normally part of the STATUS register.  Carry flag (D0 bit of status reg) =1 or else 0.  Digital Carry from the 4th bit (D3) to the 5th bit (D4) after an arithmetic or logical operation the DC flag (D1 bit of status reg) =1 or as 0.  Result of any arithmetic or logical operation is a zero it is the zero flag (D2 bit of status reg) = 1 or else 0.  Signed number exceeds the range (00-7F for positive numbers and FF-80 for negative numbers) then the overflow (D3 bit of status reg) = 1 or else 0.  D4 bit of the status register is Negative flag . If D7 bit is 1 then the result is interpreted as –ve and if D7 bit is 0 then the result is interpreted as +ve . Arithmetic and Logic Unit (ALU)
  • 8.
     WREG:  8-bitRegister, Same as ACC in Microprocessor  In PIC microcontrollers, an operation’s result can be placed in the W register or any data memory register. Arithmetic and Logic Unit (ALU) Sample program: add two 8 bit numbers: ORG 0X00 GOTO START START MOVLW 0XFE MOVWF 0X30 MOVLW 0X06 MOVWF 0X31 MOVF 0X30,W ADDWF 0X31,W MOVWF 0X32 END  ALU is associated with two registers- WREG and status.  Result may be stored in WREG register.  Status register gives the status of the current arithmetic and logical operation.  One of the input to the ALU has to be the WREG
  • 9.
     A machinecycle (MC) is defined as four pulses from the main oscillator (OSC1).  These signals synchronize fetching, decode, and execute of instructions. TMC is the duration of a machine cycle Machine Cycles and Execution of Instructions
  • 10.
     In thefetching phase, the microcontroller reads the instruction in the program memory and brings it to the CPU.  During the decoding phase, the CPU determines the operation to carry out as described by the instruction.  Finally, the operation is executed during the execution phase.  Instructions are executed in one machine cycle. Machine Cycles and Execution of Instructions
  • 11.
     Overlap twoor more instructions as they are being executed. This introduces some parallelism in the execution of instructions, thus reducing the required execution time.  In an n-stage pipeline, each instruction spends a time equal to TMC for any stage, with TMC being the time length of a machine cycle. Therefore, the time needed to move through all the stages is n × TMC. The average time to execute any instruction is TMC.  Some instructions, such as control transfer instructions, require additional machine cycles, the average instruction execution time for these instructions is slightly longer than TMC. Pipelining for Instruction Execution
  • 12.
  • 13.
     Overlap twoor more instructions as they are being executed. This introduces some parallelism in the execution of instructions, thus reducing the required execution time. Pipelining for Instruction Execution
  • 14.
     The mainoscillator in PIC microcontrollers can be a crystal oscillator, an RC oscillator, or an external clock. Some devices also have an internal RC oscillator at 4 MHz. Oscillators
  • 15.
     Increasing theoscillator frequency shortens the length of the machine cycles and therefore the time needed for executing instructions but also increases power consumption.  The type of oscillator can be selected by the configuration bits.  These also select specific modes of operation for crystal or ceramic oscillators: LP, XT, and HS. The LP mode selects oscillator frequencies between 32 kHz and 200 kHz, and is used in very low power-consumption applications. The XT mode selects oscillator frequencies between 100 kHz and 4 MHz. The HS mode selects oscillator frequencies between 8 MHz and 20 MHz.  The RC oscillator is the least expensive option for the main oscillator in the microcontroller. used when frequency accuracy and stability are not critical. medium-end PIC. Oscillators Oscillator type Frequency range LP 32khz to 200khz XT 100khz-4Mhz HS 8Mhz -20 Mhz
  • 16.
  • 17.
     All PICmicrocontrollers have specific bits for their configuration.  stored in non-volatile memory (EEPROM) when the device is being programmed.  microcontroller to better fit the intended application.  Configuration bits can modify • Type of oscillator • Watchdog timer on or off • Program memory protection • Protection of EEPROM data if available • Specifications for reset and power supply  Configuration bits in a medium-end PIC make up a word stored in address 2007h in program memory. This address is only accessible during the programming of the microcontroller. Once the program is executed, it cannot access this address. Configuration Bits
  • 20.
     the microcontrolleris temporarily paralyzed and not executing any program instructions. Following this transient state, the device moves to a predetermined state.  In PIC microcontrollers, reset sets the program counter to zero. After the reset has finished, the first instruction executed is the one located at this memory address.  most PIC microcontrollers Reset Sources : • External reset - An external reset occurs when the pin MCLR# is set to 0 • Power-on reset - MCLR# is connected to the microcontroller’s power supply. detects the triggering of VDD it creates a reset signal. • Watchdog reset - Program running in the microcontroller cannot clear the internal counter of the watchdog time before reaching its maximum value. regular working mode or when it is in the low-power consumption mode. • Brown-out reset - transient or glitch in the voltage (VDD). Reset Options Fig: Reset signal due to brown-out in several situations. The timer PWRT guarantees that VDD will be at its nominal value once it leaves the reset state.
  • 21.
     this circuitneeds to guarantee that the microcontroller will only leave the reset state if the voltage has reached a stable and high enough value. This is the task of the blocks associated with the VDD signal. Reset Options
  • 22.
     The OST/PWRT,oscillator start-up timer (OST), and power-up timer (PWRT). It takes a certain amount of time for the main oscillator to reach stable values for its frequency and amplitude after it has been turned on.  power-up timer (PWRT): An internal RC oscillator, independent of the main oscillator, introduces a 72 ms delay to the signal PWRT after the oscillator has been powered. ]  oscillator start-up timer (OST): The OST introduces an additional delay of 1024 pulses. This delay is long enough for the oscillator to reach stable amplitude and frequency values. The OST starts its operation only after the PWRT has reached its maximum value. Reset Options Figure: Time diagrams showing the sequence of signals associated with the OST/PWRT block. (a) Power-on reset—MCLR# pin connected to VDD
  • 23.
    Reset Options Figure: Resetsignal due to brown-out in several situations. The timer PWRT guarantees that VDD will be at its nominal value once it leaves the reset state.
  • 24.
     Special functionregisters such as STATUS and PCON. register STATUS in which the bit TO# is set to 0 when the watchdog timer produces a reset. Reset Options Figure: Two bits in the special function registry PCON in PIC18F4550 are used to determine the origin of a reset signal.
  • 25.
     Power consumedless than 1 μA, In low-power consumption mode (sleep mode) all functions are stopped including oscillator.  The instruction sleep, values stored in the data memory registers are not changed.  The microcontroller will wake up leaving the low-consumption mode in any of these three events: • Reset • Overflow of watchdog timer counter (if not disabled) • Interrupt, either external or from its internal peripherals  instruction after sleep has already entered the CPU, this same instruction will be executed once the microcontroller wakes up.  Reset - it will execute the instruction stored at address 0 in the program memory.  Watchdog timer - it will continue running the program, executing the instruction after the sleep instruction.  External interrupt with the interrupt system enabled - it will execute the instruction immediately after the sleep instruction, and then the program counter will jump to address 4 in the program memory searching for the interrupt routine. Low-Power Consumption Mode
  • 26.
    Low-Power Consumption Mode •The interrupt system can be enabled or disabled by modifying the global interrupt enable (GIE) bit. • GIE = 1 the interrupt system is enabled; • GIE = 0 it is disabled. • GIE is bit number 7 in the special function register INTCON.
  • 27.
     Pulse counter,Timer, Internal Oscillator.  If the pulse counter reaches its maximum value (overflows) -- the WDT times out -- generates a reset signal for the microcontroller. Watchdog Timer Figure: Block diagram of the circuits associated with a watchdog timer in medium- end PICs. PSA and PS2:PS0 are bits from the special function registry OPTION. WDTE is the configuration bit that enables the watchdog timer.
  • 28.
     Once theprogram is running normally, the WDT cannot be disabled.  The WDT times out every 18 ms. to avoid the WDT timing out and generating the reset signal for the microcontroller, the internal pulse counter needs to be set to 0 before the 18 ms have elapsed. The instruction clrwdt is used for this purpose. It is possible to extend the 18 ms timeout up to 2.3 s by assigning an additional counter to the WDT. This counter is called Prescaler. Watchdog Timer
  • 29.
     The programcounter is used by the CPU to point to the address in ROM of the next instruction to be executed.  As the CPU fetches the opcode from the program ROM, the program counter is incremented automatically to point to the next instruction.  The wider the program counter, more the memory locations. 14-bit program counter - (214 = 16K) of code - addresses 0000-3FFFH – PIC 16F 16-bit program counter - (216 = 64K) of code 21-bit program counter - (221 = 2M) of code in ROM PIC 18 family can access program HEX addresses 000000 to IFFFFFH 0 0000 0000 0000 0000 B – Min Address 1 1111 1111 1111 1111 B – Max Address  PCL is readable and writable while PCH and PCU are not directly readable or writable.  Updates to PCH and PCU are provided by PCLATH and PCLATU registers resp. Program counter in the PIC – 21 bit – 2 MB
  • 30.
    Program ROM inthe PIC  Each location 1 byte or 8 bits
  • 32.
    Program memory (ROM)for PIC18 family [1] Fernando E. Valdes-Perez, Ramon Pallas-Areny, “Microcontrollers - Fundamentals and Applications with PIC”, CRC Press, 1st Edition, 2009 [2] Mazidi M. A., McKinlay R. D., Causey D., “PIC Microcontroller And Embedded Systems.”, Pearson Education International, 2008 [3] https://ww1.microchip.com/downloads/en/devicedoc/39632c.pdf