久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標題: 求助STC8G1K08單片機的紅外通信問題 [打印本頁]

作者: 楊JJw    時間: 2025-2-26 20:30
標題: 求助STC8G1K08單片機的紅外通信問題
紅外遙控LED代碼在51單片機上能運行,但是我移植到STC8G1K08最小系統上面卻沒有效果,能接收到紅外信號(測試過按哪個按鍵LED都會亮),后面代碼改成按遙控器電源鍵LED才會亮,但是并沒有亮。問題就是為什么按電源鍵LED沒有亮?


代碼如下:
#include "STC8G.h"
#include <intrins.h>
#include "IR.h"

// 定義紅外數據位數
#define IR_BIT_COUNT    32   // NEC 協議包含 32 位數據

// 定義紅外信號的時間參數(單位:10us)
#define TIME_HIGH_0      12  // 邏輯 0 的高電平時間(~1.125ms)
#define TIME_LOW_0       6   // 邏輯 0 的低電平時間(~0.5625ms)
#define TIME_HIGH_1      12  // 邏輯 1 的高電平時間(~0.5625ms)
#define TIME_LOW_1       18  // 邏輯 1 的低電平時間(~1.6875ms)

void Delay10us(void) {
    unsigned char i;
    i = 27;
    while (--i);
}

void Delay500ms(void)
{
    unsigned char data i,j,k;

    _nop_();
    i=4;
    j=129;
    k=119;
    do
    {
        do
        {
            while (--k);
        }   while(--j);
    }   while(--i);
}

unsigned int GetTime(void) {
    unsigned int time = 0;
    while (P32 == 0) {  // 等待低電平結束
        Delay10us();
        time++;
    }
    while (P32 == 1) {  // 等待高電平結束
        Delay10us();
        time++;
    }
    return time;
}

unsigned char IrDecodeValue(void) {
    unsigned char ir_data = 0;
    unsigned char bit_count = 0;
    unsigned int time;

    // 等待起始信號
    while (P32 == 1);
    while (P32 == 0);
    Delay10us();
    while (P32 == 1) {
        Delay10us();
        time++;
        if (time > 450) return 0xFF;  // 超過 4.5ms,返回無效值
    }

    // 讀取 32 位數據
    while (bit_count < IR_BIT_COUNT) {
        while (P32 == 1) Delay10us();  // 等待高電平結束
        time = GetTime();              // 測量低電平時間

        if (time > TIME_LOW_0 && time < TIME_LOW_1) {
            ir_data <<= 1;
            ir_data |= 0;         // 邏輯 0
        } else if (time > TIME_LOW_1) {
            ir_data <<= 1;
            ir_data |= 1;         // 邏輯 1
        } else {
            return 0xFF;          // 時間異常,返回無效值
        }

        bit_count++;
    }

    return ir_data;
}

void main() {
    P1M0 = 0x00;  // 設置 P1 準雙向模式
    P1M1 = 0x00;

    while (1) {
        unsigned char key = IrDecodeValue();
        if (key == 0x45) {       // 檢測按鍵值是否為 IR_POWER (0x45)
            P12 = 1;             // 點亮 LED
            Delay500ms();        // 延時保持亮燈
            P12 = 0;             // 熄滅 LED
        }
    }
}


作者: cedtek    時間: 2025-2-27 02:59
以下內容由 DeepSeek R1 生成
根據問題描述和代碼分析,問題可能出在以下幾個方面:
1. 數據位處理錯誤
原代碼中使用unsigned char ir_data(8位)存儲32位數據,導致高位數據丟失。應改用32位變量:
unsigned long IrDecodeValue(void) {
     unsigned long ir_data = 0;
     // ...其余代碼
     // 讀取32位數據時,按位存儲到ir_data
     ir_data <<= 1;
     ir_data |= (當前位值);
     // ...
     return (ir_data >> 8) & 0xFF; // 返回數據碼(假設第三字節)
}2. 時間閾值與延時函數不匹配
STC8G1K08默認使用24MHz時鐘(1T模式),而傳統51多為12MHz(12T模式)。原Delay10us在24MHz下實際延時可能過短。需重新校準:
void Delay10us(void) {
     unsigned char i = 24; // 調整i的值,用示波器校準
     while (--i);
}3. 電平檢測邏輯錯誤
NEC協議中,邏輯0/1由高電平時間決定,而非低電平。應修改GetTime函數以測量高電平持續時間:
unsigned int GetHighTime(void) {
     unsigned int time = 0;
     while (P32 == 1) { // 測量高電平時間
         Delay10us();
         time++;
         if(time > 1000) break; // 超時退出
     }
     return time;
}4. 未正確處理引導碼
原代碼未正確檢測9ms低電平+4.5ms高電平的引導碼。需添加引導碼驗證:
// 檢測引導碼
while (P32 == 1); // 等待高電平結束
unsigned int low_time = GetTime(); // 測量低電平時間
if (low_time < 800 || low_time > 1000) return 0xFF; // 非引導碼

