0% found this document useful (0 votes)
13 views24 pages

08 - Lecture 6-Embedded

The document discusses different registers associated with ports in AVR microcontrollers and how they control the direction and state of the port pins. It explains the DDR, PORT and PIN registers and how they are used to set the pins as input or output and activate pull-up resistors. Examples of interfacing switches and LEDs and displaying numbers on 7-segment displays are also provided.

Uploaded by

Ebram Wagdy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
13 views24 pages

08 - Lecture 6-Embedded

The document discusses different registers associated with ports in AVR microcontrollers and how they control the direction and state of the port pins. It explains the DDR, PORT and PIN registers and how they are used to set the pins as input or output and activate pull-up resistors. Examples of interfacing switches and LEDs and displaying numbers on 7-segment displays are also provided.

Uploaded by

Ebram Wagdy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 24

Embedded system

Dr: sahar kamal


AVR Registers
• AVR is 8 bit microcontroller therefore all its ports are 8 bit wide. Every
port has 3 registers associated with it each one have size of 8 bits.
Every bit in those registers configures the pins of particular port. Bit0
of these registers are associated with Pin0 of the port, Bit1 of these
registers are associated with Pin1 of the port, and same as for other
bits.
• The three registers available in AVR microcontroller are as follows:
• DDRx register
• PORTx register
• PINx register
DDRx register
• Data Direction Register configures the data direction of port pins. These
registers are used for determining whether port pins will be used for input
or output. On writing 1 to a bit in DDRx makes corresponding port pin as
output, while writing 0 to a bit in DDRx makes corresponding port pin as
input
• For making all pins of port A as output pins:
1.DDRA= 0b11111111;
• For making all pins of port A as input pins:
1.DDRA= 0b00000000;
2.For making lower nibble of port B as output and higher nibble as input:
3.DDRB=0b00001111;
PINx register
• PINx register used to read the data from port pins. In order to read the data
from port pin, first we have to change the port?s data direction to input. This
is done by setting bits in DDRx to zero. If port is made output, then reading
PINx register will give a data that has been output on port pins.
• There are two input modes. Either we can use port pins as internal pull up or
as tri stated inputs. It will be explained as shown below:
• For reading the data from port A,
1.DDRA = 0x00; //Set port A as input
2.x = PINA; //Read contents of port a
PORTx register:
• In general, PORTx register can be used for two purposes:
• To output data: when port is configured as output then PORTx register is used. When we
set bits in DDRx to 1, corresponding pins becomes output pins. Now we can write the
data into respective bits in PORTx register. This will immediately change the output state
of pins according to data we have written on the ports. For example:
• To output data in variable x on port A
• DDRA = 0xFF; //make port A as outputs
• PORTA = x; //output variable on port
• To output 0xFF data on port B
• DDRB = 0b11111111; //set all the pins of port B as outputs
• PORTB = 0xFF; //write the data on port
• To output data on only 0th bit of port C
• DDRC.0 = 1; //set only 0th pin of port C as an output
• PORTC.0 = 1; //make it high signal.
pull up resistors
• To activate/deactivate pull up resistors: when port is configured as input
we set the bits in DDRx to 0, i.e. make port pins as inputs the
corresponding bits in PORTx registers used to activate/deactivate pull-up
registers associated with that pin. In order for activating pull-up resistor,
set the bit in PORTx register to 1, and for deactivating (i.e. to make port
as tri stated) set it to zero
pull up resistors
• make lower pins of port A as output, higher pins as input with pull-ups enabled
1.DDRA = 0x0F; // higher pins> input, lower pins > output
2.PORTA = 0xF0; //lower pins > set output pins to 0
• To make port B as tri stated input
1.DDRB = 0x00; //use port B as input
2.PORTB = 0x00; //Disable pull-ups register and make it tri state
• To make port C as input with pull-ups enabled and read data from port a
1.DDRC = 0x00; //make port C as input
2.PORTC = 0xFF; //enable all pull-ups
3.y = PINC; //read data from port C pins
Input/ output ports
• In this example,
We are using a switch (push bottom) as an input. The switch is connected
to PD2 of the controller.
We are using LED as an indicator of the status of pin PD2. LED is connected
to PD3.
• Note :
1. ATmega32 has internal pull-ups. We can activate it to make pin status
HIGH.
2. In this code, we will be showing the status of the pin on the LED. We will
not be showing the status of the switch on the LED.
3. When the switch is open, the pin is HIGH, LED will be ON.
4. When the switch is pressed, the pin is LOW, LED will be OFF.
Example 1
Pin States
•A port pin can have four different states depending upon the state of the bits
corresponding to this pin in the data direction register (DDRx) and a data
register (PORTx) of that specific port

DDxn PORTxn I/O Pull-Up Comment

