Commit 642b8eed authored by Petter Breedveld's avatar Petter Breedveld

ession 1 updated as suggested by comments

parent 034a8f8e
......@@ -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);
}
}
......@@ -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 depressed, 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
}
}
......
......@@ -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 << LED0); // Set LED0 as output
PORTB_DIR &= ~(1 << SW0); // Set SW0 as input
PORTB.DIRCLR = (1 << SW0); // Set SW0 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;
}
}
......
......@@ -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 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 ATtiny817 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);
}
}
......@@ -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: 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 )
*/
/**
* 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 depressed, 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.
......
......@@ -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 dont'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.
*/
......
......@@ -7,7 +7,7 @@
/*
* This time you'll have to do all the setup and coding yourself.
* You are gonna connect the "OLED1 Xplained Pro" extension card to the ATtiny817 kit,
* You are going connect the "OLED1 Xplained Pro" extension card to the ATtiny817 kit,
Please register or sign in to reply
* and make the LEDs and buttons work on the extension card.
*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment