0% found this document useful (0 votes)
4 views

Udemy Software PPT

Uploaded by

Tejas Kohali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Udemy Software PPT

Uploaded by

Tejas Kohali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 150

PIC Microcontroller

Dr. Anand. B. Kakade


1
Microcontroller Selection
Microprocessor Vs Microcontroller

3
Microcontroller Architecture

4
Microcontroller Architecture

CISC and RISC

➢ RISC-Reduced Instruction Set Computer

➢ CISC-Complex Instruction Set Computer

➢ MCs with Harvard architecture are called "RISC MCs". MCs with von Neumann's architecture are
called 'CISC microcontrollers’.

➢ The PIC18F4520 MC has a RISC architecture.

➢ Harvard architecture is a newer concept than von-Neumann’s.

➢ 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

Parameter Microprocessor Microcontroller


Microprocessors are used as processing Microcontrollers are used as control device in an
Definition
device computer systems. embedded system.
A microprocessor is a processor where the A microcontroller is a controlling device wherein the
What is it? memory and I/O component are connected memory and I/O output component are present internally.
externally.
The circuit is complex due to external Microcontrollers are present on chip memory. The circuit
Circuit complexity
connection. is less complex.
Memory and I/O The memory and I/O components are to be The memory and I/O components are available on chip.
components connected externally.
Efficiency Microprocessors are not efficient. Microcontrollers are efficient.
Microprocessors have less number of Microcontrollers have more number of registers.
Number of registers
registers.
Microprocessors are generally used in Microcontrollers are generally used in washing machines,
Applications
personal computers. and air conditioners.
6
Uses of Microcontroller

7
Microcontroller Manufacturers

▰ NXP Semiconductors -Netherlands


▰ Microchip Corporation - USA- PIC series
▰ Renesas Electronics Corporation - Japan
▰ Silicon Labs-USA
▰ STMicroelectronics- France-Italy- STM series
▰ Infineon Technologies- Germany
▰ Texas Instruments- USA- MSP430 series
▰ Cypress Semiconductor Corporation-USA.
8
Selection of Microcontrollers

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

Let’s start with the first set of slides

19
Common Terms

▰ Peripheral Devices- Timers, ADC, Analog Comparators, UART, I2C etc.


▰ Special function registers (SFRs)- Control registers, Status registers
▰ Flag bit
▰ Flash memory or program memory
▰ Data Memory- SRAM and DRAM
▰ Interrupt
▰ Vector address

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

▰ The stack operates as a 31-word by 21-bit RAM


▰ Stack Pointer
▰ The return address stack allows any combination of up to 31 program calls and interrupts to occur.
▰ Only the top of the return address stack (TOS) is readable and writable.
▰ TOP of Stack Registers: TOSL, TOSH, TOSU
▰ A CALL type instruction causes a push onto the stack;
▰ A RETURN,RETLW and RETFIE type instructions cause a pop from the stack.
▰ PUSH and POP Instructions

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

▰ It is used to recover from malfunctioning or hang condition


▰ It is not 100% accurate method to recover from hang condition.

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

▰ It is used for isolation

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

Let’s start with the first set of slides

35
8 bit PIC Microcontroller

▰ Manufacturer- Microchip Corporation USA


▰ Series- PIC10F, PIC12F, PIC16F, PIC18F
▰ Available in different no. of pins and packages.
▰ PIC10 and PIC12F series controllers have limited GPIO pins, low memory and
low features. Suitable for low end applications.
▰ 12F510 is an 8 bit microcontroller. It has total 8 pins (6 GPIO pins)

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

Let’s start with the first set of slides

38
PIC18F4520 Features

▰ Code and data Protection.


▰ 8 bit Microcontroller.
▰ Internal Oscillator Block: 8 selectable frequencies from 31 kHz to 8 MHz
▰ High Current Sink /Source 25 mA/25 mA
▰ Three Programmable External Interrupts
▰ Four Input Change Interrupts
▰ Two PWM Modules
▰ USART Module
▰ I2C module
▰ 10-Bit, up to 13-Channel Analog-to-Digital (A/D) Converter module
▰ Dual Analog Comparators with Input Multiplexing
▰ 100,000 Erase/Write Cycle Enhanced FlashProgram Memory Typical 39
PIC18F4520 Features

▰ 1,000,000 Erase/Write Cycle Data EEPROM Memory Typical


▰ Flash/Data EEPROM Retention: 100 Years Typical.
▰ Extended Watchdog Timer (WDT):Programmable period from 4 ms to 131s
▰ Wide Operating Voltage Range: 2.0V to 5.5V
▰ Programmable Brown-out Reset (BOR) with Software Enable Option

40
PIC18F4520 Pinout

44 Pin TQFP Package


40 Pin DIP Package 41
PIC18F4520 Packages

DIP 40 pin TQFP- 44 pin QFN- 44 pin

42
PIC18F4520 Peripheral details

43
I/O PORTS (GPIO pins)

▰ GPIO-General purpose Input Output


▰ I/O ports are multiplexed with an alternate function from the peripheral features on the device.
▰ When a peripheral is enabled, that pin may not be used as a general purpose I/O pin.
▰ Each port has three registers for its operation. These registers are:
❑ TRIS register (Data Direction register)
❑ PORT register (reads the levels on the pins of the device)
❑ LAT register (Data Latch register)
▰ Reading the PORTA register reads the status of the pins, whereas writing to it, will write to the port
latch.
▰ TRISA=1, PORTA becomes input, TRISA=0, PORTA becomes output.
44
I/O Port

45
Pull up and Pull down resistors

▰ Needed when port pin is digital input.


▰ Needed when out put is open collector or open drain
▰ Purpose: Needed to avoid unwanted oscillations.
▰ Never left input port pin floating. Gives erroneous data.

Resistor Four Resistor network Eight Resistor network


46
I/O Port

47
SFR bit format

Register: Oscillator Control register

48
On Chip Oscillator Frequency Setting

Register: Oscillator Control register

49
On Chip Oscillator Frequency Tuning

Register: Oscillator Tune Register

50
RESET

▰ The PIC18F2420/2520/4420/4520 devices differentiate between various kinds of Reset:

a) Power-on Reset (POR)

b) MCLR Reset during normal operation

c) MCLR Reset during power-managed modes

d) Watchdog Timer (WDT) Reset (during execution)

e) Programmable Brown-out Reset (BOR)

f) RESET Instruction

g) Stack Full Reset

h) Stack Underflow Reset

51
RESET Pin

▰ Pin No. 1 is MCLR(Master Clear). It is active low


External Reset pin.
▰ MCLR pin should not be left floated- Unwanted resets
▰ A Reset is generated by holding the pin low.
▰ The MCLR pin is not driven low by any internal Resets,
including the WDT.
▰ The MCLR input can be disabled with the MCLRE
Configuration bit. When MCLR is disabled, the pin
becomes a digital input.
▰ The diode D helps discharge the capacitor quickly when
VDD powers down.
. 52
Power on RESET

▰ 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

Let’s start with the first set of slides

56
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).

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

▰ Microchip devices must be configured to operate correctly. This is accomplished through


the use of configuration bits found in registers located in a special section of device
memory.
▰ It cannot be modified during program execution.
▰ Configuration memory can be written only with PICKit programmer.

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

▰ Name of all SFRs


cannot be used
as data variable.

67
Reserved Keywords in PIC Codes

Register: Oscillator Control register

▰ Name of bits in SFRs cannot


be used as data variable.

68
C data types

▰ Signed char 8 bit: -128 to +127


▰ Unsigned Char 8 bit: 0 to 255
▰ Signed int 16 bit: -32768 to 32767
▰ Unsigned int 16 bit: 0 to 65536
▰ Signed long 32 bit:-2,147,483,648 to 2,147,483,647
▰ Unsigned long 32 bit:0 to 4,294,967,295
▰ Signed long long 64 bit: -9223372036854775808 to 9223372036854775807
▰ Unsigned long long 64 bit : 0 to 18446744073709551615
▰ Float 24 bit: 8 bits for exponent and 16 bits for mantissa.
69
Pre-processor directive

A preprocessor directive is a command to the C preprocessor (Compiler)


▰ #define directive, which substitutes text for the specified identifier
▰ #include directive, which includes the text of an external file into a program.
#include <xc.h>
#include <stdio.h>

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.

▰ #pragma device PIC18F4520

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

▰ String is an array of ASCII characters


▰ String is always terminated in null character.

1. char str[] = “ATC Welcomes You";


