adc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. *********************************************************************************************************
  3. *
  4. * 模块名称 : ADC驱动模块
  5. * 文件名称 : adc.c
  6. * 版 本 : V1.0
  7. * 说 明 : ADC3的DMA工作方式
  8. * 修改记录 :
  9. *
  10. *********************************************************************************************************
  11. */
  12. #include "adc.h"
  13. /* define ---------------------------------------------------------------------*/
  14. #define ADC3_DR_ADDRESS ((uint32_t)0x4001224C)
  15. /* 变量 ----------------------------------------------------------------------*/
  16. __IO uint16_t ADC3ConvertedValue[2];
  17. __IO uint32_t ADC3ConvertedVoltage = 0;
  18. void dma_config()
  19. {
  20. DMA_InitTypeDef DMA_InitStructure;
  21. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); // DMA2时钟使能
  22. DMA_DeInit(DMA2_Stream0);
  23. /* DMA2 Stream0 channel2 配置 **************************************/
  24. DMA_InitStructure.DMA_Channel = DMA_Channel_2;
  25. DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;
  26. DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ADC3ConvertedValue;
  27. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  28. DMA_InitStructure.DMA_BufferSize = 2;
  29. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  30. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  31. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  32. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  33. DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  34. DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  35. DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  36. DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  37. DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  38. DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  39. DMA_Init(DMA2_Stream0, &DMA_InitStructure);
  40. DMA_Cmd(DMA2_Stream0, ENABLE);
  41. }
  42. /*
  43. *********************************************************************************************************
  44. * 函 数 名: adc3_init
  45. * 功能说明: ADC初始化
  46. * 形 参: 无
  47. * 返 回 值: 无
  48. *********************************************************************************************************
  49. */
  50. void adc3_init(void)
  51. {
  52. ADC_InitTypeDef ADC_InitStructure;
  53. ADC_CommonInitTypeDef ADC_CommonInitStructure;
  54. DMA_InitTypeDef DMA_InitStructure;
  55. GPIO_InitTypeDef GPIO_InitStructure;
  56. /* 使能 ADC3, DMA2 和 GPIO 时钟 ****************************************/
  57. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_DMA1, ENABLE);
  58. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
  59. /* DMA2 Stream0 channel2 配置 **************************************/
  60. DMA_InitStructure.DMA_Channel = DMA_Channel_2;
  61. DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;
  62. DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ADC3ConvertedValue;
  63. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  64. DMA_InitStructure.DMA_BufferSize = 2;
  65. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  66. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  67. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  68. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  69. DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  70. DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  71. DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  72. DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  73. DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  74. DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  75. DMA_Init(DMA2_Stream0, &DMA_InitStructure);
  76. DMA_Cmd(DMA2_Stream0, ENABLE);
  77. /* 配置ADC3通道7引脚为模拟输入模式******************************/
  78. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  79. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  80. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  81. GPIO_Init(GPIOF, &GPIO_InitStructure);
  82. /****************************************************************************
  83. PCLK2 = HCLK / 2
  84. 下面选择的是2分频
  85. ADCCLK = PCLK2 /2 = HCLK / 4 = 168 / 4 = 42M
  86. ADC采样频率: Sampling Time + Conversion Time = 3 + 12 cycles = 15cyc
  87. Conversion Time = 42MHz / 15cyc = 2.8Mbps.
  88. *****************************************************************************/
  89. /* ADC公共部分初始化**********************************************************/
  90. ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  91. ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  92. ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  93. ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  94. ADC_CommonInit(&ADC_CommonInitStructure);
  95. /* ADC3 初始化 ****************************************************************/
  96. ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  97. ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  98. ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  99. ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  100. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  101. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  102. ADC_InitStructure.ADC_NbrOfConversion = 1;
  103. ADC_Init(ADC3, &ADC_InitStructure);
  104. /* ADC3 规则 channel7 配置 *************************************/
  105. ADC_RegularChannelConfig(ADC3, ADC_Channel_7, 1, ADC_SampleTime_3Cycles);
  106. /* 使能DMA请求(单ADC模式) */
  107. ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);
  108. /* 使能 ADC3 DMA */
  109. ADC_DMACmd(ADC3, ENABLE);
  110. /* 使能 ADC3 */
  111. ADC_Cmd(ADC3, ENABLE);
  112. /* 软件启动ADC转换 */
  113. ADC_SoftwareStartConv(ADC3);
  114. }
  115. /***************************** 安富莱电子 www.armfly.com (END OF FILE) *********************************/