|
@@ -0,0 +1,92 @@
|
|
|
+// ----------------------------------------------------------------------------
|
|
|
+// ADC_1.c
|
|
|
+// ----------------------------------------------------------------------------
|
|
|
+// Beschreibung: ADC_1 - Interner 12 Bit ADC
|
|
|
+//
|
|
|
+// Revision: 06. Januar 2017, neu Reiling, IPE
|
|
|
+//
|
|
|
+// ----------------------------------------------------------------------------
|
|
|
+
|
|
|
+#include "BMS_Master.h"
|
|
|
+
|
|
|
+
|
|
|
+int8_t ADC_1_init(void)
|
|
|
+{
|
|
|
+ int8_t ret = 0;
|
|
|
+
|
|
|
+ SIU.PCR[20].R = 0x2000; /* Initialize PB[4] as ADC1 CH0 */
|
|
|
+ SIU.PCR[21].R = 0x2000; /* Initialize PB[5] as ADC1 CH1 */
|
|
|
+ SIU.PCR[22].R = 0x2000; /* Initialize PB[6] as ADC1 CH2 */
|
|
|
+ SIU.PCR[23].R = 0x2000; /* Initialize PB[7] as ADC1 CH3 */
|
|
|
+ SIU.PCR[48].R = 0x2000; /* Initialize PD[0] as ADC1 CH4 */
|
|
|
+ SIU.PCR[49].R = 0x2000; /* Initialize PD[1] as ADC1 CH5 */
|
|
|
+ SIU.PCR[50].R = 0x2000; /* Initialize PD[2] as ADC1 CH6 */
|
|
|
+ SIU.PCR[51].R = 0x2000; /* Initialize PD[3] as ADC1 CH7 */
|
|
|
+ SIU.PCR[52].R = 0x2000; /* Initialize PD[4] as ADC1 CH8 */
|
|
|
+ SIU.PCR[53].R = 0x2000; /* Initialize PD[5] as ADC1 CH9 */
|
|
|
+ SIU.PCR[54].R = 0x2000; /* Initialize PD[6] as ADC1 CH10 */
|
|
|
+ SIU.PCR[55].R = 0x2000; /* Initialize PD[7] as ADC1 CH11 */
|
|
|
+ ADC_1.MCR.R = 0x20000000; /* Initialize ADC1 for scan mode */
|
|
|
+ ADC_1.CTR0.R = 0x0000001F; /* Conversion times: INPSAMP=31, INPCMP=4, INPLATCH=0, SHIFT=No */
|
|
|
+
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+int8_t ADC_1_run(uint8_t Channel, uint16_t* Result)
|
|
|
+{
|
|
|
+ uint16_t i, j;
|
|
|
+ uint32_t Dummy=0;
|
|
|
+ int8_t ret=NoError;
|
|
|
+
|
|
|
+ if( Channel > 11) /* only AIN0 to AIN11 allowed */
|
|
|
+ ret = ADCwrongCH;
|
|
|
+ else {
|
|
|
+ ADC_1.NCMR0.R = 0x001 << Channel; /* Select ADC1 Channel input for conversion */
|
|
|
+
|
|
|
+ ADC_1.MCR.B.NSTART = 1; /* Trigger normal conversions for ADC1 */
|
|
|
+ for(i=0; i<64; i++){ /* 64 times Over-Sampling Loop */
|
|
|
+ for(j=0;j<32; j++){ /* TimeOut Loop */
|
|
|
+ if(j>30) return ADCtimeout; /* if TimeOut occurred end funktion */
|
|
|
+ if(ADC_1.ISR.B.ECH == 1) break; /* if 1st chain to complete then end TimeOut Loop */
|
|
|
+ }
|
|
|
+ Dummy += ADC_1.CDR[Channel].B.CDATA; /* Read ADC1 conversion result data */
|
|
|
+ }
|
|
|
+ ADC_1.MCR.B.NSTART = 0; /* Stop ADC1 */
|
|
|
+ *Result = (uint16_t)(Dummy >> 2); /* Result: 16 Bit */
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+int8_t CheckSupplyVoltages(uint16_t* U12, uint16_t* U33)
|
|
|
+{
|
|
|
+ uint32_t U12work=0, U33work=0;
|
|
|
+ uint16_t i, j;
|
|
|
+ int8_t ret=NoError;
|
|
|
+
|
|
|
+ ADC_1.NCMR0.R = 0x003; /* Select ADC1 Channel input for conversion */
|
|
|
+
|
|
|
+ ADC_1.MCR.B.NSTART = 1; /* Trigger normal conversions for ADC1 */
|
|
|
+ for(i=0; i<128; i++){ /* 64 times Over-Sampling Loop */
|
|
|
+ for(j=0;j<32; j++){ /* TimeOut Loop */
|
|
|
+ if(j>30) return ADCtimeout; /* if TimeOut occurred end funktion */
|
|
|
+ if(ADC_1.ISR.B.ECH == 1) break; /* if 1st chain to complete then end TimeOut Loop */
|
|
|
+ }
|
|
|
+ U12work += ADC_1.CDR[0].B.CDATA; /* Read ADC1 conversion result data */
|
|
|
+ U33work += ADC_1.CDR[1].B.CDATA; /* Read ADC1 conversion result data */
|
|
|
+ }
|
|
|
+ ADC_1.MCR.B.NSTART = 0; /* Stop ADC1 */
|
|
|
+
|
|
|
+ *U12 = (uint16_t)((1977 * U12work) >> 16); /* U12=12000mV, Q=12Bit, R1=91k, R2=24k */
|
|
|
+ *U33 = (uint16_t)(( 551 * U33work) >> 16); /* U33=3300mV, Q=12Bit, R1=18k, R2=56k */
|
|
|
+
|
|
|
+ if(*U12 < U12under) ret = Under12V;
|
|
|
+ if(*U12 > U12over) ret = Over12V;
|
|
|
+ if(*U33 < U33under) ret = Under3_3V;
|
|
|
+ if(*U33 > U33over) ret = Over3_3V;
|
|
|
+
|
|
|
+ return ret;
|
|
|
+}
|