2. char str[50] = " ATC Welcomes You";
3. char str[] = {‘A',’T',’C',' ',’W',’e',’l',’c',’o',’m','e',’s',' ‘,‘Y’,’o’,’u',' '\0’};
4. char str[17] = {‘A',’T',’C',' ',’W',’e',’l',’c',’o',’m','e',’s',' ‘,‘Y’,’o’,’u',' '\0’};

77
String and String Pointer

▰ When initialized, a pointer to a string points to the first character.

78
Passing Pointers to Functions

79
LED Blinking Program

▰ Create New Project and new C file


▰ Include header file
▰ Set Configuration Memory Fields
▰ Set Direction for PORTB
▰ Toggle the condition on PORTB pin after some delay.

80
6
LCD Interfacing with PIC18F4520

Let’s start with the first set of slides

81
Character LCD Display

▰ They are used to display data comprising of characters.


▰ Embedded products: Display of Parameters, modes, state of connected devices etc.
▰ Available in 16 by 2, 16 by 4, 20 by 2, 20 by 4.
▰ Display can be interfaced with microcontroller using I2C protocol: Only Two pins
needed. PCF8574 Remote 8-Bit I/O Expander for I2C Bus

82
16 by 2 LCD Interfacing

Each character is displayed in 5x7 pixel matrix.

83
16 by 2 LCD pinout

84
16 by 2 LCD

▰ It has data registers and command registers.


▰ Commands are saved in command register.
▰ Commands are used to initialize, delete characters, select line and column number for
writing characters, etc.
▰ Character to be displayed must be written to data register in ASCII format.

85
16 by 2 LCD

▰ RW- Read Write-


▰ RS- Register select (Data and Command)
▰ EN- For write or read operation pulse must be applied.
▰ For writing data to LCD RW=0.
▰ For Reading data From LCD RW=1.
▰ For writing Command to LCD RW=0, RS=0. Pulse on EN pin
▰ For writing Data to LCD RW=0, RS=1. Pulse on EN pin

86
16 by 2 LCD Commands

87
16 by 2 LCD 4 bit Mode

Hex code LCD Command

0x02 send for initialization of LCD for nibble (4-bit) mode

0x28 use 2 line and initialize 5*7 matrix in (4-bit mode)


0x0C display on cursor off
0x06 increment cursor (shift cursor to right)
0x01 Clear Entire Display

❑ Send higher nibble of command/data to LCD followed by lower nibble.

88
16 by 2 LCD 4 bit Mode

❑ It saves 4 GPIO pins of microcontroller


❑ Four bit mode is susceptible to noise leading to garbage data on the display.
❑ Avoid 4 bit mode in commercial products as possibility of garbage data is high.

Garbage data on LCD display

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

▰ Reference Voltage- Higher Vref+ and Lower Vref-


▰ Analog voltage lies between Vref+ and Vref-
▰ Microcontroller stops working if voltage on analog pin exceeds 6 V

94
ADC Specifications

▰ Reference Voltage- Higher Vref+ and Lower Vref-


▰ Resolution (Number of bits in output): The ADC resolution is defined as the smallest
incremental voltage that can be recognized and causes a change in the digital output.
▰ Number of Analog input channels
▰ Repeatability
▰ Conversion time: For ADCs, it refers to the amount of time that it takes to complete a whole
conversion.

95
Resolution of ADC

Table 1: Resolution versus Step Size for ADC (Vref = 5V)

n-bit Number of steps Step size

8 256 5V/256 = 19.53 mV

10 1024 5V/1024 = 4.88 mV

12 4096 5V/4096 = 1.2 mV

16 65,536 5V/65,536 = 0.076 mV

96
Resolution of ADC

Table 2: Vref Relation to Vin Range for an 8-bit ADC

Vref (V) Vin Range (V) Step Size (mV)


5.00 0 to 5 5 / 256 = 19.53

4.00 0 to 4 4 / 256 = 15.62

3.00 0 to 3 3 / 256 = 11.71

2.56 0 to 2.56 2.56 / 256 = 10

2.00 0 to 2 2 / 256 = 7.81

1.28 0 to 1.28 1.28 / 256 = 5

1.00 0 to 1 1 / 256 = 3.90


97
Resolution of ADC

Table 3: Vref Relation to Vin Range for an 10-bit ADC

Vref (V) Vin Range (V) Step Size (mV)


5.00 0 to 5 5 / 1024 = 4.88

4.96 0 to 4.096 4.096 / 1024 = 4

3.00 0 to 3 3 / 1024 = 2.93

2.56 0 to 2.56 2.56 / 1024 = 2.5

