低成本,將原閑置在家的MP3模塊改紅外遙控切歌,實現賴人模式
按鍵左為"Prev/U--"的左邊
按鍵右為"Prev/U--"的右邊
制作出來的實物圖如下:
IMG_20181125_105229.jpg (57.93 KB, 下載次數: 61)
下載附件
2018-11-25 11:10 上傳
1.png (66.76 KB, 下載次數: 44)
下載附件
2018-11-25 11:10 上傳
2.png (82.55 KB, 下載次數: 39)
下載附件
2018-11-25 11:10 上傳
3.png (19.77 KB, 下載次數: 58)
下載附件
2018-11-25 11:10 上傳
單片機程序源碼:
- /*
- 1 IRC_CLKO/INT2/CLKOUT1/T0/RST/P3^4 8 RSTOUT_LOW/INT1/P3^3
- 2 VCC(5V) 7 INT0/P3^2
- 3 INT3/CLKOUT0/T1/P3^5 6 P3^1/TXD
- 4 GND 5 INT4/P3^0/RXD
- 燒錄時,燒錄器的VCC接2腳,TXD接5腳,RXD接6腳,GND接4腳
- */
- //本來是想至少做上一首和下一首的,但發現在按鍵上焊線后,按鍵線同時接IO口會沖突
- //(用51單片機的4個IO口也測試過)下一首能正常時,上一首卻變成了暫停
- //看看有沒有高手幫忙找到問題
- //現在暫時只有下一首的功能
- //////STC15F104////////////
- #include <STC15F2K60S2.H>
- #define uchar unsigned char
- #define uint unsigned int
- #define T50MS (65536-50000) //12T模式
- /************* 用戶系統配置 **************/
- //燒錄頻率請選12.000MHz
- #define MAIN_Fosc 12000000L //定義主時鐘, 模擬串口和紅外接收會自動適應。5~36MHZ
- #define D_TIMER0 125 //選擇定時器時間, us, 紅外接收要求在60us~250us之間
- #define User_code 0x00FF //定義紅外接收用戶碼(38K的迷你遙控器通用)
- /************* 以下宏定義用戶請勿修改 **************/
- #define freq_base (MAIN_Fosc / 1200)
- #define Timer0_Reload (65536 - (D_TIMER0 * freq_base / 10000))
- /************* 本地變量聲明 **************/
- bit P_IR_RX_temp; //Last sample
- bit B_IR_Sync; //已收到同步標志
- uchar IR_SampleCnt; //采樣計數
- uchar IR_BitCnt; //編碼位數
- uchar IR_UserH; //用戶碼(地址)高字節
- uchar IR_UserL; //用戶碼(地址)低字節
- uchar IR_data; //數據原碼
- uchar IR_DataShit; //數據反碼
- bit B_IrUserErr; //User code error flag
- bit B_IR_Press; //Key press flag,include repeat key.
- uchar IR_code; //IR code 紅外鍵碼
- sbit AJ_L =P3^3; //按鍵左
- sbit AJ_R =P3^4; //按鍵右
- sbit P_IR_RX =P3^2; //定義紅外接收輸入端口
- uint i1,ontime,offtime; //ontime=開啟時間,offtime=關閉時間
- uchar k,data1=0x01; //k=模式值
- bit b;
-
- /************* 本地函數聲明 **************/
- void delay1ms(void) //延時
- {
- unsigned char i,j;
- for(i=0;i<10;i++)
- for(j=0;j<33;j++)
- ;
- }
- ////////////////////////////////////////////
- void delay(unsigned char n) //延時
- {
- unsigned char i;
- for(i=0;i<n;i++)
- delay1ms();
- }
- /////////////////////////////////////////////////////////////
- void Model_2(void)
- {
- ontime=2; //開啟時間
- offtime=0; //關閉時間
- }
-
- /////////////////////////////////////////////////////////////
- //判斷接收的編碼,執行相應功能(根據需要自行修改)
- void _data_(void)
- {
- switch(IR_data) //紅外接收成功
- {
- case 0x09: //遙控器按鍵值
- k=2; //Model_2
- i1=0;b=1;
- break;
- }
- IR_data=0x00; //必須清零
- }
- ////////////////////////////////////////////
- void main(void) //主函數
- {
- IE = 0x82; //開總中斷和T0中斷允許
- IE2 = 0x04; //T2中斷允許
- TMOD = 0; //for STC15Fxxx系列 Timer0 as 16bit reload timer.
- TH0 = Timer0_Reload / 256;
- TL0 = Timer0_Reload % 256;
- T2L = T50MS; //初始化計時值5ms中斷一次
- T2H = T50MS >> 8;
- TR0 =1;
- k =0;
- while(1) //無限循環
- {
- switch(k) //判斷k的值
- {
- case 2: AUXR=0x10;
- Model_2(); //模式2
- break;
- }
- }
- }
- /*************紅外接收部分程序是引用STC官方的資料******************************/
- /******************** 紅外采樣時間宏定義, 用戶不要隨意修改 *******************/
- #if ((D_TIMER0 <= 250) && (D_TIMER0 >= 60))
- #define D_IR_sample D_TIMER0 //定義采樣時間,在60us~250us之間
- #endif
- #define D_IR_SYNC_MAX (15000/D_IR_sample) //SYNC max time
- #define D_IR_SYNC_MIN (9700 /D_IR_sample) //SYNC min time
- #define D_IR_SYNC_DIVIDE (12375/D_IR_sample) //decide data 0 or 1
- #define D_IR_DATA_MAX (3000 /D_IR_sample) //data max time
- #define D_IR_DATA_MIN (600 /D_IR_sample) //data min time
- #define D_IR_DATA_DIVIDE (1687 /D_IR_sample) //decide data 0 or 1
- #define D_IR_BIT_NUMBER 32 //bit number
- //*******************************************************************************************
- //**************************** IR RECEIVE MODULE ********************************************
- void IR_RX_HT6121(void)
- {
- uchar SampleTime;
- IR_SampleCnt++; //Sample + 1
- F0 = P_IR_RX_temp; //Save Last sample status
- P_IR_RX_temp = P_IR_RX; //Read current status
- if(F0 && !P_IR_RX_temp) //Last sample is high,and current sample is low, so is fall edge
- {
- SampleTime = IR_SampleCnt; //get the sample time
- IR_SampleCnt = 0; //Clear the sample counter
- if(SampleTime > D_IR_SYNC_MAX) B_IR_Sync = 0; //large the Maxim SYNC time, then error
- else if(SampleTime >= D_IR_SYNC_MIN) //SYNC
- {
- if(SampleTime >= D_IR_SYNC_DIVIDE)
- {
- B_IR_Sync = 1; //has received SYNC
- IR_BitCnt = D_IR_BIT_NUMBER; //Load bit number
- }
- }
- else if(B_IR_Sync) //has received SYNC
- {
- if(SampleTime > D_IR_DATA_MAX) B_IR_Sync=0; //data samlpe time to large
- else
- {
- IR_DataShit >>= 1; //data shift right 1 bit
- if(SampleTime >= D_IR_DATA_DIVIDE) IR_DataShit |= 0x80; //devide data 0 or 1
- if(--IR_BitCnt == 0) //bit number is over?
- {
- B_IR_Sync = 0; //Clear SYNC
- if(~IR_DataShit == IR_data) //判斷數據正反碼
- {
- if((IR_UserH == (User_code / 256)) &&
- IR_UserL == (User_code % 256))
- B_IrUserErr = 0; //User code is righe
- else B_IrUserErr = 1; //user code is wrong
-
- IR_code = IR_data;
- B_IR_Press = 1; //數據有效
- _data_();
- }
- }
- else if((IR_BitCnt & 7)== 0) //one byte receive
- {
- IR_UserL = IR_UserH; //Save the User code high byte
- IR_UserH = IR_data; //Save the User code low byte
- IR_data = IR_DataShit; //Save the IR data byte
- }
- }
- }
- }
- }
- /********************** Timer0中斷函數************************/
- void timer0 (void) interrupt 1
- {
- IR_RX_HT6121(); //紅外
- }
- ///////////////////////////////////////////
- void time2(void ) interrupt 12
- {
- TR0=0; //T2中斷期間關閉T0中斷
- i1++; //計次數
- if(b==1) //b=開啟和關閉狀態
- {
- AJ_L=0; //按鍵左
- AJ_R=0; //按鍵右
- if(i1==ontime) //開啟時間
- {
- i1=0;
- b=0;
- k=0;
- AJ_L=1; //按鍵左
- AJ_R=0; //按鍵右
- }
- }
- if(b==0)
- {
- AJ_L=1; //按鍵左
- AJ_R=0; //按鍵右
- if(i1==offtime) //關閉時間
- {
- i1=0;
- b=1;
- }
- }
- TR0=1; //T2中斷結束,開啟T0中斷
- }
復制代碼
全部資料51hei下載地址:
STC15F104紅外 MP3模塊.zip
(35.5 KB, 下載次數: 58)
2018-11-25 11:10 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|