📄 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 + -