Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
attinykurs
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
simenpf
attinykurs
Commits
6d7fe9d0
Commit
6d7fe9d0
authored
Mar 10, 2017
by
spookysys
Browse files
Options
Browse Files
Download
Plain Diff
Merge
https://git.omegav.no/avrkurs/attinykurs
parents
0913809d
567c7c38
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
69 deletions
+66
-69
Session2/PWM/main.c
Session2/PWM/main.c
+66
-69
No files found.
Session2/PWM/main.c
View file @
6d7fe9d0
/*
* 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