EMBEDDED SYSTEMS
I/O port Programming
Dr. Muhammad Awais
Department of Electrical & Computer Engineering, COMSATS University Islamabad Wah Campus
Preamble
Include files
Macros
Global variables
Function definitions
Main method
{
//Chip configurations
//Infinite event loop
while(1)
{
}
return (0);
}
DDRx (Data Direction Register for Port x={B, C, D})
Used to define data direction of port(s).
Readable/writable
Setting a bit to 1 corresponding pin of the given port as is set as OUTPUT pin
Setting a bit to 0 corresponding pin of the given port as is set as INPUT pin
Default configuration: Input
Header file io.h need to be included to access the registers by their name
Individual bits are given names of DDxn
DDRx (Data Direction Register for Port x={B, C, D})
DDRB: DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0
Example: Write a C-statement to configure PortD as Output Port.
DDRD = 0xFF; or
DDRD = 0b11111111; or
DDRD = 255;
PORTx (Data Register for Port x={B, C, D})
Used to set logic values (i.e. 0 or 1) on a port that has already been configured as output
port.
Read/writable
Setting a bit to 1 Logic 1 (i.e. Vcc) on corresponding output pin of the given port.
Setting a bit to 0 Logic 0 (i.e. ground) on corresponding output pin of the given port.
Default configuration: Logic 0
Header file io.h need to be included to access the registers by their name
Individual bits are given names of Pxn
PORTx (Data Register for Port x={B, C, D})
Example: Write C-statements to configure PORTC as ouput port and drive logic 1
on PC3 and PC6.
DDRC = 0xFF;
PORTC=0b01001000;
PINx (Port x ={B, C, D} input pins)
If a port is configured as input port, the input data is made available in PINx
register.
Only readable.
More on this later lectures
1. Configure the direction of the port by writing its DDR register.
2. Write the value in the PORT register to make it available on pins of the port.
Build an AVR-microcntroller based circuit that
blinks 8 LEDs from right to left direction by
taking «two steps forward, one step backward».
Connect 8 LEDS with PORTD.
NOTE: Each LED need to be grounded using a
current limiting resistor (1K).
while(1) {
//Include files blinkLEDs(0b00000001);
#include <avr/io.h>
#include <util/delay.h> blinkLEDs(0b00000010);
blinkLEDs(0b00000001);
blinkLEDs(0b00000010);
//macros blinkLEDs(0b00000100);
#define DELAY 400 blinkLEDs(0b00000010);
blinkLEDs(0b00000100);
blinkLEDs(0b00001000);
//method definations blinkLEDs(0b00000100);
void blinkLEDs(uint8_t byte) blinkLEDs(0b00001000);
blinkLEDs(0b00010000);
{ blinkLEDs(0b00001000);
PORTD=byte; blinkLEDs(0b00010000);
blinkLEDs(0b00100000);
_delay_ms(DELAY); blinkLEDs(0b00010000);
} blinkLEDs(0b00100000);
blinkLEDs(0b01000000);
//main method blinkLEDs(0b00100000);
blinkLEDs(0b01000000);
int main (void) blinkLEDs(0b10000000);
{ blinkLEDs(0b01000000);
blinkLEDs(0b10000000);
//Initialization
}
DDRD = 0xFF;
//Event loop return 0;
}