Final Exam Topic 1 Example Questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Topic 1 : GPIO Control

Problem 1 : Write the C program to scan the BT0, BT1, BT3,BT4 when button-x is pressed, toggle The aprropriate LED0,LED1, LED3 and LED4.

#ifndef F_CPU

#define F_CPU 8000000UL // 8 MHz clock speed

#endif

#define BT0 0

#define BT1 1

#define BT3 3

#define BT4 4

#define LED0 4

#define LED1 5

#define LED2 6

#define LED3 7

#include <avr/io.h>

#include <util/delay.h>

int main(void)

DDRB = 0x00; //make all Port B is input (for button)

PORTB=0xFF; //Pull up resistors

DDRD = 0xFF; //make all port C is output (for led)

PORTD = 0xFF;

while(1) //loop

{
if((PINB & 1<<BT0)==0) //If BT0 is pressed

{ _delay_ms(1000);

PORTD ^= (1<<LED0);// toggle led0

//-----------

if((PINB & 1<<BT1)==0) //If BT1 is pressed

{ _delay_ms(1000);

PORTD ^= (1<<LED1);// toggle led1

//--------------

if((PINB & 1<<BT3)==0) //If BT3 is pressed

{ _delay_ms(1000);

PORTD ^= (1<<LED2);// toggle led2

if((PINB & 1<<BT4)==0) //If BT4 is pressed

{ _delay_ms(1000);

PORTD ^= (1<<LED3);// toggle led3

Problem 2 : Convert the C Code in Problem 1 into Assembly Code

.nolist

.include "m32def.inc" ; Include definitions for ATmega32

.list

.org 0x0000 ; Start program at address 0


main:

ldi R16,0x00 //DDRB=0x00

OUT DDRB,R16

ldi R16,0xFF // PORTB = 0xFF

OUT PORTB,R16

ldi R16,0xF0 // DDRD=0xF0

OUT DDRD,R16

ldi R16,0xFF // PORTD = 0x0F

OUT PORTD,R16

while_1:

IN R16, PINB

ldi R17,(1<<0)

AND R16,R17

BRNE check_1

ldi R17,(1<<4)

IN R16,PORTD

EOR R17,R16

OUT PORTD, R17

jmp while_1

;-----------------------------------------------

check_1:

IN R16,PINB

ldi R17,(1<<1)

AND R16,R17

BRNE check_2

ldi R17,(1<<5)

IN R16,PORTD

EOR R17,R16

OUT PORTD, R17

JMP while_1

;--------------------------------------------------
check_2:

IN R16,PINB

ldi R17,(1<<3)

AND R16,R17

BRNE check_3;

ldi R17,(1<<6)

IN R16,PORTD

EOR R17,R16

OUT PORTD, R17

JMP while_1

;------------------------------------------------------

check_3:

IN R16,PINB

ldi R17,(1<<4)

AND R16,R17

BRNE while_1

ldi R17,(1<<7)

IN R16,PORTD

EOR R17,R16

OUT PORTD, R17

JMP while_1

Advance questions : Compile Assembly Code in Problem 2 into AVR Binary Machine Code

You might also like