lpc_adc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**********************************************************************
  2. * $Id$ lpc_adc.h 2011-06-02
  3. *//**
  4. * @file lpc_adc.h
  5. * @brief Contains all macro definitions and function prototypes
  6. * support for ADC firmware library on LPC
  7. * @version 1.0
  8. * @date 02. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @defgroup ADC ADC (Analog-to-Digital Converter)
  34. * @ingroup LPC_CMSIS_FwLib_Drivers
  35. * @{
  36. */
  37. #ifndef __LPC_ADC_H_
  38. #define __LPC_ADC_H_
  39. /* Includes ------------------------------------------------------------------- */
  40. #include "LPC407x_8x_177x_8x.h"
  41. #include "lpc_types.h"
  42. #ifdef __cplusplus
  43. extern "C"
  44. {
  45. #endif
  46. /* Private macros ------------------------------------------------------------- */
  47. /** @defgroup ADC_Private_Macros ADC Private Macros
  48. * @{
  49. */
  50. /* -------------------------- BIT DEFINITIONS ----------------------------------- */
  51. /*********************************************************************//**
  52. * Macro defines for ADC control register
  53. **********************************************************************/
  54. /** Selects which of the AD0.0:7 pins (channels) is (are) to be sampled and converted */
  55. #define ADC_CR_CH_SEL(n) ((1UL << n))
  56. /** The APB clock (PCLK) is divided by (this value plus one)
  57. * to produce the clock for the A/D */
  58. #define ADC_CR_CLKDIV(n) ((n<<8))
  59. /** Repeated conversions A/D enable bit */
  60. #define ADC_CR_BURST ((1UL<<16))
  61. /** ADC convert in power down mode; if 0, it's in power down mode; if 1, it's in normal
  62. * operation mode */
  63. #define ADC_CR_PDN ((1UL<<21))
  64. /** Start mask bits */
  65. #define ADC_CR_START_MASK ((7UL<<24))
  66. /** Select Start Mode controll the AD Converter in case the Burst bit is 0 (zero) */
  67. #define ADC_CR_START_MODE_SEL(SEL) ((SEL<<24))
  68. /** Start conversion now */
  69. #define ADC_CR_START_NOW ((1UL<<24))
  70. /** Start conversion when the edge selected by bit 27 occurs on P2.10/EINT0 */
  71. #define ADC_CR_START_EINT0 ((2UL<<24))
  72. /** Start conversion when the edge selected by bit 27 occurs on P1.27/CAP0.1 */
  73. #define ADC_CR_START_CAP01 ((3UL<<24))
  74. /** Start conversion when the edge selected by bit 27 occurs on MAT0.1 */
  75. #define ADC_CR_START_MAT01 ((4UL<<24))
  76. /** Start conversion when the edge selected by bit 27 occurs on MAT0.3 */
  77. #define ADC_CR_START_MAT03 ((5UL<<24))
  78. /** Start conversion when the edge selected by bit 27 occurs on MAT1.0 */
  79. #define ADC_CR_START_MAT10 ((6UL<<24))
  80. /** Start conversion when the edge selected by bit 27 occurs on MAT1.1 */
  81. #define ADC_CR_START_MAT11 ((7UL<<24))
  82. /** Start conversion on a falling edge on the selected CAP/MAT signal */
  83. #define ADC_CR_EDGE ((1UL<<27))
  84. /*********************************************************************//**
  85. * Macro defines for ADC Global Data register
  86. **********************************************************************/
  87. /** When DONE is 1, this field contains result value of ADC conversion
  88. * (in 12-bit value) */
  89. #define ADC_GDR_RESULT(n) (((n>>4)&0xFFF))
  90. /** These bits contain the channel from which the LS bits were converted */
  91. #define ADC_GDR_CH(n) (((n>>24)&0x7))
  92. /** This bits is used to mask for Channel */
  93. #define ADC_GDR_CH_MASK ((7UL<<24))
  94. /** This bit is 1 in burst mode if the results of one or
  95. * more conversions was (were) lost */
  96. #define ADC_GDR_OVERRUN_FLAG ((1UL<<30))
  97. /** This bit is set to 1 when an A/D conversion completes */
  98. #define ADC_GDR_DONE_FLAG ((1UL<<31))
  99. /*********************************************************************//**
  100. * Macro defines for ADC Interrupt register
  101. **********************************************************************/
  102. /** These bits allow control over which A/D channels generate
  103. * interrupts for conversion completion */
  104. #define ADC_INTEN_CH(n) ((1UL<<n))
  105. /** When 1, enables the global DONE flag in ADDR to generate an interrupt */
  106. #define ADC_INTEN_GLOBAL ((1UL<<8))
  107. /*********************************************************************//**
  108. * Macro defines for ADC Data register
  109. **********************************************************************/
  110. /** When DONE is 1, this field contains result value of ADC conversion */
  111. #define ADC_DR_RESULT(n) (((n>>4)&0xFFF))
  112. /** These bits mirror the OVERRRUN status flags that appear in the
  113. * result register for each A/D channel */
  114. #define ADC_DR_OVERRUN_FLAG ((1UL<<30))
  115. /** This bit is set to 1 when an A/D conversion completes. It is cleared
  116. * when this register is read */
  117. #define ADC_DR_DONE_FLAG ((1UL<<31))
  118. /*********************************************************************//**
  119. * Macro defines for ADC Status register
  120. **********************************************************************/
  121. /** These bits mirror the DONE status flags that appear in the result
  122. * register for each A/D channel */
  123. #define ADC_STAT_CH_DONE_FLAG(n) ((n&0xFF))
  124. /** These bits mirror the OVERRRUN status flags that appear in the
  125. * result register for each A/D channel */
  126. #define ADC_STAT_CH_OVERRUN_FLAG(n) (((n>>8)&0xFF))
  127. /** This bit is the A/D interrupt flag */
  128. #define ADC_STAT_INT_FLAG ((1UL<<16))
  129. /*********************************************************************//**
  130. * Macro defines for ADC Trim register
  131. **********************************************************************/
  132. /** Offset trim bits for ADC operation */
  133. #define ADC_ADCOFFS(n) (((n&0xF)<<4))
  134. /** Written to boot code*/
  135. #define ADC_TRIM(n) (((n&0xF)<<8))
  136. /**
  137. * @}
  138. */
  139. /* Public Types --------------------------------------------------------------- */
  140. /** @defgroup ADC_Public_Types ADC Public Types
  141. * @{
  142. */
  143. /*********************************************************************//**
  144. * @brief ADC enumeration
  145. **********************************************************************/
  146. /** @brief Channel Selection */
  147. typedef enum
  148. {
  149. ADC_CHANNEL_0 = 0, /*!< Channel 0 */
  150. ADC_CHANNEL_1, /*!< Channel 1 */
  151. ADC_CHANNEL_2, /*!< Channel 2 */
  152. ADC_CHANNEL_3, /*!< Channel 3 */
  153. ADC_CHANNEL_4, /*!< Channel 4 */
  154. ADC_CHANNEL_5, /*!< Channel 5 */
  155. ADC_CHANNEL_6, /*!< Channel 6 */
  156. ADC_CHANNEL_7 /*!< Channel 7 */
  157. }ADC_CHANNEL_SELECTION;
  158. /** @brief Type of start option */
  159. typedef enum
  160. {
  161. ADC_START_CONTINUOUS = 0, /*!< Continuous mode */
  162. ADC_START_NOW, /*!< Start conversion now */
  163. ADC_START_ON_EINT0, /*!< Start conversion when the edge selected
  164. * by bit 27 occurs on P2.10/EINT0 */
  165. ADC_START_ON_CAP01, /*!< Start conversion when the edge selected
  166. * by bit 27 occurs on P1.27/CAP0.1 */
  167. ADC_START_ON_MAT01, /*!< Start conversion when the edge selected
  168. * by bit 27 occurs on MAT0.1 */
  169. ADC_START_ON_MAT03, /*!< Start conversion when the edge selected
  170. * by bit 27 occurs on MAT0.3 */
  171. ADC_START_ON_MAT10, /*!< Start conversion when the edge selected
  172. * by bit 27 occurs on MAT1.0 */
  173. ADC_START_ON_MAT11 /*!< Start conversion when the edge selected
  174. * by bit 27 occurs on MAT1.1 */
  175. } ADC_START_OPT;
  176. /** @brief Type of edge when start conversion on the selected CAP/MAT signal */
  177. typedef enum
  178. {
  179. ADC_START_ON_RISING = 0, /*!< Start conversion on a rising edge
  180. *on the selected CAP/MAT signal */
  181. ADC_START_ON_FALLING /*!< Start conversion on a falling edge
  182. *on the selected CAP/MAT signal */
  183. } ADC_START_ON_EDGE_OPT;
  184. /** @brief* ADC type interrupt enum */
  185. typedef enum
  186. {
  187. ADC_ADINTEN0 = 0, /*!< Interrupt channel 0 */
  188. ADC_ADINTEN1, /*!< Interrupt channel 1 */
  189. ADC_ADINTEN2, /*!< Interrupt channel 2 */
  190. ADC_ADINTEN3, /*!< Interrupt channel 3 */
  191. ADC_ADINTEN4, /*!< Interrupt channel 4 */
  192. ADC_ADINTEN5, /*!< Interrupt channel 5 */
  193. ADC_ADINTEN6, /*!< Interrupt channel 6 */
  194. ADC_ADINTEN7, /*!< Interrupt channel 7 */
  195. ADC_ADGINTEN /*!< Individual channel/global flag done generate an interrupt */
  196. }ADC_TYPE_INT_OPT;
  197. /** @brief ADC Data status */
  198. typedef enum
  199. {
  200. ADC_DATA_BURST = 0, /*Burst bit*/
  201. ADC_DATA_DONE /*Done bit*/
  202. }ADC_DATA_STATUS;
  203. /**
  204. * @}
  205. */
  206. /* Public Functions ----------------------------------------------------------- */
  207. /** @defgroup ADC_Public_Functions ADC Public Functions
  208. * @{
  209. */
  210. /* Init/DeInit ADC peripheral ----------------*/
  211. void ADC_Init(LPC_ADC_TypeDef *ADCx, uint32_t rate);
  212. void ADC_DeInit(LPC_ADC_TypeDef *ADCx);
  213. /* Enable/Disable ADC functions --------------*/
  214. void ADC_BurstCmd(LPC_ADC_TypeDef *ADCx, FunctionalState NewState);
  215. void ADC_PowerdownCmd(LPC_ADC_TypeDef *ADCx, FunctionalState NewState);
  216. void ADC_StartCmd(LPC_ADC_TypeDef *ADCx, uint8_t start_mode);
  217. void ADC_ChannelCmd (LPC_ADC_TypeDef *ADCx, uint8_t Channel, FunctionalState NewState);
  218. /* Configure ADC functions -------------------*/
  219. void ADC_EdgeStartConfig(LPC_ADC_TypeDef *ADCx, uint8_t EdgeOption);
  220. void ADC_IntConfig (LPC_ADC_TypeDef *ADCx, ADC_TYPE_INT_OPT IntType, FunctionalState NewState);
  221. /* Get ADC information functions -------------------*/
  222. uint16_t ADC_ChannelGetData(LPC_ADC_TypeDef *ADCx, uint8_t channel);
  223. FlagStatus ADC_ChannelGetStatus(LPC_ADC_TypeDef *ADCx, uint8_t channel, uint32_t StatusType);
  224. uint32_t ADC_GlobalGetData(LPC_ADC_TypeDef *ADCx);
  225. FlagStatus ADC_GlobalGetStatus(LPC_ADC_TypeDef *ADCx, uint32_t StatusType);
  226. /**
  227. * @}
  228. */
  229. #ifdef __cplusplus
  230. }
  231. #endif
  232. #endif /* LPC_ADC_H_ */
  233. /**
  234. * @}
  235. */
  236. /* --------------------------------- End Of File ------------------------------ */