ADC_1.c 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // ----------------------------------------------------------------------------
  2. // ADC_1.c
  3. // ----------------------------------------------------------------------------
  4. // Beschreibung: ADC_1 - Interner 12 Bit ADC
  5. //
  6. // Revision: 06. Januar 2017, neu Reiling, IPE
  7. //
  8. // ----------------------------------------------------------------------------
  9. #include "BMS_Master.h"
  10. int8_t ADC_1_init(void)
  11. {
  12. int8_t ret = 0;
  13. SIU.PCR[20].R = 0x2000; /* Initialize PB[4] as ADC1 CH0 */
  14. SIU.PCR[21].R = 0x2000; /* Initialize PB[5] as ADC1 CH1 */
  15. SIU.PCR[22].R = 0x2000; /* Initialize PB[6] as ADC1 CH2 */
  16. SIU.PCR[23].R = 0x2000; /* Initialize PB[7] as ADC1 CH3 */
  17. SIU.PCR[48].R = 0x2000; /* Initialize PD[0] as ADC1 CH4 */
  18. SIU.PCR[49].R = 0x2000; /* Initialize PD[1] as ADC1 CH5 */
  19. SIU.PCR[50].R = 0x2000; /* Initialize PD[2] as ADC1 CH6 */
  20. SIU.PCR[51].R = 0x2000; /* Initialize PD[3] as ADC1 CH7 */
  21. SIU.PCR[52].R = 0x2000; /* Initialize PD[4] as ADC1 CH8 */
  22. SIU.PCR[53].R = 0x2000; /* Initialize PD[5] as ADC1 CH9 */
  23. SIU.PCR[54].R = 0x2000; /* Initialize PD[6] as ADC1 CH10 */
  24. SIU.PCR[55].R = 0x2000; /* Initialize PD[7] as ADC1 CH11 */
  25. ADC_1.MCR.R = 0x20000000; /* Initialize ADC1 for scan mode */
  26. ADC_1.CTR0.R = 0x0000001F; /* Conversion times: INPSAMP=31, INPCMP=4, INPLATCH=0, SHIFT=No */
  27. return ret;
  28. }
  29. int8_t ADC_1_run(uint8_t Channel, uint16_t* Result)
  30. {
  31. uint16_t i, j;
  32. uint32_t Dummy=0;
  33. int8_t ret=NoError;
  34. if( Channel > 11) /* only AIN0 to AIN11 allowed */
  35. ret = ADCwrongCH;
  36. else {
  37. ADC_1.NCMR0.R = 0x001 << Channel; /* Select ADC1 Channel input for conversion */
  38. ADC_1.MCR.B.NSTART = 1; /* Trigger normal conversions for ADC1 */
  39. for(i=0; i<64; i++){ /* 64 times Over-Sampling Loop */
  40. for(j=0;j<32; j++){ /* TimeOut Loop */
  41. if(j>30) return ADCtimeout; /* if TimeOut occurred end funktion */
  42. if(ADC_1.ISR.B.ECH == 1) break; /* if 1st chain to complete then end TimeOut Loop */
  43. }
  44. Dummy += ADC_1.CDR[Channel].B.CDATA; /* Read ADC1 conversion result data */
  45. }
  46. ADC_1.MCR.B.NSTART = 0; /* Stop ADC1 */
  47. *Result = (uint16_t)(Dummy >> 2); /* Result: 16 Bit */
  48. }
  49. return ret;
  50. }
  51. int8_t CheckSupplyVoltages(uint16_t* U12, uint16_t* U33)
  52. {
  53. uint32_t U12work=0, U33work=0;
  54. uint16_t i, j;
  55. int8_t ret=NoError;
  56. ADC_1.NCMR0.R = 0x003; /* Select ADC1 Channel input for conversion */
  57. ADC_1.MCR.B.NSTART = 1; /* Trigger normal conversions for ADC1 */
  58. for(i=0; i<128; i++){ /* 64 times Over-Sampling Loop */
  59. for(j=0;j<32; j++){ /* TimeOut Loop */
  60. if(j>30) return ADCtimeout; /* if TimeOut occurred end funktion */
  61. if(ADC_1.ISR.B.ECH == 1) break; /* if 1st chain to complete then end TimeOut Loop */
  62. }
  63. U12work += ADC_1.CDR[0].B.CDATA; /* Read ADC1 conversion result data */
  64. U33work += ADC_1.CDR[1].B.CDATA; /* Read ADC1 conversion result data */
  65. }
  66. ADC_1.MCR.B.NSTART = 0; /* Stop ADC1 */
  67. *U12 = (uint16_t)((1977 * U12work) >> 16); /* U12=12000mV, Q=12Bit, R1=91k, R2=24k */
  68. *U33 = (uint16_t)(( 551 * U33work) >> 16); /* U33=3300mV, Q=12Bit, R1=18k, R2=56k */
  69. if(*U12 < U12under) ret = Under12V;
  70. if(*U12 > U12over) ret = Over12V;
  71. if(*U33 < U33under) ret = Under3_3V;
  72. if(*U33 > U33over) ret = Over3_3V;
  73. return ret;
  74. }