Embedded Systems
Workshop
μC Interfacing I
ENG. Zeyad Hesham
Embedded Systems
❑ Agenda:
▪ Introduction to Embedded Systems.
▪ C Programming.
▪ Micro-controller Interfacing (Part I).
▪ C for Embedded Applications.
▪ Micro-controller Interfacing (Part II).
▪ Introduction to Real Time Operating Systems.
Micro-controller Interfacing (Part I)
❑ Agenda:
▪ AVR Microcontrollers
▪ Input/output Ports
▪ Interrupt
▪ Timers
▪ Motor Interfacing
Micro-controller Interfacing (Part I)
❑ Agenda:
▪ AVR Microcontrollers
▪ Input/output Ports
▪ Interrupt
▪ Timers
▪ Motor Interfacing
AVR Microcontrollers
• AVR microcontroller was manufactured by Atmel Corporation in 1996 and its
architecture was developed by “Alf-Egil Bogen & Vegard Wollan.
• The name AVR was taken from its developers namely Alf-Egil Bogen & Vegard
Wollan RISC microcontroller.
• AVR available in 8, 16, and 32-bit . So, the most used AVR microcontrollers
are; ATmega8, ATmega16, ATmega32 &ATmega328microcontrollers.
AVR Microcontrollers
• AVR Microprocessor is fast. It executes most of the instructions in Single execution cycle.
• It consumes less power and can be operated In different power saving modes
• Free IDE(Atmel Visual Studio).
• The datasheet (Target Specification) for AVR Microcontrollers is well documented.
• AVR Microcontrollers targets are available in Egyptian market.
• AVR Is register based architecture and follows Harvard architecture.
AVR Microcontrollers
• ATmega 16/32 Microcontrollers
• There are two flavors for ATmega16/32 microcontroller:
1. ATmega16/32: Operating frequency range is 0-16 MHz.
2. ATmega16L/32L: Operating frequency range is 0-8 MHz.
• If we are using a crystal of 8 MHz = 8 x 106 Hertz = 8 Million cycles, then AVR
can execute 8 million instructions per second. Most of AVR instructions are
executed in just one clock cycle (Check AVR Instruction Set).
• The higher is the operating frequency of the microcontroller, the higher will be
it’s processing speed. We need to optimize the power consumption with
processing speed and hence need to select the operating frequency
accordingly.
AVR Microcontrollers
• ATmega 16/32 Microcontrollers
AVR Microcontrollers
• ATmega 16/32 Microcontrollers
• I/O Ports
• ATmega16/32 has four 8 fit input-output ports (PORTA. PORTB, PORTC and PORTO)
• Timers/Counters
• ATmega16/32 has two 8-bit timers/counters and one 16-bit timer/counter. Timers are
used for calculate timing operations and delays.
• PWM(Pulse Width Modulation)
• ATmega16/32 has 4 PWM Channels. One channel With Timer0, Two channels With
Timer1 and One channel With Timer2.
• Watchdog Timer
• Present With internal oscillator. Watchdog timer continuously monitors and resets the
controller if the code gets stuck at any execution action for more than a defined time
interval.
AVR Microcontrollers
• ATmega 16/32 Microcontrollers
• ADC Interface
• ATmega16/32 IS equipped with an 8 channels ADC with a resolution of 10-bits, ADC
reads the analog input for example sensor input and converts it into digital information
which is understandable by the microcontroller.
• Analog Comparator
• The Analog Comparator compares the input values of two analog signals.
• Interrupt Unit
• Consists of 21 Interrupt sources out of which four are external interrupts. The remaining
are internal Interrupts which support the peripherals like USART, ADC, Timers etc.
• USART
• It is available for interfacing with other microcontroller or external device capable of
communicating serially(data transnmsston bit by bit).
AVR Microcontrollers
• SPI (Serial Peripheral Interface)
• Used for serial communication between two devices on a common clock source.
• TWI(Two Wire Interface )
• It is also called Inter-integrated Circuits. It can be used to set up a network of devices.
Many devices can be connected over TWI interface forming a network, The devices can
transmit or receive data, and each device has Its own unique address
• ISP(ln System Programmable )
• AVR Microcontrollers family has 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.
• Memory System of Atmega 16/32:
• 16/32 KB Flash EEPROM Program Memory.
• 1/2 KB SRAM Data Memory.
• 512B/1KB EEPROM Non-Volatile Data Memory
AVR Microcontrollers
• R= 4.7 KΩ
• C= 100 nf
AVR Microcontrollers
• The AVR CPU can be driven by any of the following clock sources
• External Crystal Resonator.
• External Low Frequency Crystal (32KHz).
• External RC Oscillator (Use XTAL1 Pin only).
• Internal Calibrated RC Oscillator.
• External Clock (Use XTALI Pin only).
• Refer to the System Clock and Clock Options chapter in the
ATmega16/32
• Datasheet.
Micro-controller Interfacing (Part I)
❑ Agenda:
▪ AVR Microcontrollers
▪ Input/output Ports
▪ Interrupt
▪ Timers
▪ Motor Interfacing
Input/output Ports
❑ Agenda:
▪ I/O Ports
▪ I/O Pins Programming
▪ I/O Pins Current and Voltage
▪ Interface 𝜇c with Button, LED and Buzzer
▪ Interface 𝜇c with 7-Segment
▪ Interface 𝜇c with PIR Sensor
Input/output Ports
❑ Agenda:
▪ I/O Ports
▪ I/O Pins Programming
▪ I/O Pins Current and Voltage
▪ Interface 𝜇c with Button, LED and Buzzer
▪ Interface 𝜇c with 7-Segment
▪ Interface 𝜇c with PIR Sensor
ATmega32 I/O Ports
• ATmeg32 contain 40 Pins.
• 4 Ports (A, B, C and D)each one has 8 Pins P0:P7
• Each I/O pin is controlled by 3 bits:
• DDxn (in DDRx):
• Direction (1 = Output, 0 = Input)
• PORTxn (in PORTx):
• For Output: 0 = Low, 1 = High
• For Input: 1 = Enable Pull-up, 0 = Hi-Z
• PINxn (in PINx):
• Read pin value (input state)
ATmega32 I/O Ports
• ATmega16/32 Microcontrollers has 32 programmable I/o Pins constituting
four ports.
• The ports are listed below
1. PORTA(PA7…….PA0)
2. PORTB(PB7…….PB0)
3. PORTC(PC7…….PC0)
4. PORTD(PD7…….PD0)
• Each port has 8 pins. The pins of these for ports can be used as general-
purpose inputs/outputs.
• Each I/O pin could be Input or Output (Multiplexed Direction). This could be
controlled through the I/O Port direction register.
• Each Microcontroller pin could be assigned to different I/O functions.
• Pin could be configured for digital I/O, analog input, timer I/O, or serial I/O.
ATmega32 I/O Ports
• Each PORT is controlled by 3 registers:
• DDRX (Data Direction Register):
• This register used to decide the direction of the pins, i.e.
whether the pins will act as input pins or as output pins.
• PORTX (Output Register)
• This register is used to set the logic on the output pins HIGH
or LOW.
• PINX (Input Register)
• This register is used to read the logic level on the port input
pins.
• Note: x could be A, B, C, or D depending on which port registers
are being addressed.
ATmega32 I/O Ports
• Bit 2 – PUD:
• Pull-up disable When this bit is written to one, the pull-ups in the I/O ports are disabled even if the
DDxn and PORTxn Registers are configured to enable the pull-ups.
Input/output Ports
❑ Agenda:
▪ I/O Ports
▪ I/O Pins Programming
▪ I/O Pins Current and Voltage
▪ Interface 𝜇c with Button, LED and Buzzer
▪ Interface 𝜇c with 7-Segment
▪ Interface 𝜇c with PIR Sensor
I/O Pins Programming
• How to set values in registers:
• Configure port A as output:
• DDRA = 255;
• DDRA = 0xFF;
• DDRA = 0b11111111;
• Set port A pins:
• PORTA = 255;
• PORTA = 0xFF;
• PORTA = 0b11111111;
• Clear port A pins:
• PORTA = 0;
• PORTA = 0x00;
• PORTA = 0b00000000;
I/O Pins Programming
• How to set values in registers: • How to set values in specific
• Configure port A as output: pin:
• DDRA = 255; • Configure pin A5 as output:
• DDRA = 0xFF; • DDRA |= (1<<5);
• DDRA = 0b11111111; • Set pin A5:
• Set port A pins: • PORTA = PORTA | (1<<5);
• PORTA = 255; • Clear pin A5:
• PORTA = 0xFF; • PORTA &= ~(1<<5);
• PORTA = 0b11111111;
• Toggle pin A5:
• Clear port A pins: • PORTA ^= (1<<5)
• PORTA = 0;
• PORTA = 0x00;
• PORTA = 0b00000000;
I/O Pins Programming
• Check if bit is set (1): • Check if bit is clear (0):
• Check for pin 2 of port C • Check for pin 2 of port C
• if (PINC & (1<<2) ) • if ( ~( PINC & (1<<2) ) )
{ {
//code //code
} }
Input/output Ports
❑ Agenda:
▪ I/O Ports
▪ I/O Pins Programming
▪ I/O Pins Current and Voltage
▪ Interface 𝜇c with Button, LED and Buzzer
▪ Interface 𝜇c with 7-Segment
▪ Interface 𝜇c with PIR Sensor
I/O Pins Current and Voltage
• When working with a particular microcontroller, it is very Important to read
datasheet, to find out how much current different pins can handle. When
talking about current, datasheets will (usually) mention 2 words: Sink and
Source.
• Sink Current:
• The sink value is the amount of current that can flow into the pin (and therefore into the
microcontroller) safely.
• Source Current:
• The source current is the amount of current that can be pulled out of the pin safely.
• If you exceed the sink current or the source current on a given pin, you could
fry the entire processor, which means added expense, and more time.
I/O Pins Current and Voltage
• Current sinking and sourcing on ATmega16/32 Pins
• Each I/O pin can sink or source a maximum current of 40mA.
• Vcc pin can sink maximum current of 200mA.
• Although each pin can sink or source 40mA current, it must be ensured that the
current sourced or sinked from all the ports combined, should not exceed 200mA.
• There are further restrictions on the amount of current sourced or sinked by each
port.
• ATmega16/32 Voltage
• Operating Voltages:
➢ 4.5 – 5.5 V for Atmega 16/32.
➢ 2.7 – 5.5 V for Atmega 16L/32L.
• Maximum Operating Voltage 6 .0V.
Input/output Ports
❑ Agenda:
▪ I/O Ports
▪ I/O Pins Programming
▪ I/O Pins Current and Voltage
▪ Interface 𝜇c with Button, LED and Buzzer
▪ Interface 𝜇c with 7-Segment
▪ Interface 𝜇c with PIR Sensor
Interface 𝜇c with Button, LED and Buzzer
Interface 𝜇c with Button, LED and Buzzer
• Simple Application Structure using AVR Library:
# include <avr/io.h>
int main (void)
{
/ * Application Initializat ion * /
/ * Super Loop * /
while ( 1 )
{
/ * Application Code * /
}
}
Interface 𝜇c with Button, LED and Buzzer
• Example1) Write Embedded C code using ATmega16/32 𝜇c to control a Led
using a Push Button.
• Requirements:
• Configure the 𝜇c clock with 16Mhz Crystal Oscillator.
• The Push Button is connected to pin 2 in PORTD using Pull Up
configuration.
• The LED is connected to pin 1 in PORTC using Positive Logic
configuration.
• Turn on the Led when the Push Button status is pressed.
• Turn off the Led when the Push Button status is released.
Interface 𝜇c with Button, LED and Buzzer
• Busy Loop Delay
• Software Technique to make the processor halt its activity for a certain time
using loop.
• Delay library "util/delay.h" in AVR provides two basic functions:
• _delay_ms ( value_in_ms );
• _ delay _ us ( value_in_us );
• Before using the delay library, we have to define our system frequency
through the F CPU macro used by the delay library:
• #define F_CPU 8000000UL
• If the F CPU macro is not defined before using the delay library, It will use its
default frequency value which is 1Mhz
Interface 𝜇c with Button, LED and Buzzer
• Example2) Write Embedded C code using ATmega16/32 𝜇c to control a LED.
• Requirements:
• Configure the 𝜇c clock with 16Mhz Crystal Oscillator.
• The led is connected to pin 2 in PORTC.
• Connect the Led using Positive Logic configuration.
• Flash the led every 1 second.
Interface 𝜇c with Button, LED and Buzzer
• Example3) Write Embedded C code using ATmega16/32 𝜇c to control a LED.
• Requirements:
• Configure the 𝜇c clock with 16Mhz Crystal Oscillator.
• The led is connected to pin 2 in PORTC.
• Connect the Led using Positive Logic configuration.
• Flash the led every 1 second.
Interface 𝜇c with Button, LED and Buzzer
• Push Button De-bounce Issue
• When the Push Button is pressed. the switch does not provide a clean edge. The
input value is bouncing for 10-30 ms.
• If this signal was used as an input to a digital counter(Will be discussed later), for
example, you would get multiple counts rather than the expected single count.
• It could be fixed by software. The basic idea is to implement a delay after the first
detected edge, during which no scanning for the Push Button is done. after the delay
period is finished; scanning can proceed. This way the bouncing can be avoided.
• It could be also fixed by hardware using a capacitor.
Interface 𝜇c with Button, LED and Buzzer
• Example4) Write Embedded C code using ATmega16/32 𝜇c to control a Led
using a Push Button.
• Requirements:
• Configure the 𝜇c clock with internal 1Mhz Clock.
• The Push Button is connected to pin O in PORTB.
• Connect the Push Button using Internal Pull Up configuration.
• The Led is connected to pin O in PORTC.
• Connect the Led using Negative Logic configuration.
• If the Push Button is pressed just TOGGLE the Led every new press from the
Push Button.
Interface 𝜇c with Button, LED and Buzzer
• Exercise1)Write Embedded C code using ATmega16/32 𝜇c to control 3LEDs.
• Requirements:
• Configure the 𝜇c clock with 16Mhz Crystal Oscillator.
• Use the 3 LEDs at PC0, PC1 and PC2.
• LEDs are connected using Positive Logic configuration.
• A roll action is perform using the LEDs each led for half second. The first
LED is lit and roll down to the last LED then back to the first LED. This
operation is done continuously.
Interface 𝜇c with Button, LED and Buzzer
• Exercise2) Write Embedded C code using ATmega16/32 𝜇c to control eight LEDs
using two Push Buttons.
• Requirements:
• Configure the 𝜇c clock with internal 1 MHz Clock.
• The Push Buttons 1 & 2 are connected to pin 3 & 4 in PORTA.
• Connect both Push Buttons using Pull Down configuration.
• 8-Leds are connected to PORTD.
• Connect all LEDs using Positive Logic configuration.
• If Push Button 1 is pressed just flash the even LEDs numbers every half second.
• If Push Button 2 is pressed just flash the odd I-eds numbers every half second.
• In case both Push Buttons are pressed just flash the even LEDs numbers every half
second.
• In case no Push Buttons are pressed all LEDs are off.
Interface 𝜇c with Button, LED and Buzzer
• Buzzer is an electrical device, which is similar to a bell that makes a buzzing
noise and is used for signaling. Typical uses of buzzers and beepers include
alarm devices, timers and confirmation of user input such as a mouse click
or keystroke.
• Example5) Write Embedded C code using ATmega16/32 𝜇c to control a
buzzer using a push button.
• Requirements:
• Configure the 𝜇c clock with 16Mhz Crystal Oscillator.
• The Push Button is connected to pin 3 in POR TD using Pull Up configuration.
• The Buzzer is connected to pin 5 in PORTC.
• The buzzer should make a noise when the push button is pressed and stay silent when
the push button is released.
• Test this exercise on the Eta32mini board.
Input/output Ports
❑ Agenda:
▪ I/O Ports
▪ I/O Pins Programming
▪ I/O Pins Current and Voltage
▪ Interface 𝜇c with Button, LED and Buzzer
▪ Interface 𝜇c with 7-Segment
▪ Interface 𝜇c with PIR Sensor
Interface 𝜇c with 7-Segment
• 7 -segment displays are made up of 8 LED
segments. 7 of these LED segments are in the shape
of a line, whereas 1 segment is circular.
• The 7 line shaped LED segments are used for
displaying numbers 0 to 9 and a few letters like A, B,
C, D, E, F, G, etc. The circular segment is used for
displaying decimal point.
• Each of the 8 elements has a pin associated with it
which can be driven HIGH or LOW according to type
of display and the number to be displayed.
• There are two types of 7-segments Common Anode
and Common Cathode.
Interface 𝜇c with 7-
Segment
• Common Cathode 7-Segment
• All the cathode connections of the LED
segments are joined together to logic “0" or
ground. The individual segments are
illuminated by application of a "HIGH", or
logic "1" signal via a current limiting resistor
to forward bias the individual Anode
terminals.
Interface 𝜇c with 7-
Segment
• Common Anode 7-Segment
• All the Anodes connections of the LED
segments are joined together to logic “1" or
VCC. The individual segments are
illuminated by application of a “LOW", or
logic “0" signal via a current limiting resistor
to forward bias the individual Anode
terminals.
Interface 𝜇c with 7-Segment
• Depending upon the decimal digit to be displayed, the particular
set of LEDs is turned on.
• For example, to display the numerical digit 0, we will need to light
up six of the LED segments corresponding to a, b, c, d, e and f. so
the various digits from 0 through 9 can be displayed using a 7-
segment display as shown.
Interface 𝜇c with 7-Segment
• A binary coded decimal (BCD) to 7-segment display decoder such
as the TTL 74LS48 for common cathode 7-segment, have 4 BCD
inputs and 7 output lines, one for each LED segment.
• This allows a smaller 4-bit binary number to be used to display all
the numbers from O to 9.
Interface 𝜇c with 7-Segment
• Example6) Write Embedded C code using ATmega16/32 𝜇c to
control a 7-Segment using a Push Button.
• Requirements:
• Configure the 𝜇c clock with internal 1Mhz Clock.
• The Push Button is connected to pin 4 in PORTD.
• Connect the Push Button using Pull Down configuration.
• The 7-Segment is connected to first 4-pins of PORTC.
• If the Push Button is pressed just increase the number appeared in the 7-
segment display, and if the 7-segment reaches the maximum number (9)
overflow occurs.
Interface 𝜇c with 7-Segment
• Exercise3) Write Embedded C code using ATmega16/32 𝜇c to
control a 7-segment using two Push Buttons.
• Requirements:
• Configure the 𝜇c clock with 16Mhz Crystal Oscillator.
• The two Push Buttons are connected to pin 2 and 3 in PORTD.
• Connect the Push Button using Pull Up configuration.
• The 7 -Segment is connected directly to PORTA from PA1 => PA7
• The 7 -Segment type is common cathode and enable the first 7-segment
which its common pin controlled by PC6 pin.
• If the Push Button 1 is pressed just increase the number appeared in the
7-Segment display, and if the number reaches the maximum number (9)
then do nothing. If the Push Button 2 is pressed just decrease the number
appeared in the 7-Segment display, and if the number reaches the
minimum number (0) then do nothing.
Input/output Ports
❑ Agenda:
▪ I/O Ports
▪ I/O Pins Programming
▪ I/O Pins Current and Voltage
▪ Interface 𝜇c with Button, LED and Buzzer
▪ Interface 𝜇c with 7-Segment
▪ Interface 𝜇c with PIR Sensor
THANK
YOU