unsigned int high_time = GetTime(); // 測量高電平時間
if (high_time < 400 || high_time > 500) return 0xFF; // 非引導碼5. 引腳配置問題
確保P3.2(紅外輸入)和P1.2(LED)配置正確:
void main() {
     P1M0 = 0x00; // P1準雙向
     P1M1 = 0x00;
     P3M0 &= ~0x04; // P3.2準雙向或高阻輸入
     P3M1 &= ~0x04;
     // ...其余代碼
}修正后的示例代碼 #include "STC8G.h"
#include <intrins.h>

#define IR_DATA_BITS 32

void Delay10us() {
     unsigned char i = 24; // 根據24MHz調整
     while (--i);
}

unsigned int GetHighTime() {
     unsigned int time = 0;
     while (P32 == 1) {
         Delay10us();
         time++;
         if (time > 1000) break;
     }
     return time;
}

unsigned long IrDecode() {
     unsigned long data = 0;
     unsigned int time;

     // 檢測引導碼
     while (P32 == 1);
     time = 0;
     while (P32 == 0) { // 測量低電平時間
         Delay10us();
         time++;
         if (time > 1000) return 0;
     }
     if (time < 800) return 0; // 9ms低電平應為約900個10us

     time = 0;
     while (P32 == 1) { // 測量高電平時間
         Delay10us();
         time++;
         if (time > 500) return 0;
     }
     if (time < 400) return 0; // 4.5ms高電平應為約450個10us

     // 讀取32位數據
     for (int i=0; i<IR_DATA_BITS; i++) {
         while (P32 == 0); // 等待低電平結束
         unsigned int high_time = GetHighTime();
         data <<= 1;
         if (high_time > 100) { // 高電平時間>1ms為邏輯1
             data |= 1;
         }
     }
     return data;
}

void main() {
     P1M0 = 0x00;
     P1M1 = 0x00;
     P32 = 1; // 使能P3.2上拉

     while (1) {
         unsigned long ir_code = IrDecode();
         if ((ir_code >> 8) == 0x45) { // 數據碼在第三字節
             P12 = !P12; // 翻轉LED
         }
     }
}調試建議
通過以上修正和調試步驟,應能解決STC8G1K08紅外通信不響應的問題。


作者: lkc8210    時間: 2025-2-27 11:22
延時代碼有問題
請用AIapp-ISP生成對應的晶振的延時函數
作者: ziiyn    時間: 2025-2-27 19:06
isp軟件的例程里有紅外代碼,用那個改嘛




歡迎光臨 (http://www.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 亚洲精品永久免费 | 成年免费大片黄在线观看岛国 | 亚洲天天干 | 中文字幕精品视频 | 97精品超碰一区二区三区 | 亚洲精品电影网在线观看 | 色www精品视频在线观看 | 欧美激情国产日韩精品一区18 | 五月网婷婷 | 欧美二三区 | 久久久精品久久久 | 在线视频久久 | 91视频免费黄 | 超碰精品在线 | av影音在线 | 国产三区av | 看片wwwwwwwwwww | 国产精品a一区二区三区网址 | 国产精品一区二区视频 | 成人免费视频网站在线观看 | 日本高清视频在线播放 | 久久久久国产一区二区三区不卡 | 免费不卡视频 | 欧美一级毛片在线播放 | 成人久久18免费网站 | 欧美日韩视频在线 | 久久9视频| av一区在线观看 | av一区二区三区四区 | 国产福利精品一区 | 精品中文字幕一区 | 亚洲高清av | 蜜臀网| 国产激情偷乱视频一区二区三区 | 欧美日韩亚洲系列 | 久久久久久久夜 | 狠狠的日 | 欧美一二精品 | 免费视频一区 | 国产一级免费视频 | 国产成人精品一区二区三区四区 |