0 0 I No Tri-State (High Z)

Pxn will source


0 1 I Yes current if pulled low
externally

1 0 O No Output Low (Sink)

Output High
1 1 O No
(Source)

x is Port and can be A, B, C or D


n is Pin number of the Port, and can be 0, 1, 2, 3, 4, 5, 6 or 7
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRD = DDRD | (1<<3); /* Make PD3 as output pin */
DDRD = DDRD & (~(1<<2)); /* Make PD2 as input pin */
PORTD = PORTD | (1<<2); /* Enable pull-up on PD2 by writing 1 to it */
int pin_status;
while(1)
{
pin_status = PIND & (1<<2); /*Read status of pin PD2 */
if(pin_status) /* Transmit status of pin PD2 on to pin PD3 to drive LED. */
{
PORTD = PORTD | (1<<3); /* Switch is open, pin_status = 1, LED is ON */
}
else
{
PORTD = PORTD & (~(1<<3)); /* Switch is closed, pin_status = 0, LED is OFF */
}
}
return 0;
}
Interfacing seven segment with Atmega32
• A seven-segment display is a set of seven bar-shaped LED (light-emitting
diode) elements, arranged to form a squared-off figure 8. It’s also the
most common, simple-to-use and cheap display.

• There are two types of Seven Segment display available in the


market:
1.Common Cathode In common cathode, the negative terminal of all
LED’s inside are connected which must be grounded using a COM port.
To light up a particular LED, one need to apply positive input of +5v with
current limiting resistors;

2.Common Anode—in common anode, the positive terminal of all the


LED’s are connected that must be applied to a positive +5V supply. To
light up a particular LED, connect to ground the specific pin.
Interfacing seven segment with Atmega32
1. 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.
2. The 7 line-shaped LED segments are used for displaying numbers
0 to 9 and a few letters like A, c, d, e, F, H, L, O, P, U, etc. The
circular segment is used for displaying a decimal point.
3. Each of the 8 elements has a pin associated with it which can be
driven HIGH or LOW according to the type of display and the
number or alphabet to be displayed.
4. The common anode and common cathode types are available in a
7-segment display. Depending on which type is used, the control
signal required to light up a segment in the display changes.
Common anode requires a LOW signal whereas common cathode
requires a HIGH signal to light up a segment
Interfacing 7-Segment LED Display With AVR ATmega32
Program to Display 0 to 9 on Seven Segment
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
char array[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};

DDRA=0xff;
/* define LED port direction is output */

/* write hex value for CA display from 0 to 9 */


while(1)
{

for(int i=0;i<10;i++)
{
PORTA= array[i]; /* write data on to the LED port */
_delay_ms(1000); /* wait for 1 second */
}
}
}
Program to Display 0 to 99 on Seven Segment
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
/* Replace with your application code */
int arr [10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

while (1)
{
int i;
int j;
DDRA=0xFF;
DDRB=0xFF;

for(i=0;i<=9;i++)
{
PORTA=arr[i];
{
for(j=0;j<=9;j++)
{
PORTB=arr[j];
_delay_ms(300);
}
}
}
}
}
7-segment with BCD 7-segment decoder
• Why to use driver (BCD decoder)
• Display driver requires only 4 pins of microcontroller to drive the 7-segment
display.
• We do not need to convert decimal numbers into 7 segment display format
(from 0-9).We just need to provide BCD (Binary Coded Decimal) numbers and
it will take care rest of seven segment conversion.
• No external Resistor is required.
• It makes easy to interface with microcontroller.
• 7-Segment Display Driver IC 7447
• 7447 is a 16 pin IC, used to drive common anode type 7-segment LED display.
• This driver IC accepts 4 line of BCD (Binary Coded Decimal) input data, and
drives 7-segments display directly.
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRA=0xff; /* define LED port direction is output */
PORTA= 0xff;
char array[]={0,1,2,3,4,5,6,7,8,9};
/* write BCD value for CA display from 0 to 9 */
while(1)
{
for(int i=0;i<10;i++)
{
PORTA = array[i];/* write data on to the LED port */
_delay_ms(1000); /* wait for 1 second */
}
}
}
#define F_CPU 16000000UL /* with input port push botton
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRA=0xFF; /* DDRAis output */
PORTA= 0x00;
DDRB=0x00; /* DDRB input */
PORTB= 0xFF;

switch(PINB)
{
case 1: PORTA=1; break;
case 2: PORTA=2; break;
case 3: PORTA=3; break;
case 4: PORTA=4; break;
case 5: PORTA=5; break;
case 6: PORTA=6; break;
case 7: PORTA=7; break;
case 8: PORTA=8; break;
case 9: PORTA=9; break;
defualt: PORTA=0;
_delay_ms(1000); }
}

You might also like