Udemy Software PPT
Udemy Software PPT
3
Microcontroller Architecture
4
Microcontroller Architecture
➢ MCs with Harvard architecture are called "RISC MCs". MCs with von Neumann's architecture are
called 'CISC microcontrollers’.
➢ In Harvard architecture, data bus and address bus are separate. Thus a greater flow of data is 5
possible through the CPU, and of course, a greater speed of work.
Microcontroller Vs Microprocessor
7
Microcontroller Manufacturers
Criteria 1
▰ No. of General Purpose Input Output (GPIO) pins
▰ Size of Flash ROM
▰ Size of RAM and EEPROM
▰ On chip peripherals: Timers, ADC, Interrupts, Analog comparators, PWM modules etc.
▰ Communication protocols supported: UART, SPI, I2C
▰ Reliability and susceptibility to electrical noise.
▰ Clock speed
▰ Operating voltage: 3.3 V or 5 V
▰ No. of bits: 8 bit, 16 bit and 32 bit.
▰ If an application does not have very high demands on processing power, and is of relatively small
size, then it can make sense to consider an 8-bit microcontroller. 9
Selection of Microcontrollers
Criteria 2
▰ Availability of development tools assembler, Code efficient C compiler, debugger,
technical support etc
Criteria 3
▰ Availability in needed quantity and cost per unit.
10
PIC Microcontrollers used by us in different products
PIC12F509- 8 PIN
PIC16F1503- 14 pin
PIC16F676- 14 pin
PIC16F690- 20 pin
PIC16F886- 28 pin
PIC18F4520-40 pin It is part of your syllabus
PIC18F4620-40 pin
PIC18F4523-40 pin
PIC18F46k22-40 pin
PIC18F8722- 80 pin SMD
11
PIC18F Series Microcontroller Block Diagram
12
WREG Register
13
WREG Register
14
PIC Microcontroller ROM types
▰ UV Erasable EPROM
▰ Flash version of PIC: Flash ROM
▰ OTP version of PIC - One time programmable ROM
▰ Masked version of PIC
15
Pipeline Vs Non Pipeline concept
16
Pipeline Vs Non Pipeline concept
17
RAM memory organization
18
2
Common Terms used in Microcontroller
Programming
19
Common Terms
20
Common Terms (Program Counter)
▰ A program counter is a register in the CPU containing the address of the next instruction to be
executed from memory.
21
Common Terms (Stack memory and Stack Pointer)
▰ Stack is used to store and retrieve return addresses during function or ISR calls. .
▰ It is last in first out type of memory
▰ Stack pointer always points to the top of the stack memory.
22
Common Terms (Stack memory and Stack Pointer)
23
Stack Memory
24
Flash Program Memory
▰ The Flash program memory is readable, writable and erasable during normal operation over the
entire VDD range.
▰ A read from program memory is executed on one byte at a time.
▰ A write to program memory is executed on blocks of 32 bytes at a time.
▰ Program memory is erased in blocks of 64 bytes at a time.
▰ Writing or erasing program memory will cease instruction fetches until the operation is complete.
▰ Table Reads and Table Writes: 1) Table Read (TBLRD) 2) Table Write (TBLWT)
▰ TBLAT Register
25
Flash Program Memory
26
Flash Program Memory
27
Flash Program Memory
28
Common Terms (Configuration Memory)
▰ It is used to control the hardware features: clock source, Power-up Timer, Watchdog Timer,
Memory Code Protection, etc.
▰ The configuration bits are set or rest using the directives written in the software.
▰ Configuration bits are programmed into the PIC MCU with the application code.
▰ The configuration bits are mapped to the program memory location.
▰ These bits are not executable code as their addresses are not accessible by the program
counter.
29
Watchdog Timer
30
Brown out Reset
▰ A Brown-out Reset (BOR) is a circuit that monitors the VDD level during operation
by comparing it to a fixed threshold level.
▰ When VDD drops below the threshold level, the Brown-out Reset is activated.
▰ When VDD rises again, the controller is restarted after a specified delay
31
Power up Timer (PWRT)
▰ Purpose: To add initial delay for stabilization of clock oscillator and other on chip
hardware.
▰ The Power-up Timer (PWRT) is an 11-bit counter which uses the internal RC
oscillator(31.25KHz) source as the clock input.
▰ This yields an approximate time interval of 2048 x 32 μs= 65.6 ms.
▰ While the PWRT is counting, the device is held in Reset.
32
Digital Buffer
33
Tristate, input and output pin configuration of
Microcontroller
➢ Tristate:
▻ Neither logic 1, nor logic zero.
▻ It has very high input impedance
➢ Output pin configuration- Impedance is low
➢ Input pin configuration- Input impedance is very
high
34
3
8-bit PIC Microcontrollers
35
8 bit PIC Microcontroller
36
PIC Microcontrollers used by us in different products
PIC12F509- 8 PIN
PIC16F1503- 14 pin
PIC16F676- 14 pin
PIC16F690- 20 pin
PIC16F886- 28 pin
PIC18F4520-40 pin It is used in the case study of IoT Based Water Level Control System
PIC18F4620-40 pin
PIC18F4523-40 pin
PIC18F46k22-40 pin
PIC18F8722- 80 pin SMD
37
3
PIC18F4520- 8-bit PIC Microcontroller
38
PIC18F4520 Features
40
PIC18F4520 Pinout
42
PIC18F4520 Peripheral details
43
I/O PORTS (GPIO pins)
45
Pull up and Pull down resistors
47
SFR bit format
48
On Chip Oscillator Frequency Setting
49
On Chip Oscillator Frequency Tuning
50
RESET
f) RESET Instruction
51
RESET Pin
▰ A Power-on Reset pulse is generated on-chip whenever VDD rises above a certain
threshold.
▰ This allows the device to start in the initialized state when VDD is adequate for
operation.
▰ POR sets SFRs to default state.
▰ POR events are captured by the POR bit (RCON<1>). The state of the bit is set to ‘0’
whenever a POR occurs.
53
Memory Types in PIC18F4520
▰ Data Memory
❑ SFRs and GPRs
▰ Flash Program Memory
▰ Stack Memory
▰ EEPROM Memory
54
STATUS Register
55
4
MPLABX IDE and XC8 Compiler
56
MPLAB X-IDE
57
MPLAB X-IDE
▰ MPLAB® X IDE is a software program that is used to develop applications for Microchip
microcontrollers and digital signal controllers.
▰ This development tool is called an Integrated Development Environment(IDE).
▰ MPLAB X IDE includes:
➢ a full-featured text editor.
➢ a project manager that provides integration and communication between the IDE and the
language tools.
➢ a debugger engine that provides breakpoints, single stepping, Watches windows and all the
features of a modern debugger.
➢ a software simulator for all PIC MCU and dsPIC DSC devices.
➢ MPLAB X IDE decides which one to use based on your project’s device. 58
Compiler (XC8)
59
Software Debuggers
▰ Simulators are built into MPLAB X IDE so a program can be tested without any
additional hardware.
▰ Usually, a simulator runs somewhat slower than an actual microcontroller, since the CPU
in the computer is being used to simulate the operations of the microcontroller.
▰ Limited features are available in simulator.
60
MPLABX Desktop
61
Configuration Bits
62
Control Program Execution with Breakpoints
▰ When debugging code, it can be useful to suspend execution at a specific location in code
so that variable values can be examined.
▰ To do this, use breakpoints.
▰ To set a breakpoint on a line, do one of the following:
✓ Click the left margin of the line in the Source Editor
✓ Press Ctrl-F8
63
Control Program Execution with Breakpoints
64
5
Embedded C Programming
65
Reserved Keywords C
66
Reserved Keywords in PIC Codes
67
Reserved Keywords in PIC Codes
68
C data types
70
#pragma
▰ The pragma command instructs the compiler to perform a particular action at the
compile time such as specifying the PIC microcontroller being used or the file format
generated.
▰ It is also used to set different fields in the configuration memory.
71
Bitwise Operators
72
C Program Control Statements
▰ if
▰ if-else
▰ for
▰ while
▰ do-while
▰ break
▰ continue
▰ switch
▰ return
73
If Statements
if (expression)
{
.
statement;
.
}
74
for Loop and While Loop
{
Statement;
}
75
break statement
▰ The break statement allows you to exit any loop from any point within the body.
▰ When a break statement is encountered in a loop, the program jumps to the next
statement after the loop
76
String and String Pointer
77
String and String Pointer
78
Passing Pointers to Functions
79
LED Blinking Program
80
6
LCD Interfacing with PIC18F4520
81
Character LCD Display
82
16 by 2 LCD Interfacing
83
16 by 2 LCD pinout
84
16 by 2 LCD
85
16 by 2 LCD
86
16 by 2 LCD Commands
87
16 by 2 LCD 4 bit Mode
88
16 by 2 LCD 4 bit Mode
89
7
Analog to Digital Converter (ADC)
90
What is Analog to Digital Converter ?
▰ It is abbreviated as ADC
▰ It is used to convert analogue voltage into equivalent digital number.
▰ Computers and Microcontrollers cannot process analogue signal.
91
Some Applications of ADC
▰ Temperature Measurement
▰ AC/DC Voltage Measurement
▰ AC/DC Current Measurement
▰ Weighing scale
▰ Light Intensity Measurement
▰ Capacitance Measurement
▰ Resistance Measurement
92
Analog to Digital Converter (ADC)
93
Analog to Digital Converter
94
ADC Specifications
95
Resolution of ADC
96
Resolution of ADC
2.00 0 to 2 2 / 1024 = 2
102
Transfer Characteristics of ADC
103
Built in ADC of PIC18F4520 Microcontroller
104
Built in ADC of PIC18F4520 Microcontroller
106
Analog to Digital Conversion (ADC)
107
Analog to Digital Conversion (ADC)
108
8
Reading and Writing EEPROM in
PIC18F4520
109
DATA EEPROM MEMORY
▰ The data EEPROM is a nonvolatile memory array, separate from the data RAM and program
memory.
▰ The EEPROM is readable and writable during normal operation over the entire VDD range.
▰ Four SFRs are used to read and write to the data EEPROM as well as the program memory. They
are:
❑ EECON1
❑ EECON2
❑ EEDATA
❑ EEADR
▰ EEDATA holds the 8-bit data for read/write
110
▰ EEADR register holds the address of the EEPROM location being accessed.
DATA EEPROM MEMORY
111
Registers Associated with EEPROM
112
5
Serial Communication
Let’s start with the first set of slides
113
UART Communication
114
UART Frame without Parity Bit
115
UART Frames
116
UART Frame with Parity Bit
117
Overrun Error in UART Communication
▰ Overrun error occurs when another byte of data arrives even before the
previous byte has not been read from the UART's receive buffer.
▰ This is mainly due to time taken by CPU to service the UART interrupt in
order to remove characters from receive buffer.
118
Standard Baud rates in UART Communication
Baud Rate: Baud rate represents the number of signal changes transmitted per second.
Bit Rate: Bit rate refers to the number of bits (0s and 1s) transmitted per second.
1200 bps, 2400 bps, 4800 bps, 9600 bps, 19200 bps, 38400 bps, 57600 bps, 115200 bps,
230400 bps, 460800 bps, 921600 bps.
119
UART Module in PIC18F4520
120
Baud Rate Generator
121
Serial Transmission using PIC18F4520
125
Interrupt
126
Interrupt
▰ There are ten registers which are used to control interrupt operation. These registers are:
• RCON
• INTCON
• INTCON2
• INTCON3
• PIR1, PIR2
• PIE1, PIE2
• IPR1, IPR2
▰ 20 Interrupt Sources
127
Interrupt
▰ 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
▪ The return address is pushed onto the stack and the PC is loaded with the interrupt vector
address 0008h
▪ In the Interrupt Service Routine, the source of the interrupt can be determined by polling
▪ The interrupt flag bits must be cleared in software before re-enabling interrupts to avoid
recursive interrupts.
▪ The “return from interrupt” instruction, RETFIE, exits the interrupt routine. 129
6
Stepper Motor Interfacing
Let’s start with the first set of slides
130
Stepper Motor Specifications
▰ Torque
131
Stepper Motor Specifications
132
Stepper Motor Types- Unipolar
133
Stepper Motor Types- Unipolar
28BYJ-48
5 volts
• Lower Torque
• Driver Circuit is simple
134
28BYJ-48 Stepper Motor Specifications
•Rated Voltage: 5V DC
•Number of Phases: 4
•Stride Angle/ Step Angle: 5.625°/64
•Pull in torque: 300 gf.cm
•Coil: Unipolar 5 lead coil
135
Unipolar Stepper Motor- full step sequence
1 1 1 0 0
2 0 1 1 0
3 0 0 1 1
4 1 0 0 1
136
Unipolar Stepper Motor- half step sequence
1 1 1 0 0
2 0 1 0 0
3 0 1 1 0
4 0 0 1 0
5 0 0 1 1
6 0 0 0 1
7 1 0 0 1
8 1 0 0 0
137
Stepper Motor Types- Unipolar
138
Stepper Motor Types- Unipolar
• Higher Torque
• Driver Circuit is Complex
139
I2C Protocol
Inter-Integrated Circuit Protocol
▰ MSSP module is a serial interface, useful for communicating with other peripheral or microcontroller devices.
▪ E.g. EEPROMs, shift registers, display drivers, A/D Converters, etc.
▰ The MSSP module can operate in one of two modes:
o Serial Peripheral Interface (SPI)
o Inter-Integrated Circuit (I2C)
❖ - Full Master mode
❖ - Slave mode (with general address call)
▰ MSSP module implements the standard mode specifications, as well as 7-Bit and 10-Bit Addressing modes
▰ Two pins are used for data transfer:
o Serial clock (SCL) – RC3/SCK/SCL
o Serial data (SDA) – RC4/SDI/SDA
Control Registers in MSSP
▰ SSPADD register holds the slave device address when the MSSP is
configured in I2C Slave mode.
▰ When the MSSP is configured in Master mode, the lower seven bits of
SSPADD act as the Baud Rate Generator reload value.
Control Registers in MSSP
▰.
150