2.00 0 to 2 2 / 1024 = 2

1.28 0 to 1.28 1.28 / 1024 = 1.25

1.024 0 to 1.024 1.024 / 1024 = 1


98
Resolution of ADC

Table 4: 10-bit ADC with Vref+ =5 V, Step Size=4.88 mV

Vin (V) Decimal equivalent


0 0
1 205
2 410
2.5 512
3 615
4 820
5 1023
5.1 1023
>5.1 1023 99
Resolution of ADC

Table 5: 10-bit ADC with Vref+ =3.3 V, Step Size=3.222 mV

Vin (V) Decimal equivalent


0 0
1 310
1.5 465
2 620
3 931
3.3 1023
3.4 1023
>3.4 1023
100
Resolution of ADC

Table 6: 16-bit ADC with Vref+ =5 V, Step Size=76.293 uV

Vin (V) Decimal equivalent


0 0
1 13107
2 26215
2.5 32767
3 39322
4 52429
5 65535
5.1 65535
>5.1 65535
101
Impact of ADC Resolution

▰ Accuracy of physical quantity measurement using ADC depends on resolution


and repeatability of ADC.

102
Transfer Characteristics of ADC

103
Built in ADC of PIC18F4520 Microcontroller

▰ All PIC microcontrollers have built in ADC module.


▰ Built in 10 bit ADC module
▰ AD Converter module has 13 analog inputs
▰ Reference voltage selection through SFRs
▰ ADC module is controlled through user program

104
Built in ADC of PIC18F4520 Microcontroller

▰ AD Converter module has 13 analog inputs.


▰ This module allows conversion of an analog input signal to a corresponding 10-bit
digital number.

▰ The module has five registers:


o A/D Result High Register (ADRESH)
o A/D Result Low Register (ADRESL)
o A/D Control Register 0 (ADCON0)
o A/D Control Register 1 (ADCON1)
o A/D Control Register 2 (ADCON2)
105
Analog to Digital Conversion (ADC)

▰ Analog port pin must be input pin. TRIS bit must be 1.


▰ Select reference voltage source through ADCON1.
▰ Make selected port pin analog through ADCON1.
▰ Decide result format left or right justified. I prefer to have right justified.
▰ Select Acquisition time and ADC clock frequency using ADCON2.

106
Analog to Digital Conversion (ADC)

107
Analog to Digital Conversion (ADC)

108
8
Reading and Writing EEPROM in
PIC18F4520

Let’s start with the first set of slides

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

▰ EEPROM (also E2PROM) stands for electrically erasable programmable read-


only memory and is a type of non-volatile memory.
▰ It retains data even if power is removed.
▰ IN PIC microcontroller it can be read or write using application program.
▰ Number of write operations, 10,00000 (10 lacs) .

111
Registers Associated with EEPROM

112
5
Serial Communication
Let’s start with the first set of slides
113
UART Communication

▰ It is used for Device to Device serial communication using three wires.


▰ UART is a serial communication device that performs parallel – to – serial
data conversion at the transmitter side and serial – to – parallel data
conversion at the receiver side.
▰ It is universal because the parameters like transfer speed, data speed, etc. are
configurable.

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.

Standard bit rates:

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

▰ Transmit Status and Control (TXSTA)


▰ Receive Status and Control (RCSTA)
▰ Baud Rate Control (BAUDCON)
▰ Baud Rate Generator SPBRGH and SPBRG
▰ TXREG
TSR(Transfer Shift Register. Used internally only)
▰ RXREG
RSR(Receive Shift Register. Used internally only)

120
Baud Rate Generator

121
Serial Transmission using PIC18F4520

To set up an Asynchronous Transmission:


1. Initialize the SPBRGH:SPBRG registers for the appropriate baud rate. Set or clear the BRGH
and BRG16 bits, as required, to achieve the desired baud rate.
2. Enable the asynchronous serial port by clearing bit, SYNC, and setting bit, SPEN.
3. If interrupts are desired, set enable bit, TXIE.
4. If 9-bit transmission is desired, set transmit bit, TX9. Can be used as address/data bit.
5. Enable the transmission by setting bit, TXEN, which will also set bit, TXIF.
6. If 9-bit transmission is selected, the ninth bit should be loaded in bit, TX9D.
7. Load data to the TXREG register (starts transmission).
8. If using interrupts, ensure that the GIE and PEIE bits in the INTCON register (INTCON<7:6>)
are set.
122
Serial Reception using PIC18F4520

