本帖最后由 fengruixin1992 于 2019-3-28 15:24 编辑
我是在原子哥ADC的例程结合串口通信例程和DMA例程上改的,但是用串口调试助手试了,没任何数据,程序编译没问题,求指教哪点有问题,急求!!!,用16进制显示,显示出来的都是00
void Adc_Init(u16 mar)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//IO口使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //APB2时钟使能
//初始化ADC1的通道0、1、5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE); //ADC1复位
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE); //使能ADC1时钟
//DMA初始化
DMA_InitStructure.DMA_Channel = DMA_Channel_0; //选择DMA通道0
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR;//外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = mar;//DMA 目标内存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//外设到内存
DMA_InitStructure.DMA_BufferSize = 3;//数据传输量
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_Circular;//DMA循环模式
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//优先级
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE); //使能DMA2的数据流0
//ADC1初始化
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//转换间隔时钟周期
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; //DMA使能模式1
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//
ADC_CommonInit(&ADC_CommonInitStructure);//ˉ
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12位采样
ADC_InitStructure.ADC_ScanConvMode = ENABLE;/扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//连续转换
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//软件触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
ADC_InitStructure.ADC_NbrOfConversion = 3;//3个通道数
ADC_Init(ADC1, &ADC_InitStructure);//初始化ADC1
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_480Cycles );
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_480Cycles );
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 3, ADC_SampleTime_480Cycles );
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);//使能ADC1的DMA
ADC_Cmd(ADC1, ENABLE);/使能ADC1
}
//主函数
#define BUF_NUM 3
u16 buffer[BUF_NUM];
int main(void)
{
u8 t;
u8 data1;
u8 data2;
u16 adcx;
float temp;
delay_init(168);
uart_init(115200);
LED_Init();
Adc_Init((u32)buffer);
while(1)
{
for(t=0;t<3;t++)
{
data1=(buffer[t]>>8)&0xFF; //AD是12位的,获取高8位数据
USART_SendData(USART1, data1); //向串口1发送数据
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待串口发送完成
data2=buffer[t]&0xFF; //获取AD低8位数据
USART_SendData(USART1, data2);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
LED0=!LED0;
delay_ms(10);
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>