1 | #include "mcc_generated_files/mcc.h"
|
2 | #include "defines.h"
|
3 | #include <string.h>
|
4 | #include "functionsCenter.h"
|
5 | #include "devicectrl.h"
|
6 | #include "mcc_generated_files/tmr2.h"
|
7 | #include "ads1120.h"
|
8 |
|
9 |
|
10 | void main(void)
|
11 | {
|
12 | SYSTEM_Initialize();
|
13 | // Enable high priority global interrupts
|
14 | INTERRUPT_GlobalInterruptHighEnable();
|
15 | // Enable low priority global interrupts.
|
16 | INTERRUPT_GlobalInterruptLowEnable();
|
17 |
|
18 | LED1_SetLow();
|
19 | LED2_SetLow();
|
20 |
|
21 | #if defined(ADS1120_ON)
|
22 | ADS1120_Init();
|
23 | ADS1120_OffsetCalibration(ADC1);
|
24 | ADS1120_OffsetCalibration(ADC2);
|
25 | devicectrl_InitStructures();
|
26 | WWDT_SoftEnable();
|
27 | while (1)
|
28 | {
|
29 | WWDT_TimerClear();
|
30 | if(STARTUP_FINISHED)
|
31 | MY_StateMachine();
|
32 | if(devctrl_SetToSleepMode)
|
33 | devicectrl_EnterPowerDownMode(); //Set PIC into power saving mode
|
34 | }
|
35 | }
|
36 |
|
37 | void SYSTEM_Initialize(void)
|
38 | {
|
39 | //INTERRUPT_Initialize
|
40 | INTCON0bits.IPEN = 1;
|
41 |
|
42 | // Assign peripheral interrupt priority vectors
|
43 | // UTXI - high priority
|
44 | IPR3bits.U1TXIP = 1;
|
45 | // URXI - high priority
|
46 | IPR3bits.U1RXIP = 1;
|
47 | // TMRI - high priority
|
48 | IPR3bits.TMR0IP = 1;
|
49 | // IOCI - low priority
|
50 | IPR0bits.IOCIP = 0;
|
51 | // URXI - low priority
|
52 | IPR6bits.U2RXIP = 0;
|
53 | // UTXI - low priority
|
54 | IPR6bits.U2TXIP = 0;
|
55 | //
|
56 |
|
57 | //PMD_Initialize
|
58 | // CLKRMD CLKR enabled; SYSCMD SYSCLK enabled; SCANMD SCANNER enabled; FVRMD FVR enabled; IOCMD IOC enabled; CRCMD CRC enabled; HLVDMD HLVD enabled; NVMMD NVM enabled;
|
59 | PMD0 = 0x00;
|
60 | // NCO1MD DDS(NCO1) enabled; TMR0MD TMR0 enabled; TMR1MD TMR1 enabled; TMR4MD TMR4 enabled; TMR5MD TMR5 enabled; TMR2MD TMR2 enabled; TMR3MD TMR3 enabled; TMR6MD TMR6 enabled;
|
61 | PMD1 = 0x00;
|
62 | // ZCDMD ZCD enabled; DACMD DAC enabled; CMP1MD CMP1 enabled; ADCMD ADC enabled; CMP2MD CMP2 enabled;
|
63 | PMD2 = 0x00;
|
64 | // CCP2MD CCP2 enabled; CCP1MD CCP1 enabled; CCP4MD CCP4 enabled; CCP3MD CCP3 enabled; PWM6MD PWM6 enabled; PWM5MD PWM5 enabled; PWM8MD PWM8 enabled; PWM7MD PWM7 enabled;
|
65 | PMD3 = 0x00;
|
66 | // CWG3MD CWG3 enabled; CWG2MD CWG2 enabled; CWG1MD CWG1 enabled;
|
67 | PMD4 = 0x00;
|
68 | // U2MD UART2 enabled; U1MD UART1 enabled; SPI1MD SPI1 enabled; I2C2MD I2C2 enabled; I2C1MD I2C1 enabled;
|
69 | PMD5 = 0x00;
|
70 | // DSMMD DSM1 enabled; CLC3MD CLC3 enabled; CLC4MD CLC4 enabled; SMT1MD SMT1 enabled; CLC1MD CLC1 enabled; CLC2MD CLC2 enabled;
|
71 | PMD6 = 0x00;
|
72 | // DMA1MD DMA1 enabled; DMA2MD DMA2 enabled;
|
73 | PMD7 = 0x00;
|
74 | //
|
75 |
|
76 | //PIN_MANAGER_Initialize
|
77 | /**
|
78 | LATx registers
|
79 | */
|
80 | LATE = 0x00;
|
81 | LATD = 0x30;
|
82 | LATA = 0xF8;
|
83 | LATB = 0x12;
|
84 | LATC = 0x08;
|
85 |
|
86 | /**
|
87 | TRISx registers
|
88 | */
|
89 | TRISE = 0x07;
|
90 | TRISA = 0x86;
|
91 | TRISB = 0xF5;
|
92 | TRISC = 0xB1;
|
93 | TRISD = 0x88;
|
94 |
|
95 | /**
|
96 | ANSELx registers
|
97 | */
|
98 | ANSELD = 0x08;
|
99 | ANSELC = 0x90;
|
100 | ANSELB = 0xE1;
|
101 | ANSELE = 0x07;
|
102 | ANSELA = 0x02;
|
103 |
|
104 | /**
|
105 | WPUx registers
|
106 | */
|
107 | WPUD = 0x00;
|
108 | WPUE = 0x00;
|
109 | WPUB = 0x10;
|
110 | WPUA = 0x80;
|
111 | WPUC = 0x08;
|
112 |
|
113 | /**
|
114 | RxyI2C registers
|
115 | */
|
116 | RB1I2C = 0x00;
|
117 | RB2I2C = 0x00;
|
118 | RC3I2C = 0x00;
|
119 | RC4I2C = 0x00;
|
120 | RD0I2C = 0x00;
|
121 | RD1I2C = 0x00;
|
122 | /**
|
123 | ODx registers
|
124 | */
|
125 | ODCONE = 0x00;
|
126 | ODCONA = 0x00;
|
127 | ODCONB = 0x00;
|
128 | ODCONC = 0x00;
|
129 | ODCOND = 0x00;
|
130 | /**
|
131 | SLRCONx registers
|
132 | */
|
133 | SLRCONA = 0xFF;
|
134 | SLRCONB = 0xFF;
|
135 | SLRCONC = 0xFF;
|
136 | SLRCOND = 0xFF;
|
137 | SLRCONE = 0x07;
|
138 | /**
|
139 | INLVLx registers
|
140 | */
|
141 | INLVLA = 0xFF;
|
142 | INLVLB = 0xFF;
|
143 | INLVLC = 0xFF;
|
144 | INLVLD = 0xFF;
|
145 | INLVLE = 0x0F;
|
146 | /**
|
147 | IOCx registers
|
148 | */
|
149 | //interrupt on change for group IOCAF - flag
|
150 | IOCAFbits.IOCAF2 = 0;
|
151 | //interrupt on change for group IOCAF - flag
|
152 | IOCAFbits.IOCAF7 = 0;
|
153 | //interrupt on change for group IOCAN - negative
|
154 | IOCANbits.IOCAN2 = 1;
|
155 | //interrupt on change for group IOCAN - negative
|
156 | IOCANbits.IOCAN7 = 1;
|
157 | //interrupt on change for group IOCAP - positive
|
158 | IOCAPbits.IOCAP2 = 1;
|
159 | //interrupt on change for group IOCAP - positive
|
160 | IOCAPbits.IOCAP7 = 0;
|
161 | //interrupt on change for group IOCBF - flag
|
162 | IOCBFbits.IOCBF4 = 0;
|
163 | //interrupt on change for group IOCBN - negative
|
164 | IOCBNbits.IOCBN4 = 1;
|
165 | //interrupt on change for group IOCBP - positive
|
166 | IOCBPbits.IOCBP4 = 0;
|
167 | // register default IOC callback functions at runtime; use these methods to register a custom function
|
168 | IOCAF2_SetInterruptHandler(IOCAF2_DefaultInterruptHandler);
|
169 | IOCAF7_SetInterruptHandler(IOCAF7_DefaultInterruptHandler);
|
170 | IOCBF4_SetInterruptHandler(IOCBF4_DefaultInterruptHandler);
|
171 | // Enable IOCI interrupt
|
172 | PIE0bits.IOCIE = 1;
|
173 | U2RXPPS = 0x1F; //RD7->UART2:RX2;
|
174 | SPI1SCKPPS = 0x09; //RB1->SPI1:SCK1;
|
175 | RB1PPS = 0x1E; //RB1->SPI1:SCK1;
|
176 | RC1PPS = 0x13; //RC1->UART1:TX1;
|
177 | RB3PPS = 0x1F; //RB3->SPI1:SDO1;
|
178 | RC6PPS = 0x15; //RC6->UART1:RTS1;
|
179 | U1RXPPS = 0x10; //RC0->UART1:RX1;
|
180 | RD6PPS = 0x16; //RD6->UART2:TX2;
|
181 | SPI1SDIPPS = 0x0A; //RB2->SPI1:SDI1;
|
182 | U1CTSPPS = 0x15; //RC5->UART1:CTS1;
|
183 | //
|
184 |
|
185 | //WWDT_Initialize();
|
186 | WDTCON0 = 18;
|
187 | WDTCON1 = 7|48;
|
188 | //
|
189 | OSCILLATOR_Initialize();
|
190 | TMR2_Initialize();
|
191 | TMR0_Initialize();
|
192 | SPI1_Initialize();
|
193 | }
|