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

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

/*
* System headers bellow
*/

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

/*
BuildTools's avatar
BuildTools committed
18
* Check out the ATmega4809 datasheet to find the correct ports and pins 
Petter Breedveld's avatar
Petter Breedveld committed
19
20
21
*/

// LED
22
#define LED0 4 //On port D
Petter Breedveld's avatar
Petter Breedveld committed
23
24

// Button
25
#define SW0 2 //On port C
Petter Breedveld's avatar
Petter Breedveld committed
26
27
28
29
30
31

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

	/**
	 * 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.
BuildTools's avatar
BuildTools committed
44
	 * On the ATmega4809, we enable pull-up by setting the "PORT_PULLUPEN" flag in "PORTx.PINnCTRL" high.
45
	 * See datasheet section 15 (I/O-ports).
Petter Breedveld's avatar
Petter Breedveld committed
46
47
48
49
50
51
52
53
54
55
56
57
	 */
    
	/* 
	 * It's your time to do some stuff! Do the following:
	 * 1 - Set LED0 as output
	 * 2 - Set SW0 as input
	 * 3 - Enable pull-up on button SW0
	 */
	
    while (1) 
    {
    	/*
58
		 * Here, you want to check if a button is pressed, and if yes, turn on the LED.
Petter Breedveld's avatar
Petter Breedveld committed
59
		 * If no, then do the opposite.
60
		 * Similar to setting pins with PORTx.OUT, we can read pins with PORTx.IN
Petter Breedveld's avatar
Petter Breedveld committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
		 * 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).
		 */
		
		
		/* 
		* Do the following:
		* 1 - check if button SW0 is pressed
		* 2 - if so, turn the LED on
		* 3 - if not, turn the LED off
		*/
		
    }
}