A bank of 8 LEDS is mapped to memory address $1100. Each LED will be identified
ID: 3537469 • Letter: A
Question
A bank of 8 LEDS is mapped to memory address $1100. Each LED will be identified by the corresponding bit positions at address $1100. A logic %u20181%u2019 in a bit position will turn a particular LED on while a logic %u20180%u2019 will turn that LED off.
Write C code to perform the following without affecting any of the other bits. The delay() function should use inline assembly and create a 5 machine cycle delay.
- Turn LED 0 on
- Call delay()
- Turn LED 1 off
- Call delay()
- Toggle LED 2
- Call delay()
- Turn LEDs 3 and 4 on
- Call delay()
- Turn LEDs 5 off
- Call delay()
- Toggle LEDs 6 and 7
- Call delay()
Explanation / Answer
#include<stdio.h>
#define SETBIT(port,bit) (port |= (1 << bit))
#define CLEARBIT(port,bit) (port &= ~(1 << bit))
#define CHKBIT(port,bit) ((port&(1<<bit))>>bit)
void delay()
{
for(i =1; i<6; i++)
{
}
}
void main()
{
SETBIT(port,0); //Turn LED 0 on
delay();//Call delay()
CLEARBIT(port,0);//Turn LED 1 off
delay();//Call delay()
port ^= (1 << 2); //Toggle LED2
delay();//Call delay()
SETBIT(port,3);//Turn LEDs 3 on
SETBIT(port,4);//Turn LEDs 4 on
delay();//Call delay()
CLEARBIT(port,5);//Turn LEDs 5 off
delay();//Call delay()
port ^= (1 << 6); //Toggle LED6
port ^= (1 << 7); //Toggle LED7
delay();//Call delay()
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.