Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Lab 2

In our Arduino Uno board Rev 3 we use ATMEGA328P

ATMEGA328 Pinout and the corresponding Arduino header pins

We have 3 I/O Ports PORT B , C and D


The switch will be connected to PORTD bit 2 (PD2)

// Port D initialization

// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In

DDRD=(0<<DDD7) | (0<<DDD6) | (0<<DDD5) | (0<<DDD4) | (0<<DDD3) | (0<<DDD2) | (0<<DDD1) |


(0<<DDD0);
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=P Bit1=T Bit0=T

PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (1<<PORTD2) |


(0<<PORTD1) | (0<<PORTD0);

To setup bit 5 from PORTB as output connected to the LED (PB5)

// Port B initialization

// Function: Bit7=In Bit6=In Bit5=Out Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In

DDRB=(0<<DDB7) | (0<<DDB6) | (1<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (0<<DDB1) |


(0<<DDB0);

// State: Bit7=T Bit6=T Bit5=0 Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T

PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) |


(0<<PORTB1) | (0<<PORTB0);

SW is connected to
PD2 with internal
configure PB5 as pull up resistor
output to connect activated on that
the LED pin
and put 0 to turn it
off
void setup() {

// put your setup code here, to run once:

// To setup bit 5 from PORTB as output connected to the LED (PB5)

//DDRB=(0<<DDB7) | (0<<DDB6) | (1<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (0<<DDB1) |


(0<<DDB0);

DDRB = 0b00100000;

// to turn off the LED ( 1 turns is ON and 0 turn it OFF) ===> PORTB5 = 0

//PORTB &= !(1 << PORTB5);


PORTB = PORTB & !(1 << PORTB5)
PORTB &= 0b1101111;

PORTB = PORTB & 0b1101111

//The switch will be connected to PORTD bit 2 (PD2), we will use the internal pull up resistor

//DDRB=(0<<DDB7) | (0<<DDB6) | (1<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (0<<DDB1) |


(0<<DDB0);

DDRD = 0b00000000;

//PORTD =(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (1<<PORTD2)


| (0<<PORTD1) | (0<<PORTD0);

PORTD = 0b00000100; // to activate the internal pull up resistor on bit 2 of PORTD

void loop() {

// put your main code here, to run repeatedly:

// PORTB = PORTB ^ ( 1 << PORTB5 );


while( (PIND & ( 1 << PIND2)) == 0)

PORTB ^= (1<<PORTB5);

delay(1000);

PORTB &= !(1 << PORTB5);

You might also like