1. Initialize the SPBRGH:SPBRG registers for the appropriate baud rate.


2. Enable the asynchronous serial port by clearing bit, SYNC, and setting bit, SPEN.
3. If interrupts are desired, set enable bit, RCIE.
4. If 9-bit reception is desired, set bit, RX9.
5. Enable the reception by setting bit, CREN.
6. Flag bit, RCIF, will be set when reception is complete and an interrupt will be generated if enable bit, RCIE,
was set.
7. Read the RCSTA register to get the 9th bit (if enabled) and determine if any error occurred during reception.
8. Read the 8-bit received data by reading the RCREG register.
9. If any error occurred, clear the error by clearing enable bit, CREN.
10. If using interrupts, ensure that the GIE and PEIE bits in the INTCON register (INTCON<7:6>) are set.
123
6
Interrupt
Let’s start with the first set of slides
124
Interrupt

▰ Interrupts are signals that tell the microcontroller to stop what it is


doing and execute a specific function, called an interrupt service
routine (ISR).

▰ Interrupts can be triggered by various sources, such as buttons,


sensors, serial communication, or other peripherals.

▰ What is application of interrupt?

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

▰ High-priority interrupt vector address 0008h

▰ Low-priority interrupt vector address 0018h.

▰ Interrupt sources have three bits to control their operation. They are:

• Flag bit to indicate that an interrupt event occurred

• 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


128
Interrupt

▰ Occurrence of High Priority Interrupt

▪ 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.

▪ 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

▰ Step Angle – 0.9 degree, 1.8 degree

▰ Torque

▰ Type- Bipolar or Unipolar

131
Stepper Motor Specifications

•Rated Voltage: 5, 12, 18, 24


•Number of Phases: 2,4
•Stride Angle/step Angle: 0.9, 1.8, 5.625 degrees etc
•Pull in torque: gf.cm

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

Step Blue Pink Yellow Orange

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

Step Blue Pink Yellow Orange

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

Virtual Internship on "IoT Project


Development" by Dr. A. B. Kakade
I2C Protocol reserved address in 7 bit address mode
Examples of I2C Peripherals
▰ ADS1115- 4 channel ADC
▰ Capacitance to digital converter(FDC1004).
▰ Differential Pressure Sensor (SDP611)
▰ IR Temperature Sensor (MLX90614)
▰ SHT3x Temperature and Humidity Sensor
▰ Adafruit 10-DOF IMU Breakout
▰ Accelerometer & Compass Module (LSM303)
▰ Compass (LSM303)
▰ Accelerometer (LSM303)
▰ Accelerometer (ADXL345)
▰ RGB Color Sensor (TCS34725)
▰ Gyroscope (L3GD20H)
▰ Gyroscope (PS-ITG-3200)
▰ ToF Range Finder Sensor (VL6180X)
▰ Ultrasonic Range Finder (SRF02)
▰ Temperature and Pressure Sensor (BMP180)
▰ Digital I/O Expander (SX1509)
Interfacing of ADS1115 with PIC18f4520

▰ PIC18F4520 microcontroller has on chip hardware for I2C protocol implementation.


MASTER SYNCHRONOUS SERIAL PORT (MSSP) MODULE

▰ 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

▰ The MSSP module has six registers for I2C operation.


o Status register (SSPSTAT) and two control registers (SSPCON1 and
SSPCON2).
o The Serial Receive/Transmit Buffer Register (SSPBUF)
o Shift Register (SSPSR) – Not directly accessible
o MSSP Address Register (SSPADD)

▰ 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

▰ In receive operations, SSPSR and SSPBUF together create a


double-buffered receiver. BF flag is set.
▰ When SSPSR receives a complete byte, it is transferred to
SSPBUF and the SSPIF flag bit is set.
▰ During transmission, the SSPBUF is not double buffered. A
write to SSPBUF will write to both SSPBUF and SSPSR.

Page no. 172


I2C Mode

▰ In I2C mode SCL and SDA pins becomes open-drain.


▰ SCL and SDA pins must be configured input through TRIS
register.
▰ In Master mode, the SCL and SDA lines are manipulated by
the MSSP hardware.
Page no. 172, 187
I2C Mode
I2C Mode

▰.

Page no. 172, 187


THANKS!
You can find me at
[email protected]

150

You might also like