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
1436e0a1
Commit
1436e0a1
authored
Mar 05, 2019
by
siguhe
Browse files
Me and Johan read through and fixed tasks session2.
parent
ec65c6fd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Session2/PWM/main.c
View file @
1436e0a1
...
...
@@ -14,7 +14,7 @@
/*
In this exercise, you will use PWM to control the brightness of LEDs
In this exercise, you will use PWM to control the brightness of LEDs
.
Once we have PWM set up, controling the brightness is super easy!
*/
...
...
@@ -22,8 +22,9 @@ int main(void)
{
//Set up the leds and buttons. You might want to copy this from previous exercises.
/*We will be using timer 1 in split (not single) mode. We use split to get access to WO4 and WO5, see Figure 19-13.
It is highly recommended that you read chapter 19.3.3.4 and 19.3.3.6 in the datasheet.
/*We will be using timer TCA0 in Split (not Single) mode. We use Split mode to output
waveforms on WO4 and WO5, see Figure 19-13.
It is highly recommended that you read chapter 19.3.3.4 and 19.3.3.6 in the megaAVR® 0-series.
There you will find a sub-chapter on the single-slope PWM we will be using.
*/
...
...
@@ -33,21 +34,24 @@ int main(void)
//We have to override the normal pin opperation so the PWM is pushed directly to the pin
//Hint: WO4 and WO5 are connected to leds. We need to override them to get the PWM out.
//See Datasheet to ATmega4809 for pins.
/*Timer A is a 16 bit timer.
When we use split mode, this will be split into two 8 bit timers. This will give us a frequency of 25Hz/2, we can see it flicker.
By lowering the period, (PER) we get higher frequency at the cost of lower resolution. (Since in split mode, we must use HPER or LPER.)
When we use split mode, this will be split into two 8 bit timers.
By lowering the period, (PER) we get higher frequency at the cost of lower resolution.
(Since in split mode, we must use LPER(for WO0-WO2) and HPER (for WO3-WO5).
*/
//Set the period to 0xff bit. (What frequency does this give?)
//We can now control the PWM duty cycle by simply writing values to the CMP0 and CMP1 registers.
TCA0
.
SPLIT
.
HCMP1
=
0x00
;
//Max brightness (Leds are inverted)
TCA0
.
SPLIT
.
HCMP2
=
0xff
;
//Min brightness (Leds are inverted)
//Set Duty cycle of WO4 to 0x7f to see it twinkle.
while
(
1
){
...
...
Session2/Timer/main.c
View file @
1436e0a1
...
...
@@ -20,21 +20,21 @@ int main(void)
*/
/*We will be using timer A that will trigger an overflow interupt.
/*We will be using timer A
(TCA0)
that will trigger an overflow interupt.
This is a 16 bit timer that can run in 2 modes
-single mode as
1
16-bit timer
-dual/split mode as
2
8-bit timers
-single mode as
one
16-bit timer
-dual/split mode as
two
8-bit timers
We will be using single mode in this exercise.
Hint because the register names can be hard to understand:
TCA0.SINGLE.CTRLA addresses the control A register for timer A
First we set the prescaler to clk=clk/256 and enable the timer.
This is done by setting the
righ
t bits in the control A register.
This is done by setting the
correc
t bits in the control A register.
*/
//Next we Enable timer interupt for overflow on
timer A
.
//Next we Enable timer interupt for overflow on
TCA0
.
//Finally we have to set the max value of the timer, the top.
...
...
@@ -44,7 +44,7 @@ int main(void)
//To be able to react to the interrupts from the module we have to enable interrupts globally.
//This is done in the function sei(), whih is located in the already included header file <avr/interrupt.h>
//This is done in the function sei(), whi
c
h is located in the already included header file <avr/interrupt.h>
sei
();
while
(
1
){
...
...
@@ -61,4 +61,4 @@ ISR(TCA0_OVF_vect){
//Clear the interrupt flag.
//If we do not clear the flag, we will instantly jump back into the ISR again
}
\ No newline at end of file
}
Session2/UART/main.c
View file @
1436e0a1
...
...
@@ -8,16 +8,17 @@
#include <util/delay.h>
/*
In this exercise we will set up and use UART communication.
In this exercise we will set up and use U
S
ART communication.
The embedded debugger has a virtual com port that we will use to communicate with the computer.
*/
void
uart_init
(
unsigned
long
baud
){
void
u
s
art_init
(
unsigned
long
baud
){
//In this function, we want to initialize the UART.
//Chapter 24.3 in datasheet tells us how this is done:
//In this function, we want to initialize the USART.
//Chapter 22.3 in datasheet tells us how this is done:
//You can see on the datasheet for Curiosity Nano that USART3 connects to the usb.
//Set TX pin as output
//Set the TX pin high
...
...
@@ -29,38 +30,38 @@ void uart_init(unsigned long baud){
}
// function to transmit data
void
uart_transmit
(
unsigned
char
data
){
//In this function we will
be
send data.
void
u
s
art_transmit
(
unsigned
char
data
){
//In this function we will 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
//Put our new data into
se sending
register
//Put our new data into
the DATA
register
}
int
main
(
void
)
{
//Initialize the UART with our function.
//Initialize the U
S
ART with our function.
//We will be using a baudrate of 9600 (defined as BAUD_9600 at the top of the file)
sei
();
//Enable global interrupt, important for anything here to work
sei
();
//Enable global interrupt,
this is
important for anything here to work
while
(
1
)
{
//We don't really need to do anything here.
//If you want, you can continously send something over usart. It will be a good idea to include a small delay.
//the ISR will handle receiving.
}
}
//Interrupt service routine for the receiver.
ISR
(
USART
0
_RXC_vect
){
ISR
(
USART
3
_RXC_vect
){
//Read out the received data to a variable
//Do things with data. Perhaps send something back?
//Do things with data. Perhaps send something back? Maybe not the same as recieved?
}
\ No newline at end of file
}
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