1 | /*
|
2 | * Chip.h
|
3 | *
|
4 | * Created: 12.10.2014 12:52:28
|
5 | * Author: Daniel
|
6 | */
|
7 |
|
8 |
|
9 | #ifndef CHIP_H_
|
10 | #define CHIP_H_
|
11 |
|
12 | // RTC Konfiguration
|
13 | #define RTC_Interrupt_Level RTC32_OVFINTLVL_gm
|
14 | #define RTC_Clock_Source CLK_RTCSRC_TOSC_gc
|
15 | #define RTC_Prescaler 1
|
16 |
|
17 | #include <avr/interrupt.h>
|
18 |
|
19 | void Enable_Interrupt(void);
|
20 | void Enable_2MHz_Clock(void);
|
21 | void Enable_32MHz_Clock(void);
|
22 | void Enable_PLL(void);
|
23 | void Enable_Ext_Clock(void);
|
24 | void Set_Output(PORT_t *Port, int Pin);
|
25 | void Set_Input(PORT_t *Port, int Pin);
|
26 | void Set_IO(PORT_t *Port, int Pin, int Status);
|
27 | void RTC_Init(void);
|
28 | void RTC_Config(int Periode);
|
29 |
|
30 |
|
31 | void Enable_Interrupt(void)
|
32 | {
|
33 | sei();
|
34 | PMIC.CTRL |= PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
|
35 | }
|
36 |
|
37 | void Enable_2MHz_Clock(void)
|
38 | {
|
39 | OSC.CTRL |= OSC_RC2MEN_bm;
|
40 | while(!(OSC.STATUS & OSC_RC2MRDY_bm));
|
41 | CCP = CCP_IOREG_gc;
|
42 | CLK.CTRL = CLK_SCLKSEL_RC2M_gc;
|
43 | }
|
44 |
|
45 | void Enable_32MHz_Clock(void)
|
46 | {
|
47 | OSC.CTRL |= OSC_RC32MEN_bm;
|
48 | while(!(OSC.STATUS & OSC_RC32MRDY_bm));
|
49 | CCP = CCP_IOREG_gc;
|
50 | CLK.CTRL = CLK_SCLKSEL_RC32M_gc;
|
51 | }
|
52 |
|
53 | void Enable_Ext_Clock(void)
|
54 | {
|
55 |
|
56 | }
|
57 |
|
58 | void Set_Output(PORT_t *Port, int Pin)
|
59 | {
|
60 | Port->DIR |= (1 << Pin);
|
61 |
|
62 | return;
|
63 | }
|
64 |
|
65 | void Set_Input(PORT_t *Port, int Pin)
|
66 | {
|
67 | Port->DIR &= ~(1 << Pin);
|
68 |
|
69 | return;
|
70 | }
|
71 |
|
72 | void Set_IO(PORT_t *Port, int Pin, int Status)
|
73 | {
|
74 | if(Status == 1)
|
75 | {
|
76 | Port->OUT |= (1 << Pin);
|
77 | }
|
78 | else if(Status == 0)
|
79 | {
|
80 | Port->OUT &= ~(1 << Pin);
|
81 | }
|
82 |
|
83 | return;
|
84 | }
|
85 |
|
86 |
|
87 | void RTC_Init()
|
88 | {
|
89 | // Oszillator aktivieren
|
90 | OSC.CTRL |= OSC_XOSCEN_bm;
|
91 | while (OSC.STATUS & (OSC_XOSCRDY_bm));
|
92 |
|
93 | // 32kHz Quarz am TOSC Pin im Low-Power Mode aktivieren
|
94 | OSC.XOSCCTRL |= OSC_X32KLPM_bm | OSC_XOSCSEL_32KHz_gc;
|
95 |
|
96 | // RTC akvieren
|
97 | CLK.RTCCTRL = RTC_Clock_Source | CLK_RTCEN_bm;
|
98 | }
|
99 |
|
100 | void RTC_Config(int Periode)
|
101 | {
|
102 | RTC32.PER = Periode;
|
103 | RTC32.CTRL = RTC32_ENABLE_bm;
|
104 | while(OSC.STATUS & RTC32_SYNCBUSY_bm);
|
105 |
|
106 | // RTC Interrupt konfigurieren
|
107 | RTC32.INTCTRL = RTC_Interrupt_Level;
|
108 | }
|
109 |
|
110 |
|
111 | #endif /* CHIP_H_ */
|