main.c 2.25 KB
Newer Older
BuildTools's avatar
BuildTools committed
1

Petter Breedveld's avatar
Petter Breedveld committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * Task1.c
 *
 * Created: 11.02.2017 23:37:53
 * Author : Petter
 */ 


/**
 * 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.
 * 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.
 */

BuildTools's avatar
BuildTools committed
18
#define F_CPU 3333333UL //The ATmega4809 operates at 20MHz with a default scaling factor of 6: 20/6 = 3.333333MHz
Petter Breedveld's avatar
Petter Breedveld committed
19
20
21
22
23
24
25
26
27
28

/**
 * System headers bellow
 */

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

/**
 * Define helpers for pin bit positions.
BuildTools's avatar
BuildTools committed
29
 * The LED0 on the ATmega4809 main board is connected to pin 5 on port F.
30
 * Check the datasheet to find out which pins on which ports the different LEDs and buttons are connected to.
Petter Breedveld's avatar
Petter Breedveld committed
31
32
 */

33
34


BuildTools's avatar
BuildTools committed
35
#define LED0	5			// LED 1 is connected to pin 5 on PORTF
36
37


Petter Breedveld's avatar
Petter Breedveld committed
38
39
40
41
42

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:
43
	 * PORTx.DIR: 1 is output, 0 is input
Petter Breedveld's avatar
Petter Breedveld committed
44
45
46
47
 	 * 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:
48
49
50
	 * 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
Petter Breedveld's avatar
Petter Breedveld committed
51
52
	 */

BuildTools's avatar
BuildTools committed
53
	PORTF.DIR |= (1 << LED0); // Set LED0 as output - Using "D" in PORTx.DIR since the LED is connected to port D on the microcontroller
Petter Breedveld's avatar
Petter Breedveld committed
54
55
56
57
58
59
60
	
	/*
	* The usual way to run microcontrollers is using a simple infinite loop
	*/

    while (1) 
    {
BuildTools's avatar
BuildTools committed
61
62
63
		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);
		//Can also refer to toogle register: PORTF.OUTTGL = (1 << LED0). IMPORTANT: only use equal sign here (=)
Petter Breedveld's avatar
Petter Breedveld committed
64
65
66
		_delay_ms(500);
    }
}