Microcontroller Interfacing Essentials
Microcontroller Interfacing Essentials
The AT90S8515 was the first microcontroller which was based on AVR
architecture.
USART
• interface is available for interfacing with external device capable of
communicating serially (data transmission bit by bit).
Atmega16 Architecture cont.
General Purpose Registers
• Atmega16 is equipped with 32 general purpose registers which are coupled
directly with the Arithmetic Logical Unit (ALU) of CPU.
Atmega16 Architecture cont.
Memory
Atmega16 consist of three different memory sections:
• Flash EEPROM:
• Byte Addressable EEPROM
• SRAM
Atmega16 Architecture cont.
ISP
• AVR family of controllers have In System Programmable Flash Memory which
can be programmed without removing the IC from the circuit, ISP allows to
reprogram the controller while it is in the application circuit.
SPI
• Serial Peripheral Interface, SPI port is used for serial communication between
two devices on a common clock source.
• The data transmission rate of SPI is more than that of USART.
Atmega16 Architecture cont.
TWI
• Two Wire Interface (TWI) can be used to set up a network of devices, many
devices can be connected over TWI interface forming a network, the devices
can simultaneously transmit and receive and have their own unique address.
DAC
• Digital to Analog Converter (DAC) interface which can be
used for reverse action performed by ADC.
• DAC can be used when there is a need of converting a digital signal to analog
signal.
AVR reset circuit
AVR crystal circuit
AVR GPIO
• Every micro-controller has GPIO ports.
• GPIO stands for general purpose input output. These GPIO ports are
used to take input on a micro-controller pin or output a value on
micro-controller pin.
AVR GPIO Ports
AVR GPIO Ports
GPIO control registers
• DDRn – Data Direction Register
• PORTn – Port Output data Register
• PINn – Port Input Register
• n- Indicates the port name i.e. A, B, C & D
GPIO control registers
GPIO control registers cont.
DDRn Register
• Data Direction Register configures data direction of a port or a port pin.
• Writing a value 0 configure that port pin as INPUT and writing a value 1
configures a port pin as OUTPUT.
GPIO control registers cont.
• PORTn Register
• To output data when port is configured as output
• To activate/deactivate internal pull-up registers when port
is configures as input
GPIO control registers cont.
• activate/deactivate internal pull-up register
• When you set bits in DDRn to 0, i.e. make port pins as inputs
• then corresponding bits in PORTn register are used to activate/deactivate
pull-up registers associated with that pin.
• activate pull-up resister, set bit in PORTn to 1, and to deactivate (i.e to make
port pin tri stated) set it to 0.
GPIO control registers cont.
GPIO control registers cont.
PINn Register
• The PINn register keeps the status of all the pins in that port. By
reading this register we can get the current logic level (0 or 1) on the
pin.
Example
LEDS and Switches
Switch Debouncing
Debouncing solution
Seven Segment
Seven Segment
A seven segment consists of eight LEDs which are aligned in a manner
so as to display digits from 0 to 9 when proper combination of LED is
switched on.
Seven segment uses seven LED’s to display digits from 0 to 9 and the
eighth LED is used for the dot.
Seven Segment types
• Seven Segment are available in two configuration
• Common Anode
• Common Cathode.
Display digits.
Multiplexing 7segments using decoder
Multiplexing 7segments using decoder
Relay interfacing
Keypad Interfacing
Keypad Interfacing
Keypad is organized as a matrix of switches in rows and column.
The concept of interfacing keypad with the MCU is:
• Every number is assigned two unique parameters, i.e. row and column
number (n(R, C) for example 6 (2, 3)).
• Hence every time a key is pressed the number is identified by detecting
the row and column number of the key pressed.
Keypad Interfacing Cont.
Keypad scanning
Initially all the rows are set to zero by the controller and the columns are scanned
to check if any key is pressed
In case no key is pressed the output of all the columns will be high.
Whenever a key is pressed the row and column corresponding to the key will get
short, resulting in the output of the corresponding column goes to go low
since we have made all the rows zero this gives the column number of the
pressed key.
LCD Interfacing
LCD?
LCD (Liquid Crystal Display) screen is an electronic display module and find a wide
range of applications.
A 16x2 LCD display is very basic module and is very commonly used in various
devices and circuits
The reasons being: LCDs are economical; easily programmable; have no limitation
of displaying special & even custom characters (unlike in seven segments),
animations and so on.
Interfacing LCD
LCD Interfacing signals
• 8-bit data bus (RD7-RD0)
• Three control signals
• RS – Register Select (RA3)
• R/W – Read/Write (RA2)
• E – Enable (RA1)
• Three power connections
• Power, ground, and variable resistor to control brightness
Interfacing LCD
Driver has two 8-bit internal registers
Instruction Register (IR):
• The command register stores the command instructions given to
the LCD.
• A command is an instruction given to LCD to do a predefined task
like initializing it, clearing its screen, setting the cursor position,
controlling display etc.
Data Register (DR) to write data (ASCII characters)
• The data register stores the data to be displayed on the LCD.
• The data is the ASCII value of the character to be displayed on the
LCD.
LCD Interfacing modes
Can be interfaced either in 8-bit mode or 4-bit mode
• In 8-bit mode, all eight data lines are connected
• In 4-bit mode, only four data lines are connected
• Two transfers per character (or instruction) are needed
LCD Operation
When the microcontroller writes an instruction to IR or data to DR, the
controller:
Sets DB7 high indicating that the controller is busy
Sets DB7 low after the completion of the operation
The microcontroller should always check whether DB7 is low before sending
an instruction or a data byte
Note : we skip this check and make delays instead of it.
Timing diagram: writing to LCD
initialize LCD
• The following instructions are used to initialize LCD.
• 0x38 to initialize LCD in 8-bit mode
• 0x01 to clear LCD
• 0x0E to make cursor ON
• 0x80 to go to first line and 0th position in LCD
• Delay of around 50 micro second is necessary b/w two commands for
execution of instructions.
Send command function
• Global Interrupt Enable (GIE) bit must be set in the status register
(SREG)
• The individual interrupt enable bits must be set in the proper control
register.
• See datasheet
External Interrupts
• External Interrupts:
Level- or edge-triggered
Level-triggered
threshold
t
trigger point
Edge-triggered
t
trigger point
Using interrupts
• Keep the ISR short!
• If you change variables: Keep in mind that you could jump to the ISR
from everywhere in your program!ƒ
• If you want to create a section were no IRs are allowed clear the
Interrupt flag in the SREG register (careful!)
Using interrupts