这是我的程序大家能帮我看看吗,有问题请指出来,我用keil调试时到usart发出数据时时,查询发送是否完成时总是发送不成功,发送完成标志不置位, while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);就是一直在执行这句,不知道什么原因。下面是源程序:板子原理图在附件中
#include "stm32f10x.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
void RCC_Config(void);
void GPIO_Config(void);
void USART_Config(void);
void DMA_Config(void);
void ADC_Config(void);
vu16 ADC_ConvertedValue;
int main(void)
{RCC_Config();
GPIO_Config();
USART_Config();
DMA_Config();
ADC_Config();
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//启动adc转换
while (1)
{
while(DMA_GetFlagStatus(DMA1_FLAG_TC1) == RESET);//等待DMA传输完成
DMA_ClearFlag(DMA1_FLAG_TC1);
USART_SendData(USART1, ADC_ConvertedValue);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待发送完成
}
}
void RCC_Config(void)
{SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1|RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOC| RCC_APB2Periph_AFIO, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//配置PC0为模拟输入
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//设置复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置Rx引脚为浮空输入模式
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_11); //485只发送
}
void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);//复位DMA通道1
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);//初始化DMA1通道1
DMA_Cmd(DMA1_Channel1, ENABLE); //使能DMA1通道1
}
void ADC_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//ADC1和ADC2工作在独立模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //使能扫描
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//ADC转换工作在连续模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//转换数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 1;//转换通道数1
ADC_Init(ADC1, &ADC_InitStructure); //初始化ADC
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_28Cycles5);
ADC_DMACmd(ADC1, ENABLE);//使能ADC1模块DMA
ADC_Cmd(ADC1, ENABLE);//使能ADC1
ADC_ResetCalibration(ADC1); //重置ADC1校准寄存器
while(ADC_GetResetCalibrationStatus(ADC1));//等待ADC1校准重置完成
ADC_StartCalibration(ADC1);//开始ADC1校准
while(ADC_GetCalibrationStatus(ADC1));//等待ADC1校准完成
}
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;//波特率9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位数据
USART_InitStructure.USART_StopBits = USART_StopBits_1;//1个停止位
USART_InitStructure.USART_Parity = USART_Parity_No ; //无校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//禁用RTSCTS硬件流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//使能发送接收
/*USART_InitStructure.USART_Clock = USART_Clock_Disable; */ //串口时钟禁止
/*USART_InitStructure.USART_CPOL = USART_CPOL_Low;*/ //时钟下降沿有效
/*USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;*/ //数据在第1个时钟沿捕捉
/*USART_InitStructure.USART_LastBit = USART_LastBit_Disable;*/ //最后数据位的时钟脉冲不输出到SCLK引脚
USART_Init(USART1, &USART_InitStructure);//初始化串口1
USART_Cmd(USART1, ENABLE);//串口1使能
}
|