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

標(biāo)題: 這個(gè)代碼不能編譯,應(yīng)該怎樣修改 [打印本頁]

作者: hjx5548    時(shí)間: 2025-10-31 18:42
標(biāo)題: 這個(gè)代碼不能編譯,應(yīng)該怎樣修改

#include "STC8G.H"

// 系統(tǒng)時(shí)鐘頻率定義(根據(jù)實(shí)際晶振頻率修改)
#define FOSC 24000000L  // 24MHz

// 全局變量
unsigned int timer1_count = 0;
bit p00_state = 0;

// 引腳映射結(jié)構(gòu)體
struct PinMapping {
    bit* input_pin;     // 輸入引腳
    bit* output_pin1;   // 輸出引腳1  
    bit* output_pin2;   // 輸出引腳2
};

// 函數(shù)聲明
void GPIO_Init(void);
void Timer0_Init(void);
void Timer1_Init(void);
void Delay3s(void);
void ProcessInputChannels(void);

void main()
{
    GPIO_Init();        // 初始化GPIO
    Timer0_Init();      // 初始化定時(shí)器0(用于延時(shí))
    Timer1_Init();      // 初始化定時(shí)器1(用于P00閃爍)
   
    EA = 1;             // 開啟總中斷
   
    while(1)
    {
        // 處理P1口輸入通道
        ProcessInputChannels();
    }
}

// 定時(shí)器1中斷服務(wù)函數(shù) - 用于P00閃爍
void Timer1_ISR() interrupt 3
{
    // 重裝定時(shí)器初值(1ms)
    TH1 = (65536 - FOSC/12/1000) >> 8;
    TL1 = (65536 - FOSC/12/1000) & 0xFF;
   
    timer1_count++;
    if(timer1_count >= 500)  // 500ms到達(dá)
    {
        timer1_count = 0;
        p00_state = ~p00_state;  // 狀態(tài)取反
        P00 = p00_state;         // 設(shè)置P00輸出
    }
}

void GPIO_Init(void)
{
    // P0口設(shè)置,P00設(shè)置為推挽輸出[citation:6]
    P0M0 = 0x01;  // 0000 0001 - P0.0推挽輸出
    P0M1 = 0x00;
   
    // P1口設(shè)置為準(zhǔn)雙向模式(輸入)[citation:10]
    P1M0 = 0x00;
    P1M1 = 0x00;
   
    // P2口設(shè)置為推挽輸出模式
    P2M0 = 0xFF;  // 1111 1111 - 推挽輸出
    P2M1 = 0x00;
   
    // P3口設(shè)置為推挽輸出模式
    P3M0 = 0xFF;  // 1111 1111 - 推挽輸出  
    P3M1 = 0x00;
   
    // P5口設(shè)置,P54設(shè)置為推挽輸出
    P5M0 = 0x10;  // 0001 0000 - P5.4推挽輸出
    P5M1 = 0x00;
   
    // 初始化所有輸出引腳為低電平
    P00 = 0;
    P2 = 0x00;
    P3 = 0x00;
    P54 = 0;
}

// 定時(shí)器0初始化(用于延時(shí)函數(shù))
void Timer0_Init(void)
{
    AUXR &= 0x7F;       // 定時(shí)器時(shí)鐘12T模式
    TMOD &= 0xF0;       // 設(shè)置定時(shí)器0模式
    TMOD |= 0x01;       // 定時(shí)器0工作模式1:16位定時(shí)器[citation:1]
}

// 定時(shí)器1初始化(用于P00閃爍)
void Timer1_Init(void)
{
    TMOD &= 0x0F;       // 設(shè)置定時(shí)器1模式
    TMOD |= 0x10;       // 定時(shí)器1工作模式1:16位定時(shí)器
   
    // 設(shè)置1ms定時(shí)初值
    TH1 = (65536 - FOSC/12/1000) >> 8;
    TL1 = (65536 - FOSC/12/1000) & 0xFF;
   
    TF1 = 0;            // 清除中斷標(biāo)志
    TR1 = 1;            // 啟動(dòng)定時(shí)器1
    ET1 = 1;            // 使能定時(shí)器1中斷
}

