[其他ST产品] ADC采集数据,串口打印乱码

[复制链接]
5106|70
 楼主| elephant00 发表于 2023-3-20 09:34 | 显示全部楼层 |阅读模式
这是我的主函数和ADC配置的函数,目前不清楚为什么在串口打印出来会乱码。


uint16_t ADC_Value;


int main(void)
{
      
        Serial_Init();
        AD_Init();


        while(1)
{
        ADC_Value = AD_GetValue();
      
        printf("\r\n %c",ADC_Value);
        delay_ms(100);
}
}




void AD_Init(void)
{
//开启adc时钟,使能GPIOA
        RCC_APB2PeriphClockCmd(RCC_APB2ENR_ADC1EN,ENABLE );
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE );
      
//预分频,主频72M,adc最高14M
        RCC_ADCCLKConfig(RCC_PCLK2_Div6);

        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入模式,防止干扰,ADC专属模式
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Speed  = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);

//规则化通道,16通道,但只有一位数据可读,避免数据覆盖,使用DMA转运数据
//通道0,序列1,55.5转换时间
        ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_55Cycles5);

        ADC_InitTypeDef ADC_InitStructure;
        ADC_InitStructure.ADC_ContinuousConvMode = DISABLE ;//连续转换还是单次转换,单次转换
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//数据右对齐,高4位为0
        ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//外部触发源,该处使用软件触发,则无外部触发
        ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式,一个ADC转换
        ADC_InitStructure.ADC_NbrOfChannel = 1;//扫描模式下,使用的通道数
        ADC_InitStructure.ADC_ScanConvMode = DISABLE ;//扫描转换模式,非扫描

        ADC_Init(ADC1,&ADC_InitStructure);

        ADC_Cmd(ADC1,ENABLE);

//开启转换,要求校准
        ADC_ResetCalibration(ADC1);//复位校准
        while(ADC_GetResetCalibrationStatus(ADC1) == SET );//
        ADC_StartCalibration(ADC1);
        while(ADC_GetCalibrationStatus(ADC1) == SET);
      
}

uint16_t AD_GetValue(void)
{
        ADC_SoftwareStartConvCmd(ADC1,ENABLE);
        while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) == RESET);
        return ADC_GetConversionValue(ADC1);
}
998386417b82210696.png
V853 发表于 2023-3-20 10:00 | 显示全部楼层
printf("\r\n %c",ADC_Value);改成printf("\r\n %d",ADC_Value);试试
朝生 发表于 2023-3-20 10:01 | 显示全部楼层
uint16_t ADC_Value;是16位类型的,怎么能用%c输出呢?
软核硬核 发表于 2023-3-20 10:01 | 显示全部楼层
你printf的输出语句有问题,可以参考楼上的意见。
sonicll 发表于 2023-3-20 14:11 | 显示全部楼层
这个是C语言使用的问题,其实和ADC本身没有关系
AloneKaven 发表于 2023-3-23 23:39 | 显示全部楼层
print语句有问题吧
chenqianqian 发表于 2023-3-26 10:38 来自手机 | 显示全部楼层
printf 函数的格式有点问题吧
morrisk 发表于 2023-4-6 20:02 | 显示全部楼层
printf("\r\n %c",ADC_Value);改成printf("\r\n %d",ADC_Value);试试
supernan 发表于 2023-4-6 20:03 | 显示全部楼层
uint16_t ADC_Value;是16位类型的,怎么能用%c输出呢?
xxrs 发表于 2023-4-6 20:04 | 显示全部楼层
你printf的输出语句有问题,可以参考楼上的意见。
zhenykun 发表于 2023-4-6 20:07 | 显示全部楼层
这个是C语言使用的问题,其实和ADC本身没有关系
morrisk 发表于 2023-4-6 20:09 | 显示全部楼层
print语句有问题吧
dengdc 发表于 2023-4-6 20:10 | 显示全部楼层
printf 函数的格式有点问题吧
heweibig 发表于 2023-4-6 20:11 | 显示全部楼层
怎么能用%c输出呢?
香水城 发表于 2023-4-7 17:06 | 显示全部楼层
你的PRINTF是基于什么输出的?

半主机? uart ?

还有 你的Printf格式要斟酌下,其它同仁也给出了建议。



SantaBunny 发表于 2023-4-9 13:51 | 显示全部楼层
printf的输出语句有问题
jimmhu 发表于 2023-4-9 14:40 | 显示全部楼层
你转换的adc有问题的吗              
adolphcocker 发表于 2023-4-9 15:17 | 显示全部楼层
把Use MicroLIB 选项勾上  
bartonalfred 发表于 2023-4-9 15:26 | 显示全部楼层
是不是波特率配置的不正确呢              
beacherblack 发表于 2023-4-9 15:33 | 显示全部楼层
串口助手的通信参数是否和板卡的一致;串口线和接口是否牢固
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1031

主题

3946

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部