/* * Task3-LF.c * * Created: 29.01.2017 23:43:39 * Author : Petter */ #define F_CPU 3333333UL //3.33... MHz #include #include #include //LED #define LED0 5 //on port D //Button #define SW0 6 //on port C int main(void) { /* * PORTx.DIR: set direction - 1 is output, 0 is input * PORTx.OUT: set value of pins * PORTx.IN: read value of pins * LED: 1 LED is off, 0 LED is on * SW: 1 Button is open, 0 button is pressed * Set to 1: REG |= ( 1 << BIT_POS ) * Set to 0: REG &= ~( 1 << BIT_POS ) * This is called "read-modify-write". * * The ATmega4809 also has special port registers that can do some of this for you. * Read what PORTx.DIRSET, .DIRCLR and .DIRTGL do to the PORTx.DIR register, * And similarly what PORTx.OUTSET, .OUTCLR and .OUTTGL do to PORTx.OUT * (HINT: see chapter 15.5 of the datasheet) LF: They set clear and toggle bit in the register. eks: PORTB.DIRCLR = (1 << 5); to clear bit 5 */ PORTF.DIRSET = (1 << LED0); // Set LED0 as output //Alt: PORTF.DIR |= (1 << LED0); PORTF.OUTSET = (1 << LED0); // Set LED0 output high (turns off the led, since the led is active low) //Alt: PORTF.OUT |= (1 << LED0); PORTF.DIRCLR = (1 << SW0); // Set SW0 as input (default) //Alt: PORTF.DIR &= ~(1 << SW0); PORTF.PIN2CTRL |= (1 << 3); // Enable pull-up on button SW //eller bruk PORT_PULLUPEN_bp som er lik 3 (_bp = Bit Position) int buttonState = 0; // To hold the button pressed state while (1) { if(!(PORTF.IN & (1 << SW0))){ if(buttonState == 0){ PORTF.OUTTGL = ( 1 << LED0); //Alt: PORTF.OUT ^= (1 << LED0); buttonState = 1; } } else{ buttonState = 0; } _delay_ms(1); // For deboucing } }