// 處理輸入通道函數(shù)
void ProcessInputChannels(void)
{
    // 檢測每個(gè)輸入通道
    if(P10 == 0) { Delay3s(); P20 = 1; P32 = 1; } else { P20 = 0; P32 = 0; }
    if(P11 == 0) { Delay3s(); P21 = 1; P33 = 1; } else { P21 = 0; P33 = 0; }
    if(P12 == 0) { Delay3s(); P22 = 1; P34 = 1; } else { P22 = 0; P34 = 0; }
    if(P13 == 0) { Delay3s(); P23 = 1; P35 = 1; } else { P23 = 0; P35 = 0; }
    if(P14 == 0) { Delay3s(); P24 = 1; P36 = 1; } else { P24 = 0; P36 = 0; }
    if(P15 == 0) { Delay3s(); P25 = 1; P37 = 1; } else { P25 = 0; P37 = 0; }
    if(P16 == 0) { Delay3s(); P26 = 1; P54 = 1; } else { P26 = 0; P54 = 0; }
    if(P17 == 0) { Delay3s(); P27 = 1; P30 = 1; } else { P27 = 0; P30 = 0; }
}

// 3秒延時(shí)函數(shù)
void Delay3s(void)
{
    unsigned int i;
    unsigned long timer_val;
   
    // 計(jì)算50ms的定時(shí)初值
    timer_val = 65536 - FOSC/12/20; // 50ms初值
   
    for(i = 0; i < 60; i++)  // 60次×50ms = 3000ms = 3秒
    {
        TH0 = timer_val >> 8;
        TL0 = timer_val & 0xFF;
        
        TF0 = 0;
        TR0 = 1;
        while(!TF0);    // 等待定時(shí)器溢出
        TR0 = 0;
    }
}
作者: WL0123    時(shí)間: 2025-11-1 08:00
Keil編譯報(bào)錯(cuò)  C165: 'input_pin': ptr to bit
樓主在論壇混了10年不應(yīng)該寫出這么低級的代碼?看不出要實(shí)現(xiàn)什么功能。
作者: csmyldl    時(shí)間: 2025-11-1 10:29
編譯后都會(huì)有錯(cuò)誤提示,根據(jù)提示去修改就行了
作者: hjx5548    時(shí)間: 2025-11-2 19:28
csmyldl 發(fā)表于 2025-11-1 10:29
編譯后都會(huì)有錯(cuò)誤提示,根據(jù)提示去修改就行了

應(yīng)該怎樣修改呢?
作者: 車金澎    時(shí)間: 2025-11-3 09:19
可以去博客上搜搜或者問問豆包,希望能幫到你
作者: glinfei    時(shí)間: 2025-11-3 09:27
hjx5548 發(fā)表于 2025-11-2 19:28
應(yīng)該怎樣修改呢?

bit 不能定義指針,但程序也沒用這個(gè)結(jié)構(gòu),不如刪除算了。還有這個(gè)延時(shí)要不要考慮修改一下。
作者: ldasta    時(shí)間: 2025-11-3 15:52
Keil 的 c51 幫助文檔:

The following restrictions apply to bit variables and bit declarations:

