adc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_Mode_Config(u32 RCC_APBnPeriphn, u32 ADC_ExternalTrigConv, ADC_TypeDef* ADCn, u8 ADC_Channel[], 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 = ENABLE;//扫描模式 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; //定时器5事件触发ADC
  31. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
  32. ADC_InitStructure.ADC_NbrOfConversion = Channel_num;//1个转换在规则序列中 也就是只转换规则序列1
  33. ADC_Init(ADCn, &ADC_InitStructure);//ADC初始化
  34. for(int i=0; i<Channel_num; i++)
  35. {
  36. ADC_RegularChannelConfig(ADC3, ADC_Channel[i] , i+1 , ADC_SampleTime_144Cycles);
  37. }
  38. ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE); //连续使能DMA
  39. ADC_DMACmd(ADCn, ENABLE); //使能ADC_DMA
  40. ADC_Cmd(ADCn, ENABLE); //开启AD转换器
  41. }
  42. void ADC_DMA_Config(u32 RCC_AHB_DMA,u32 DMA_Channel,ADC_TypeDef* ADCn,DMA_Stream_TypeDef *DMA_Stream)
  43. {
  44. //时钟
  45. RCC_AHB1PeriphClockCmd(RCC_AHB_DMA, ENABLE);
  46. //DMA设置
  47. DMA_InitTypeDef DMA_InitStructure;
  48. DMA_InitStructure.DMA_Channel = DMA_Channel; //选择通道号
  49. DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(ADCn->DR); //外围设备地址,ADC_DR_DATA规则数据寄存器
  50. DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)(u16 *)uAD_Buff_A; //DMA存储器地址,自己设置的缓存地址
  51. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //传输方向,外设->存储器
  52. DMA_InitStructure.DMA_BufferSize = ADC_CHANNEL; //DMA缓存大小,数据传输量32*6 ADC_BUFFSIZE * ADC_CHANNEL
  53. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设是否为增量模式
  54. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //存储器是否为增量模式
  55. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外设数据长度半个字(16位)
  56. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //存储器数据长度半字(16位)
  57. DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA模式,普通模式、循环模式,还有双缓冲模式,需要特殊设置
  58. DMA_InitStructure.DMA_Priority = DMA_Priority_Low; //DMA优先级,高优先级
  59. DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //不使用FIFO
  60. DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; //FIFO阈值
  61. DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //存储器突发,单次传输
  62. DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //外设突发,单次传输
  63. DMA_Init(DMA_Stream, &DMA_InitStructure); //初始化DMA2_Stream0,对应为ADC3
  64. //双缓冲模式设置
  65. DMA_DoubleBufferModeConfig(DMA_Stream,(uint32_t)(u16 *)(uAD_Buff_B),DMA_Memory_0); //DMA_Memory_0首先被传输
  66. DMA_DoubleBufferModeCmd(DMA_Stream,ENABLE);
  67. //设置DMA中断
  68. DMA_ClearITPendingBit(DMA_Stream, DMA_IT_TC); //清除中断标志
  69. DMA_ITConfig(DMA_Stream, DMA_IT_TC, ENABLE); //传输完成中断
  70. DMA_Cmd(DMA_Stream, ENABLE); //使能DMA
  71. }
  72. void ADC_TIM_Config(TIM_TypeDef* TIMx,u16 psc, u16 per,
  73. void(*ClockCMD)(uint32_t, FunctionalState),u32 RCC_APBnPeriphn, //APB时钟使能设置
  74. void(*TIM_OCInit)(TIM_TypeDef*, TIM_OCInitTypeDef*), //OC通道设置
  75. void(*TIM_OCPreloadConfig)(TIM_TypeDef*, uint16_t))
  76. {
  77. (*ClockCMD)(RCC_APBnPeriphn,ENABLE);
  78. //时基初始化
  79. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  80. TIM_TimeBaseStructure.TIM_Prescaler = psc; //定时器分频
  81. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
  82. TIM_TimeBaseStructure.TIM_Period = per; //自动重装载值
  83. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  84. TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  85. TIM_TimeBaseInit(TIMx,&TIM_TimeBaseStructure);//初始化定时器
  86. //输出比较初始化
  87. TIM_OCInitTypeDef TIM_OCInitStructure;
  88. TIM_OCInitStructure.TIM_Pulse = 10;
  89. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
  90. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  91. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
  92. (*TIM_OCInit)(TIMx,&TIM_OCInitStructure);
  93. (*TIM_OCPreloadConfig)(TIMx,TIM_OCPreload_Enable);
  94. TIM_CtrlPWMOutputs(TIMx, ENABLE);
  95. //TIM_ARRPreloadConfig(TIMx, ENABLE);//允许重载
  96. TIM_SelectOutputTrigger(TIMx, TIM_TRGOSource_OC1);//触发源
  97. //TIM_ITConfig(TIMx,TIM_IT_Update,ENABLE);
  98. TIM_Cmd(TIMx, ENABLE);
  99. //TIM_UpdateDisableConfig(TIMx, DISABLE);
  100. }
  101. void ADC_NVIC_Config(u8 NVIC_IRQChannel, u8 pre, u8 sub)
  102. {
  103. NVIC_InitTypeDef NVIC_InitStructure;
  104. NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel; //DMA2_Stream0中断
  105. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre; //抢占优先级1
  106. NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub; //子优先级1
  107. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  108. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化NVIC寄存器
  109. }