Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
avrkurs
avrkurs
Commits
b75291c7
Commit
b75291c7
authored
Feb 23, 2019
by
BuildTools
Browse files
Session 1 ported to ATmega4809
parent
ac61bd30
Changes
24
Hide whitespace changes
Inline
Side-by-side
Session1-LF/Task1-LF.c
View file @
b75291c7
...
@@ -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 AT
tiny
4809 operates at 20MHz with a default scaling factor of 6: 20/6 = 3.333333MHz
#define F_CPU 3333333UL //The AT
mega
4809 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 AT
tiny
4809 main board is connected to pin
4
on port
D
.
* The LED0 on the AT
mega
4809 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 PORT
D
#define LED0
5
// LED 1 is connected to pin
5
on PORT
F
...
@@ -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
*/
*/
PORT
D
.
DIR
|=
(
1
<<
LED0
);
// Set LED0 as output - Using "D" in PORTx.DIR since the LED is connected to port D on the microcontroller
PORT
F
.
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
)
{
{
PORT
D
.
OUT
^=
(
1
<<
LED0
);
// Changes the state of LED0 by XOR-ing the last state. Check the XOR-table to find out how this works.
PORT
F
.
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: PORT
D
.OUT = PORT
D
.OUT ^ (1 << LED0);
// Non-compressed: PORT
F
.OUT = PORT
F
.OUT ^ (1 << LED0);
//Can also refer to toogle register: PORT
D
.OUTTGL = (1 << LED0). IMPORTANT: only use equal sign here (=)
//Can also refer to toogle register: PORT
F
.OUTTGL = (1 << LED0). IMPORTANT: only use equal sign here (=)
_delay_ms
(
500
);
_delay_ms
(
500
);
}
}
}
}
Session1-LF/Task2-LF.c
View file @
b75291c7
...
@@ -15,21 +15,21 @@
...
@@ -15,21 +15,21 @@
#include <util/delay.h>
#include <util/delay.h>
/*
/*
* Check out the AT
tiny
4809 datasheet to find the correct ports and pins
* Check out the AT
mega
4809 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 PORT
D
.DIR and PORT
C
.DIR)
* This is done by setting bits in the PORTx.DIR register (in this case PORT
F
.DIR and PORT
F
.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 AT
tiny
4809, we enable pull-up by setting the "PORT_PULLUPEN" flag in "PORTx.PINnCTRL" high.
* On the AT
mega
4809, 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
*/
*/
PORT
D
.
DIR
|=
(
1
<<
LED0
);
// Set LED0 as output
PORT
F
.
DIR
|=
(
1
<<
LED0
);
// Set LED0 as output
PORT
C
.
DIR
&=
~
(
1
<<
SW0
);
//Set SW0 as input
PORT
F
.
DIR
&=
~
(
1
<<
SW0
);
//Set SW0 as input
PORT
B
.
PIN
2
CTRL
|=
(
1
<<
PORT_PULLUPEN_bp
);
//Enable pull-up on button SW0 (pin5)
PORT
F
.
PIN
5
CTRL
|=
(
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
(
!
(
PORT
C
.
IN
&
(
1
<<
SW0
))){
// If button is pressed (0 - LOW
if
(
!
(
PORT
F
.
IN
&
(
1
<<
SW0
))){
// If button is pressed (0 - LOW
PORT
D
.
OUT
&=
~
(
1
<<
LED0
);
// Sets output to 0, turns LED0 on
PORT
F
.
OUT
&=
~
(
1
<<
LED0
);
// Sets output to 0, turns LED0 on
}
}
else
{
else
{
PORT
D
.
OUT
|=
(
1
<<
LED0
);
// Sets output to 1, turns LED off
PORT
F
.
OUT
|=
(
1
<<
LED0
);
// Sets output to 1, turns LED off
}
}
}
}
...
...
Session1-LF/Task3-LF.c
View file @
b75291c7
...
@@ -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 AT
tiny
4809 also has special port registers that can do some of this for you.
* The AT
mega
4809 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)
*/
*/
PORT
D
.
DIRSET
=
(
1
<<
LED0
);
// Set LED0 as output
PORT
F
.
DIRSET
=
(
1
<<
LED0
);
// Set LED0 as output
//Alt: PORT
D
.DIR |= (1 << LED0);
//Alt: PORT
F
.DIR |= (1 << LED0);
PORT
D
.
OUTSET
=
(
1
<<
LED0
);
// Set LED0 output high (turns off the led, since the led is active low)
PORT
F
.
OUTSET
=
(
1
<<
LED0
);
// Set LED0 output high (turns off the led, since the led is active low)
//Alt: PORT
D
.OUT |= (1 << LED0);
//Alt: PORT
F
.OUT |= (1 << LED0);
PORT
C
.
DIRCLR
=
(
1
<<
SW0
);
// Set SW0 as input (default)
PORT
F
.
DIRCLR
=
(
1
<<
SW0
);
// Set SW0 as input (default)
//Alt: PORT
C
.DIR &= ~(1 << SW0);
//Alt: PORT
F
.DIR &= ~(1 << SW0);
PORT
C
.
PIN2CTRL
|=
(
1
<<
3
);
// Enable pull-up on button SW
PORT
F
.
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
(
!
(
PORT
C
.
IN
&
(
1
<<
SW0
))){
if
(
!
(
PORT
F
.
IN
&
(
1
<<
SW0
))){
if
(
buttonState
==
0
){
if
(
buttonState
==
0
){
PORT
D
.
OUTTGL
=
(
1
<<
LED0
);
PORT
F
.
OUTTGL
=
(
1
<<
LED0
);
//Alt: PORT
D
.OUT ^= (1 << LED0);
//Alt: PORT
F
.OUT ^= (1 << LED0);
buttonState
=
1
;
buttonState
=
1
;
}
}
...
...
Session1-LF/Tasks_session1.txt
View file @
b75291c7
---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.
...
...
Session1/Task1/.gitignore
→
Session1/Task1
-blink
/.gitignore
View file @
b75291c7
File moved
Session1/Task1/Task1.atsln
→
Session1/Task1
-blink
/Task1.atsln
View file @
b75291c7
File moved
Session1/Task1/Task1.componentinfo.xml
→
Session1/Task1
-blink
/Task1.componentinfo.xml
View file @
b75291c7
File moved
Session1/Task1/Task1.cproj
→
Session1/Task1
-blink
/Task1.cproj
View file @
b75291c7
File moved
Session1/Task1/main.c
→
Session1/Task1
-blink
/main.c
View file @
b75291c7
/*
/*
* 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 AT
tiny
4809 operates at 20MHz with a default scaling factor of 6: 20/6 = 3.333333MHz
#define F_CPU 3333333UL //The AT
mega
4809 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 AT
tiny
4809 main board is connected to pin
4
on port
D
.
* The LED0 on the AT
mega
4809 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 PORT
D
#define LED0
5
// LED 1 is connected to pin
5
on PORT
F
...
@@ -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
*/
*/
PORT
D
.
DIR
|=
(
1
<<
LED0
);
// Set LED0 as output - Using "D" in PORTx.DIR since the LED is connected to port D on the microcontroller
PORT
F
.
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
)
{
{
PORT
D
.
OUT
^=
(
1
<<
LED0
);
// Changes the state of LED0 by XOR-ing the last state. Check the XOR-table to find out how this works.
PORT
F
.
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: PORT
D
.OUT = PORT
D
.OUT ^ (1 << LED0);
// Non-compressed: PORT
F
.OUT = PORT
F
.OUT ^ (1 << LED0);
//Can also refer to toogle register: PORT
D
.OUTTGL = (1 << LED0). IMPORTANT: only use equal sign here (=)
//Can also refer to toogle register: PORT
F
.OUTTGL = (1 << LED0). IMPORTANT: only use equal sign here (=)
_delay_ms
(
500
);
_delay_ms
(
500
);
}
}
}
}
Session1/Task2/.gitignore
→
Session1/Task2
-button
/.gitignore
View file @
b75291c7
File moved
Session1/Task2/Task2.atsln
→
Session1/Task2
-button
/Task2.atsln
View file @
b75291c7
File moved
Session1/Task2/Task2.componentinfo.xml
→
Session1/Task2
-button
/Task2.componentinfo.xml
View file @
b75291c7
File moved
Session1/Task2/Task2.cproj
→
Session1/Task2
-button
/Task2.cproj
View file @
b75291c7
File moved
Session1/Task2/main.c
→
Session1/Task2
-button
/main.c
View file @
b75291c7
...
@@ -15,7 +15,7 @@
...
@@ -15,7 +15,7 @@
#include <util/delay.h>
#include <util/delay.h>
/*
/*
* Check out the AT
tiny
4809 datasheet to find the correct ports and pins
* Check out the AT
mega
4809 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 PORT
C
.DIR)
* This is done by setting bits in the PORTx.DIR register (in this case PORTD.DIR and PORT
D
.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 AT
tiny
4809, we enable pull-up by setting the "PORT_PULLUPEN" flag in "PORTx.PINnCTRL" high.
* On the AT
mega
4809, 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).
*/
*/
...
...
Session1/Task3/.gitignore
→
Session1/Task3
-debouncing
/.gitignore
View file @
b75291c7
File moved
Session1/Task3/Task3.atsln
→
Session1/Task3
-debouncing
/Task3.atsln
View file @
b75291c7
File moved
Session1/Task3/Task3.componentinfo.xml
→
Session1/Task3
-debouncing
/Task3.componentinfo.xml
View file @
b75291c7
File moved
Session1/Task3/Task3.cproj
→
Session1/Task3
-debouncing
/Task3.cproj
View file @
b75291c7
File moved
Session1/Task3/main.c
→
Session1/Task3
-debouncing
/main.c
View file @
b75291c7
...
@@ -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 AT
tiny
4809 also has special port registers that can do some of this for you.
* The AT
mega
4809 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)
...
...
Session1/Task4/.gitignore
→
Session1/Task4
-extra
/.gitignore
View file @
b75291c7
File moved
Prev
1
2
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment