adc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  2. {
  3. /* Check the parameters */
  4. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  5. assert_param(IS_FUNCTIONAL_STATE(NewState));
  6. if (NewState != DISABLE)
  7. {
  8. /* Set the ADON bit to wake up the ADC from power down mode */
  9. ADCx->CR2 |= CR2_ADON_Set;
  10. }
  11. else
  12. {
  13. /* Disable the selected ADC peripheral */
  14. ADCx->CR2 &= CR2_ADON_Reset;
  15. }
  16. }
  17. void ADC_ResetCalibration(ADC_TypeDef* ADCx)
  18. {
  19. /* Check the parameters */
  20. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  21. /* Resets the selected ADC calibration registers */
  22. ADCx->CR2 |= CR2_RSTCAL_Set;
  23. }
  24. FlagStatus ADC_GetResetCalibrationStatus(ADC_TypeDef* ADCx)
  25. {
  26. FlagStatus bitstatus = RESET;
  27. /* Check the parameters */
  28. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  29. /* Check the status of RSTCAL bit */
  30. if ((ADCx->CR2 & CR2_RSTCAL_Set) != (uint32_t)RESET)
  31. {
  32. /* RSTCAL bit is set */
  33. bitstatus = SET;
  34. }
  35. else
  36. {
  37. /* RSTCAL bit is reset */
  38. bitstatus = RESET;
  39. }
  40. /* Return the RSTCAL bit status */
  41. return bitstatus;
  42. }
  43. void ADC_StartCalibration(ADC_TypeDef* ADCx)
  44. {
  45. /* Check the parameters */
  46. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  47. /* Enable the selected ADC calibration process */
  48. ADCx->CR2 |= CR2_CAL_Set;
  49. }
  50. FlagStatus ADC_GetCalibrationStatus(ADC_TypeDef* ADCx)
  51. {
  52. FlagStatus bitstatus = RESET;
  53. /* Check the parameters */
  54. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  55. /* Check the status of CAL bit */
  56. if ((ADCx->CR2 & CR2_CAL_Set) != (uint32_t)RESET)
  57. {
  58. /* CAL bit is set: calibration on going */
  59. bitstatus = SET;
  60. }
  61. else
  62. {
  63. /* CAL bit is reset: end of calibration */
  64. bitstatus = RESET;
  65. }
  66. /* Return the CAL bit status */
  67. return bitstatus;
  68. }
  69. void ADC_SoftwareStartConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  70. {
  71. /* Check the parameters */
  72. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  73. assert_param(IS_FUNCTIONAL_STATE(NewState));
  74. if (NewState != DISABLE)
  75. {
  76. /* Enable the selected ADC conversion on external event and start the selected
  77. ADC conversion */
  78. ADCx->CR2 |= CR2_EXTTRIG_SWSTART_Set;
  79. }
  80. else
  81. {
  82. /* Disable the selected ADC conversion on external event and stop the selected
  83. ADC conversion */
  84. ADCx->CR2 &= CR2_EXTTRIG_SWSTART_Reset;
  85. }
  86. }
  87. void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)
  88. {
  89. uint32_t tmpreg1 = 0;
  90. uint8_t tmpreg2 = 0;
  91. /* Check the parameters */
  92. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  93. assert_param(IS_ADC_MODE(ADC_InitStruct->ADC_Mode));
  94. assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ScanConvMode));
  95. assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode));
  96. assert_param(IS_ADC_EXT_TRIG(ADC_InitStruct->ADC_ExternalTrigConv));
  97. assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign));
  98. assert_param(IS_ADC_REGULAR_LENGTH(ADC_InitStruct->ADC_NbrOfChannel));
  99. /*---------------------------- ADCx CR1 Configuration -----------------*/
  100. /* Get the ADCx CR1 value */
  101. tmpreg1 = ADCx->CR1;
  102. /* Clear DUALMOD and SCAN bits */
  103. tmpreg1 &= CR1_CLEAR_Mask;
  104. /* Configure ADCx: Dual mode and scan conversion mode */
  105. /* Set DUALMOD bits according to ADC_Mode value */
  106. /* Set SCAN bit according to ADC_ScanConvMode value */
  107. tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_Mode | ((uint32_t)ADC_InitStruct->ADC_ScanConvMode << 8));
  108. /* Write to ADCx CR1 */
  109. ADCx->CR1 = tmpreg1;
  110. /*---------------------------- ADCx CR2 Configuration -----------------*/
  111. /* Get the ADCx CR2 value */
  112. tmpreg1 = ADCx->CR2;
  113. /* Clear CONT, ALIGN and EXTSEL bits */
  114. tmpreg1 &= CR2_CLEAR_Mask;
  115. /* Configure ADCx: external trigger event and continuous conversion mode */
  116. /* Set ALIGN bit according to ADC_DataAlign value */
  117. /* Set EXTSEL bits according to ADC_ExternalTrigConv value */
  118. /* Set CONT bit according to ADC_ContinuousConvMode value */
  119. tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_DataAlign | ADC_InitStruct->ADC_ExternalTrigConv |
  120. ((uint32_t)ADC_InitStruct->ADC_ContinuousConvMode << 1));
  121. /* Write to ADCx CR2 */
  122. ADCx->CR2 = tmpreg1;
  123. /*---------------------------- ADCx SQR1 Configuration -----------------*/
  124. /* Get the ADCx SQR1 value */
  125. tmpreg1 = ADCx->SQR1;
  126. /* Clear L bits */
  127. tmpreg1 &= SQR1_CLEAR_Mask;
  128. /* Configure ADCx: regular channel sequence length */
  129. /* Set L bits according to ADC_NbrOfChannel value */
  130. tmpreg2 |= (uint8_t) (ADC_InitStruct->ADC_NbrOfChannel - (uint8_t)1);
  131. tmpreg1 |= (uint32_t)tmpreg2 << 20;
  132. /* Write to ADCx SQR1 */
  133. ADCx->SQR1 = tmpreg1;
  134. }
  135. uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
  136. {
  137. /* Check the parameters */
  138. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  139. /* Return the selected ADC conversion value */
  140. return (uint16_t) ADCx->DR;
  141. }