stm8l15x_adc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. ******************************************************************************
  3. * @file stm8l15x_adc.h
  4. * @author MCD Application Team
  5. * @version V1.6.1
  6. * @date 30-September-2014
  7. * @brief This file contains all the functions prototypes for the ADC
  8. * firmware library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  13. *
  14. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  15. * You may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at:
  17. *
  18. * http://www.st.com/software_license_agreement_liberty_v2
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. *
  26. ******************************************************************************
  27. */
  28. /* Define to prevent recursive inclusion -------------------------------------*/
  29. #ifndef __STM8L15x_ADC_H
  30. #define __STM8L15x_ADC_H
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "stm8l15x.h"
  33. /** @addtogroup STM8L15x_StdPeriph_Driver
  34. * @{
  35. */
  36. /** @addtogroup ADC
  37. * @{
  38. */
  39. /* Exported types ------------------------------------------------------------*/
  40. /** @defgroup ADC_Exported_Types
  41. * @{
  42. */
  43. /** @defgroup ADC_Channels
  44. * @{
  45. */
  46. typedef enum
  47. {
  48. ADC_Channel_0 = ((uint16_t)0x0301), /*!< Channel 00 */
  49. ADC_Channel_1 = ((uint16_t)0x0302), /*!< Channel 01 */
  50. ADC_Channel_2 = ((uint16_t)0x0304), /*!< Channel 02 */
  51. ADC_Channel_3 = ((uint16_t)0x0308), /*!< Channel 03 */
  52. ADC_Channel_4 = ((uint16_t)0x0310), /*!< Channel 04 */
  53. ADC_Channel_5 = ((uint16_t)0x0320), /*!< Channel 05 */
  54. ADC_Channel_6 = ((uint16_t)0x0340), /*!< Channel 06 */
  55. ADC_Channel_7 = ((uint16_t)0x0380), /*!< Channel 07 */
  56. ADC_Channel_8 = ((uint16_t)0x0201), /*!< Channel 08 */
  57. ADC_Channel_9 = ((uint16_t)0x0202), /*!< Channel 09 */
  58. ADC_Channel_10 = ((uint16_t)0x0204), /*!< Channel 10 */
  59. ADC_Channel_11 = ((uint16_t)0x0208), /*!< Channel 11 */
  60. ADC_Channel_12 = ((uint16_t)0x0210), /*!< Channel 12 */
  61. ADC_Channel_13 = ((uint16_t)0x0220), /*!< Channel 13 */
  62. ADC_Channel_14 = ((uint16_t)0x0240), /*!< Channel 14 */
  63. ADC_Channel_15 = ((uint16_t)0x0280), /*!< Channel 15 */
  64. ADC_Channel_16 = ((uint16_t)0x0101), /*!< Channel 16 */
  65. ADC_Channel_17 = ((uint16_t)0x0102), /*!< Channel 17 */
  66. ADC_Channel_18 = ((uint16_t)0x0104), /*!< Channel 18 */
  67. ADC_Channel_19 = ((uint16_t)0x0108), /*!< Channel 19 */
  68. ADC_Channel_20 = ((uint16_t)0x0110), /*!< Channel 20 */
  69. ADC_Channel_21 = ((uint16_t)0x0120), /*!< Channel 21 */
  70. ADC_Channel_22 = ((uint16_t)0x0140), /*!< Channel 22 */
  71. ADC_Channel_23 = ((uint16_t)0x0180), /*!< Channel 23 */
  72. ADC_Channel_24 = ((uint16_t)0x0001), /*!< Channel 24 */
  73. ADC_Channel_25 = ((uint16_t)0x0002), /*!< Channel 25 */
  74. ADC_Channel_26 = ((uint16_t)0x0004), /*!< Channel 26 */
  75. ADC_Channel_27 = ((uint16_t)0x0008), /*!< Channel 27 */
  76. ADC_Channel_Vrefint = ((uint16_t)0x0010), /*!< Vrefint Channel */
  77. ADC_Channel_TempSensor = ((uint16_t)0x0020), /*!< Temperature sensor Channel */
  78. /* combination*/
  79. ADC_Channel_00To07 = ((uint16_t)0x03FF), /*!<select from channel00 to channel07 */
  80. ADC_Channel_08To15 = ((uint16_t)0x02FF), /*!<select from channel08 to channel15 */
  81. ADC_Channel_16To23 = ((uint16_t)0x01FF), /*!<select from channel16 to channel23 */
  82. ADC_Channel_24To27 = ((uint16_t)0x00FF) /*!<select from channel24 to channel27 */
  83. }ADC_Channel_TypeDef;
  84. /**
  85. * @}
  86. */
  87. /** @defgroup ADC_Conversion_Mode
  88. * @{
  89. */
  90. typedef enum
  91. {
  92. ADC_ConversionMode_Single = ((uint8_t)0x00), /*!< Single Conversion Mode */
  93. ADC_ConversionMode_Continuous = ((uint8_t)0x04) /*!< Continuous Conversion Mode */
  94. }ADC_ConversionMode_TypeDef;
  95. #define IS_ADC_CONVERSION_MODE(MODE) (((MODE) == ADC_ConversionMode_Single) || \
  96. ((MODE) == ADC_ConversionMode_Continuous))
  97. /**
  98. * @}
  99. */
  100. /** @defgroup ADC_Resolution
  101. * @{
  102. */
  103. typedef enum
  104. {
  105. ADC_Resolution_12Bit = ((uint8_t)0x00), /*!< 12 bit resolution */
  106. ADC_Resolution_10Bit = ((uint8_t)0x20), /*!< 10 bit resolution */
  107. ADC_Resolution_8Bit = ((uint8_t)0x40), /*!< 8 bit resolution */
  108. ADC_Resolution_6Bit = ((uint8_t)0x60) /*!< 6 bit resolution */
  109. }ADC_Resolution_TypeDef;
  110. #define IS_ADC_RESOLUTION(RESOLUTION) (((RESOLUTION) == ADC_Resolution_12Bit) || \
  111. ((RESOLUTION) == ADC_Resolution_10Bit) || \
  112. ((RESOLUTION) == ADC_Resolution_8Bit) || \
  113. ((RESOLUTION) == ADC_Resolution_6Bit))
  114. /**
  115. * @}
  116. */
  117. /** @defgroup ADC_Clock_Prescaler
  118. * @{
  119. */
  120. typedef enum
  121. {
  122. ADC_Prescaler_1 = ((uint8_t)0x00), /*!< ADC Clock frequency is divided by 1 */
  123. ADC_Prescaler_2 = ((uint8_t)0x80) /*!< ADC Clock frequency is divided by 2 */
  124. }ADC_Prescaler_TypeDef;
  125. #define IS_ADC_PRESCALER(PRESCALER) (((PRESCALER) == ADC_Prescaler_1) || \
  126. ((PRESCALER) == ADC_Prescaler_2))
  127. /**
  128. * @}
  129. */
  130. /** @defgroup ADC_External_Trigger_Sensitivity
  131. * @{
  132. */
  133. typedef enum
  134. {
  135. ADC_ExtTRGSensitivity_Rising = ((uint8_t)0x20), /*!< External Trigger Sensitivity is Rising Edge */
  136. ADC_ExtTRGSensitivity_Falling = ((uint8_t)0x40), /*!< External Trigger Sensitivity is Falling Edge */
  137. ADC_ExtTRGSensitivity_All = ((uint8_t)0x60) /*!< External Trigger Sensitivity is Falling and Rising Edge */
  138. }ADC_ExtTRGSensitivity_TypeDef;
  139. #define IS_ADC_EXT_TRG_SENSITIVITY(SENSITIVITY) (((SENSITIVITY) == ADC_ExtTRGSensitivity_Rising) || \
  140. ((SENSITIVITY) == ADC_ExtTRGSensitivity_Falling) || \
  141. ((SENSITIVITY) == ADC_ExtTRGSensitivity_All))
  142. /**
  143. * @}
  144. */
  145. /** @defgroup ADC_External_Event_Source_Selection
  146. * @{
  147. */
  148. typedef enum
  149. {
  150. ADC_ExtEventSelection_None = ((uint8_t)0x00), /*!< Conversion starts only by software start */
  151. ADC_ExtEventSelection_Trigger1 = ((uint8_t)0x08), /*!< Trigger 1 Enables conversion */
  152. ADC_ExtEventSelection_Trigger2 = ((uint8_t)0x10), /*!< Trigger 2 Enables conversion */
  153. ADC_ExtEventSelection_Trigger3 = ((uint8_t)0x18) /*!< Trigger 3 Enables conversion */
  154. }ADC_ExtEventSelection_TypeDef;
  155. #define IS_ADC_EXT_EVENT_SELECTION(SELECTION) (((SELECTION) == ADC_ExtEventSelection_None) || \
  156. ((SELECTION) == ADC_ExtEventSelection_Trigger1) || \
  157. ((SELECTION) == ADC_ExtEventSelection_Trigger2) || \
  158. ((SELECTION) == ADC_ExtEventSelection_Trigger3))
  159. /**
  160. * @}
  161. */
  162. /** @defgroup ADC_Group_Channel_Definition
  163. * @{
  164. */
  165. typedef enum
  166. {
  167. ADC_Group_SlowChannels = ((uint8_t)0x00), /*!<Slow Channels group(Channel 0 to 23) */
  168. ADC_Group_FastChannels = ((uint8_t)0x01) /*!<Fast Channels group Channel 24 to 27,
  169. Channel Vrefint, Channel TempSensor)*/
  170. }ADC_Group_TypeDef;
  171. #define IS_ADC_GROUP(GROUP) (((GROUP) == ADC_Group_SlowChannels) || \
  172. ((GROUP) == ADC_Group_FastChannels))
  173. /**
  174. * @}
  175. */
  176. /** @defgroup ADC_Sampling_Time
  177. * @{
  178. */
  179. typedef enum
  180. {
  181. ADC_SamplingTime_4Cycles = ((uint8_t)0x00), /*!< Sampling Time Cycles is 4 */
  182. ADC_SamplingTime_9Cycles = ((uint8_t)0x01), /*!< Sampling Time Cycles is 9 */
  183. ADC_SamplingTime_16Cycles = ((uint8_t)0x02), /*!< Sampling Time Cycles is 16 */
  184. ADC_SamplingTime_24Cycles = ((uint8_t)0x03), /*!< Sampling Time Cycles is 24 */
  185. ADC_SamplingTime_48Cycles = ((uint8_t)0x04), /*!< Sampling Time Cycles is 48 */
  186. ADC_SamplingTime_96Cycles = ((uint8_t)0x05), /*!< Sampling Time Cycles is 96 */
  187. ADC_SamplingTime_192Cycles = ((uint8_t)0x06), /*!< Sampling Time Cycles is 192 */
  188. ADC_SamplingTime_384Cycles = ((uint8_t)0x07) /*!< Sampling Time Cycles is 384 */
  189. }ADC_SamplingTime_TypeDef;
  190. #define IS_ADC_SAMPLING_TIME_CYCLES(TIME) (((TIME) == ADC_SamplingTime_4Cycles) || \
  191. ((TIME) == ADC_SamplingTime_9Cycles) || \
  192. ((TIME) == ADC_SamplingTime_16Cycles) || \
  193. ((TIME) == ADC_SamplingTime_24Cycles) || \
  194. ((TIME) == ADC_SamplingTime_48Cycles) || \
  195. ((TIME) == ADC_SamplingTime_96Cycles) || \
  196. ((TIME) == ADC_SamplingTime_192Cycles) || \
  197. ((TIME) == ADC_SamplingTime_384Cycles))
  198. /**
  199. * @}
  200. */
  201. /** @defgroup ADC_Analog_Watchdog_Channel_selection
  202. * @{
  203. */
  204. typedef enum
  205. {
  206. ADC_AnalogWatchdogSelection_Channel0 = ((uint8_t)0x00), /*!< AWD affected to Channel 0 */
  207. ADC_AnalogWatchdogSelection_Channel1 = ((uint8_t)0x01), /*!< AWD affected to Channel 1 */
  208. ADC_AnalogWatchdogSelection_Channel2 = ((uint8_t)0x02), /*!< AWD affected to Channel 2 */
  209. ADC_AnalogWatchdogSelection_Channel3 = ((uint8_t)0x03), /*!< AWD affected to Channel 3 */
  210. ADC_AnalogWatchdogSelection_Channel4 = ((uint8_t)0x04), /*!< AWD affected to Channel 4 */
  211. ADC_AnalogWatchdogSelection_Channel5 = ((uint8_t)0x05), /*!< AWD affected to Channel 5 */
  212. ADC_AnalogWatchdogSelection_Channel6 = ((uint8_t)0x06), /*!< AWD affected to Channel 6 */
  213. ADC_AnalogWatchdogSelection_Channel7 = ((uint8_t)0x07), /*!< AWD affected to Channel 7 */
  214. ADC_AnalogWatchdogSelection_Channel8 = ((uint8_t)0x08), /*!< AWD affected to Channel 8 */
  215. ADC_AnalogWatchdogSelection_Channel9 = ((uint8_t)0x09), /*!< AWD affected to Channel 9 */
  216. ADC_AnalogWatchdogSelection_Channel10 = ((uint8_t)0x0A), /*!< AWD affected to Channel 10 */
  217. ADC_AnalogWatchdogSelection_Channel11 = ((uint8_t)0x0B), /*!< AWD affected to Channel 11 */
  218. ADC_AnalogWatchdogSelection_Channel12 = ((uint8_t)0x0C), /*!< AWD affected to Channel 12 */
  219. ADC_AnalogWatchdogSelection_Channel13 = ((uint8_t)0x0D), /*!< AWD affected to Channel 13 */
  220. ADC_AnalogWatchdogSelection_Channel14 = ((uint8_t)0x0E), /*!< AWD affected to Channel 14 */
  221. ADC_AnalogWatchdogSelection_Channel15 = ((uint8_t)0x0F), /*!< AWD affected to Channel 15 */
  222. ADC_AnalogWatchdogSelection_Channel16 = ((uint8_t)0x10), /*!< AWD affected to Channel 16 */
  223. ADC_AnalogWatchdogSelection_Channel17 = ((uint8_t)0x11), /*!< AWD affected to Channel 17 */
  224. ADC_AnalogWatchdogSelection_Channel18 = ((uint8_t)0x12), /*!< AWD affected to Channel 18 */
  225. ADC_AnalogWatchdogSelection_Channel19 = ((uint8_t)0x13), /*!< AWD affected to Channel 19 */
  226. ADC_AnalogWatchdogSelection_Channel20 = ((uint8_t)0x14), /*!< AWD affected to Channel 20 */
  227. ADC_AnalogWatchdogSelection_Channel21 = ((uint8_t)0x15), /*!< AWD affected to Channel 21 */
  228. ADC_AnalogWatchdogSelection_Channel22 = ((uint8_t)0x16), /*!< AWD affected to Channel 22 */
  229. ADC_AnalogWatchdogSelection_Channel23 = ((uint8_t)0x17), /*!< AWD affected to Channel 23 */
  230. ADC_AnalogWatchdogSelection_Channel24 = ((uint8_t)0x18), /*!< AWD affected to Channel 24 */
  231. ADC_AnalogWatchdogSelection_Channel25 = ((uint8_t)0x19), /*!< AWD affected to Channel 25 */
  232. ADC_AnalogWatchdogSelection_Channel26 = ((uint8_t)0x1A), /*!< AWD affected to Channel 26 */
  233. ADC_AnalogWatchdogSelection_Channel27 = ((uint8_t)0x1B), /*!< AWD affected to Channel 27 */
  234. ADC_AnalogWatchdogSelection_Vrefint = ((uint8_t)0x1C), /*!< AWD affected to Internal Vref Channel */
  235. ADC_AnalogWatchdogSelection_TempSensor = ((uint8_t)0x1D) /*!< AWD affected to Temperature Sensor Channel */
  236. }ADC_AnalogWatchdogSelection_TypeDef;
  237. #define IS_ADC_ANALOGWATCHDOG_SELECTION(CHANNEL) (((CHANNEL) <= 0x1D))
  238. /**
  239. * @}
  240. */
  241. /** @defgroup ADC_Interrupts
  242. * @{
  243. */
  244. typedef enum
  245. {
  246. ADC_IT_EOC = ((uint8_t)0x08), /*!< End of Conversion Interrupt */
  247. ADC_IT_AWD = ((uint8_t)0x10), /*!< Analog Watchdog Interrupt */
  248. ADC_IT_OVER = ((uint8_t)0x80) /*!< Over Run Interrupt */
  249. }ADC_IT_TypeDef;
  250. #define IS_ADC_IT(IT) ((((IT) & (uint8_t)0x67) == 0x00) && ((IT) != 0x00))
  251. #define IS_ADC_GET_IT(IT) (((IT) == ADC_IT_EOC) || ((IT) == ADC_IT_AWD) || \
  252. ((IT) == ADC_IT_OVER))
  253. /**
  254. * @}
  255. */
  256. /** @defgroup ADC_Flags
  257. * @{
  258. */
  259. typedef enum
  260. {
  261. ADC_FLAG_EOC = ((uint8_t)0x01), /*!< End of Conversion flag */
  262. ADC_FLAG_AWD = ((uint8_t)0x02), /*!< Analog Watchdog flag */
  263. ADC_FLAG_OVER = ((uint8_t)0x04) /*!< Over Run flag */
  264. }ADC_FLAG_TypeDef;
  265. #define IS_ADC_CLEAR_FLAG(FLAG) ((((FLAG) & (uint8_t)0xF8) == 0x00) && ((FLAG) != 0x00))
  266. #define IS_ADC_GET_FLAG(FLAG) (((FLAG) == ADC_FLAG_EOC) || ((FLAG) == ADC_FLAG_AWD) || \
  267. ((FLAG) == ADC_FLAG_OVER))
  268. /**
  269. * @}
  270. */
  271. /**
  272. * @}
  273. */
  274. /* Exported constants --------------------------------------------------------*/
  275. /* Exported Macros -----------------------------------------------------------*/
  276. /** @defgroup ADC_Exported_Macros
  277. * @{
  278. */
  279. #define IS_ADC_THRESHOLD(THRESHOLD) ((THRESHOLD) <= 0xFFF)
  280. /**
  281. * @}
  282. */
  283. /* Exported functions --------------------------------------------------------*/
  284. /* Function used to set the ADC configuration to the default reset state *****/
  285. void ADC_DeInit(ADC_TypeDef* ADCx);
  286. /* Initialization and Configuration functions *********************************/
  287. void ADC_Init(ADC_TypeDef* ADCx,
  288. ADC_ConversionMode_TypeDef ADC_ConversionMode,
  289. ADC_Resolution_TypeDef ADC_Resolution,
  290. ADC_Prescaler_TypeDef ADC_Prescaler);
  291. void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  292. void ADC_SoftwareStartConv(ADC_TypeDef* ADCx);
  293. void ADC_ExternalTrigConfig(ADC_TypeDef* ADCx,
  294. ADC_ExtEventSelection_TypeDef ADC_ExtEventSelection,
  295. ADC_ExtTRGSensitivity_TypeDef ADC_ExtTRGSensitivity);
  296. /* Analog Watchdog configuration functions ************************************/
  297. void ADC_AnalogWatchdogChannelSelect(ADC_TypeDef* ADCx,
  298. ADC_AnalogWatchdogSelection_TypeDef ADC_AnalogWatchdogSelection);
  299. void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,
  300. uint16_t LowThreshold);
  301. void ADC_AnalogWatchdogConfig(ADC_TypeDef* ADCx,
  302. ADC_AnalogWatchdogSelection_TypeDef ADC_AnalogWatchdogSelection,
  303. uint16_t HighThreshold,
  304. uint16_t LowThreshold);
  305. /* Temperature Sensor & Vrefint (Voltage Reference internal) management functions */
  306. void ADC_TempSensorCmd(FunctionalState NewState);
  307. void ADC_VrefintCmd(FunctionalState NewState);
  308. /* Channels Configuration functions *******************************************/
  309. void ADC_ChannelCmd(ADC_TypeDef* ADCx, ADC_Channel_TypeDef ADC_Channels,
  310. FunctionalState NewState);
  311. void ADC_SamplingTimeConfig(ADC_TypeDef* ADCx, ADC_Group_TypeDef ADC_GroupChannels,
  312. ADC_SamplingTime_TypeDef ADC_SamplingTime);
  313. void ADC_SchmittTriggerConfig(ADC_TypeDef* ADCx, ADC_Channel_TypeDef ADC_Channels,
  314. FunctionalState NewState);
  315. uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx);
  316. /* Channels DMA Configuration function ****************************************/
  317. void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  318. /* Interrupts and flags management functions **********************************/
  319. void ADC_ITConfig(ADC_TypeDef* ADCx,
  320. ADC_IT_TypeDef ADC_IT,
  321. FunctionalState NewState);
  322. FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, ADC_FLAG_TypeDef ADC_FLAG);
  323. void ADC_ClearFlag(ADC_TypeDef* ADCx, ADC_FLAG_TypeDef ADC_FLAG);
  324. ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, ADC_IT_TypeDef ADC_IT);
  325. void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, ADC_IT_TypeDef ADC_IT);
  326. #endif /*__STM8L15x_ADC_H */
  327. /**
  328. * @}
  329. */
  330. /**
  331. * @}
  332. */
  333. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/