標(biāo)題: 求助STC8G1K08單片機(jī)的紅外通信問(wèn)題 [打印本頁(yè)]
作者: 楊JJw 時(shí)間: 2025-2-26 20:30
標(biāo)題: 求助STC8G1K08單片機(jī)的紅外通信問(wèn)題
紅外遙控LED代碼在51單片機(jī)上能運(yùn)行,但是我移植到STC8G1K08最小系統(tǒng)上面卻沒(méi)有效果,能接收到紅外信號(hào)(測(cè)試過(guò)按哪個(gè)按鍵LED都會(huì)亮),后面代碼改成按遙控器電源鍵LED才會(huì)亮,但是并沒(méi)有亮。問(wèn)題就是為什么按電源鍵LED沒(méi)有亮?
代碼如下:
#include "STC8G.h"
#include <intrins.h>
#include "IR.h"
// 定義紅外數(shù)據(jù)位數(shù)
#define IR_BIT_COUNT 32 // NEC 協(xié)議包含 32 位數(shù)據(jù)
// 定義紅外信號(hào)的時(shí)間參數(shù)(單位:10us)
#define TIME_HIGH_0 12 // 邏輯 0 的高電平時(shí)間(~1.125ms)
#define TIME_LOW_0 6 // 邏輯 0 的低電平時(shí)間(~0.5625ms)
#define TIME_HIGH_1 12 // 邏輯 1 的高電平時(shí)間(~0.5625ms)
#define TIME_LOW_1 18 // 邏輯 1 的低電平時(shí)間(~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) { // 等待低電平結(jié)束
Delay10us();
time++;
}
while (P32 == 1) { // 等待高電平結(jié)束
Delay10us();
time++;
}
return time;
}
unsigned char IrDecodeValue(void) {
unsigned char ir_data = 0;
unsigned char bit_count = 0;
unsigned int time;
// 等待起始信號(hào)
while (P32 == 1);
while (P32 == 0);
Delay10us();
while (P32 == 1) {
Delay10us();
time++;
if (time > 450) return 0xFF; // 超過(guò) 4.5ms,返回?zé)o效值
}
// 讀取 32 位數(shù)據(jù)
while (bit_count < IR_BIT_COUNT) {
while (P32 == 1) Delay10us(); // 等待高電平結(jié)束
time = GetTime(); // 測(cè)量低電平時(shí)間
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; // 時(shí)間異常,返回?zé)o效值
}
bit_count++;
}
return ir_data;
}
void main() {
P1M0 = 0x00; // 設(shè)置 P1 準(zhǔn)雙向模式
P1M1 = 0x00;
while (1) {
unsigned char key = IrDecodeValue();
if (key == 0x45) { // 檢測(cè)按鍵值是否為 IR_POWER (0x45)
P12 = 1; // 點(diǎn)亮 LED
Delay500ms(); // 延時(shí)保持亮燈
P12 = 0; // 熄滅 LED
}
}
}
作者: cedtek 時(shí)間: 2025-2-27 02:59
以下內(nèi)容由 DeepSeek R1 生成
根據(jù)問(wèn)題描述和代碼分析,問(wèn)題可能出在以下幾個(gè)方面:
1. 數(shù)據(jù)位處理錯(cuò)誤原代碼中使用unsigned char ir_data(8位)存儲(chǔ)32位數(shù)據(jù),導(dǎo)致高位數(shù)據(jù)丟失。應(yīng)改用32位變量:
unsigned long IrDecodeValue(void) {
unsigned long ir_data = 0;
// ...其余代碼
// 讀取32位數(shù)據(jù)時(shí),按位存儲(chǔ)到ir_data
ir_data <<= 1;
ir_data |= (當(dāng)前位值);
// ...
return (ir_data >> 8) & 0xFF; // 返回?cái)?shù)據(jù)碼(假設(shè)第三字節(jié))
}2. 時(shí)間閾值與延時(shí)函數(shù)不匹配STC8G1K08默認(rèn)使用24MHz時(shí)鐘(1T模式),而傳統(tǒng)51多為12MHz(12T模式)。原Delay10us在24MHz下實(shí)際延時(shí)可能過(guò)短。需重新校準(zhǔn):
void Delay10us(void) {
unsigned char i = 24; // 調(diào)整i的值,用示波器校準(zhǔn)
while (--i);
}3. 電平檢測(cè)邏輯錯(cuò)誤NEC協(xié)議中,邏輯0/1由高電平時(shí)間決定,而非低電平。應(yīng)修改GetTime函數(shù)以測(cè)量高電平持續(xù)時(shí)間:
unsigned int GetHighTime(void) {
unsigned int time = 0;
while (P32 == 1) { // 測(cè)量高電平時(shí)間
Delay10us();
time++;
if(time > 1000) break; // 超時(shí)退出
}
return time;
}4. 未正確處理引導(dǎo)碼原代碼未正確檢測(cè)9ms低電平+4.5ms高電平的引導(dǎo)碼。需添加引導(dǎo)碼驗(yàn)證:
// 檢測(cè)引導(dǎo)碼
while (P32 == 1); // 等待高電平結(jié)束
unsigned int low_time = GetTime(); // 測(cè)量低電平時(shí)間
if (low_time < 800 || low_time > 1000) return 0xFF; // 非引導(dǎo)碼
unsigned int high_time = GetTime(); // 測(cè)量高電平時(shí)間
if (high_time < 400 || high_time > 500) return 0xFF; // 非引導(dǎo)碼5. 引腳配置問(wèn)題確保P3.2(紅外輸入)和P1.2(LED)配置正確:
void main() {
P1M0 = 0x00; // P1準(zhǔn)雙向
P1M1 = 0x00;
P3M0 &= ~0x04; // P3.2準(zhǔn)雙向或高阻輸入
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;
// 檢測(cè)引導(dǎo)碼
while (P32 == 1);
time = 0;
while (P32 == 0) { // 測(cè)量低電平時(shí)間
Delay10us();
time++;
if (time > 1000) return 0;
}
if (time < 800) return 0; // 9ms低電平應(yīng)為約900個(gè)10us
time = 0;
while (P32 == 1) { // 測(cè)量高電平時(shí)間
Delay10us();
time++;
if (time > 500) return 0;
}
if (time < 400) return 0; // 4.5ms高電平應(yīng)為約450個(gè)10us
// 讀取32位數(shù)據(jù)
for (int i=0; i<IR_DATA_BITS; i++) {
while (P32 == 0); // 等待低電平結(jié)束
unsigned int high_time = GetHighTime();
data <<= 1;
if (high_time > 100) { // 高電平時(shí)間>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; // 翻轉(zhuǎn)LED
}
}
}調(diào)試建議使用示波器:檢查紅外接收頭的輸出波形,確認(rèn)信號(hào)是否符合NEC協(xié)議。
打印鍵值:通過(guò)串口輸出接收到的鍵值,確認(rèn)是否正確解析電源鍵(如0x45)。
調(diào)整延時(shí)參數(shù):根據(jù)實(shí)際時(shí)鐘頻率,校準(zhǔn)Delay10us的準(zhǔn)確性。
檢查硬件:確認(rèn)紅外接收器電源穩(wěn)定,信號(hào)線連接正確(P3.2),LED電路無(wú)故障。
通過(guò)以上修正和調(diào)試步驟,應(yīng)能解決STC8G1K08紅外通信不響應(yīng)的問(wèn)題。
作者: lkc8210 時(shí)間: 2025-2-27 11:22
延時(shí)代碼有問(wèn)題
請(qǐng)用AIapp-ISP生成對(duì)應(yīng)的晶振的延時(shí)函數(shù)
作者: ziiyn 時(shí)間: 2025-2-27 19:06
isp軟件的例程里有紅外代碼,用那個(gè)改嘛
| 歡迎光臨 (http://www.raoushi.com/bbs/) |
Powered by Discuz! X3.1 |