14mte02 Embedded Programming For Mechatronics Systems: Course Outcomes
14mte02 Embedded Programming For Mechatronics Systems: Course Outcomes
COURSE OUTCOMES
On completion of the course the students will be able to
UNIT – II 9
PIC 18 Embedded C Programming: I/O ports: Register configuration–
programming – Timers: modes–programming – Counters – ADC:
configuration registers–programming – External Hardware Interrupts:
types–programming.
UNIT – III 9
I/O Interfacing using PIC 18 Microcontroller: Sensor interfacing –
LED interfacing – LCD interfacing – Motor interfacing – High power
devices using relays - Case studies: Automotive applications - Robotic
applications.
UNIT – IV 9
ATMEGA 8 Microcontroller: Architecture of ATMEGA 8 – Pin Description–.
Memory organization: Program memory – Data Memory - I/O Ports – Timers –
Counters – Analog comparator – Serial Peripheral Interface – USART – External
Hardware Interrupts – ADC.
UNIT – V 9
ATMEGA 8 Embedded C Programming: I/O ports: Register configuration–
programming – Timers: modes–programming – Counters – ADC: configuration
registers–programming – External Hardware Interrupts: types –programming.
TOTAL: 45
TEXT BOOKS:
1. Mazidi, Muhammad Ali, Mckinlay, Rolin D., and Causey Danny, “PIC Microcontroller and
Embedded Systems using Assembly and C for PIC 18”, Pearson Education Asia, 2015.
2. Data sheet – ATMEGA 8.
REFERENCE BOOKS:
1. Peatman John B., “Design with PIC Microcontrollers”, Pearson Education, New Delhi, 2009.
2. Valvano Jonathan W., “Embedded Microcomputer Systems: Real Time Interfacing”, 3 rd
Edition, Thomson Asia, Singapore, 2011.
3. Data sheet –PIC18FXXX.
4. http://nptel.ac.in/courses/101106041/
5. http://nptel.ac.in/downloads/108105057/
PIC18 Microcontroller
Basic Features
Introduction
Program
Memory
Simplified View
PIC 18 Microcontroller
(Harvard Architecture)
Features of the PIC18 microcontroller
RISC Architecture
8-bit CPU
2 MB program memory space
4 KB to 128KB flash program memory
256 bytes to 1KB of data EEPROM
Up to 3968 bytes of on-chip SRAM
Sophisticated timer functions that include: input capture,
output compare, PWM, real-time interrupt, and watchdog
timer
Serial communication interfaces: SCI, SPI, I2C, and CAN
Background debug mode (BDM)
10-bit A/D converter
Memory protection capability
Instruction pipelining
Operates at up to 40 MHz crystal oscillator
PIC 18 vs 8051
Data bus
- 16-bit instruction/data bus for program memory
- 8-bit data bus for data memory
PIC 18 Memory Organization
Enhanced FLASH Program Memory
Data Memory
EEPROM Data Memory
Low bytes goes to low memory location & high bytes goes to high
memory location – Little Endian
Low bytes goes to high memory location & high bytes goes to low
memory location – Big Endian
PIC18 CPU Registers
The group of registers
from 0xFD8 to 0xFFF
are dedicated to the
general control of
MCU operation.
RESET Modes:
a) Power on Reset (POR)
b) MCLR Reset during normal operation
c) MCLR Reset during SLEEP
d) Watchdog Timer (WDT) Reset (during normal operation)
e) Programmable Brown-out Reset (PBOR)
f) RESET Instruction
g) Stack Full Reset
h) Stack Underflow Reset
HS, XT & LP Mode RC Mode
EC Mode
ECIO Mode
Power ON Reset Circuit
D7 D0
Timer1 Module
• 16-bit timer or counter
• TMR1H:TMR1L
D7
T2CON (Timer2 Control Register)
Timer3 Module
• 16-bit timer or counter
• Important Registers:
i) T3CON (Timer3 Control Register)
• To start & stop Timer3 and other configurations
ii) TMR3H:TMR3L (for counting purposes)
• Act as counting buffer
iii) PIR2 (Peripheral Interrupt Request Register 2)
T3CON (Timer3 Control Register)
CAPTURE/COMPARE/PWM (CCP)
MODULES
• Control register (CCPxCON)
• Data register (CCPRx) - two 8-bit registers
– CCPRxL (low byte)
– CCPRxH (high byte)
• CCP mode – Timer Resource
CCP1CON: CAPTURE/COMPARE/PWM CONTROL REGISTER
CCP1IF (PIR1 Register)
Capture Mode
Tosc = 1/ Fosc
PR2 = [ (20 x 106) / (1.22 x 103 x 4 x 16) ] – 1 = 255 --------- 1:16 prescalar
Result :
PR2 = 249
CCPR1L = 1011 1010
DC1B1:DC1B0 = 1 1
PIC18 Interrupts
• Interrupts are mechanisms which enable instant
response to events such as counter overflow,
pin change, data received, etc.
• In normal mode, microcontroller executes the
main program as long as there are no
occurrences that would cause an interrupt
• Upon interrupt, microcontroller stops the
execution of main program and commences the
special part of the program(ISR) which will
analyze and handle the 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 Microcontrollers 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
Interrupt service routine (ISR)
• When an interrupt is
invoked, the microcontroller
runs the Interrupt Service
Routine(ISR)
• Interrupt vector table holds
the address of ISRs
– Power-on Reset: 0000h
– High priority interrupt :0008h
– Low priority interrupt: 0018h
Steps in executing an interrupt
• Finish the executing instruction, and save the
address of the next instruction (program
counter) on the stack
• Jump to a fixed location by interrupt vector table
which directs the address of ISR
• Execute the ISR until reach RETFIE (return from
interrupt exit)
• Get PC from stack and return to the place where
it was interrupted
11-70
Sources of interrupts in PIC18
• External hardware interrupts :RB0(INT0),
RB1(INT1), RB2(INT2)
• PORTB change interrupts (any one of the upper
four Port B pins)
• Timer interrupts :Timer0 , Timer1 ,Timer2
• ADC (analog to digital converter)
• CCP (compare capture pulse width modulation,
PWM)
• Serial communication’s USART interrupts:
receive and transmit, respectively
11-71
Sources of interrupts in PIC18
INTCON
Register
• The switch is connected to the pin RB0 (INT0)
and an LED is connected to the pin RB7. Write
C18 program, every time INT0 is activated, it
toggles LED, while at the same time data is being
transferred from PORTC to PORTD.
Universal Synchronous
Asynchronous Receiver and
Transmitter (USART)
Basics of Serial Communication
• Computers transfer data in two ways: Parallel and Serial.
• Parallel: Eight or more data lines, few feet only, short
time
• Serial: Single data line, long distance
• The byte of data must be converted to serial bits using a
parallel-in-serial-out shift register
Transfers a block
Transfers a single of data at a time
byte at a time
Half-and Full-Duplex Transmission
Start and Stop Bits
• In the asynchronous method, each
character is placed between start and stop
bits (framing)
MSB LSB
10-82
• USART has both
– Synchronous
– Asynchronous
• 6 registers
– SPBRG
– TXREG
– RCREG
– TXSTA
– RCSTA
– PIR1
SPBRG Register and Baud Rate
• The baud rate in is programmable
• loaded into the SPBRG decides the baud rate
If Fosc = 10MHz
X = (156250/Desired Baud Rate) - 1
Example:
X = (156250/1200) – 1
X = 129.21 = 129 = 81H
TXREG Register
• 8-bit register used for serial communication
• For a byte of data to be transferred via the
Tx pin, it must be placed in the TXREG
register first.
• The moment a byte is written into TXREG,
it is fetched into a non-accessible register
TSR (Transmit Shift Register)
• The frame contains 10 bits including
START and STOP bits
RCREG Register
• 8-bit register used for serial communication
10-89
RCSTA
(Receive Status and Control Register)
10-91
PIR1
(Peripheral Interrupt Request Register 1)
10-93
Simplified USART
Transmit Block Diagram
PIC 18 Microcontroller
Analog to Digital Converter
(ADC)
Vref