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
8de5c12b
Commit
8de5c12b
authored
Feb 23, 2019
by
BuildTools
Browse files
Session 2 ported for ATmega4809
parent
b75291c7
Changes
6
Hide whitespace changes
Inline
Side-by-side
Session2-LF/PWM-LF.c
View file @
8de5c12b
...
...
@@ -7,6 +7,10 @@
#include <avr/interrupt.h>
/**
In LF, we use the OLDED Xplained extension bhoard to pwm two leds at the same time. Can they do that?
*/
/*
In the pin definitions below, we have defined the PORT as well.
Everything works the same way as before, except you have to change PORTx with the PORT definition.
...
...
@@ -24,13 +28,6 @@ Example: LED1_PORT.OUTSET = (1 << LED1);
#define SW1 2 //pin
#define SW2 1 //pin
#define SW3 2 //pin
#define SW1_PORT PORTC //port
#define SW2_PORT PORTD //port
#define SW3_PORT PORTD //port
#define TOP_PERIOD 0x55
...
...
Session2-LF/TIMER-LF.c
View file @
8de5c12b
...
...
@@ -12,9 +12,9 @@
#include <util/delay.h>
#include <avr/interrupt.h>
#define LED1
0
// port
B
#define LED2
1
// port
B
#define LED3 4 // Port B
#define LED1
4
// port
D
#define LED2
5
// port
D
int
main
(
void
)
...
...
@@ -23,8 +23,8 @@ int main(void)
First set up the LEDs like in the previous exercise. You can either copy paste from the previous exercise or start fresh.
*/
PORT
B
.
DIR
=
(
1
<<
LED1
)
|
(
1
<<
LED2
);
PORT
B
.
OUTTGL
=
(
1
<<
LED1
);
//Starting with only 1 LED on
PORT
D
.
DIR
=
(
1
<<
LED1
)
|
(
1
<<
LED2
);
PORT
D
.
OUTTGL
=
(
1
<<
LED1
);
//Starting with only 1 LED on
/*We will be using timer A that will trigger an overflow interupt.
This is a 16 bit timer that can run in 2 modes
...
...
@@ -61,7 +61,7 @@ int main(void)
ISR
(
TCA0_OVF_vect
){
//Do something with the led(s), like toggle.
PORT
B
.
OUTTGL
=
(
1
<<
LED1
)
|
(
1
<<
LED2
);
PORT
D
.
OUTTGL
=
(
1
<<
LED1
)
|
(
1
<<
LED2
);
//Clear the interrupt flag.
//If we do not clear the flag, we will instantly jump back into the ISR again
...
...
Session2-LF/UART-LF.c
View file @
8de5c12b
...
...
@@ -5,8 +5,8 @@
* Author : Petter
*/
#define F_CPU 3333333
UL
#define BAUD_9600
((4UL
*F_CPU
)
/9600
)
#define F_CPU 3333333
#define BAUD_9600
4
*F_CPU/9600
#include <avr/io.h>
#include <avr/interrupt.h>
...
...
@@ -15,43 +15,57 @@
/*
In this exercise we will set up and use UART communication.
The embedded debugger has a virtual com port that we will use to communicate with the computer.
*/
*/
#define TX_PORT PORTB
#define TX_PIN 0
void
uart_init
(
unsigned
long
baud
){
//From chapter 24.3 in datasheet
PORTB
.
DIRSET
=
(
1
<<
PIN2_bp
);
//Setting up TX pin as output
PORTB
.
OUTSET
=
(
1
<<
PIN2_bp
);
//Setting the TX pin high
//From chapter 22.3.1 in datasheet
USART0
.
BAUDH
=
(
baud
>>
8
);
//Shift register right by 8 bits to get the 8 high bits
USART0
.
BAUDL
=
baud
;
//Set baud rate without shifting to get the 8 low bits
TX_PORT
.
OUTSET
=
(
1
<<
TX_PIN
);
//Setting up TX pin as output
TX_PORT
.
DIRSET
=
(
1
<<
TX_PIN
);
//Setting up TX pin as output
//It turns out the compiler can handle this automatically, meaning this works just as well:
//USART0.BAUD = baud;
//Set baud rate register
USART3
.
BAUDL
=
(
uint8_t
)
baud
;
//Set baud rate without shifting to get the 8 low bits
USART3
.
BAUDH
=
(
uint8_t
)(
baud
>>
8
);
//Shift register right by 8 bits to get the 8 high bits
//USART.CTRLC CMODE bits default to async, 1 stop bit, 8 bit character size
//Since all bits are default 0 we only need to change the character size part
USART3
.
CTRLC
=
(
0x3
<<
USART_CHSIZE0_bp
);
USART0
.
CTRLB
|=
(
1
<<
USART_RXEN_bp
)
|
(
1
<<
USART_TXEN_bp
);
//Enable RX and TX
//Enable RX and TX
USART3
.
CTRLB
=
(
1
<<
USART_RXEN_bp
)
|
(
1
<<
USART_TXEN_bp
);
USART0
.
CTRLA
|=
(
1
<<
USART_RXCIE_bp
);
//Enable interrupts on incoming data
//Enable interrupts on incoming data
USART3
.
CTRLA
|=
(
1
<<
USART_RXCIE_bp
);
}
// function to transmit data
void
uart_transmit
(
unsigned
char
data
){
void
uart_transmit
(
char
data
){
//In this function we will be send data.
//First we should check that there isn't already data being sent
// if there is, we should probably wait for it to finish first
while
(
!
(
USART
0
.
STATUS
&
(
1
<<
USART_DREIF_bp
))){
while
(
!
(
USART
3
.
STATUS
&
(
1
<<
USART_DREIF_bp
))){
//wait for previous transmit to finish
};
//Put our new data into se sending register
USART0
.
TXDATAL
=
data
;
//Put our new data into tx data register
USART3
.
TXDATAL
=
data
;
}
//To send a string we can do this
void
uart_transmit_string
(
char
*
data
)
{
while
(
*
data
!=
'\0'
)
{
uart_transmit
(
*
data
);
data
++
;
}
}
...
...
@@ -67,16 +81,21 @@ int main(void)
{
//We don't really need to do anything here.
//the ISR will handle receiving.
uart_transmit_string
(
"Dette er en test
\n
"
);
_delay_ms
(
200
);
}
}
//Interrupt service routine for the receiver.
ISR
(
USART0_RXC_vect
){
ISR
(
USART3_RXC_vect
)
{
//In the interrupt we will read the data in the receive buffer
//Read out the received data to a variable
uint8_t
data
=
USART0_RXDATAL
;
//Do things with data:
uart_transmit
(
data
+
1
);
//Example: Shift all characters by one step A -> B, B -> C etc.
//First we should check that new data has arrived the receive buffer
while
(
!
(
USART3
.
STATUS
&
(
1
<<
USART_RXCIF_bp
))){
//wait for previous transmit to finish
};
//Store the data in a temporarily variable
uint8_t
tmp
=
USART3
.
RXDATAL
;
}
\ No newline at end of file
Session2/PWM/main.c
View file @
8de5c12b
...
...
@@ -10,7 +10,6 @@
#define LED1 4 // port D, connected to WO4
#define LED2 5 // port D, connected to WO5
#define SW0 2 //on port C
...
...
Session2/Timer
_old
/main.c
→
Session2/Timer/main.c
View file @
8de5c12b
...
...
@@ -7,9 +7,9 @@
#include <util/delay.h>
#include <avr/interrupt.h>
#define LED1
0
// port
B
#define LED2
1
// port
B
#define LED3 4 // Port B
#define LED1
4
// port
D
#define LED2
5
// port
D
int
main
(
void
)
...
...
Session2/UART
_old
/main.c
→
Session2/UART/main.c
View file @
8de5c12b
File moved
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