欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標題: STC8H3K64S4單片機輸入口檢查不到輸入口狀態 [打印本頁]

作者: 長風007    時間: 2024-1-23 11:26
標題: STC8H3K64S4單片機輸入口檢查不到輸入口狀態
運行的結果是,按鍵只能按一下檢測到后中,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();        


      }



}

作者: 范莉萍    時間: 2024-1-23 13:32
設置成雙向
作者: wulin    時間: 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);
        }
}
作者: cocolala    時間: 2024-1-23 20:22
    1、STC8H3K64S4的IO口帶內部上拉電阻,如果硬件外部沒有上拉,那么程序配置將按鍵用IO口上拉打開。
    2、按鍵檢測有連按與非連按的算法,你的程序按著有反應,說明硬件上面應該是好的。可以著重從軟件入手。


    3、手頭有一塊艾克姆的STC8H3K64S4的板子,和你的單片機型號完全一樣,附件把按鍵動作翻轉指示燈的程序附上,尤其指示燈翻轉的函數你也可以參考。主要換一下IO口和你的一致即可。


key.rar

37.03 KB, 下載次數: 0


作者: laoxu    時間: 2024-1-24 07:19
按鍵 輸入, 影響 LED 掃描延遲, 所以閃爍.

解決方法:
利用 2次或多次的  LED 掃描 時間差, 作為鍵輸入的延遲消抖
作者: 長風007    時間: 2024-1-24 09:51
wulin 發表于 2024-1-23 17:10
初學盡量簡單些,整明白了就可循序漸進。這樣可以提振信心。給你改了,你試試。
#include "stc8h.h"

謝謝,這樣是條理清楚,結構明了。
作者: 長風007    時間: 2024-1-24 09:55
cocolala 發表于 2024-1-23 20:22
1、STC8H3K64S4的IO口帶內部上拉電阻,如果硬件外部沒有上拉,那么程序配置將按鍵用IO口上拉打開。
   ...

很有參考價值,謝謝!
作者: 長風007    時間: 2024-12-6 09:54
cocolala 發表于 2024-1-23 20:22
1、STC8H3K64S4的IO口帶內部上拉電阻,如果硬件外部沒有上拉,那么程序配置將按鍵用IO口上拉打開。
   ...

非常感謝你的幫助!前段時間因為兩個緊急的項目在做,一直沒有時間來調試。現在回來解決這個遺留問題。
作者: lkc8210    時間: 2024-12-6 17:56
長風007 發表于 2024-12-6 09:54
非常感謝你的幫助!前段時間因為兩個緊急的項目在做,一直沒有時間來調試。現在回來解決這個遺留問題。

你貼的代碼不完整啊
u8沒定義
Key從哪里冒出來的?




歡迎光臨 (http://www.raoushi.com/bbs/) Powered by Discuz! X3.1