?? main.c
字號:
#include <string.h>
#include <AT89X52.h>
#include <intrins.h>
#include "nRF2401.H"
#define INT8U unsigned char
#define INT16U unsigned int
#define FOSC 24000000
#define BAUD 9600
#define TIMER1 0XFD //256-(110592/(12*32*96))
#define TIMER0H (65535-3*FOSC/12/1000)/256
#define TIMER0L (65535-3*FOSC/12/1000)%256 //定時3MSVR
#define time0h (65535-3000)/256
#define time0l (65535-3000)%256 //定時3MS
sbit LED1 = P1^4;
sbit LED2 = P1^5;
sbit key1 = P1^6;
sbit key2 = P1^7;
sbit key3 = P3^2;
sbit key4 = P3^3;
sbit key5 = P3^4;
sbit key6 = P3^5;
sbit key7 = P3^6;
sbit key8 = P3^7;
bit ledcon ;
INT16U delay=0;
INT16U timer[2]; //超時計數器
unsigned char RxBuf[25]; // 接收緩沖,保存接收到的數據
/*****************************************************************************************
//函數名:UartInit()
//輸入:無
//輸出:無
//功能描述:串口初始化程序
/*****************************************************************************************/
void UartInit(void)
{
SCON = 0x50; //串口方式1,允許接收
TMOD = 0x21; //定時器1工作方式2,定時器0工作方式1
TH1 = 0xf3;
TL1 = 0xf3;
PCON=PCON|0x80;
TR1 = 1; //啟動定時器1
}
/*****************************************************************************************
//函數名:SendCh(ch)
//輸入:無
//輸出:無
//功能描述:串口發送一個字符
/*****************************************************************************************/
void SendCh(INT8U ch)
{
SBUF = ch;
while(!TI);
TI = 0;
}
/*****************************************************************************************
//函數名:void SendStr(INT8U *arr)
//輸入:發送的字符串
//輸出:無
//功能描述:發送一個字符串
/*****************************************************************************************/
void SendStr(INT8U *arr)
{
INT8U i;
i = 0;
while(arr[i] != '\0')
{
SendCh(arr[i]);
i++;
}
}
/********************************************************************************/
/* 含回車換行的字符串輸出函數 */
int Puts(char *s)
{
while (*s)
{
SendCh(*s);
s++;
}
SendCh(0x0a);//回車換行
SendCh(0x0d);
// Putstr("\n");
return 1;
}
void hexASCII(unsigned char *dat,unsigned char len)
{
const unsigned char tabel[]="0123456789ABCDEF";
unsigned char i,temp;
// SendStr("HEX->ASCII字符串輸出len=");
for (i=len;i>0;i--)
{
SendCh(0x20);
SendCh(0x30);
SendCh(0x78);
temp=dat[len-i]&0xf0;
temp=temp>>4;
SendCh(tabel[temp]);
temp=dat[len-i]&0x0f;
SendCh(tabel[temp]);
}
SendCh(0x0a);//回車換行
SendCh(0x0d);
}
/*****************************************************************************************
//函數名:void TimerInit(void)
//輸入:無
//輸出:無
//功能描述:定時器0初始化程序
/*****************************************************************************************/
void TimerInit(void)
{
TH0 = TIMER0H;
TL0 = TIMER0L;
ET0 = 1; //定時器0中斷允許
TF0 = 0;
TR0 = 1; //啟動定時器0
EA = 1; //開全局中斷
}
/*****************************************************************************************
//函數名:ResetTimer(INT8U n)
//輸入:要復位的計時器
//輸出:無
//功能描述:復位計時器
/*****************************************************************************************/
void ResetTimer(INT8U n)
{
ET0 = 0; // Disable Timer0 interrupt
timer[n & 0x01] = 0; // Clear timer[n]
ET0 = 1; // Enable Timer0 interrupt
}
/*****************************************************************************************
//函數名:INT16U ReadTimer(INT8U n)
//輸入:要讀的計時器
//輸出:讀出值
//功能描述:讀計時器
/*****************************************************************************************/
INT16U ReadTimer(INT8U n)
{
INT16U tmp;
ET0 = 0; // Disable Timer0 interrupt
tmp = timer[n]; // Clear timer[n]
ET0 = 1; // Enable Timer0 interrupt
return tmp;
}
/******************************************************************************************
*******************************************************************************************
************************************中斷服務程序*******************************************
*******************************************************************************************
******************************************************************************************/
void Timer0ISR(void) interrupt 1
{
EA = 0;
//TH0+=TIMER0H;
//TL0+=TIMER0L;
//timer[0]++;
//timer[1]++;
TH0=0xff;
TL0=0xff;
delay++;
if(delay>10000)
{
delay=0;
if(ledcon)
{
LED1=0;
LED2=1;
ledcon=0;
}
else
{
LED1=1;
LED2=0;
ledcon=1;
}
}
EA = 1;
}
main()
{
UartInit();
TimerInit();
nRF2401_Initial(); // nRF2401A初始化
nRF2401_Mode(0); // 設置nRF2401A工作方式:接收
// nRF2401_SetAddress(Address,5); // 設置接收端地址 00 00 00 00 01
;
Puts(" 設置nRF2401A工作方式:接收");
while(1)
{
if((nRF2401_RxStatus()) == 1) // nRF2401A有數據請求
{
LED1=0;
LED2=1;
nRF2401_ReceiveByte(RxBuf); // 接收數據
// SendStr(RxBuf);
hexASCII(RxBuf,26);
/* switch(RxBuf[2])
{
case 1:
Puts("Key1");
break;
case 2:
Puts("Key2");
break;
case 3:
Puts("Key3");
break;
case 4:
Puts("Key4");
break;
case 5:
Puts("Key5");
break;
case 6:
Puts("Key6");
break;
case 7:
Puts("Key7");
break;
case 8:
Puts("Key8");
break;
default:
break;
} */
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -