Wire up a circuit with three pushbuttons (one for red, one for green, and one fo
ID: 1766227 • Letter: W
Question
Wire up a circuit with three pushbuttons (one for red, one for green, and one for blue) connected to pins D8, D9, and D10 and an RGB LED connected to three other pins of your choice. Pressing a pushbutton will turn on one color LED. When the pushbutton is not pressed, that color LED will turn off. Pressing multiple buttons generates secondary colors and the color white. When this has been successfully completed, demonstrate it to your instructor to receive a stamp. Include your software code with this activityExplanation / Answer
ANS 1)
// INTURREPT PIN PB0,PB1,PB2
#include <avr/io.h>
#include <stdint.h> // has to be added to use uint8_t
#include <avr/interrupt.h> // Needed to use interrupts
volatile uint8_t portbhistory = 0xFF; // default is high because the pull-up
int main(void)
{
DDRD = DDRD | ( 1<<4) ; //Make pin 4 of port D as a output
DDRD = DDRD | ( 1<<5) ; //Make pin 5of port D as a output
DDRD = DDRD | ( 1<<6) ; //Make pin 6 of port D as a output
DDRB &= ~((1 << DDB0) | (1 << DDB1) | (1 << DDB2)); // Clear the PB0, PB1, PB2 pin
// PB0,PB1,PB2 (PCINT0, PCINT1, PCINT2 pin) are now inputs
PORTB |= ((1 << PORTB0) | (1 << PORTB1) | (1 << PORTB2)); // turn On the Pull-up
// PB0, PB1 and PB2 are now inputs with pull-up enabled
PCICR |= (1 << PCIE0); // set PCIE0 to enable PCMSK0 scan
PCMSK0 |= (1 << PCINT0); // set PCINT0 to trigger an interrupt on state change
sei(); // turn on interrupts
while(1)
{
/*main program loop here */
}
}
ISR (PCINT0_vect)
{
uint8_t changedbits;
changedbits = PINB ^ portbhistory;
portbhistory = PINB;
if(changedbits & (1 << PINB0))
{
/* PCINT0 changed */
PORTD = PORTD | ( 1<<4) ; //PIN4 of port D is high red
}
if(changedbits & (1 << PINB1))
{
/* PCINT1 changed */
PORTD = PORTD | ( 1<<5) ; //PIN5 of port D is high green led
}
if(changedbits & (1 << PINB2))
{
/* PCINT2 changed */
PORTD = PORTD | ( 1<<6) ; //PIN6 of port D is high blue led
}
}
ANS 2)
PCICR=0x07;
PCMSK0=0x07;
The PCIEx bits in the PCICR registers enable External Interrupts and tells the MCU to check PCMSKx on a pin change state. When a pin changes states (HIGH to LOW, or LOW to HIGH) and the corresponding PCINTx bit in the PCMSKx register is HIGH the corresponding PCIFx bit in the PCIFR register is set to HIGH and the MCU jumps to the corresponding Interrupt vector.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.