Commit b75291c7 authored by BuildTools's avatar BuildTools
Browse files

Session 1 ported to ATmega4809

parent ac61bd30
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@
 * 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
@@ -26,13 +26,13 @@

/**
 * 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.
 */



#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){
	 * 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
@@ -58,9 +58,9 @@ int main(void){

    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. 
		// Non-compressed: PORTD.OUT = PORTD.OUT ^ (1 << LED0);
		//Can also refer to toogle register: PORTD.OUTTGL = (1 << LED0). IMPORTANT: only use equal sign here (=)
		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: PORTF.OUT = PORTF.OUT ^ (1 << LED0);
		//Can also refer to toogle register: PORTF.OUTTGL = (1 << LED0). IMPORTANT: only use equal sign here (=)
		_delay_ms(500);
    }
}
+11 −11
Original line number Diff line number Diff line
@@ -15,21 +15,21 @@
#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
#define LED0 4 //On port D
#define LED0 5 //On port F

// Button
#define SW0 2 //On port C
#define SW0 6 //On port C

int main(void)
{
	/*
	 * 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.
	 * 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
	 * LED: 1 LED is off, 0 LED is on
	 * Button: 1 Button is open, 0 button is pressed
@@ -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.
	 * 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).
	 */
    
@@ -52,10 +52,10 @@ int main(void)
	 * 3 - Enable pull-up on button SW0
	 */
	
	PORTD.DIR |= (1 << LED0); // Set LED0 as output
	PORTC.DIR &= ~(1 << SW0); //Set SW0 as input
	PORTF.DIR |= (1 << LED0); // Set LED0 as output
	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) 
    {
@@ -77,12 +77,12 @@ int main(void)
		* 3 - if not, turn the LED off
		*/
		
		if(!(PORTC.IN & (1 << SW0))){ // If button is pressed (0 - LOW
			PORTD.OUT &= ~(1 << LED0); // Sets output to 0, turns LED0 on
		if(!(PORTF.IN & (1 << SW0))){ // If button is pressed (0 - LOW
			PORTF.OUT &= ~(1 << LED0); // Sets output to 0, turns LED0 on
		}
		
		else{
			PORTD.OUT |= (1 << LED0); // Sets output to 1, turns LED off
			PORTF.OUT |= (1 << LED0); // Sets output to 1, turns LED off
		}
		
    }
+13 −13
Original line number Diff line number Diff line
@@ -12,10 +12,10 @@
#include <stdbool.h>

//LED
#define LED0 4 //on port D
#define LED0 5 //on port D

//Button
#define SW0 2 //on port C
#define SW0 6 //on port C

int main(void)
{
@@ -29,7 +29,7 @@ int main(void)
	 * Set to 0: REG &= ~( 1 << BIT_POS )
	 * 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, 
	 * And similarly what PORTx.OUTSET, .OUTCLR and .OUTTGL do to PORTx.OUT 
	 * (HINT: see chapter 15.5 of the datasheet)
@@ -40,16 +40,16 @@ int main(void)

	*/

	PORTD.DIRSET = (1 << LED0); // Set LED0 as output
	//Alt: PORTD.DIR |= (1 << LED0); 
	PORTF.DIRSET = (1 << LED0); // Set LED0 as output
	//Alt: PORTF.DIR |= (1 << LED0); 
	
	PORTD.OUTSET = (1 << LED0); // Set LED0 output high (turns off the led, since the led is active low)
	//Alt: PORTD.OUT |= (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);

	PORTC.DIRCLR = (1 << SW0); // Set SW0 as input (default)
	//Alt: PORTC.DIR &= ~(1 << SW0); 
	PORTF.DIRCLR = (1 << SW0); // Set SW0 as input (default)
	//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)

	
@@ -57,11 +57,11 @@ int main(void)
	
    while (1) 
    {
		if(!(PORTC.IN & (1 << SW0))){
		if(!(PORTF.IN & (1 << SW0))){
			if(buttonState == 0){
				
				PORTD.OUTTGL = ( 1 << LED0);
				//Alt: PORTD.OUT ^= (1 << LED0);
				PORTF.OUTTGL = ( 1 << LED0);
				//Alt: PORTF.OUT ^= (1 << LED0);

				buttonState = 1;
			}
+4 −0
Original line number Diff line number Diff line
---Session 1---



Task 1:

Learn how to open Atmel Studio.
@@ -8,16 +9,19 @@ Read the code, what do you think it does?
Learn how to upload code.



Task 2:

Set up a button with pullup. Use this to control the led.



Task 3:

Add aditional logic so that the button toggles the led.



Task 4: (OPTIONAL)

Add the OLED board.
Loading