Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume four LEDs are connected to Pins A4..7. Implement the following functions

ID: 3602138 • Letter: A

Question

Assume four LEDs are connected to Pins A4..7. Implement the following functions with bit operators only (|, &, ^, ~, <<, |=, &=, ^=).

(1) Initialize the LEDs.

(2) Turn one selected LED on.

(3) Turn one selected LED off.

(4) Toggle one selected LED.

(5) Set a value to the LEDs.

a) Show the code for Atmega128.

b) Show the code for Pic24ep512gu810.

There is example code below for both a and b

//Atmega128 Example//

#include <avr/io.h>

#include <stdint.h>

// leds are connected to port A0..2

void initLeds() {

DDRA |= 0x7;

}

void ledOn(uint8_t sel) {

//if (sel > 7) return;

PORTA |= 1<<sel;

}

void ledOff(uint8_t sel) {

//if (sel > 7) return;

PORTA &= ~(1<<sel);

}

void ledToggle(uint8_t sel) {

//if (sel > 7) return;

PORTA ^= (1<<sel);

}

void ledSet(uint8_t val) {

PORTA = (PORTA & 0xF8) | (val & 0x7);

}

int main() {

initLeds();

ledOn(1);

ledOff(1);

ledToggle(1);

ledSet(5);

return 0;

}

//Pic24ep512gu810 Example//

#include <stdint.h>
#include "Compiler.h"
#include "ConfigurationBits.h"

// leds are connected to port D1..3

void initLeds() {
TRISD &= 0xFFF1;
}

void ledOn(uint8_t sel) {
LATD |= 1<<(sel+1);
}

void ledOff(uint8_t sel) {
LATD &= ~(1<<(sel+1));
}

void ledToggle(uint8_t sel) {
LATD ^= (1<<(sel+1));
}

void ledSet(uint8_t val) {
LATD = (LATD & 0xFFF1) | ((val & 0x7) <<1);
}

int main() {
initLeds();
ledOn(1);
ledOff(1);
ledToggle(1);
ledSet(5);
return 0;
}

Explanation / Answer

/Atmega128 Example//

#include <avr/io.h>

#include <stdint.h>

// leds are connected to port A0..2

void initLeds() {

DDRA |= 0x7;

}

void ledOn(uint8_t sel) {

//if (sel > 7) return;

PORTA |= 1<<sel;

}

void ledOff(uint8_t sel) {

//if (sel > 7) return;

PORTA &= ~(1<<sel);

}

void ledToggle(uint8_t sel) {

//if (sel > 7) return;

PORTA ^= (1<<sel);

}

void ledSet(uint8_t val) {

PORTA = (PORTA & 0xF8) | (val & 0x7);

}

int main() {

initLeds();

ledOn(1);

ledOff(1);

ledToggle(1);

ledSet(5);

return 0;

}

//Pic24ep512gu810 Example//

#include <stdint.h>
#include "Compiler.h"
#include "ConfigurationBits.h"

// leds are connected to port D1..3

void initLeds() {
TRISD &= 0xFFF1;
}

void ledOn(uint8_t sel) {
LATD |= 1<<(sel+1);
}

void ledOff(uint8_t sel) {
LATD &= ~(1<<(sel+1));
}

void ledToggle(uint8_t sel) {
LATD ^= (1<<(sel+1));
}

void ledSet(uint8_t val) {
LATD = (LATD & 0xFFF1) | ((val & 0x7) <<1);
}

int main() {
initLeds();
ledOn(1);
ledOff(1);
ledToggle(1);
ledSet(5);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote