Added first implementation of hardware interrupt, and example for GPIO usage.

This commit is contained in:
Andrew Lalis 2022-12-02 17:58:49 +01:00
parent cf4cd2813b
commit 1f3e31c295
2 changed files with 56 additions and 5 deletions

37
examples/digital_io.c Normal file
View File

@ -0,0 +1,37 @@
/*
This example showcases digital input and output using the AVR chip's GPIO
pins. For all pins, we first have to configure them as either input or output.
We do this by writing to the DDRx register (data direction register) either a 1
(for output) or 0 (for input).
We can set the output of a pin by writing to PORTx, using bitwise operators.
Reading can be done by reading from PINx, using bitwise operators.
*/
#include <avr/io.h>
#include <util/delay.h>
int main() {
// Pin 12 == Port B4, aka 4th bit on the B register.
// Set pin 12 as output.
DDRB |= 1 << PORTB4;
// Set pin 5 as input.
DDRD &= ~(1 << PORTD5);
// Write a 0 to pin 12 initially.
PORTB &= ~(1 << PORTB4);
uint8_t led = 0;
while (1) {
// Write current led status to pin 12.
if (led) {
PORTB |= 1 << PORTB4;
} else {
PORTB &= ~(1 << PORTB4);
}
led = !led;
_delay_ms(1000.0);
// Read input from pin 5. If it's HIGH, keep the LED on.
uint8_t pin5Value = (PIND & (1 << PIND5)) >> PIND5;
if (pin5Value) led = 1;
}
return 0;
}

View File

@ -1,19 +1,33 @@
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h> #include <util/delay.h>
volatile uint8_t a = 0;
void updatePin12(uint8_t status) { void updatePin12(uint8_t status) {
PORTB = status << PORTB4; PORTB = status << PORTB4;
} }
ISR(PCINT2_vect) {
uint8_t value = (PIND & (1 << PIND5)) >> PIND5;
PORTB = value << PORTB4;
}
int main() { int main() {
PCICR |= 1 << PCIE2;
PCMSK2 |= 1 << PCINT21;
sei();
// Pin 12 == Port B4, aka 4th bit on the B register. // Pin 12 == Port B4, aka 4th bit on the B register.
// Set pin 12 as output. // Set pin 12 as output.
DDRB = 1 << PORTB4; DDRB |= 1 << PORTB4;
uint8_t ledOn = 1; // Set pin 5 as input.
DDRD &= ~(1 << PORTD5);
PORTB &= ~(1 << PORTB4);
while (1) { while (1) {
updatePin12(ledOn); // updatePin12(ledOn);
ledOn = !ledOn; // ledOn = !ledOn;
_delay_ms(1000.0); // _delay_ms(1000.0);
// DO NOTHING! rely on interrupt.
} }
return 0; return 0;
} }