lpc_dac.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**********************************************************************
  2. * $Id$ lpc_dac.c 2011-06-02
  3. *//**
  4. * @file lpc_dac.c
  5. * @brief Contains all functions support for DAC firmware library on
  6. * 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. /** @addtogroup DAC
  34. * @{
  35. */
  36. #ifdef __BUILD_WITH_EXAMPLE__
  37. #include "lpc_libcfg.h"
  38. #else
  39. #include "lpc_libcfg_default.h"
  40. #endif /* __BUILD_WITH_EXAMPLE__ */
  41. #ifdef _DAC
  42. /* Includes ------------------------------------------------------------------- */
  43. #include "lpc_dac.h"
  44. #include "lpc_clkpwr.h"
  45. #include "lpc_pinsel.h"
  46. /* Private Functions ---------------------------------------------------------- */
  47. /*********************************************************************//**
  48. * @brief Get pointer to DAC peripheral
  49. * @param[in] compId Component ID, normally is zero (0).
  50. * @param[in] pinnum Pin number value, should be in range from 0..31
  51. * @return Pointer to DAC peripheral component
  52. **********************************************************************/
  53. static LPC_DAC_TypeDef * DAC_GetPointer(uint8_t compId)
  54. {
  55. LPC_DAC_TypeDef *pComponent = LPC_DAC;
  56. return pComponent;
  57. }
  58. /* Public Functions ----------------------------------------------------------- */
  59. /** @addtogroup DAC_Public_Functions
  60. * @{
  61. */
  62. /*********************************************************************//**
  63. * @brief Initial ADC configuration
  64. * - Maximum current is 700 uA
  65. * - Value to AOUT is 0
  66. * @param[in] DAC_Id the ID of the DAC component that is using, should be: zero (0)
  67. * @return None
  68. ***********************************************************************/
  69. void DAC_Init(uint8_t DAC_Id)
  70. {
  71. /*
  72. * Init DAC pin connect
  73. * AOUT on P0.26
  74. */
  75. PINSEL_ConfigPin(0, 26, 2);
  76. PINSEL_SetAnalogPinMode(0,26,ENABLE);
  77. //Enable DAC for the pin
  78. PINSEL_DacEnable(0, 26, ENABLE);
  79. //Set maximum current output as default
  80. DAC_SetBias(DAC_Id, DAC_MAX_CURRENT_700uA);
  81. }
  82. /*********************************************************************//**
  83. * @brief Update value to DAC
  84. * @param[in] DAC_Id the ID of the DAC component that is using, should be: zero (0)
  85. * @param[in] dac_value : value 10 bit to be converted to output
  86. * @return None
  87. ***********************************************************************/
  88. void DAC_UpdateValue (uint8_t DAC_Id,uint32_t dac_value)
  89. {
  90. uint32_t tmp;
  91. LPC_DAC_TypeDef* pDac = DAC_GetPointer(DAC_Id);
  92. tmp = pDac->CR & DAC_BIAS_EN;
  93. tmp |= DAC_VALUE(dac_value);
  94. // Update value
  95. pDac->CR = tmp;
  96. }
  97. /*********************************************************************//**
  98. * @brief Set Maximum current for DAC
  99. * @param[in] DAC_Id the ID of the DAC component that is using, should be: zero (0)
  100. * @param[in] bias : 0 is 700 uA
  101. * 1 350 uA
  102. * @return None
  103. ***********************************************************************/
  104. void DAC_SetBias (uint8_t DAC_Id, uint32_t bias)
  105. {
  106. LPC_DAC_TypeDef* pDac = DAC_GetPointer(DAC_Id);
  107. pDac->CR &=~DAC_BIAS_EN;
  108. if (bias == DAC_MAX_CURRENT_350uA)
  109. {
  110. pDac->CR |= DAC_BIAS_EN;
  111. }
  112. }
  113. /*********************************************************************//**
  114. * @brief To enable the DMA operation and control DMA timer
  115. * @param[in] DAC_Id the ID of the DAC component that is using, should be: zero (0)
  116. * @param[in] DAC_ConverterConfigStruct pointer to DAC_CONVERTER_CFG_Type
  117. * - DBLBUF_ENA : enable/disable DACR double buffering feature
  118. * - CNT_ENA : enable/disable timer out counter
  119. * - DMA_ENA : enable/disable DMA access
  120. * @return None
  121. ***********************************************************************/
  122. void DAC_ConfigDAConverterControl (uint8_t DAC_Id, DAC_CONVERTER_CFG_Type *DAC_ConverterConfigStruct)
  123. {
  124. LPC_DAC_TypeDef* pDac = DAC_GetPointer(DAC_Id);
  125. pDac->CTRL &= ~DAC_DACCTRL_MASK;
  126. if (DAC_ConverterConfigStruct->DBLBUF_ENA)
  127. pDac->CTRL |= DAC_DBLBUF_ENA;
  128. if (DAC_ConverterConfigStruct->CNT_ENA)
  129. pDac->CTRL |= DAC_CNT_ENA;
  130. if (DAC_ConverterConfigStruct->DMA_ENA)
  131. pDac->CTRL |= DAC_DMA_ENA;
  132. }
  133. /*********************************************************************//**
  134. * @brief Set reload value for interrupt/DMA counter
  135. * @param[in] DAC_Id the ID of the DAC component that is using, should be: zero (0)
  136. * @param[in] time_out time out to reload for interrupt/DMA counter
  137. * @return None
  138. ***********************************************************************/
  139. void DAC_SetDMATimeOut(uint8_t DAC_Id, uint32_t time_out)
  140. {
  141. LPC_DAC_TypeDef* pDac = DAC_GetPointer(DAC_Id);
  142. pDac->CNTVAL = DAC_CCNT_VALUE(time_out);
  143. }
  144. /*********************************************************************//**
  145. * @brief Check the interrupt/DMA counter is occured or not because of the timer counter
  146. * @param[in] DAC_Id the ID of the DAC component that is using, should be: zero (0)
  147. * @return None
  148. ***********************************************************************/
  149. uint8_t DAC_IsIntRequested(uint8_t DAC_Id)
  150. {
  151. LPC_DAC_TypeDef* pDac = DAC_GetPointer(DAC_Id);
  152. //Return the INT_DMA_REQ bit of D/A control register
  153. return (pDac->CTRL & 0x01);
  154. }
  155. /**
  156. * @}
  157. */
  158. #endif /*_DAC*/
  159. /**
  160. * @}
  161. */
  162. /* --------------------------------- End Of File ------------------------------ */