标题: proteus仿真stm32单片机上拉输入无效吗? [打印本页]

作者: 百二秦关终属我    时间: 2024-3-17 12:21
标题: proteus仿真stm32单片机上拉输入无效吗?

但是在我仿真的时候我发现这个按键接入的PC12和PC13口,默认是低电平。
但是我在配置这个端口的时候配置的是上拉输入,按理说仿真是端口默认不该是高电平吗?
然后我如果按键按下,端口就会输入一个低电平,灯亮。
所以到底是我这个端口配置错了,还是延时函数有问题,我不知道这个延时函数对不对。单片机主频72Mhz。
现在我有疑问的点主要有两个,一个是GPIO端口这个模式,应该配置成上拉输入吗?
另一个是延时函数,单片机主频72Mhz,我要实现按键延时,KEY.C中的按键延时函数Delay_ms(20)对吗?
求大神解答。

ex4_按键控制LED.zip

383.35 KB, 下载次数: 4

源文件和仿真


作者: 百二秦关终属我    时间: 2024-3-17 12:23
主程序
#include "stm32f10x.h"  // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"

uint8_t Key_Num;

int main(void)
{
        LED_Init();       
        Key_Init();
        while(1)
        {
                LED1_OFF();
                LED2_OFF();
                Key_Num = Key_GetNum();
                if(Key_Num == 1)
                {
                        LED1_Turn();
                }
                if(Key_Num == 2)
                {
                        LED2_Turn();
                }
               
        }
       
}
按键控制程序
#include "stm32f10x.h"                  // Device header
#include "Delay.h"

void Key_Init(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //使能GPIOC时钟
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //配置为上拉输入
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  //GPIOC速度为50MHz
       
        GPIO_Init(GPIOC, &GPIO_InitStructure);   //初始化PC口
}

uint8_t Key_GetNum(void)
{
        uint8_t KeyNum = 0;
        if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_12)==0)
        {
                Delay_ms(20);
                while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_12)==0)
                {
                        Delay_ms(20);
                        KeyNum = 1;
                }
        };
        if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13)==0)
        {
                Delay_ms(20);
                while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13)==0)
                {
                        Delay_ms(20);
                        KeyNum = 2;
                }
        }
       
        return KeyNum;
}

Snipaste_2024-03-17_12-22-40.png (28.33 KB, 下载次数: 80)

仿真图

仿真图

作者: Hephaestus    时间: 2024-3-17 17:47
如果仿真是上拉,那么现在的现象就是无法仿真这个特征。
作者: 百二秦关终属我    时间: 2024-3-17 22:52
Hephaestus 发表于 2024-3-17 17:47
如果仿真是上拉,那么现在的现象就是无法仿真这个特征。

好吧,那看来就是proteus有bug。

作者: Hephaestus    时间: 2024-3-18 01:32
百二秦关终属我 发表于 2024-3-17 22:52
好吧,那看来就是proteus有bug。

也不能说有bug,确切的说是模型不完备。
作者: nxplpcexpresso    时间: 2024-3-20 10:28
10K电阻不是这么用的。上拉接VCC
作者: nxplpcexpresso    时间: 2024-3-20 10:30
开关直接一端接I/O口,另一端接GND.




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1