1 | #include <asf.h>
|
2 | #include <stdio.h>
|
3 | #define F_CPU 32000000UL
|
4 | #include <util/delay.h>
|
5 |
|
6 | #define LED_ORANGE IOPORT_CREATE_PIN(PORTD, 0)
|
7 |
|
8 | int main (void)
|
9 | {
|
10 | system_clocks_init();
|
11 | ioport_configure_pin(LED_ORANGE, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH);
|
12 |
|
13 | while(1) {
|
14 | ioport_set_pin_low(LED_ORANGE);
|
15 | _delay_ms(1000);
|
16 | ioport_set_pin_high(LED_ORANGE);
|
17 | }
|
18 | // Insert application code here, after the board has been initialized.
|
19 | }
|
20 |
|
21 | void system_clocks_init(void)
|
22 | {
|
23 | unsigned char n,s;
|
24 |
|
25 | // Optimize for speed
|
26 | #pragma optsize-
|
27 | // Save interrupts enabled/disabled state
|
28 | s=SREG;
|
29 | // Disable interrupts
|
30 | #asm("cli")
|
31 |
|
32 | // Internal 32 kHz RC oscillator initialization
|
33 | // Enable the internal 32 kHz RC oscillator
|
34 | OSC.CTRL|=OSC_RC32KEN_bm;
|
35 | // Wait for the internal 32 kHz RC oscillator to stabilize
|
36 | while ((OSC.STATUS & OSC_RC32KRDY_bm)==0);
|
37 |
|
38 | // Internal 32 MHz RC oscillator initialization
|
39 | // Enable the internal 32 MHz RC oscillator
|
40 | OSC.CTRL|=OSC_RC32MEN_bm;
|
41 |
|
42 | // System Clock prescaler A division factor: 1
|
43 | // System Clock prescalers B & C division factors: B:1, C:1
|
44 | // ClkPer4: 32000,000 kHz
|
45 | // ClkPer2: 32000,000 kHz
|
46 | // ClkPer: 32000,000 kHz
|
47 | // ClkCPU: 32000,000 kHz
|
48 | n=(CLK.PSCTRL & (~(CLK_PSADIV_gm | CLK_PSBCDIV1_bm | CLK_PSBCDIV0_bm))) |
|
49 | CLK_PSADIV_1_gc | CLK_PSBCDIV_1_1_gc;
|
50 | CCP=CCP_IOREG_gc;
|
51 | CLK.PSCTRL=n;
|
52 |
|
53 | // Internal 32 MHz RC osc. calibration reference clock source: 32.768 kHz Internal Osc.
|
54 | OSC.DFLLCTRL&= ~(OSC_RC32MCREF_bm | OSC_RC2MCREF_bm);
|
55 | // Enable the autocalibration of the internal 32 MHz RC oscillator
|
56 | DFLLRC32M.CTRL|=DFLL_ENABLE_bm;
|
57 |
|
58 | // Wait for the internal 32 MHz RC oscillator to stabilize
|
59 | while ((OSC.STATUS & OSC_RC32MRDY_bm)==0);
|
60 |
|
61 | // Select the system clock source: 32 MHz Internal RC Osc.
|
62 | n=(CLK.CTRL & (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_RC32M_gc;
|
63 | CCP=CCP_IOREG_gc;
|
64 | CLK.CTRL=n;
|
65 |
|
66 | // Disable the unused oscillators: 2 MHz, external clock/crystal oscillator, PLL
|
67 | OSC.CTRL&= ~(OSC_RC2MEN_bm | OSC_XOSCEN_bm | OSC_PLLEN_bm);
|
68 |
|
69 | // Lock the CLK.CTRL and CLK.PSCTRL registers
|
70 | n=CLK.LOCK | CLK_LOCK_bm;
|
71 | CCP=CCP_IOREG_gc;
|
72 | CLK.LOCK=n;
|
73 |
|
74 | // ClkPer output: Disabled bit 7
|
75 | PORTCFG.CLKEVOUT=(PORTCFG.CLKEVOUT & (~PORTCFG_CLKOUT_gm)) | PORTCFG_CLKOUT_OFF_gc;
|
76 |
|
77 | // Restore interrupts enabled/disabled state
|
78 | SREG=s;
|
79 | // Restore optimization for size if needed
|
80 | #pragma optsize_default
|
81 | }
|