adc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "adc.h"
  2. u16 uAD_Buff_A[ADC_CHANNEL]; //ADC采集缓存A
  3. u16 uAD_Buff_B[ADC_CHANNEL]; //ADC采集缓存B
  4. void ADC_GPIO_Config(u32 RCC_APBnPeriphn, GPIO_TypeDef *GPIOx,u16 PIN)
  5. {
  6. RCC_AHB1PeriphClockCmd(RCC_APBnPeriphn, ENABLE);
  7. GPIO_InitTypeDef GPIO_InitStructure;
  8. GPIO_InitStructure.GPIO_Pin = PIN;
  9. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//模拟输入
  10. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉
  11. GPIO_Init(GPIOx, &GPIO_InitStructure);//初始化
  12. }
  13. void ADC_SOFT_Mode_Config(u32 RCC_APBnPeriphn, ADC_TypeDef* ADCn, u8 Channel_num)
  14. {
  15. RCC_APB2PeriphClockCmd(RCC_APBnPeriphn, ENABLE); //使能ADC3时钟
  16. RCC_APB2PeriphResetCmd(RCC_APBnPeriphn, ENABLE); //ADC3复位
  17. RCC_APB2PeriphResetCmd(RCC_APBnPeriphn, DISABLE); //复位结束
  18. ADC_CommonInitTypeDef ADC_CommonInitStructure;
  19. ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
  20. ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//两个采样阶段之间的延迟5个时钟
  21. ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //DMA失能
  22. ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//预分频4分频。ADCCLK=PCLK2/4=84/4=21Mhz,ADC时钟最好不要超过36Mhz
  23. ADC_CommonInit(&ADC_CommonInitStructure);//初始化
  24. ADC_InitTypeDef ADC_InitStructure;
  25. ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12位模式
  26. ADC_InitStructure.ADC_ScanConvMode = DISABLE;//扫描模式 ENABLE
  27. ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//关闭连续转换
  28. ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//禁止触发检测
  29. //ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; //上升沿触发
  30. //ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv; //事件触发ADC
  31. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
  32. ADC_InitStructure.ADC_NbrOfConversion = Channel_num;//1个转换在规则序列中 也就是只转换规则序列1
  33. //ADC_RegularChannelConfig(ADCn, ADC_Channel_14, 1, ADC_SampleTime_480Cycles);
  34. ADC_Init(ADCn, &ADC_InitStructure);//ADC初始化
  35. ADC_Cmd(ADCn, ENABLE); //开启AD转换器
  36. }
  37. u16 Get_SOFT_ADC(ADC_TypeDef* ADCn, u8 ADC_Channe)
  38. {
  39. ADC_RegularChannelConfig(ADCn, ADC_Channe, 1, ADC_SampleTime_480Cycles);
  40. ADC_SoftwareStartConv(ADCn);
  41. while(!ADC_GetFlagStatus(ADCn, ADC_FLAG_EOC));
  42. return ADC_GetConversionValue(ADCn);
  43. }
  44. //u16 Get_SOFT_ADC_xTIME(ADC_TypeDef* ADCn, u8 ADC_Channe, u8 times)
  45. //{
  46. // u32 i, temp_value = 0;
  47. //
  48. // for(i=0; i<times; i++)
  49. // {
  50. // temp_value += Get_SOFT_ADC(ADCn ,ADC_Channe);
  51. // delay_ms(1);
  52. // }
  53. //}
  54. void ADC_DMA_Mode_Config(u32 RCC_APBnPeriphn, u32 ADC_ExternalTrigConv, ADC_TypeDef* ADCn, u8 ADC_Channel[], u8 Channel_num)
  55. {
  56. RCC_APB2PeriphClockCmd(RCC_APBnPeriphn, ENABLE); //使能ADC3时钟
  57. RCC_APB2PeriphResetCmd(RCC_APBnPeriphn, ENABLE); //ADC3复位
  58. RCC_APB2PeriphResetCmd(RCC_APBnPeriphn, DISABLE); //复位结束
  59. ADC_CommonInitTypeDef ADC_CommonInitStructure;
  60. ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
  61. ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//两个采样阶段之间的延迟5个时钟
  62. ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //DMA失能
  63. ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//预分频4分频。ADCCLK=PCLK2/4=84/4=21Mhz,ADC时钟最好不要超过36Mhz
  64. ADC_CommonInit(&ADC_CommonInitStructure);//初始化
  65. ADC_InitTypeDef ADC_InitStructure;
  66. ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12位模式
  67. ADC_InitStructure.ADC_ScanConvMode = ENABLE;//扫描模式 ENABLE
  68. ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//关闭连续转换
  69. //ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//禁止触发检测
  70. ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; //上升沿触发
  71. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv; //定时器5事件触发ADC
  72. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
  73. ADC_InitStructure.ADC_NbrOfConversion = Channel_num;//1个转换在规则序列中 也就是只转换规则序列1
  74. ADC_Init(ADCn, &ADC_InitStructure);//ADC初始化
  75. for(int i=0; i<Channel_num; i++)
  76. {
  77. ADC_RegularChannelConfig(ADCn, ADC_Channel[i] , i+1 , ADC_SampleTime_144Cycles);
  78. }
  79. ADC_DMARequestAfterLastTransferCmd(ADCn, ENABLE); //连续使能DMA
  80. ADC_DMACmd(ADCn, ENABLE); //使能ADC_DMA
  81. ADC_Cmd(ADCn, ENABLE); //开启AD转换器
  82. }
  83. void ADC_DMA_Config(u32 RCC_AHB_DMA,u32 DMA_Channel,ADC_TypeDef* ADCn,DMA_Stream_TypeDef *DMA_Stream)
  84. {
  85. //时钟
  86. RCC_AHB1PeriphClockCmd(RCC_AHB_DMA, ENABLE);
  87. //DMA设置
  88. DMA_InitTypeDef DMA_InitStructure;
  89. DMA_InitStructure.DMA_Channel = DMA_Channel; //选择通道号
  90. DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(ADCn->DR); //外围设备地址,ADC_DR_DATA规则数据寄存器
  91. DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)(u16 *)uAD_Buff_A; //DMA存储器地址,自己设置的缓存地址
  92. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //传输方向,外设->存储器
  93. DMA_InitStructure.DMA_BufferSize = ADC_CHANNEL; //DMA缓存大小,数据传输量32*6 ADC_BUFFSIZE * ADC_CHANNEL
  94. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设是否为增量模式
  95. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //存储器是否为增量模式
  96. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外设数据长度半个字(16位)
  97. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //存储器数据长度半字(16位)
  98. DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA模式,普通模式、循环模式,还有双缓冲模式,需要特殊设置
  99. DMA_InitStructure.DMA_Priority = DMA_Priority_Low; //DMA优先级,高优先级
  100. DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //不使用FIFO
  101. DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; //FIFO阈值
  102. DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //存储器突发,单次传输
  103. DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //外设突发,单次传输
  104. DMA_Init(DMA_Stream, &DMA_InitStructure); //初始化DMA2_Stream0,对应为ADC3
  105. //双缓冲模式设置
  106. DMA_DoubleBufferModeConfig(DMA_Stream,(uint32_t)(u16 *)(uAD_Buff_B),DMA_Memory_0); //DMA_Memory_0首先被传输
  107. DMA_DoubleBufferModeCmd(DMA_Stream,ENABLE);
  108. //设置DMA中断
  109. DMA_ClearITPendingBit(DMA_Stream, DMA_IT_TC); //清除中断标志
  110. DMA_ITConfig(DMA_Stream, DMA_IT_TC, ENABLE); //传输完成中断
  111. DMA_Cmd(DMA_Stream, ENABLE); //使能DMA
  112. }
  113. void ADC_TIM_Config(TIM_TypeDef* TIMx,u16 psc, u16 per,
  114. void(*ClockCMD)(uint32_t, FunctionalState),u32 RCC_APBnPeriphn, //APB时钟使能设置
  115. void(*TIM_OCInit)(TIM_TypeDef*, TIM_OCInitTypeDef*), //OC通道设置
  116. void(*TIM_OCPreloadConfig)(TIM_TypeDef*, uint16_t))
  117. {
  118. (*ClockCMD)(RCC_APBnPeriphn,ENABLE);
  119. //时基初始化
  120. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  121. TIM_TimeBaseStructure.TIM_Prescaler = psc; //定时器分频
  122. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
  123. TIM_TimeBaseStructure.TIM_Period = per; //自动重装载值
  124. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  125. TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  126. TIM_TimeBaseInit(TIMx,&TIM_TimeBaseStructure);//初始化定时器
  127. //输出比较初始化
  128. TIM_OCInitTypeDef TIM_OCInitStructure;
  129. TIM_OCInitStructure.TIM_Pulse = 10;
  130. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
  131. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  132. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
  133. (*TIM_OCInit)(TIMx,&TIM_OCInitStructure);
  134. (*TIM_OCPreloadConfig)(TIMx,TIM_OCPreload_Enable);
  135. TIM_CtrlPWMOutputs(TIMx, ENABLE);
  136. //TIM_ARRPreloadConfig(TIMx, ENABLE);//允许重载
  137. TIM_SelectOutputTrigger(TIMx, TIM_TRGOSource_OC1);//触发源
  138. //TIM_ITConfig(TIMx,TIM_IT_Update,ENABLE);
  139. TIM_Cmd(TIMx, ENABLE);
  140. //TIM_UpdateDisableConfig(TIMx, DISABLE);
  141. }
  142. void ADC_NVIC_Config(u8 NVIC_IRQChannel, u8 pre, u8 sub)
  143. {
  144. NVIC_InitTypeDef NVIC_InitStructure;
  145. NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel; //DMA2_Stream0中断
  146. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre; //抢占优先级1
  147. NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub; //子优先级1
  148. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  149. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化NVIC寄存器
  150. }