找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 635|回復: 3
打印 上一主題 下一主題
收起左側

求助STC8G1K08單片機的紅外通信問題

[復制鏈接]
跳轉到指定樓層
樓主
ID:1144471 發(fā)表于 2025-2-26 20:30 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
紅外遙控LED代碼在51單片機上能運行,但是我移植到STC8G1K08最小系統(tǒng)上面卻沒有效果,能接收到紅外信號(測試過按哪個按鍵LED都會亮),后面代碼改成按遙控器電源鍵LED才會亮,但是并沒有亮。問題就是為什么按電源鍵LED沒有亮?


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

// 定義紅外數(shù)據(jù)位數(shù)
#define IR_BIT_COUNT    32   // NEC 協(xié)議包含 32 位數(shù)據(jù)

// 定義紅外信號的時間參數(shù)(單位: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 位數(shù)據(jù)
    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
        }
    }
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發(fā)
ID:866313 發(fā)表于 2025-2-27 02:59 | 只看該作者
以下內(nèi)容由 DeepSeek R1 生成
根據(jù)問題描述和代碼分析,問題可能出在以下幾個方面:
1. 數(shù)據(jù)位處理錯誤
原代碼中使用unsigned char ir_data(8位)存儲32位數(shù)據(jù),導致高位數(shù)據(jù)丟失。應改用32位變量:
unsigned long IrDecodeValue(void) {
     unsigned long ir_data = 0;
     // ...其余代碼
     // 讀取32位數(shù)據(jù)時,按位存儲到ir_data
     ir_data <<= 1;
     ir_data |= (當前位值);
     // ...
     return (ir_data >> 8) & 0xFF; // 返回數(shù)據(jù)碼(假設第三字節(jié))
}2. 時間閾值與延時函數(shù)不匹配
STC8G1K08默認使用24MHz時鐘(1T模式),而傳統(tǒng)51多為12MHz(12T模式)。原Delay10us在24MHz下實際延時可能過短。需重新校準:
void Delay10us(void) {
     unsigned char i = 24; // 調(diào)整i的值,用示波器校準
     while (--i);
}3. 電平檢測邏輯錯誤
NEC協(xié)議中,邏輯0/1由高電平時間決定,而非低電平。應修改GetTime函數(shù)以測量高電平持續(xù)時間:
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; // 根據(jù)24MHz調(diào)整
     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位數(shù)據(jù)
     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) { // 數(shù)據(jù)碼在第三字節(jié)
             P12 = !P12; // 翻轉LED
         }
     }
}調(diào)試建議
  • 使用示波器:檢查紅外接收頭的輸出波形,確認信號是否符合NEC協(xié)議。
  • 打印鍵值:通過串口輸出接收到的鍵值,確認是否正確解析電源鍵(如0x45)。
  • 調(diào)整延時參數(shù):根據(jù)實際時鐘頻率,校準Delay10us的準確性。
  • 檢查硬件:確認紅外接收器電源穩(wěn)定,信號線連接正確(P3.2),LED電路無故障。

通過以上修正和調(diào)試步驟,應能解決STC8G1K08紅外通信不響應的問題。

回復

使用道具 舉報

板凳
ID:161164 發(fā)表于 2025-2-27 11:22 | 只看該作者
延時代碼有問題
請用AIapp-ISP生成對應的晶振的延時函數(shù)
回復

使用道具 舉報

地板
ID:152164 發(fā)表于 2025-2-27 19:06 來自手機 | 只看該作者
isp軟件的例程里有紅外代碼,用那個改嘛
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表