初级会员
- 积分
- 133
- 金钱
- 133
- 注册时间
- 2021-11-28
- 在线时间
- 26 小时
|
1金钱
各位大佬,我使用32内部ADC进行采样,按照32的ADC范围,采样最大数值应该是4095,但是接入3.3V显示是65535,这是为什么?
定时器2配置
//用于触发ADC采集,主频/(arr+1)*(psc+1)
void TIM2_Init(uint16_t arr,uint16_t psc)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //打开定时器2的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //打开GPIOA的时钟
GPIO_InitStruct.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin =GPIO_Pin_1;
GPIO_InitStruct.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct); //设置gpio端口的工作方式,为推挽复用输出
TIM_TimeBaseInitStruct.TIM_ClockDivision =TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode =TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period =arr;
TIM_TimeBaseInitStruct.TIM_Prescaler =psc;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct); //设置定时器的装载值和分频系数
TIM_OCInitStruct.TIM_OCMode =TIM_OCMode_PWM1 ;
TIM_OCInitStruct.TIM_OCPolarity =TIM_OCPolarity_Low;
TIM_OCInitStruct.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse =arr/2;
TIM_OC2Init(TIM2, &TIM_OCInitStruct); //设置定时器的PWM已经他的占空比 占空比在这里没有实际的意义 只是希望定时器能够提供上升沿信号给adc,然后触发adc的转换
//TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); //打开输出比较通道2的自动重装值
TIM_CtrlPWMOutputs(TIM2, ENABLE);
TIM_Cmd(TIM2, ENABLE); //使能定时器2
}
ADC和DMA配置
void AD_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_GPIOA, ENABLE); //ADC1通道
//GPIO设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1);//复位ADC1
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //ADC_CLK = PCLK/6=72M/6=12
//规则组配置,单通道
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_13Cycles5);
//ADC设置
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC独立模式
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //参数右对齐对应数据低位是ADCL的最低位
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2; //触发方式定时器2通道2触发
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //定时器触发单次转换
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //单通道模式
ADC_InitStructure.ADC_NbrOfChannel = 1; //通道数目——1
ADC_Init(ADC1, &ADC_InitStructure);
ADC_ExternalTrigConvCmd(ADC1,ENABLE); //ADC外部触发使能
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1); //复位校准
while (ADC_GetResetCalibrationStatus(ADC1)); //返回复位校准的状态
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
//ADC_SoftwareStartConvCmd(ADC1, ENABLE); //软件触发
ADC_DMACmd(ADC1, ENABLE);
}
uint8_t flag = 0;
uint16_t AD_Value[256];
void DMA_init(uint16_t Size)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE); //开启DMA时钟
// NVIC_InitTypeDef NVIC_InitStruct;
DMA_InitTypeDef DMA_InitStructure;
DMA_InitStructure.DMA_BufferSize = Size; //转换计数器
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //转换方向控制从外设到存储器
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //内存到内存
//外设
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; //外设起始地址
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外设缓冲区大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //地址是否递增
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //优先级设置
//存储器
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)AD_Value; //存储器起始地址
DMA_InitStructure.DMA_MemoryDataSize =DMA_PeripheralDataSize_HalfWord; //存储器缓冲区大小
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //地址是否递增
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular ; //自动重装
DMA_Init(DMA1_Channel1,&DMA_InitStructure);
// NVIC_InitStruct.NVIC_IRQChannel =DMA1_Channel1_IRQn;
// NVIC_InitStruct.NVIC_IRQChannelCmd =ENABLE;
// NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority =1;
// NVIC_InitStruct.NVIC_IRQChannelSubPriority =0;
// NVIC_Init(& NVIC_InitStruct); //开启DMA1通道1的中断,并编写中断服务函数,若有什么任务要执行,则可以再dma传输完成之后挂起标志位,然后执行相应的dma传输完成的程序
DMA_Cmd(DMA1_Channel1, ENABLE);
// DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);//开启定时器dma1的中断,并且使能相应的中断类型
}
|
|