找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 652|回复: 6
收起左侧

STC8H3K64S4单片机输入口检查不到输入口状态

[复制链接]
ID:337139 发表于 2024-1-23 11:26 | 显示全部楼层 |阅读模式
运行的结果是,按键只能按一下检测到后中,LED不停地闪烁,给人的感觉是按键没有弹起一直有测到低电平。
出现这种现象是为哪里设置引起的?

请各位大师帮助 看一下哪里的问题?

#include "stc8h.h"

sbit Led_Red   = P1^7;
sbit Led_Green = P3^4;
sbit Led_Blue  = P5^5;


sbit   Key1 = P3^6;       // 00模式
sbit   Key2 = P3^5;       // 00模式
sbit   Key3 = P5^1;       // 00模式



bit  Key_flag=0;        //按键松开按下标识

u8   Counter,Key_Modle;

extent  void delay_ms(u16 mS);

void IO_Int(void)
{
    P_SW2 |= 0x80;  //扩展寄存器(XFR)访问使能

    P0M1 = 0x00;   P0M0 = 0x00;   //设置P0.4、P0.5为漏极开路
    P1M1 = 0x00;   P1M0 = 0x00;   //设置P1.4、P1.5为漏极开路
    P2M1 = 0x00;   P2M0 = 0xff;   //设置P2.2~P2.5为漏极开路
    P3M1 = 0x00;   P3M0 = 0x80;   //设置P3.3、P3.5、P3.6为漏极开路 01101000  10000000     
    P4M1 = 0x00;   P4M0 = 0x0f;   //设置P4.2~P4.5为漏极开路
    P5M1 = 0x00;   P5M0 = 0x01;   //设置P5.2、P5.3为漏极开路00001010  00000011
    P6M1 = 0xff;   P6M0 = 0xff;   //设置为漏极开路(实验箱加了上拉电阻到3.3V)
    P7M1 = 0x00;   P7M0 = 0x00;   //设置为准双向口
   
    P3PU=0x68;
    P5PU=0x02;
    P0=0X00;
    P1=0X00;
    P2=0XFF;
    //P3=0X68;      // 35 36
    P4=0X0F;
   
    Led_Red  =1;
    Led_Green=1;
    Led_Blue =1;   

}

void Key_Check(void)
  {
       if((Key1==0)&&(Key_flag))           
               {delay_ms(80);
                     if(Key1==0)
                        {                                                     
                            Key_Modle=1;                                                                                                                                                
                          
                          Led_Red =1;
                          Led_Blue =1;                                 
                            Led_Green =~Led_Green;
                            Key_flag = 0;   
                           
                          Counter = 0;     
                            }
                    }   
       if((Key2==0)&&(Key_flag))           
                {delay_ms(80);                 
          if(Key2==0)
                        {   
                         Key_Modle=2;                                                   
                       
                         Led_Green =1;
                         Led_Red =1;         
                         Led_Blue =~Led_Blue;
                         Key_flag = 0;   
                       
                            }
                        }   
                if(Key3==0)&&(Key_flag)           
               {delay_ms(80);            
                       if(Key3==0)
                        {   
                         Key_Modle=3;                                                                                            
                            DIR = 1;
                            EN  = 0;   
                            Led_Green =1;
                          Led_Blue =1;     
                        Led_Red=~Led_Red;            
                            Counter = 0;
                            Key_flag = 0;
                        
                          }
                    }   
                if((Key==0)&&(Key_flag))           
               {delay_ms(40);        
                        if(Key==0)
                        {   
                            Key_Modle=4;                                                               
                        
                            Counter = 0;
                            Key_flag = 0;   
                          
                        
                            }                                 
                 }
        if((Key1)&&(Key2)&&(Key3)&&(Key))  
                 {
                Key_flag=1;
                 }                    
    }


void main(void)
{   
      IO_Int();
while(1)
    {

     delay_ms(100);
    Key_Check();        


      }



}
回复

使用道具 举报

ID:773929 发表于 2024-1-23 13:32 | 显示全部楼层
设置成双向
回复

使用道具 举报

ID:213173 发表于 2024-1-23 17:10 | 显示全部楼层
初学尽量简单些,整明白了就可循序渐进。这样可以提振信心。给你改了,你试试。
#include "stc8h.h"

typedef unsigned char u8;
typedef unsigned int u16;

sbit Led_R = P1^7;
sbit Led_G = P3^4;
sbit Led_B = P5^5;

sbit Key1 = P3^6;       // 00模式
sbit Key2 = P3^5;       // 00模式
sbit Key3 = P5^1;       // 00模式

bit  Key_flag=0;        //按键松开按下标识
u8   Count;

void delay_ms(u16 ms)        //@11.0592MHz
{
        unsigned char data i, j;
        while (ms--)
        {
                i = 15;
                j = 90;
                do
                {
                        while (--j);
                } while (--i);
        }
}

void IO_Int(void)
{
        P0M1 = 0x00;   P0M0 = 0x00;
        P1M1 = 0x00;   P1M0 = 0x00;
        P2M1 = 0x00;   P2M0 = 0x00;
        P3M1 = 0x00;   P3M0 = 0x00;
        P4M1 = 0x00;   P4M0 = 0x00;
        P5M1 = 0x00;   P5M0 = 0x00;
        P6M1 = 0xff;   P6M0 = 0x00;
        P7M1 = 0x00;   P7M0 = 0x00;
}

void Key_Check(void)
{
        if(!Key1||!Key2||!Key3)
        {
                if(++Count>=20 && Key_flag==0)
                {         
                        Key_flag=1;
                        if(!Key1)Led_R=~Led_R;
                        if(!Key2)Led_B=~Led_B;
                        if(!Key3)Led_G=~Led_G;
                }
        }   
        else
        {
                Count=0;
                Key_flag=0;
        }
}

void main(void)
{   
        IO_Int();
        while(1)
        {
                Key_Check();
                delay_ms(1);
        }
}
回复

使用道具 举报

ID:226055 发表于 2024-1-23 20:22 | 显示全部楼层
    1、STC8H3K64S4的IO口带内部上拉电阻,如果硬件外部没有上拉,那么程序配置将按键用IO口上拉打开。
    2、按键检测有连按与非连按的算法,你的程序按着有反应,说明硬件上面应该是好的。可以着重从软件入手。
程序1.jpg 程序2.jpg

    3、手头有一块艾克姆的STC8H3K64S4的板子,和你的单片机型号完全一样,附件把按键动作翻转指示灯的程序附上,尤其指示灯翻转的函数你也可以参考。主要换一下IO口和你的一致即可。
开发板.jpg

key.rar

37.03 KB, 下载次数: 0

回复

使用道具 举报

ID:198296 发表于 2024-1-24 07:19 | 显示全部楼层
按键 输入, 影响 LED 扫描延迟, 所以闪烁.

解决方法:
利用 2次或多次的  LED 扫描 时间差, 作为键输入的延迟消抖
回复

使用道具 举报

ID:337139 发表于 2024-1-24 09:51 | 显示全部楼层
wulin 发表于 2024-1-23 17:10
初学尽量简单些,整明白了就可循序渐进。这样可以提振信心。给你改了,你试试。
#include "stc8h.h"

谢谢,这样是条理清楚,结构明了。
回复

使用道具 举报

ID:337139 发表于 2024-1-24 09:55 | 显示全部楼层
cocolala 发表于 2024-1-23 20:22
1、STC8H3K64S4的IO口带内部上拉电阻,如果硬件外部没有上拉,那么程序配置将按键用IO口上拉打开。
   ...

很有参考价值,谢谢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表