Commit b75291c7 authored by BuildTools's avatar BuildTools
Browse files

Session 1 ported to ATmega4809

parent ac61bd30
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* F_CPU must be defined before including headers. * F_CPU must be defined before including headers.
*/ */
#define F_CPU 3333333UL //The ATtiny4809 operates at 20MHz with a default scaling factor of 6: 20/6 = 3.333333MHz #define F_CPU 3333333UL //The ATmega4809 operates at 20MHz with a default scaling factor of 6: 20/6 = 3.333333MHz
/** /**
* System headers bellow * System headers bellow
...@@ -26,13 +26,13 @@ ...@@ -26,13 +26,13 @@
/** /**
* Define helpers for pin bit positions. * Define helpers for pin bit positions.
* The LED0 on the ATtiny4809 main board is connected to pin 4 on port D. * The LED0 on the ATmega4809 main board is connected to pin 5 on port F.
* Check the datasheet to find out which pins on which ports the different LEDs and buttons are connected to. * Check the datasheet to find out which pins on which ports the different LEDs and buttons are connected to.
*/ */
#define LED0 4 // LED 1 is connected to pin 4 on PORTD #define LED0 5 // LED 1 is connected to pin 5 on PORTF
...@@ -50,7 +50,7 @@ int main(void){ ...@@ -50,7 +50,7 @@ int main(void){
* Toogle bit: FLAG ^= ( 1 << BIT_POS ) - using bitwise xor, toogles the bit at BIT_POS * Toogle bit: FLAG ^= ( 1 << BIT_POS ) - using bitwise xor, toogles the bit at BIT_POS
*/ */
PORTD.DIR |= (1 << LED0); // Set LED0 as output - Using "D" in PORTx.DIR since the LED is connected to port D on the microcontroller PORTF.DIR |= (1 << LED0); // Set LED0 as output - Using "D" in PORTx.DIR since the LED is connected to port D on the microcontroller
/* /*
* The usual way to run microcontrollers is using a simple infinite loop * The usual way to run microcontrollers is using a simple infinite loop
...@@ -58,9 +58,9 @@ int main(void){ ...@@ -58,9 +58,9 @@ int main(void){
while (1) while (1)
{ {
PORTD.OUT ^= (1 << LED0); // Changes the state of LED0 by XOR-ing the last state. Check the XOR-table to find out how this works. PORTF.OUT ^= (1 << LED0); // Changes the state of LED0 by XOR-ing the last state. Check the XOR-table to find out how this works.
// Non-compressed: PORTD.OUT = PORTD.OUT ^ (1 << LED0); // Non-compressed: PORTF.OUT = PORTF.OUT ^ (1 << LED0);
//Can also refer to toogle register: PORTD.OUTTGL = (1 << LED0). IMPORTANT: only use equal sign here (=) //Can also refer to toogle register: PORTF.OUTTGL = (1 << LED0). IMPORTANT: only use equal sign here (=)
_delay_ms(500); _delay_ms(500);
} }
} }
...@@ -15,21 +15,21 @@ ...@@ -15,21 +15,21 @@
#include <util/delay.h> #include <util/delay.h>
/* /*
* Check out the ATtiny4809 datasheet to find the correct ports and pins * Check out the ATmega4809 datasheet to find the correct ports and pins
*/ */
// LED // LED
#define LED0 4 //On port D #define LED0 5 //On port F
// Button // Button
#define SW0 2 //On port C #define SW0 6 //On port C
int main(void) int main(void)
{ {
/* /*
* We want to send signals to the LEDs, in order to turn it off and on. * We want to send signals to the LEDs, in order to turn it off and on.
* We also want to be able to read the switches. * We also want to be able to read the switches.
* This is done by setting bits in the PORTx.DIR register (in this case PORTD.DIR and PORTC.DIR) * This is done by setting bits in the PORTx.DIR register (in this case PORTF.DIR and PORTF.DIR)
* PORTx.DIR: 1 is output, 0 is input * PORTx.DIR: 1 is output, 0 is input
* LED: 1 LED is off, 0 LED is on * LED: 1 LED is off, 0 LED is on
* Button: 1 Button is open, 0 button is pressed * Button: 1 Button is open, 0 button is pressed
...@@ -41,7 +41,7 @@ int main(void) ...@@ -41,7 +41,7 @@ int main(void)
/** /**
* In order to read from the switches, we need to give it a ground reference, via a pull-up resistor. * In order to read from the switches, we need to give it a ground reference, via a pull-up resistor.
* If we don't, the switch will have a floating ground, and hence its value will be undefined. * If we don't, the switch will have a floating ground, and hence its value will be undefined.
* On the ATtiny4809, we enable pull-up by setting the "PORT_PULLUPEN" flag in "PORTx.PINnCTRL" high. * On the ATmega4809, we enable pull-up by setting the "PORT_PULLUPEN" flag in "PORTx.PINnCTRL" high.
* See datasheet section 15 (I/O-ports). * See datasheet section 15 (I/O-ports).
*/ */
...@@ -52,10 +52,10 @@ int main(void) ...@@ -52,10 +52,10 @@ int main(void)
* 3 - Enable pull-up on button SW0 * 3 - Enable pull-up on button SW0
*/ */
PORTD.DIR |= (1 << LED0); // Set LED0 as output PORTF.DIR |= (1 << LED0); // Set LED0 as output
PORTC.DIR &= ~(1 << SW0); //Set SW0 as input PORTF.DIR &= ~(1 << SW0); //Set SW0 as input
PORTB.PIN2CTRL |= (1 << PORT_PULLUPEN_bp); //Enable pull-up on button SW0 (pin5) PORTF.PIN5CTRL |= (1 << PORT_PULLUPEN_bp); //Enable pull-up on button SW0 (pin5)
while (1) while (1)
{ {
...@@ -77,12 +77,12 @@ int main(void) ...@@ -77,12 +77,12 @@ int main(void)
* 3 - if not, turn the LED off * 3 - if not, turn the LED off
*/ */
if(!(PORTC.IN & (1 << SW0))){ // If button is pressed (0 - LOW if(!(PORTF.IN & (1 << SW0))){ // If button is pressed (0 - LOW
PORTD.OUT &= ~(1 << LED0); // Sets output to 0, turns LED0 on PORTF.OUT &= ~(1 << LED0); // Sets output to 0, turns LED0 on
} }
else{ else{
PORTD.OUT |= (1 << LED0); // Sets output to 1, turns LED off PORTF.OUT |= (1 << LED0); // Sets output to 1, turns LED off
} }
} }
......
...@@ -12,10 +12,10 @@ ...@@ -12,10 +12,10 @@
#include <stdbool.h> #include <stdbool.h>
//LED //LED
#define LED0 4 //on port D #define LED0 5 //on port D
//Button //Button
#define SW0 2 //on port C #define SW0 6 //on port C
int main(void) int main(void)
{ {
...@@ -29,7 +29,7 @@ int main(void) ...@@ -29,7 +29,7 @@ int main(void)
* Set to 0: REG &= ~( 1 << BIT_POS ) * Set to 0: REG &= ~( 1 << BIT_POS )
* This is called "read-modify-write". * This is called "read-modify-write".
* *
* The ATtiny4809 also has special port registers that can do some of this for you. * 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, * 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 * And similarly what PORTx.OUTSET, .OUTCLR and .OUTTGL do to PORTx.OUT
* (HINT: see chapter 15.5 of the datasheet) * (HINT: see chapter 15.5 of the datasheet)
...@@ -40,16 +40,16 @@ int main(void) ...@@ -40,16 +40,16 @@ int main(void)
*/ */
PORTD.DIRSET = (1 << LED0); // Set LED0 as output PORTF.DIRSET = (1 << LED0); // Set LED0 as output
//Alt: PORTD.DIR |= (1 << LED0); //Alt: PORTF.DIR |= (1 << LED0);
PORTD.OUTSET = (1 << LED0); // Set LED0 output high (turns off the led, since the led is active low) PORTF.OUTSET = (1 << LED0); // Set LED0 output high (turns off the led, since the led is active low)
//Alt: PORTD.OUT |= (1 << LED0); //Alt: PORTF.OUT |= (1 << LED0);
PORTC.DIRCLR = (1 << SW0); // Set SW0 as input (default) PORTF.DIRCLR = (1 << SW0); // Set SW0 as input (default)
//Alt: PORTC.DIR &= ~(1 << SW0); //Alt: PORTF.DIR &= ~(1 << SW0);
PORTC.PIN2CTRL |= (1 << 3); // Enable pull-up on button SW PORTF.PIN2CTRL |= (1 << 3); // Enable pull-up on button SW
//eller bruk PORT_PULLUPEN_bp som er lik 3 (_bp = Bit Position) //eller bruk PORT_PULLUPEN_bp som er lik 3 (_bp = Bit Position)
...@@ -57,11 +57,11 @@ int main(void) ...@@ -57,11 +57,11 @@ int main(void)
while (1) while (1)
{ {
if(!(PORTC.IN & (1 << SW0))){ if(!(PORTF.IN & (1 << SW0))){
if(buttonState == 0){ if(buttonState == 0){
PORTD.OUTTGL = ( 1 << LED0); PORTF.OUTTGL = ( 1 << LED0);
//Alt: PORTD.OUT ^= (1 << LED0); //Alt: PORTF.OUT ^= (1 << LED0);
buttonState = 1; buttonState = 1;
} }
......
---Session 1--- ---Session 1---
Task 1: Task 1:
Learn how to open Atmel Studio. Learn how to open Atmel Studio.
...@@ -8,16 +9,19 @@ Read the code, what do you think it does? ...@@ -8,16 +9,19 @@ Read the code, what do you think it does?
Learn how to upload code. Learn how to upload code.
Task 2: Task 2:
Set up a button with pullup. Use this to control the led. Set up a button with pullup. Use this to control the led.
Task 3: Task 3:
Add aditional logic so that the button toggles the led. Add aditional logic so that the button toggles the led.
Task 4: (OPTIONAL) Task 4: (OPTIONAL)
Add the OLED board. Add the OLED board.
......
/* /*
* Task1.c * Task1.c
* *
...@@ -14,7 +15,7 @@ ...@@ -14,7 +15,7 @@
* F_CPU must be defined before including headers. * F_CPU must be defined before including headers.
*/ */
#define F_CPU 3333333UL //The ATtiny4809 operates at 20MHz with a default scaling factor of 6: 20/6 = 3.333333MHz #define F_CPU 3333333UL //The ATmega4809 operates at 20MHz with a default scaling factor of 6: 20/6 = 3.333333MHz
/** /**
* System headers bellow * System headers bellow
...@@ -25,13 +26,13 @@ ...@@ -25,13 +26,13 @@
/** /**
* Define helpers for pin bit positions. * Define helpers for pin bit positions.
* The LED0 on the ATtiny4809 main board is connected to pin 4 on port D. * The LED0 on the ATmega4809 main board is connected to pin 5 on port F.
* Check the datasheet to find out which pins on which ports the different LEDs and buttons are connected to. * Check the datasheet to find out which pins on which ports the different LEDs and buttons are connected to.
*/ */
#define LED0 4 // LED 1 is connected to pin 4 on PORTD #define LED0 5 // LED 1 is connected to pin 5 on PORTF
...@@ -49,7 +50,7 @@ int main(void){ ...@@ -49,7 +50,7 @@ int main(void){
* Toogle bit: FLAG ^= ( 1 << BIT_POS ) - using bitwise xor, toogles the bit at BIT_POS * Toogle bit: FLAG ^= ( 1 << BIT_POS ) - using bitwise xor, toogles the bit at BIT_POS
*/ */
PORTD.DIR |= (1 << LED0); // Set LED0 as output - Using "D" in PORTx.DIR since the LED is connected to port D on the microcontroller PORTF.DIR |= (1 << LED0); // Set LED0 as output - Using "D" in PORTx.DIR since the LED is connected to port D on the microcontroller
/* /*
* The usual way to run microcontrollers is using a simple infinite loop * The usual way to run microcontrollers is using a simple infinite loop
...@@ -57,9 +58,9 @@ int main(void){ ...@@ -57,9 +58,9 @@ int main(void){
while (1) while (1)
{ {
PORTD.OUT ^= (1 << LED0); // Changes the state of LED0 by XOR-ing the last state. Check the XOR-table to find out how this works. PORTF.OUT ^= (1 << LED0); // Changes the state of LED0 by XOR-ing the last state. Check the XOR-table to find out how this works.
// Non-compressed: PORTD.OUT = PORTD.OUT ^ (1 << LED0); // Non-compressed: PORTF.OUT = PORTF.OUT ^ (1 << LED0);
//Can also refer to toogle register: PORTD.OUTTGL = (1 << LED0). IMPORTANT: only use equal sign here (=) //Can also refer to toogle register: PORTF.OUTTGL = (1 << LED0). IMPORTANT: only use equal sign here (=)
_delay_ms(500); _delay_ms(500);
} }
} }
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
#include <util/delay.h> #include <util/delay.h>
/* /*
* Check out the ATtiny4809 datasheet to find the correct ports and pins * Check out the ATmega4809 datasheet to find the correct ports and pins
*/ */
// LED // LED
...@@ -29,7 +29,7 @@ int main(void) ...@@ -29,7 +29,7 @@ int main(void)
/* /*
* We want to send signals to the LEDs, in order to turn it off and on. * We want to send signals to the LEDs, in order to turn it off and on.
* We also want to be able to read the switches. * We also want to be able to read the switches.
* This is done by setting bits in the PORTx.DIR register (in this case PORTD.DIR and PORTC.DIR) * This is done by setting bits in the PORTx.DIR register (in this case PORTD.DIR and PORTD.DIR)
* PORTx.DIR: 1 is output, 0 is input * PORTx.DIR: 1 is output, 0 is input
* LED: 1 LED is off, 0 LED is on * LED: 1 LED is off, 0 LED is on
* Button: 1 Button is open, 0 button is pressed * Button: 1 Button is open, 0 button is pressed
...@@ -41,7 +41,7 @@ int main(void) ...@@ -41,7 +41,7 @@ int main(void)
/** /**
* In order to read from the switches, we need to give it a ground reference, via a pull-up resistor. * In order to read from the switches, we need to give it a ground reference, via a pull-up resistor.
* If we don't, the switch will have a floating ground, and hence its value will be undefined. * If we don't, the switch will have a floating ground, and hence its value will be undefined.
* On the ATtiny4809, we enable pull-up by setting the "PORT_PULLUPEN" flag in "PORTx.PINnCTRL" high. * On the ATmega4809, we enable pull-up by setting the "PORT_PULLUPEN" flag in "PORTx.PINnCTRL" high.
* See datasheet section 15 (I/O-ports). * See datasheet section 15 (I/O-ports).
*/ */
......
...@@ -30,7 +30,7 @@ int main(void) ...@@ -30,7 +30,7 @@ int main(void)
* Set to 0: REG &= ~( 1 << BIT_POS ) * Set to 0: REG &= ~( 1 << BIT_POS )
* This is called "read-modify-write". * This is called "read-modify-write".
* *
* The ATtiny4809 also has special port registers that can do some of this for you. * 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, * 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 * And similarly what PORTx.OUTSET, .OUTCLR and .OUTTGL do to PORTx.OUT
* (HINT: see chapter 15.5 of the datasheet) * (HINT: see chapter 15.5 of the datasheet)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment