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
2999711f
Commit
2999711f
authored
Mar 08, 2017
by
Petter Breedveld
Browse files
Corrected mistake in comments
parent
0cbb0c29
Changes
1
Hide whitespace changes
Inline
Side-by-side
Session2/PWM/main.c
View file @
2999711f
/*
* PWM-LF.c
*
* Created: 29.01.2017 23:43:39
* Author : Petter
*/
#define F_CPU 3333333UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define LED1 0 // port B, connected to WO0
#define LED2 1 // port B, connected to WO1
#define LED3 4 // Port B
#define SW1 5 // Port A
#define SW2 6 // Port A
#define SW3 7 // Port A
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 single (not split) mode.
It is highly recommended that you read chapter 20.3.3.4 in the datasheet
on timer compare channels.
There you will find a sub-capter on the single-slope PWM we will be using.
*/
//First, enable the timer
//Set the mode of the timer to single slope PWM
//We have to override the normal pin opperation so the PWM is pushed directly to the pin
//Hint: WO0 and WO1 are connected to leds. We need to override them to get the PWM out.
/*Timer A is a 16 bit timer. This will give us a frequency of 25Hz, we can see it flicker.
By lowering the period, (PER) we get higher frequency at the cost of lower resolution.
*/
//Set the period to 12 bit. (This results in a frequency of ~400Hz.)
//We can now control the PWM duty cycle by simply writing values to the CMP0 and CMP1 registers.
TCA0
.
SINGLE
.
CMP0
=
0x0000
;
//Max brightness (Leds are inverted)
TCA0
.
SINGLE
.
CMP1
=
0x0fff
;
//Min brightness (Leds are inverted)
while
(
1
){
/*Have some fun with the leds. Examples:
Have them fade between max and min
Have them fade in a pattern (Heartbeat?)
Change the brightness based on buttons
*/
}
}
#define F_CPU 3333333UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define LED1 0 // port B, connected to WO0
#define LED2 1 // port B, connected to WO1
#define LED3 4 // Port B
#define SW1 5 // Port A
#define SW2 6 // Port A
#define SW3 7 // Port A
/*
In this exercise, you will use PWM to control thwe brightness of LEDs
Once we have PWM set up, controling the brightness is super easy!
*/
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 single (not split) mode.
It is highly recommended that you read chapter 20.3.3.4 in the datasheet
on timer compare channels.
There you will find a sub-capter on the single-slope PWM we will be using.
*/
//First, enable the timer
//Set the mode of the timer to single slope PWM
//We have to override the normal pin opperation so the PWM is pushed directly to the pin
//Hint: WO0 and WO1 are connected to leds. We need to override them to get the PWM out.
/*Timer A is a 16 bit timer. This will give us a frequency of 25Hz, we can see it flicker.
By lowering the period, (PER) we get higher frequency at the cost of lower resolution.
*/
//Set the period to 12 bit. (This results in a frequency of ~400Hz.)
//We can now control the PWM duty cycle by simply writing values to the CMP0 and CMP1 registers.
TCA0
.
SINGLE
.
CMP0
=
0x0000
;
//Max brightness (Leds are inverted)
TCA0
.
SINGLE
.
CMP1
=
0x0fff
;
//Min brightness (Leds are inverted)
while
(
1
){
/*Have some fun with the leds. Examples:
Have them fade between max and min
Have them fade in a pattern (Heartbeat?)
Change the brightness based on buttons
*/
}
}
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