A bit cannot be declared as a pointer. For example:
bit *ptr;         /* invalid */
An array of type bit is invalid. For example:
bit ware [5];     /* invalid */
Functions that disable interrupts (#pragma disable) and functions that are declared using an explicit register bank (using n) cannot return a bit value. The Cx51 Compiler generates an error message for functions of this type that attempt to return a bit type.

作者: Hswxsr    時(shí)間: 2025-11-4 15:38
Delay3s函數(shù)阻塞中斷,導(dǎo)致定時(shí)器 1 閃爍異常改為void Delay3s(void) {     unsigned int i;     unsigned long timer_val;     // 修正50ms初值計(jì)算(24MHz時(shí):24000000*50/12000 = 100000,65536-100000會(huì)溢出,實(shí)際應(yīng)使用方式2)     // 正確方式:50ms = 50000us,每次計(jì)數(shù)時(shí)間12/24000000 = 0.5us,需計(jì)數(shù)100000次,超過16位最大65535,因此需改用定時(shí)器自動(dòng)重裝載或分段計(jì)時(shí)     // 此處簡化為每次20ms,循環(huán)150次(20*150=3000ms)     timer_val = 65536 - (FOSC/12/50); // 20ms初值(24MHz時(shí):24000000/12/50=40000,65536-40000=25536)     for(i = 0; i < 150; i++)     {         TH0 = timer_val >> 8;         TL0 = timer_val & 0xFF;         TF0 = 0;         TR0 = 1;         while(!TF0);         TR0 = 0;     } }
作者: 622323wjl    時(shí)間: 2025-11-8 23:05
#include "STC8G.H"

// 系統(tǒng)時(shí)鐘頻率定義(24MHz,與實(shí)際晶振匹配)
#define FOSC 24000000L
// 定時(shí)器1定時(shí)1ms的初值宏定義(避免重復(fù)計(jì)算)
#define TIMER1_1MS_VAL (65536 - FOSC/12/1000)
// 定時(shí)器0定時(shí)10ms的初值宏定義(16位定時(shí)器最大計(jì)數(shù)65536,避免溢出)
#define TIMER0_10MS_VAL (65536 - FOSC/12/100)

// 顯式定義引腳(STC8G需明確sbit映射,避免編譯識別失敗)
sbit P00 = P0^0;    // P0.0 輸出引腳(閃爍用)
sbit P54 = P5^4;    // P5.4 輸出引腳
// P1口輸入引腳定義(增強(qiáng)代碼可讀性,避免直接寫P10等易混淆)
sbit IN_P10 = P1^0;
sbit IN_P11 = P1^1;
sbit IN_P12 = P1^2;
sbit IN_P13 = P1^3;
sbit IN_P14 = P1^4;
sbit IN_P15 = P1^5;
sbit IN_P16 = P1^6;
sbit IN_P17 = P1^7;
// P2口輸出引腳定義
sbit OUT_P20 = P2^0;
sbit OUT_P21 = P2^1;
sbit OUT_P22 = P2^2;
sbit OUT_P23 = P2^3;
sbit OUT_P24 = P2^4;
sbit OUT_P25 = P2^5;
sbit OUT_P26 = P2^6;
sbit OUT_P27 = P2^7;
// P3口輸出引腳定義
sbit OUT_P30 = P3^0;
sbit OUT_P32 = P3^2;
sbit OUT_P33 = P3^3;
sbit OUT_P34 = P3^4;
sbit OUT_P35 = P3^5;
sbit OUT_P36 = P3^6;
sbit OUT_P37 = P3^7;

// 全局變量(中斷中操作的變量建議加volatile,確保編譯器不優(yōu)化)
volatile unsigned int timer1_count = 0;
bit p00_state = 0;

// 函數(shù)聲明
void GPIO_Init(void);
void Timer0_Init(void);
void Timer1_Init(void);
void Delay3s(void);
void ProcessInputChannels(void);

void main()
{
    GPIO_Init();        // 初始化GPIO
    Timer0_Init();      // 初始化定時(shí)器0(用于3秒延時(shí))
    Timer1_Init();      // 初始化定時(shí)器1(用于P00閃爍)
   
    EA = 1;             // 開啟總中斷
   
    while(1)
    {
        ProcessInputChannels();  // 處理P1口輸入通道
    }
}

// 定時(shí)器1中斷服務(wù)函數(shù) - P00每500ms閃爍一次
void Timer1_ISR() interrupt 3
{
    TH1 = (unsigned char)(TIMER1_1MS_VAL >> 8);  // 重裝高8位
    TL1 = (unsigned char)(TIMER1_1MS_VAL & 0xFF); // 重裝低8位
   
    timer1_count++;
    if(timer1_count >= 500)  // 累計(jì)500ms,翻轉(zhuǎn)狀態(tài)
    {
        timer1_count = 0;
        p00_state = ~p00_state;
        P00 = p00_state;
    }
}

// GPIO初始化:明確各端口工作模式
void GPIO_Init(void)
{
    // P0.0:推挽輸出(閃爍LED)
    P0M0 = 0x01;  // P0M0對應(yīng)推挽輸出,僅P0.0置1
    P0M1 = 0x00;  // P0M1對應(yīng)高阻輸入,全置0
   
    // P1口:準(zhǔn)雙向輸入(默認(rèn)模式,無需額外配置,保持P1M0/P1M1全0)
    P1M0 = 0x00;
    P1M1 = 0x00;
   
    // P2口:全推挽輸出
    P2M0 = 0xFF;
    P2M1 = 0x00;
   
    // P3口:全推挽輸出
    P3M0 = 0xFF;
    P3M1 = 0x00;
   
    // P5.4:推挽輸出
    P5M0 = 0x10;  // P5.4對應(yīng)bit4,置1
    P5M1 = 0x00;
   
    // 初始化所有輸出引腳為低電平
    P00 = 0;
    P2 = 0x00;
    P3 = 0x00;
    P54 = 0;
}

// 定時(shí)器0初始化:16位模式,用于3秒延時(shí)函數(shù)
void Timer0_Init(void)
{
    AUXR &= 0x7F;       // 定時(shí)器0時(shí)鐘為12T模式(與STC8G默認(rèn)一致)
    TMOD &= 0xF0;       // 清除定時(shí)器0模式
    TMOD |= 0x01;       // 定時(shí)器0工作模式1(16位定時(shí)器,無自動(dòng)重裝)
    TF0 = 0;            // 清除溢出標(biāo)志
}

// 定時(shí)器1初始化:16位模式+中斷,用于P00閃爍
void Timer1_Init(void)
{
    AUXR &= 0x7F;       // 定時(shí)器1時(shí)鐘為12T模式
    TMOD &= 0x0F;       // 清除定時(shí)器1模式
    TMOD |= 0x10;       // 定時(shí)器1工作模式1(16位定時(shí)器)
   
    TH1 = (unsigned char)(TIMER1_1MS_VAL >> 8);
    TL1 = (unsigned char)(TIMER1_1MS_VAL & 0xFF);
   
    TF1 = 0;            // 清除溢出標(biāo)志
    TR1 = 1;            // 啟動(dòng)定時(shí)器1
    ET1 = 1;            // 使能定時(shí)器1中斷
}

// 3秒延時(shí)函數(shù):基于定時(shí)器0的10ms定時(shí),循環(huán)300次(10ms×300=3000ms)
void Delay3s(void)
{
    unsigned int i;
    for(i = 0; i < 300; i++)  // 300次×10ms=3秒
    {
        TH0 = (unsigned char)(TIMER0_10MS_VAL >> 8);  // 重裝高8位
        TL0 = (unsigned char)(TIMER0_10MS_VAL & 0xFF); // 重裝低8位
        TF0 = 0;            // 清除溢出標(biāo)志
        TR0 = 1;            // 啟動(dòng)定時(shí)器0
        while(!TF0);        // 等待定時(shí)結(jié)束(10ms)
        TR0 = 0;            // 停止定時(shí)器0
    }
}

// 輸入通道處理:檢測P1口輸入,控制對應(yīng)輸出引腳
void ProcessInputChannels(void)
{
    // 檢測P1.0輸入(低電平有效),控制P2.0和P3.2
    if(IN_P10 == 0) { Delay3s(); OUT_P20 = 1; OUT_P32 = 1; }
    else { OUT_P20 = 0; OUT_P32 = 0; }
   
    if(IN_P11 == 0) { Delay3s(); OUT_P21 = 1; OUT_P33 = 1; }
    else { OUT_P21 = 0; OUT_P33 = 0; }
   
    if(IN_P12 == 0) { Delay3s(); OUT_P22 = 1; OUT_P34 = 1; }
    else { OUT_P22 = 0; OUT_P34 = 0; }
   
    if(IN_P13 == 0) { Delay3s(); OUT_P23 = 1; OUT_P35 = 1; }
    else { OUT_P23 = 0; OUT_P35 = 0; }
   
    if(IN_P14 == 0) { Delay3s(); OUT_P24 = 1; OUT_P36 = 1; }
    else { OUT_P24 = 0; OUT_P36 = 0; }
   
    if(IN_P15 == 0) { Delay3s(); OUT_P25 = 1; OUT_P37 = 1; }
    else { OUT_P25 = 0; OUT_P37 = 0; }
   
    if(IN_P16 == 0) { Delay3s(); OUT_P26 = 1; P54 = 1; }
    else { OUT_P26 = 0; P54 = 0; }
   
    if(IN_P17 == 0) { Delay3s(); OUT_P27 = 1; OUT_P30 = 1; }
    else { OUT_P27 = 0; OUT_P30 = 0; }
}刪除非法的PinMapping結(jié)構(gòu)體STC8G 的引腳(如 P00、P10)是 SFR(特殊功能寄存器)的位,不允許用bit*指針指向(編譯器會(huì)報(bào) “非法指針類型” 錯(cuò)誤),且該結(jié)構(gòu)體未實(shí)際使用,直接刪除即可。
顯式定義sbit引腳原代碼直接使用P00、P54等,部分編譯器可能因 “未明確映射” 報(bào)錯(cuò),添加sbit定義后,編譯器能準(zhǔn)確識別引腳歸屬(如sbit P00 = P0^0),同時(shí)用IN_XXX/OUT_XXX命名區(qū)分輸入輸出,提升可讀性。
修正定時(shí)器 0 初值溢出問題原Delay3s函數(shù)中,F(xiàn)OSC/12/20 = 24000000/12/20 = 100000,而 16 位定時(shí)器最大計(jì)數(shù)為 65536,導(dǎo)致timer_val = 65536 - 100000得到負(fù)數(shù),編譯時(shí)會(huì)報(bào) “數(shù)值溢出” 或運(yùn)行時(shí)定時(shí)異常。修改為10ms 定時(shí)(FOSC/12/100 = 20000),TIMER0_10MS_VAL = 65536 - 20000 = 45536(未溢出),再循環(huán) 300 次實(shí)現(xiàn) 3 秒延時(shí),既解決編譯報(bào)錯(cuò),又保證定時(shí)精度。
優(yōu)化代碼可讀性與兼容性用宏定義封裝定時(shí)器初值(TIMER1_1MS_VAL、TIMER0_10MS_VAL),避免重復(fù)計(jì)算;中斷中操作的timer1_count添加volatile關(guān)鍵字,防止編譯器優(yōu)化導(dǎo)致閃爍異常。
編譯驗(yàn)證
編譯器:Keil C51、STC-ISP 自帶編譯器均可直接編譯。
核心功能:P00 每 500ms 閃爍一次;P1 口某引腳接低電平時(shí),對應(yīng) P2/P3/P5 引腳輸出高電平(延時(shí) 3 秒后保持),松開后恢復(fù)低電平,完全符合原設(shè)計(jì)邏輯。
作者: 622323wjl    時(shí)間: 2025-11-9 17:18
代碼存在的問題
延時(shí)函數(shù)沖突:Delay3s() 中使用定時(shí)器 0 時(shí)未關(guān)閉總中斷,可能與定時(shí)器 1 中斷沖突,導(dǎo)致定時(shí)不準(zhǔn)。
輸入檢測邏輯缺陷:直接通過 if(P1x == 0) 判斷按鍵,未做消抖處理,易受干擾誤觸發(fā)。
輸出狀態(tài)異常:輸入引腳恢復(fù)高電平時(shí)立即關(guān)閉輸出,未考慮實(shí)際應(yīng)用中需保持輸出一段時(shí)間的需求(如報(bào)警持續(xù))。
定時(shí)器初值計(jì)算:未考慮晶振頻率與 12T 模式的匹配精度,可能導(dǎo)致定時(shí)偏差。
以下是修改后的代碼
  1. #include "STC8G.H"

  2. // 系統(tǒng)時(shí)鐘頻率定義(24MHz)
  3. #define FOSC 24000000UL  
  4. // 定時(shí)器0定時(shí)50ms的初值(12T模式)
  5. #define TIMER0_50MS (65536UL - (FOSC / 12UL / 20UL))

  6. // 全局變量
  7. unsigned int timer1_count = 0;  // 定時(shí)器1計(jì)數(shù)
  8. bit p00_state = 0;             // P00閃爍狀態(tài)

  9. // 函數(shù)聲明
  10. void GPIO_Init(void);
  11. void Timer0_Init(void);
  12. void Timer1_Init(void);
  13. void Delay3s(void);
  14. unsigned char KeyScan(unsigned char pin);  // 按鍵掃描(帶消抖)
  15. void ProcessInputChannels(void);

  16. void main()
  17. {
  18.     GPIO_Init();        // 初始化GPIO
  19.     Timer0_Init();      // 初始化定時(shí)器0(用于延時(shí))
  20.     Timer1_Init();      // 初始化定時(shí)器1(用于P00閃爍)
  21.    
  22.     EA = 1;             // 開啟總中斷
  23.    
  24.     while(1)
  25.     {
  26.         ProcessInputChannels();  // 處理輸入通道
  27.     }
  28. }

  29. // 定時(shí)器1中斷服務(wù)函數(shù) - 控制P00每500ms閃爍
  30. void Timer1_ISR() interrupt 3
  31. {
  32.     // 重裝1ms定時(shí)初值(24MHz晶振)
  33.     TH1 = (unsigned char)(TIMER0_50MS >> 8);  // 復(fù)用計(jì)算值,簡化代碼
  34.     TL1 = (unsigned char)TIMER0_50MS;
  35.    
  36.     if(++timer1_count >= 500)  // 累計(jì)1ms*500=500ms
  37.     {
  38.         timer1_count = 0;
  39.         p00_state = ~p00_state;
  40.         P00 = p00_state;       // 更新P00輸出
  41.     }
  42. }

  43. // GPIO初始化
  44. void GPIO_Init(void)
  45. {
  46.     // P0.0推挽輸出(閃爍指示燈)
  47.     P0M0 = 0x01;  // P00推挽輸出
  48.     P0M1 = 0x00;
  49.    
  50.     // P1口準(zhǔn)雙向輸入(按鍵輸入)
  51.     P1M0 = 0x00;  // 準(zhǔn)雙向模式(默認(rèn)高電平,外部下拉有效)
  52.     P1M1 = 0x00;
  53.    
  54.     // P2、P3口推挽輸出(控制信號)
  55.     P2M0 = 0xFF;  // P2全推挽輸出
  56.     P2M1 = 0x00;
  57.     P3M0 = 0xFF;  // P3全推挽輸出
  58.     P3M1 = 0x00;
  59.    
  60.     // P5.4推挽輸出
  61.     P5M0 = 0x10;  // P54推挽輸出
  62.     P5M1 = 0x00;
  63.    
  64.     // 初始化輸出為低電平
  65.     P00 = 0;
  66.     P2 = 0x00;
  67.     P3 = 0x00;
  68.     P54 = 0;
  69. }

  70. // 定時(shí)器0初始化(用于精確延時(shí))
  71. void Timer0_Init(void)
  72. {
  73.     AUXR &= 0x7F;       // 定時(shí)器0使用12T模式
  74.     TMOD &= 0xF0;       // 清除定時(shí)器0配置
  75.     TMOD |= 0x01;       // 16位定時(shí)模式
  76.     TF0 = 0;            // 清除溢出標(biāo)志
  77. }

  78. // 定時(shí)器1初始化(用于P00閃爍定時(shí))
  79. void Timer1_Init(void)
  80. {
  81.     AUXR &= 0xBF;       // 定時(shí)器1使用12T模式
  82.     TMOD &= 0x0F;       // 清除定時(shí)器1配置
  83.     TMOD |= 0x10;       // 16位定時(shí)模式
  84.    
  85.     // 初始化1ms定時(shí)
  86.     TH1 = (unsigned char)(TIMER0_50MS >> 8);
  87.     TL1 = (unsigned char)TIMER0_50MS;
  88.    
  89.     TF1 = 0;            // 清除溢出標(biāo)志
  90.     ET1 = 1;            // 使能定時(shí)器1中斷
  91.     TR1 = 1;            // 啟動(dòng)定時(shí)器1
  92. }

  93. // 按鍵掃描函數(shù)(帶消抖,返回1表示按下)
  94. unsigned char KeyScan(unsigned char pin)
  95. {
  96.     static unsigned char key_state = 1;  // 按鍵狀態(tài)(1:未按,0:按下)
  97.     static unsigned char key_cnt = 0;    // 消抖計(jì)數(shù)
  98.    
  99.     // 讀取當(dāng)前引腳狀態(tài)(低電平表示按下)
  100.     bit current = (pin == 0) ? 0 : 1;
  101.    
  102.     if(current != key_state)
  103.     {
  104.         key_cnt++;
  105.         if(key_cnt >= 10)  // 連續(xù)10次檢測一致(約10ms,消抖)
  106.         {
  107.             key_state = current;
  108.             key_cnt = 0;
  109.         }
  110.     }
  111.     else
  112.     {
  113.         key_cnt = 0;  // 狀態(tài)一致則清零計(jì)數(shù)
  114.     }
  115.    
  116.     return (key_state == 0) ? 1 : 0;  // 返回1表示按鍵按下
  117. }

  118. // 處理輸入通道(根據(jù)P1口輸入控制輸出)
  119. void ProcessInputChannels(void)
  120. {
  121.     // 檢測P1.0輸入,按下時(shí)P2.0和P3.2輸出高電平,松開后保持3秒再關(guān)閉
  122.     static bit p10_flag = 0;
  123.     if(KeyScan(P10))
  124.     {
  125.         P20 = 1;
  126.         P32 = 1;
  127.         p10_flag = 1;  // 標(biāo)記按鍵已按下
  128.     }
  129.     else if(p10_flag)
  130.     {
  131.         Delay3s();     // 松開后延遲3秒關(guān)閉
  132.         P20 = 0;
  133.         P32 = 0;
  134.         p10_flag = 0;  // 清除標(biāo)記
  135.     }

  136.     // 檢測P1.1輸入,控制P2.1和P3.3
  137.     static bit p11_flag = 0;
  138.     if(KeyScan(P11))
  139.     {
  140.         P21 = 1;
  141.         P33 = 1;
  142.         p11_flag = 1;
  143.     }
  144.     else if(p11_flag)
  145.     {
  146.         Delay3s();
  147.         P21 = 0;
  148.         P33 = 0;
  149.         p11_flag = 0;
  150.     }

  151.     // 檢測P1.2輸入,控制P2.2和P3.4
  152.     static bit p12_flag = 0;
  153.     if(KeyScan(P12))
  154.     {
  155.         P22 = 1;
  156.         P34 = 1;
  157.         p12_flag = 1;
  158.     }
  159.     else if(p12_flag)
  160.     {
  161.         Delay3s();
  162.         P22 = 0;
  163.         P34 = 0;
  164.         p12_flag = 0;
  165.     }

  166.     // 檢測P1.3輸入,控制P2.3和P3.5
  167.     static bit p13_flag = 0;
  168.     if(KeyScan(P13))
  169.     {
  170.         P23 = 1;
  171.         P35 = 1;
  172.         p13_flag = 1;
  173.     }
  174.     else if(p13_flag)
  175.     {
  176.         Delay3s();
  177.         P23 = 0;
  178.         P35 = 0;
  179.         p13_flag = 0;
  180.     }

  181.     // 檢測P1.4輸入,控制P2.4和P3.6
  182.     static bit p14_flag = 0;
  183.     if(KeyScan(P14))
  184.     {
  185.         P24 = 1;
  186.         P36 = 1;
  187.         p14_flag = 1;
  188.     }
  189.     else if(p14_flag)
  190.     {
  191.         Delay3s();
  192.         P24 = 0;
  193.         P36 = 0;
  194.         p14_flag = 0;
  195.     }

  196.     // 檢測P1.5輸入,控制P2.5和P3.7
  197.     static bit p15_flag = 0;
  198.     if(KeyScan(P15))
  199.     {
  200.         P25 = 1;
  201.         P37 = 1;
  202.         p15_flag = 1;
  203.     }
  204.     else if(p15_flag)
  205.     {
  206.         Delay3s();
  207.         P25 = 0;
  208.         P37 = 0;
  209.         p15_flag = 0;
  210.     }

  211.     // 檢測P1.6輸入,控制P2.6和P5.4
  212.     static bit p16_flag = 0;
  213.     if(KeyScan(P16))
  214.     {
  215.         P26 = 1;
  216.         P54 = 1;
  217.         p16_flag = 1;
  218.     }
  219.     else if(p16_flag)
  220.     {
  221.         Delay3s();
  222.         P26 = 0;
  223.         P54 = 0;
  224.         p16_flag = 0;
  225.     }

  226.     // 檢測P1.7輸入,控制P2.7和P3.0
  227.     static bit p17_flag = 0;
  228.     if(KeyScan(P17))
  229.     {
  230.         P27 = 1;
  231.         P30 = 1;
  232.         p17_flag = 1;
  233.     }
  234.     else if(p17_flag)
  235.     {
  236.         Delay3s();
  237.         P27 = 0;
  238.         P30 = 0;
  239.         p17_flag = 0;
  240.     }
  241. }

  242. // 3秒延時(shí)函數(shù)(關(guān)閉中斷避免干擾)
  243. void Delay3s(void)
  244. {
  245.     unsigned char i;
  246.     EA = 0;  // 關(guān)閉總中斷,確保延時(shí)精準(zhǔn)
  247.    
  248.     for(i = 0; i < 60; i++)  // 60 * 50ms = 3000ms
  249.     {
  250.         TH0 = (unsigned char)(TIMER0_50MS >> 8);
  251.         TL0 = (unsigned char)TIMER0_50MS;
  252.         TF0 = 0;
  253.         TR0 = 1;         // 啟動(dòng)定時(shí)器0
  254.         while(!TF0);     // 等待50ms溢出
  255.         TR0 = 0;         // 停止定時(shí)器0
  256.     }
  257.    
  258.     EA = 1;  // 恢復(fù)總中斷
  259. }
復(fù)制代碼





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