0% found this document useful (0 votes)
24 views

Lab Task Codes

The document contains four lab tasks related to microcontroller programming. Lab Task 1 configures ports and reads/writes values. Lab Task 2 performs bitwise operations and shifts. Lab Task 3 configures an ADC and displays analog readings on different ports based on voltage thresholds.

Uploaded by

Haisham Ali
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lab Task Codes

The document contains four lab tasks related to microcontroller programming. Lab Task 1 configures ports and reads/writes values. Lab Task 2 performs bitwise operations and shifts. Lab Task 3 configures an ADC and displays analog readings on different ports based on voltage thresholds.

Uploaded by

Haisham Ali
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab Task 1.

#include <avr/io.h>

int main(void)
{
while(1)
{
DDRA=0b00000000; //configures Port A as input
DDRB=0b00000000; //configures Port B as input
DDRD=0b11111111; //configures Port D as output
unsigned int x; //declare variable x
unsigned int y; //declare variable y
x=PINA; //reads and stores input at Port A in variable x
y=PINB; //reads and stores input at Port B in variable y
PORTD=(x*55)+y; //Multiply x with 5 and outputs it on Port D
}
}
-----------------------------------------------------------------------------------
-------------------------------------
Lab Task 1.2

DDRA = 0b00000000;
DDRB = 0b11111111;
unsigned int a;
unsigned int b;
unsigned int c;
unsigned int d;
a = PINA & 0b11110000;
b = PINA & 0b00001111;
c = a >> 4;
d = a + c;
PORTB = d;
-----------------------------------------------------------------------------------
-------------------------------------
Lab Task 1.3

DDRD = 0b00000000;
unsigned int win1;
unsigned int win2;
unsigned int door;
win1 = PIND & 0b00000001;
win2 = PIND & 0b00000010;
door = PIND & 0b00000100;
if (win1 || win2 || door)
{
PORTD = PIND | 0b00001000;
}
else
{
PORTD = PIND & 0b11110111;
}
-----------------------------------------------------------------------------------
------------------------------------
Lab Task 3.1

int main(void)
{
// Enabling Outputs
DDRB= 0xFF;
DDRC= 0xFF;
DDRD= 0xFF;
ADCSRA= 0b10000110; // Enabling ADC
ADMUX= 0b01100000; // For Reference Voltage
// loop for continuous running
while(1)
{
adc_func();
}
}
// Functions Body
void adc_func()
{
ADCSRA |= 0x40;
ADMUX |= 0x60;
// loop to wait up to condition
while(!(ADCSRA&0x10)){}
COND_OUTPUT();
}
// Sub Function Body
void COND_OUTPUT()
{
// setting conditions to display on PORT B
if(ADCH<=99)
{
PORTB = ADCH;
PORTC = 0X00;
PORTD = 0X00;
}
// setting conditions to display on PORT C
else if(ADCH>=100 && ADCH<=199)
{
PORTB = 0X00;
PORTC = ADCH;
PORTD = 0X00;
}
// setting conditions to display on PORT D
else
{
PORTB = 0x00;
PORTC = 0X00;
PORTD = ADCH;
}
}

You might also like