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
simenpf
attinykurs
Commits
642b8eed
Commit
642b8eed
authored
Feb 22, 2017
by
Petter Breedveld
Browse files
ession 1 updated as suggested by comments
parent
034a8f8e
Changes
7
Hide whitespace changes
Inline
Side-by-side
Session1-LF/Task1-LF.c
View file @
642b8eed
...
...
@@ -13,7 +13,7 @@
* F_CPU must be defined before including headers.
*/
#define F_CPU 3333333UL //The ATtiny817 operates at 20MHz with a scaling factor of 6: 20/6 = 3.333333MHz
#define F_CPU 3333333UL //The ATtiny817 operates at 20MHz with a
default
scaling factor of 6: 20/6 = 3.333333MHz
/**
* System headers bellow
...
...
@@ -24,8 +24,8 @@
/**
* Define helpers for pin bit positions.
* The LED0 on the ATmega324PB main board is connected to pin
7
on port
C (PINC7)
.
* Check the datasheet to find out which pins on which ports
(PINxn)
the different LEDs and buttons are connected to.
* The LED0 on the ATmega324PB main board is connected to pin
4
on port
B
.
* Check the datasheet to find out which pins on which ports the different LEDs and buttons are connected to.
*/
#define LED0 4 // LED0 is connected to pin 4 on port B
...
...
@@ -43,7 +43,7 @@ int main(void){
* Set to 0: FLAG &= ~( 1 << BIT_POS ) - same thing here but using 'and', find paper and a logic-table, and try it out if you want to know how it works
*/
PORTB
_
DIR
|=
(
1
<<
LED0
);
// Set LED0 as output - Using "B" in PORTx
_
DIR since the LED is connected to port B on the microcontroller
PORTB
.
DIR
|=
(
1
<<
LED0
);
// Set LED0 as output - Using "B" in PORTx
.
DIR since the LED is connected to port B on the microcontroller
/*
* The usual way to run microcontrollers is using a simple infinite loop
...
...
@@ -51,8 +51,8 @@ int main(void){
while
(
1
)
{
PORTB
_
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: PORTB_OUT = PORTB
_
OUT ^ (1 << LED0);
PORTB
.
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: PORTB_OUT = PORTB
.
OUT ^ (1 << LED0);
_delay_ms
(
500
);
}
}
Session1-LF/Task2-LF.c
View file @
642b8eed
...
...
@@ -29,8 +29,8 @@ int main(void)
/*
* 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.
* This is done by setting bits in the PORTx
_
DIR register (in this case PORTB
_
DIR)
* PORTx
_
DIR: 1 is output, 0 is input
* This is done by setting bits in the PORTx
.
DIR register (in this case PORTB
.
DIR)
* PORTx
.
DIR: 1 is output, 0 is input
* LED: 1 LED is off, 0 LED is on
* Button: 1 Button is open, 0 button is pressed
* Bit set logic:
...
...
@@ -41,33 +41,33 @@ int main(void)
/**
* 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.
* On the ATtiny817, we enable pull-up by setting the "PORT_PULLUPEN_bp" flag in "PORTx
_
PINnCTRL" high.
* On the ATtiny817, we enable pull-up by setting the "PORT_PULLUPEN_bp" flag in "PORTx
.
PINnCTRL" high.
* See datasheet section 16 (I/O-ports) and user guide section 4 (Hardware User Guide).
*/
PORTB
_
DIR
|=
(
1
<<
LED0
);
// Set LED0 as output
PORTB
_
DIR
&=
~
(
1
<<
SW0
);
//Set SW0 as input
PORTB
.
DIR
|=
(
1
<<
LED0
);
// Set LED0 as output
PORTB
.
DIR
&=
~
(
1
<<
SW0
);
//Set SW0 as input
PORTB
_
PIN5CTRL
|=
(
1
<<
PORT_PULLUPEN_bp
);
//Enable pull-up on button SW0 (pin5)
PORTB
.
PIN5CTRL
|=
(
1
<<
PORT_PULLUPEN_bp
);
//Enable pull-up on button SW0 (pin5)
while
(
1
)
{
/*
* Here, you want to check if a button is
de
pressed, and if yes, turn on the
corresponding
LED.
* Here, you want to check if a button is pressed, and if yes, turn on the LED.
* If no, then do the opposite.
* Similar to setting pins with PORTx_OUT, we can read pins with PORTx
_
IN
* Similar to setting pins with PORTx_OUT, we can read pins with PORTx
.
IN
* In order to check a pin value, mask out that particular bit. (use bitwise AND)
* Bit masking is done like this:
* (REGISTER & (1 << BIT_POS)), which selects bit BIT_POS from register.
* If that bit is 0, the result will be 0. If it is 1, the result will be other than 0 (depending on bit pos).
*/
if
(
!
(
PORTB
_
IN
&
(
1
<<
SW0
))){
// If button is pressed (0 - LOW
PORTB
_
OUT
&=
~
(
1
<<
LED0
);
// Sets output to 0, turns LED0 on
if
(
!
(
PORTB
.
IN
&
(
1
<<
SW0
))){
// If button is pressed (0 - LOW
PORTB
.
OUT
&=
~
(
1
<<
LED0
);
// Sets output to 0, turns LED0 on
}
else
{
PORTB
_
OUT
|=
(
1
<<
LED0
);
// Sets output to 1, turns LED off
PORTB
.
OUT
|=
(
1
<<
LED0
);
// Sets output to 1, turns LED off
}
}
...
...
Session1-LF/Task3-LF.c
View file @
642b8eed
...
...
@@ -20,28 +20,46 @@
int
main
(
void
)
{
/*
* PORTx
_
DIR: set direction - 1 is output, 0 is input
* PORTx
_
OUT: set value of pins
* PORTx
_
IN: read value of pins
* PORTx
.
DIR: set direction - 1 is output, 0 is input
* PORTx
.
OUT: set value of pins
* PORTx
.
IN: read value of pins
* LED: 1 LED is off, 0 LED is on
* SW: 1 Button is open, 0 button is pressed
* Set to 1: FLAG |= ( 1 << BIT_POS )
* Set to 0: FLAG &= ~( 1 << BIT_POS )
* Set to 1: REG |= ( 1 << BIT_POS )
* Set to 0: REG &= ~( 1 << BIT_POS )
* This is called "read-modify-write".
*
* The ATtiny817 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,
* And similarly what PORTx.OUTSET, .OUTCLR and .OUTTGL do to PORTx.OUT
* (HINT: see chapter 16.5 of the datasheet)
LF: They set clear and toggle bit in the register. eks: PORTB.DIRCLR = (1 << 5); to clear bit 5
*/
PORTB
.
DIRSET
=
(
1
<<
LED0
);
// Set LED0 as output
//Alt: PORTB.DIR |= (1 << LED0);
PORTB
_
DIR
|
=
(
1
<<
LED
0
);
// Set
LED
0 as
output
PORTB
_
DIR
&=
~
(
1
<<
SW0
);
// Set SW0 as input
PORTB
.
DIR
CLR
=
(
1
<<
SW
0
);
// Set
SW
0 as
input (default)
//Alt:
PORTB
.
DIR &= ~(1 << SW0);
PORTB_PIN5CTRL
|=
(
1
<<
PORT_PULLUPEN_bp
);
// Enable pull-up on button SW
PORTB
.
PIN5CTRL
|=
(
1
<<
3
);
// Enable pull-up on button SW
//eller bruk PORT_PULLUPEN_bp som er lik 3 (_bp = Bit Position)
int
buttonState
=
0
;
// To hold the button pressed state
while
(
1
)
{
if
(
!
(
PORTB
_
IN
&
(
1
<<
SW0
))){
if
(
!
(
PORTB
.
IN
&
(
1
<<
SW0
))){
if
(
buttonState
==
0
){
PORTB_OUT
^=
(
1
<<
LED0
);
PORTB
.
OUTTGL
=
(
1
<<
LED0
);
//Alt: PORTB.OUT ^= (1 << LED0);
buttonState
=
1
;
}
}
...
...
Session1/Task1/main.c
View file @
642b8eed
...
...
@@ -14,7 +14,7 @@
* F_CPU must be defined before including headers.
*/
#define F_CPU 3333333UL //The ATtiny817 operates at 20MHz with a scaling factor of 6: 20/6 = 3.333333MHz
#define F_CPU 3333333UL //The ATtiny817 operates at 20MHz with a
default
scaling factor of 6: 20/6 = 3.333333MHz
/**
* System headers bellow
...
...
@@ -25,8 +25,8 @@
/**
* Define helpers for pin bit positions.
* The LED0 on the AT
mega324PB
main board is connected to pin
7
on port
C (PINC7)
.
* Check the datasheet to find out which pins on which ports
(PINxn)
the different LEDs and buttons are connected to.
* The LED0 on the AT
tiny817
main board is connected to pin
4
on port
B
.
* Check the datasheet to find out which pins on which ports the different LEDs and buttons are connected to.
*/
#define LED0 4 // LED0 is connected to pin 4 on port B
...
...
@@ -35,7 +35,7 @@ int main(void){
/**
* We want to send signals to the LEDs, in order to turn the off and on.
* In the AVR world we have the following:
* 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, this is called active-low
* Button: 1 button is open, 0 button is pressed, this is called active-low
*
...
...
@@ -44,7 +44,7 @@ int main(void){
* Set to 0: FLAG &= ~( 1 << BIT_POS ) - same thing here but using 'and', find paper and a logic-table, and try it out if you want to know how it works
*/
PORTB
_
DIR
|=
(
1
<<
LED0
);
// Set LED0 as output - Using "B" in PORTx
_
DIR since the LED is connected to port B on the microcontroller
PORTB
.
DIR
|=
(
1
<<
LED0
);
// Set LED0 as output - Using "B" in PORTx
.
DIR since the LED is connected to port B on the microcontroller
/*
* The usual way to run microcontrollers is using a simple infinite loop
...
...
@@ -52,8 +52,8 @@ int main(void){
while
(
1
)
{
PORTB
_
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: PORTB
_
OUT = PORTB
_
OUT ^ (1 << LED0);
PORTB
.
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: PORTB
.
OUT = PORTB
.
OUT ^ (1 << LED0);
_delay_ms
(
500
);
}
}
Session1/Task2/main.c
View file @
642b8eed
...
...
@@ -29,19 +29,19 @@ int main(void)
/*
* 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.
* This is done by setting bits in the PORTx
_
DIR register (in this case PORTB
_
DIR)
* PORTx
_
DIR: 1 is output, 0 is input
* This is done by setting bits in the PORTx
.
DIR register (in this case PORTB
.
DIR)
* PORTx
.
DIR: 1 is output, 0 is input
* LED: 1 LED is off, 0 LED is on
* Button: 1 Button is open, 0 button is pressed
* Bit set logic:
* Set to 1:
FLA
G |= ( 1 << BIT_POS )
* Set to 0:
FLA
G &= ~( 1 << BIT_POS )
* Set to 1:
RE
G |= ( 1 << BIT_POS )
* Set to 0:
RE
G &= ~( 1 << BIT_POS )
*/
/**
* 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.
* On the ATtiny817, we enable pull-up by setting the "PORT_PULLUPEN
_bp
" flag in "PORTx
_
PINnCTRL" high.
* On the ATtiny817, we enable pull-up by setting the "PORT_PULLUPEN" flag in "PORTx
.
PINnCTRL" high.
* See datasheet section 16 (I/O-ports) and user guide section 4 (Hardware User Guide).
*/
...
...
@@ -55,9 +55,9 @@ int main(void)
while
(
1
)
{
/*
* Here, you want to check if a button is
de
pressed, and if yes, turn on the
corresponding
LED.
* Here, you want to check if a button is pressed, and if yes, turn on the LED.
* If no, then do the opposite.
* Similar to setting pins with PORTx
_
OUT, we can read pins with PORTx
_
IN
* Similar to setting pins with PORTx
.
OUT, we can read pins with PORTx
.
IN
* In order to check a pin value, mask out that particular bit. (use bitwise AND)
* Bit masking is done like this:
* (REGISTER & (1 << BIT_POS)), which selects bit BIT_POS from register.
...
...
Session1/Task3/main.c
View file @
642b8eed
...
...
@@ -21,13 +21,19 @@
int
main
(
void
)
{
/*
* PORTx
_
DIR: set direction - 1 is output, 0 is input
* PORTx
_
OUT: set value of pins
* PORTx
_
IN: read value of pins
* PORTx
.
DIR: set direction - 1 is output, 0 is input
* PORTx
.
OUT: set value of pins
* PORTx
.
IN: read value of pins
* LED: 1 LED is off, 0 LED is on
* SW: 1 Button is open, 0 button is pressed
* Set to 1: FLAG |= ( 1 << BIT_POS )
* Set to 0: FLAG &= ~( 1 << BIT_POS )
* Set to 1: REG |= ( 1 << BIT_POS )
* Set to 0: REG &= ~( 1 << BIT_POS )
* This is called "read-modify-write".
*
* The ATtiny817 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,
* And similarly what PORTx.OUTSET, .OUTCLR and .OUTTGL do to PORTx.OUT
* (HINT: see chapter 16.5 of the datasheet)
*/
...
...
@@ -40,8 +46,8 @@ int main(void)
{
/*
* Do the following:
* Check if button is pressed, but this time you want to toggle the light instead of turning it on/off (hint: XOR)
* Add a button-pressed-state so that you don
t
't have to hold the button to turn on the light, but so that
* Check if button is pressed, but this time you want to toggle the light instead of turning it on/off (hint:
OUTTGL or
XOR)
* Add a button-pressed-state so that you don't have to hold the button to turn on the light, but so that
* you instead toggle between light on/off when the button is pressed (like a light switch):
* When the button is not pressed the light should hold it's current state.
*/
...
...
Session1/Task4/main.c
View file @
642b8eed
...
...
@@ -7,7 +7,7 @@
/*
* This time you'll have to do all the setup and coding yourself.
* You are go
nna
connect the "OLED1 Xplained Pro" extension card to the ATtiny817 kit,
* You are go
ing
connect the "OLED1 Xplained Pro" extension card to the ATtiny817 kit,
* and make the LEDs and buttons work on the extension card.
*/
...
...
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