Commit ec65c6fd authored by siguhe's avatar siguhe

Me and Johan read trough all tasks in Session 1.

parent ae6facf2
......@@ -4,12 +4,15 @@
*
* Created: 11.02.2017 23:37:53
* Author : Petter
*/
*
* Updated: 05.03.2017
* Author AVRurd
*/
/**
* F_CPU tells the timer what the CPU's clock frequency is, such that the timer will count correctly.
* Note that this DOES NOT set the clock speed, it just informs the timer.
* Note that this DOES NOT set the clock speed, it just informs the compiler. Here it affects the _delay_ms() function.
* If this is set wrong, the chip will work as normal, but software delays will be too long (if set too low) or too short (if set too high).
* Example: set F_CPU to 2 MHz => "#define F_CPU 2000000UL". UL = unsigned long
* F_CPU must be defined before including headers.
......@@ -26,31 +29,29 @@
/**
* Define helpers for pin bit positions.
* The LED0 on the ATmega4809 main board is connected to pin 5 on port F.
* Check the datasheet to find out which pins on which ports the different LEDs and buttons are connected to.
* The LED0 on the ATmega4809 Curiosity Nano is connected to pin 5 on port F.
* You can also check the datasheet or the silk screen to find out which pins on which ports the different LEDs and buttons are connected to. For example PF5 means PORTF, pin 5.
*/
#define LED0 5 // LED 1 is connected to pin 5 on PORTF
#define LED0 5 // LED 1 is connected to pin 5 on PORTF
int main(void){
/**
* We want to send signals to the LEDs, in order to turn the off and on.
* We want to send signals to the LED, in order to turn it on and off.
* In the AVR world we have the following:
* 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
*
* Remember bit set logic:
* Set to 1: FLAG |= ( 1 << BIT_POS ) - using 'or' so we only change the one we want and leave the others untouched
* 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
* Toogle bit: FLAG ^= ( 1 << BIT_POS ) - using bitwise xor, toogles the bit at BIT_POS
* Set to 1: REGISTER |= ( 1 << BIT_POS ) - using 'or' ('|') so we only change the one we want and leave the others untouched
* Set to 0: REGISTER &= ~( 1 << BIT_POS ) - same thing here but using 'and' ('&') and bitwise 'not' ('~'), find paper and a logic-table, and try it out if you want to know how it works.
* Toogle bit: REGISTER ^= ( 1 << BIT_POS ) - using bitwise 'xor' ('^'), toogles the bit at BIT_POS
*/
PORTF.DIR |= (1 << LED0); // Set LED0 as output - Using "D" in PORTx.DIR since the LED is connected to port D on the microcontroller
PORTF.DIR |= (1 << LED0); // Set LED0 as output - Using "F" in PORTx.DIR since the LED is connected to port F on the microcontroller
/*
* The usual way to run microcontrollers is using a simple infinite loop
......@@ -59,7 +60,7 @@ int main(void){
while (1)
{
PORTF.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: PORTF.OUT = PORTF.OUT ^ (1 << LED0);
//Non-compressed: PORTF.OUT = PORTF.OUT ^ (1 << LED0);
//Can also refer to toogle register: PORTF.OUTTGL = (1 << LED0). IMPORTANT: only use equal sign here (=)
_delay_ms(500);
}
......
......@@ -3,6 +3,9 @@
*
* Created: 11.02.2017 23:39:47
* Author : Petter
*
* Updated: 05.03.2019
* Author: AVRurd
*/
#define F_CPU 3333333UL //3.33... MHz
......@@ -19,17 +22,17 @@
*/
// LED
#define LED0 4 //On port D
#define LED0 5 //On port F
// Button
#define SW0 2 //On port C
#define SW0 6 //On port F
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 PORTD.DIR and PORTD.DIR)
* This is done by setting bits in the PORTx.DIR register (in this case PORTF.DIR and PORTF.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
......@@ -41,7 +44,7 @@ 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 ATmega4809, we enable pull-up by setting the "PORT_PULLUPEN" flag in "PORTx.PINnCTRL" high.
* On the ATmega4809, we enable pull-up by setting the "PORT_PULLUPEN" bit in "PORTx.PINnCTRL" high.
* See datasheet section 15 (I/O-ports).
*/
......@@ -55,21 +58,21 @@ int main(void)
while (1)
{
/*
* Here, you want to check if a button is pressed, and if yes, turn on the LED.
* Here, you want to check if a button is pressed, and if yes, turn on LED0.
* If no, then do the opposite.
* 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 that bit is 0, the result will be 0. If it is 1, the result will be greater than 0 (depending on bit pos).
*/
/*
* Do the following:
* 1 - check if button SW0 is pressed
* 2 - if so, turn the LED on
* 3 - if not, turn the LED off
* 2 - if so, turn LED0 on
* 3 - if not, turn LED0 off
*/
}
......
......@@ -3,6 +3,10 @@
*
* Created: 11.02.2017 23:45:00
* Author : Petter
*
* Updated: 05.03.2019
* Author: AVRurd
*
*/
// F_CPU
......
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