Added first implementation of hardware interrupt, and example for GPIO usage.
This commit is contained in:
		
							parent
							
								
									cf4cd2813b
								
							
						
					
					
						commit
						1f3e31c295
					
				| 
						 | 
				
			
			@ -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;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								src/gympal.c
								
								
								
								
							
							
						
						
									
										24
									
								
								src/gympal.c
								
								
								
								
							| 
						 | 
				
			
			@ -1,19 +1,33 @@
 | 
			
		|||
#include <avr/io.h>
 | 
			
		||||
#include <avr/interrupt.h>
 | 
			
		||||
#include <util/delay.h>
 | 
			
		||||
 | 
			
		||||
volatile uint8_t a = 0;
 | 
			
		||||
 | 
			
		||||
void updatePin12(uint8_t status) {
 | 
			
		||||
    PORTB = status << PORTB4;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ISR(PCINT2_vect) {
 | 
			
		||||
    uint8_t value = (PIND & (1 << PIND5)) >> PIND5;
 | 
			
		||||
    PORTB = value << PORTB4;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    PCICR |= 1 << PCIE2;
 | 
			
		||||
    PCMSK2 |= 1 << PCINT21;
 | 
			
		||||
    sei();
 | 
			
		||||
    // Pin 12 == Port B4, aka 4th bit on the B register.
 | 
			
		||||
    // Set pin 12 as output.
 | 
			
		||||
    DDRB = 1 << PORTB4;
 | 
			
		||||
    uint8_t ledOn = 1;
 | 
			
		||||
    DDRB |= 1 << PORTB4;
 | 
			
		||||
    // Set pin 5 as input.
 | 
			
		||||
    DDRD &= ~(1 << PORTD5);
 | 
			
		||||
    PORTB &= ~(1 << PORTB4);
 | 
			
		||||
    while (1) {
 | 
			
		||||
        updatePin12(ledOn);
 | 
			
		||||
        ledOn = !ledOn;
 | 
			
		||||
        _delay_ms(1000.0);
 | 
			
		||||
        // updatePin12(ledOn);
 | 
			
		||||
        // ledOn = !ledOn;
 | 
			
		||||
        // _delay_ms(1000.0);
 | 
			
		||||
        // DO NOTHING! rely on interrupt.
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue