Task2-LF.c 2.84 KB
Newer Older
Petter Breedveld's avatar
Petter Breedveld committed
1
/*
2
 * Task2.c
Petter Breedveld's avatar
Petter Breedveld committed
3
 *
4
 * Created: 11.02.2017 23:39:47
Petter Breedveld's avatar
Petter Breedveld committed
5
6
7
8
9
10
11
12
13
14
15
16
17
 * Author : Petter
 */ 

#define F_CPU 3333333UL //3.33... MHz

/*
* System headers bellow
*/

#include <avr/io.h>
#include <util/delay.h>

/*
18
19
20
21
22
23
 * Take a look at the Atmega4809 Curiosity Nano board and locate "SW0" and "LED0".
 * Right below each of them you can see which pin they are connected to. For example, if it said PD5, this would mean Port D and Pin 5.
 * This info can also be found in the ATmega4809 Curiosity Nano Hardware User Guide here: http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega4809-Curiosity-Nano-HW-UG-DS50002804A.pdf to find the correct ports and pins 
 */

//Fill in the defines with the information you found on the board/in the user guide.
Petter Breedveld's avatar
Petter Breedveld committed
24
25

// LED
26
#define LED0 5 //Fill in pin number
Petter Breedveld's avatar
Petter Breedveld committed
27
28

// Button
29
#define SW0 6 //Fill in pin number
Petter Breedveld's avatar
Petter Breedveld committed
30
31
32
33
34
35

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.
36
	 * This is done by setting bits in the PORTx.DIR register (in this case PORTF.DIR)
37
	 * PORTx.DIR: 1 is output, 0 is input
Petter Breedveld's avatar
Petter Breedveld committed
38
39
40
	 * LED: 1 LED is off, 0 LED is on
	 * Button: 1 Button is open, 0 button is pressed
	 * Bit set logic:
41
42
	 * Set to 1: REG |= ( 1 << BIT_POS )
	 * Set to 0: REG &= ~( 1 << BIT_POS )
Petter Breedveld's avatar
Petter Breedveld committed
43
44
45
46
47
	 */

	/**
	 * 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.
48
	 * On the ATmega4809, we enable pull-up by setting the "PORT_PULLUPEN" flag in "PORTx.PINnCTRL" high.
49
50
51
52
53
54
55
	 * See datasheet section 15 (I/O-ports).
	 */
    
	/* 
	 * It's your time to do some stuff! Do the following:
	 * 1 - Set LED0 as output
	 * 2 - Set SW0 as input
56
	 * 3 - Enable pull-up on button SW0 (Not necessary here as we have an external pull-up, but in case you dont have one in the future, dont forget!).
Petter Breedveld's avatar
Petter Breedveld committed
57
	 */
58
59
60
	PORTF.DIRSET = (1 << LED0);
	PORTF.DIRCLR = (1 << SW0);	
	//PORTF.PIN6CTRL |= (1<<PORT_PULLUPEN_bp); //Not necessary as the button already had an external pullup.
Petter Breedveld's avatar
Petter Breedveld committed
61
62
63
    while (1) 
    {
    	/*
64
		 * Here, you want to check if a button is pressed, and if yes, turn on the LED.
Petter Breedveld's avatar
Petter Breedveld committed
65
		 * If no, then do the opposite.
66
		 * Similar to setting pins with PORTx.OUT, we can read pins with PORTx.IN
Petter Breedveld's avatar
Petter Breedveld committed
67
68
69
70
71
		 * 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).
		 */
72
73
74
75
76
77
78
79
		
		
		/* 
		* Do the following:
		* 1 - check if button SW0 is pressed
		* 2 - if so, turn the LED on
		* 3 - if not, turn the LED off
		*/
80
81
82
83
84
85
86
87
		if(!(PORTF.IN & (1<<SW0)))//Check if SW0 is pressed
		{
			PORTF.OUTCLR = (1<<LED0);//Set LED0 high
		}
		else
		{
			PORTF.OUTSET = (1<<LED0);//Set LED0 low
		}
88
		
Petter Breedveld's avatar
Petter Breedveld committed
89
90
    }
}