In: Computer Science
Q1- Write code in C language(Atmel atmega32 avr
microcontroller)
(a)-Switches are connected to Port C and Port B and LED’s with
PORTD. Write a code to read input from
PortB and C and perform following.
Addition of both inputs, Subtraction, Multiplication, Division,
And, Or, Nor,Xor.
(b)- A switch is connected to PD0 and LED’s to Port B and Port C.
Write a code to perform following:
When switch is 0 send your roll number to Port B.
When Switch is 1, toggle PortC 20 times.
Ans:
Code(a):
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
int main()
{
DDRB = 0; // make portB input
DDRC = 0; //make portC
input
DDRD = 0xff; // make
portD output
while (1)
{
PORTD = PINB + PINC ;
// Addition
_delay_ms(2000);
// 2
second delay
PORTD = PINB - PINC ;
//Subtraction
_delay_ms(2000);
// 2
second delay
PORTD = PINB * PINC ;
//Multiplication
_delay_ms(2000);
// 2
second delay
PORTD = PINB / PINC ;
//Division
_delay_ms(2000);
// 2
second delay
PORTD = PINB & PINC ;
//And
_delay_ms(2000);
// 2
second delay
PORTD = PINB | PINC ;
//Or
_delay_ms(2000);
// 2
second delay
PORTD = ~ PORTD ;
//Nor
_delay_ms(2000);
// 2
second delay
PORTD = PINB ^ PINC ;
//Xor
_delay_ms(2000);
// 2
second delay
}
return 0;
}
//diagram(a)
Code(b):
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
int main()
{ int i;
DDRD = 0xFE; // make portD0
input
DDRC = 0xFF; //make
portC input
DDRB = 0xff; // make
portB output
PORTC =0 ;
while (1)
{
if(PIND & 0x01){
for( i; i<40; i++){
PORTB =0 ;
PORTC =~PORTC ;
//toggle PortC 20 times.
_delay_ms(500); //delay 0.5 second
if((PIND &
0x01)==0){
PORTB =0x55 ;
// here id number is 55 (you
put your number in PORTB = 0x_ _ )
i= 0;
while((PIND
& 0x01)==0);
}
}
}
else{
PORTB =0x55 ;
// here id number is 55 (you put your number in
PORTB = 0x_ _ )
i= 0;
}
}
return 0;
}
//diagram(b)