定时器控制采样频率,触发AD,DMA传输到一个数组,传输完成,中断中由串口传出。。。。。。。AD引脚 接开发板上的地和3.3 都是正常显示。。悬空时显示在1V左右。。。 但是接上正弦信号后 信号幅值只有0.8V 但采样的幅值都显示到2点几 3点几V 是怎么回事 从AD1换到AD2 DMA通道也换过 都没用 怎么办
[mw_shl_code=c,true]#include "stm32f4xx.h"
void Delay(__IO uint32_t nCount);
void Adc_Init(uint32_t Frequency);
void COM1Init(u32 BaudRate1);
void NVIC_Config(void);
void LED_Init(void);
__IO uint16_t uhADCxConvertedValue[50000];
// u16 tmp;
// const double Vref_ = 3.28;
// unsigned int i,i_tmp = 0;
// double ADC_Infact_V = 0;
float tempt;
u32 i;
int main(void)
{
LED_Init();
Adc_Init(50000);
NVIC_Config();
COM1Init(115200);
while(1){
GPIO_SetBits(GPIOF,GPIO_Pin_10);
Delay(0x7FFFFF);
GPIO_ResetBits(GPIOF,GPIO_Pin_10);
Delay(0x7FFFFF);
}
}
void Delay(__IO uint32_t nCount)
{
while(nCount--){}
}
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIOF时钟
//GPIOF9,F10初始化设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化
GPIO_ResetBits(GPIOF, GPIO_Pin_10);//GPIOF9,F10设置高,灯灭
}
void Adc_Init(uint32_t Frequency)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); //使能ADC2时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);//使能DMA2
/* DMA2 Stream3 channel1 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_1;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC2->DR;//0x0000FFFF
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&uhADCxConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 50000;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream3, &DMA_InitStructure);
DMA_ITConfig(DMA2_Stream3, DMA_IT_TC, ENABLE); //DMA2传输完成中断
DMA_ClearITPendingBit(DMA2_Stream3, DMA_IT_TEIF3); //清标志
DMA_ClearITPendingBit(DMA2_Stream3, DMA_IT_TCIF3); //清标志
DMA_Cmd(DMA2_Stream3, ENABLE);
//先初始化ADC2通道 IO口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//Pb0 通道8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//模拟输入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2,ENABLE); //ADC2复位
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2,DISABLE); //复位结束
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//两个采样阶段之间的延迟5个时钟
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //DMA失能
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//预分频4分频。ADCCLK=PCLK2/4=84/4=21Mhz,ADC时钟最好不要超过36Mhz
ADC_CommonInit(&ADC_CommonInitStructure);//初始化
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12位模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//非扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//关闭连续转换
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;//禁止触发检测,使用软件触发
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
ADC_InitStructure.ADC_NbrOfConversion = 1;//1个转换在规则序列中 也就是只转换规则序列1
ADC_Init(ADC2, &ADC_InitStructure);//ADC初始化
ADC_RegularChannelConfig(ADC2, ADC_Channel_8, 1, ADC_SampleTime_144Cycles ); //ADC2,ADC通道,144个周期,提高采样时间可以提高精确度,转换频率 134K
/* Enable DMA request after last transfer (Single-ADC mode) */
ADC_DMARequestAfterLastTransferCmd(ADC2, ENABLE);
/* Enable ADC2 DMA */
ADC_DMACmd(ADC2, ENABLE);
ADC_Cmd(ADC2, ENABLE);//开启AD转换器
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 1000000/Frequency-1; //PWM 周期 10us 50K
TIM_TimeBaseStructure.TIM_Prescaler = 83; //预分频因子 计数频率 84M/84=1M
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_Pulse = 0x04;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
}
void COM1Init(u32 BaudRate1)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = BaudRate1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_Cmd(USART1, ENABLE);
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组 2
/* Enable the DMA Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void DMA2_Stream3_IRQHandler(void)
{
if(DMA_GetITStatus(DMA2_Stream3, DMA_IT_TCIF3)) //通道5传输完成中断TC 还有传输 过半中断HT 错误中断TE 全局中断GL
{
DMA_ClearITPendingBit(DMA2_Stream3, DMA_IT_TCIF3); //清除全部中断标志
for(i=0;i<=49999;i++)
{
// tmp=uhADCxConvertedValue;
// tmp=tmp>>8;
// while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
// USART_SendData(USART1 ,tmp );
// //tmp=tmp>>8;
// while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
// USART_SendData(USART1 , uhADCxConvertedValue);
tempt=(float)uhADCxConvertedValue*(3.3/4096);
tempt*=1000;
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , 0x30+(unsigned int)tempt/1000%10);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , '.');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , 0x30+(unsigned int)tempt/100%10);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , 0x30+(unsigned int)tempt/10%10);
//
// i_tmp = uhADCxConvertedValue;
// ADC_Infact_V = ((double)i_tmp)*Vref_/4095*1000;
// i_tmp = (unsigned int)ADC_Infact_V;
//
// while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
// USART_SendData(USART1 , 0x30+i_tmp/1000%10);
// while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
// USART_SendData(USART1 , '.');
// while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
// USART_SendData(USART1 ,0x30+i_tmp/100%10);
// while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
// USART_SendData(USART1,0x30+i_tmp/10%10);
// while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
// USART_SendData(USART1 , 0x30+i_tmp%10);
// while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
// USART_SendData(USART1 , 'V');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , '\r');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , '\n');
}
}
}
[/mw_shl_code]
|