这是我写的AD转换并在串口显示程序,请各位大神帮忙看看我这个程序有什么问题?
/*PB1作为模拟输入*/(电位器与PB1相连)
问题:/编译成功了,下载运行后串口没有显示/
求各位给看看、、、
#include "stm32f10x.h"
#include "stdio.h"
#define ADC1_DR_Address ((u32)0x4001244C)
ADC_InitTypeDef ADC_InitStructure;
USART_InitTypeDef USART_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
vu16 ADCConvertedValue;
void GPIO_Config(void);
void RCC_Config(void);
void USART_Config(void);
static void Delay (u32 ncount);
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
int main (void)
{
u16 ADCConvertedValueLocal,percent=0,voltage=0;
RCC_Config();
GPIO_Config();
USART_Config();
/*DMA配置*/
DMA_DeInit(DMA1_Channel1); //开启DMA1的第一通道
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//DMA对应的外设基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADCConvertedValue;//内存存储基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //DMA的转换模式为SRC模式,由外设搬移到内存
DMA_InitStructure.DMA_BufferSize = 1; //DMA缓存大小,1个
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//接收一次数据后,设备地址禁止后移
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; //关闭接收一次数据后,目标内存地址禁止后移
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //定义外设数据宽度为16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//DMA搬移数据尺寸,HalfWord就是16位
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //转换模式,循环缓存模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA优先级高
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //M2M模式禁用
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/*使能Channel 1*/
DMA_Cmd(DMA1_Channel1,ENABLE);
/*ADC配置*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立的转换模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //开启扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //开启连续转换模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //ADC外部开关,关闭状态
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //对齐方式,ADC为12位中,右对齐方式
ADC_InitStructure.ADC_NbrOfChannel = 1; //开启通道数,1个
ADC_Init(ADC1, &ADC_InitStructure);
/*配置采样时间*/
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5); //ADC通道组, 第14个通道 采样顺序1,转换时间
/*ADC DMA使能*/
ADC_DMACmd(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);
while (1)
{
ADCConvertedValueLocal = ADCConvertedValue;
percent = ADCConvertedValueLocal*100/(0x1000); //算出百分比
voltage = percent*33; //算出电压
printf("\n\r ADCConvertedValue is0x%X\nThe Percent is %d\nThe voltage is %d.%d%d\n\r",
ADCConvertedValueLocal,percent,voltage/1000,(voltage%1000)/100,(voltage%100)/10);
Delay(0x8FFFF);
}
}
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch); //发送一字节数据
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{} //等待发送完成
return ch;
}
void RCC_Config(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE); //开DMA1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_ADC1|RCC_APB2Periph_USART1,ENABLE);//开GPIOB、ADC1、USART1的时钟
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入模式
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void USART_Config(void)
{
USART_InitStructure.USART_BaudRate = 115200; //波特率设置 //串口初始化定义
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; //硬件流控制失能
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //发送使能、接收使能
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE); //USART1s使能
}
static void Delay(u32 ncount) //延时程序
{
for(;ncount!=0;ncount--);
} |