STM8L051F3 IO表现异常

本文记录了使用STM8L051F3单片机过程中遇到的IO驱动问题及解决过程。从多路输出管脚配置异常开始,逐步排查到电源脚未正确接地的问题,并最终解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    大概两个月前吧,公司有个电源模块输出控制的小项目,准备用单片机。硬件组同事选了STM8L051F3,STM8内核,虽然不是传统的C51,但基本寄存器操作都差不多,花了3天时间依次调好IO,定时器,串口(用来调试)和I2C(单片机与控制板通信)。整个项目一周左右就搞定了。

    前天公司又有个改版的电源模块输出控制项目,还是用这款单片机,只是需要控制的IO口由原来的4路改成了8路(需要控制更多的电源输出)。本想着就几分钟的事情,结果搞了一下午都没搞定:对这8路输出管脚PB0~PB7初始化:"GPIO_Init(GPIOB, GPIO_Pin_All, GPIO_Mode_Out_PP_High_Fast); " Out:输出;High:输出1;Fast:高速10M模式。

烧进去,万用表一测,输出PB0输出0.8V,再测其他几路,都是0.8V,什么情况这是?

    进入DEBUG模式,发现走到函数void GPIO_Init(GPIO_TypeDef* GPIOx, uint8_t GPIO_Pin, GPIO_Mode_TypeDef GPIO_Mode)中GPIOx->CR1 |= GPIO_Pin;这一步直接报错了,寄存器CR1设置失败。

    改一下初始化”GPIO_Init(GPIOB, GPIO_Pin_0, GPIO_Mode_Out_PP_High_Fast);“,运行正常,测到BP0电压3.58V,正确;

    再改成”GPIO_Init(GPIOB, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3, GPIO_Mode_Out_PP_High_Fast);“,依然正常,但BP0~BP3电压位3.2V;

    再改成”GPIO_Init(GPIOB, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6, GPIO_Mode_Out_PP_High_Fast);“,BP0~BP6都是2.8V。

    控制输出的IO管脚越多,差错越大,这明显是驱动不够啊。但单片机的IO管脚不都是独立驱动的吗,看了一下原理图:

    图中PB0~PB7即8路输出控制管脚

    图中只截取了EN_VCC1和EN_VCC3的电路,由前面那张图可以看到他们分别对应PB0和PB4,通过1k的电阻下拉倒地,其他六个管脚也一样处理的。所以我就只能将这几路输出管脚配置成推挽模式了(开漏模式需要上拉)。

    。。。。。。。   

    昨天上午继续折腾,让同事把那8个下拉电阻都去掉,再拉测。发现ST-LINK找不到单片机,单片机工作不正常?先检测单片机的输入电源,VDD脚电压3.6V,正常;再测VSS脚,2.1V,不会吧!!!这是电源地脚呢,不应该是0V么?只能找来硬件组同事帮忙查一下了。反馈的结果是这个VSS确实是没有接地的,飞个线,拿回来一切都OK啦。

    电源脚没接地居然还可以跑起来, 可能是PB的下拉电阻间接将其拉低了,外在表现就是单片机工作逻辑正常,输出驱动不够。

Rebuild started: Project: Project *** Using Compiler 'V6.22', folder: 'E:\Keil_v5\ARM\ARMCLANG\Bin' Rebuild target 'Target 1' assembling startup_stm32f10x_md.s... Start/core_cm3.c(445): error: non-ASM statement in naked function is not supported 445 | uint32_t result=0; | ^ Start/core_cm3.c(442): note: attribute is here 442 | uint32_t __get_PSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(465): error: parameter references not allowed in naked functions 465 | "BX lr \n\t" : : "r" (topOfProcStack) ); | ^ Start/core_cm3.c(461): note: attribute is here 461 | void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(479): error: non-ASM statement in naked function is not supported 479 | uint32_t result=0; | ^ Start/core_cm3.c(476): note: attribute is here 476 | uint32_t __get_MSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(499): error: parameter references not allowed in naked functions 499 | "BX lr \n\t" : : "r" (topOfMainStack) ); | ^ Start/core_cm3.c(495): note: attribute is here 495 | void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); | ^ 4 errors generated. compiling core_cm3.c... compiling misc.c... compiling system_stm32f10x.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_exti.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_dma.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_bkp.c... compiling stm32f10x_can.c... compiling stm32f10x_flash.c... compiling stm32f10x_pwr.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_
03-31
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值