?? sc2262.c
字號:
#include <iom32v.h>
#include <macros.h>
#define wuxian_js PA3 //無線接收
#define study_key PC1 //報警
#define zhuangtai_d PC7 //LED
#define power_led PC6
#define UDRE 5
#define DATA_REGISTER_EMPTY (1<<UDRE)
unsigned char D[3];
#define uchar unsigned char
uchar UP[7]={0xff,0x01,0x00,0x08,0x00,0x1f,0x28};
uchar DOWN[7]={0xff,0x01,0x00,0x10,0x00,0x1f,0x30};
uchar left[7]={0xff,0x01,0x00,0x04,0x1f,0x00,0x24};
uchar right[7]={0xff,0x01,0x00,0x02,0x1f,0x00,0x22};
uchar STOP[7]={0xff,0x01,0x00,0x00,0x00,0x00,0x01};
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x19; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x08;
}
//發送單字節字符
void uart_transmit_byte( unsigned char data)
{
while(!(UCSRA&DATA_REGISTER_EMPTY));
UDR=data;
}
//發送字符串
void uart_transmit_string( unsigned char *string)
{
unsigned char i,j;
for(i=0;i<7;i++)
{
j=*string;
uart_transmit_byte(j);
string++;
}
}
unsigned long decode(void) //無線解碼
{
unsigned long DATA; //記錄數據
unsigned char count,i; //暫時計數
/*一共接收三字節*/
DATA=0; //
for (count=0;count<24;count++) //開始收集
{
/*測量高電平寬度*/
i=0; //寬度計數
while (PINA&(1<<3)) //高位計數
{
asm("nop"); //
i++; //計數增加
if (i>192) goto end; //限時解碼
}
/*分辨出電平高低*/
DATA*=2; //數據左移
if (i<64) //分辨高低
{
if (!((56>i)&(i>24))) goto end; //電平合法
DATA+=0; //記低電平
}
else
{
if (!((168>i)&(i>72))) goto end; //電平合法
DATA+=1; //記高電平
}
/*測量低電平寬度*/
while (!(PINA&(1<<3))) //低位計數
{
asm("nop"); //
i++; //計數增加
if (i>224) goto end; //限時解碼
}
/*低電平是否過窄*/
if (i<96) goto end; //對比寬度
}
/*解碼成功返結果*/
return DATA; //成功返回
/*有誤碼結束解碼*/
end:
return 0; //誤碼結束
}
void main(void)
{
unsigned long DATA; //
// unsigned char D[3]; //
signed char i; //
DDRC|=(1<<power_led);PORTC&=~(1<<power_led); //電源顯示燈
DDRA |=0x40; //無線輸入通道
PORTA|=0x40; //上拉選擇
uart0_init();
while (1)
{
DATA =decode(); //解碼程序
if (DATA>0) //
{
//spaz(); //解碼成功
for (i=2;i>=0;i--) //數據轉換
{
D[i]=DATA; //
DATA>>=8; //
}
//i=0; //轉換完畢
if(D[2]==0xf0) //上
{
for(i=0;i<7;i++)
{
uart_transmit_byte(UP[i]);
}
}
else if(D[2]==0x0c)//下
{
for(i=0;i<7;i++)
{
uart_transmit_byte(DOWN[i]);
}
}
else if(D[2]==0xcc)//左
{
for(i=0;i<7;i++)
{
uart_transmit_byte(left[i]);
}
}
else if(D[2]==0x3c)//右
{
for(i=0;i<7;i++)
{
uart_transmit_byte(right[i]);
}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -