Referring to the PCICR and PCMSK registers of the ATMega328P microcontroller, write a fragment of code in C to write to the PCICR and the correct PCIMSK register of the ATMega328P microcontroller to set it up for the following operation:
Pin Changes on Port D Bit 3 or on Port B bit 4 cause a Pin Change Interrupt request.
You may also assume the existence of the macro sei(); to enable global interrupts. Include this in your program fragment in the correct place. There should be three lines in your code fragment.
You can find details of the PCICR, PCMSK0, PCMSK1 and PCMSK2 registers in the ATMega328P datasheet.
#include <conio.h>
#include <stdio.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
void sei(){
}
int main()
{
int
DDRB,DDB0,DDB1,DDB2,PORTB,PORTB0,PORTB1,PORTB2,PCICR,PCIE0,PCMSK0,PCINT0;
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 */
}
if(changedbits & (1 << PINB1))
{
/* PCINT1 changed */
}
if(changedbits & (1 << PINB2))
{
/* PCINT2 changed */
}
}
--Bold lines are the comments in the program.
Referring to the PCICR and PCMSK registers of the ATMega328P microcontroller, write a fragment of code...
Question 3: In class we showed how to simply light up an LED on our Teensy microcontroller. In fact, the procedure is similar for all processors including a simpler 8-bit ATmega 328P (a.k.a Arduino Uno) For purposes of this exercise, let's say that the ATmega328 has 4 regular GPIO ports; A, B, C, and D, each with 8 pins. Every GPIO port has three registers (outlined in table below) PORTX: used to write output on a pin on port X...
1a) We want you to feel comfortable reading documentation to understand the operation of a device. To that end, open the ATmega328/P datasheet linked on our website from the Tools and Links Page. Go to section 18.2 and read over pages 98-99. Note that "toggle" means flip to its opposite value, regardless of current value. What effect does writing a 1 to a PIN register bit have? Consider, how this might be used to generate the E signal pulse in...
1.) a.) Using the simplified instruction set shown for part b, write code for the following. Suppose memory locations 1400 to 1449 contain 16-bit words. Each word represents 2 ASCII characters. Write code to read in and write out these 100 characters. Left-side character from location 1400 should be first, right-side character from location 1400 should be second, and remaining characters follow in numeric order. Assume you have access to 4 registers: R1, R2, R3, R4. Each register holds one...