📄 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;
INT8U timer[2]; //超时计数器
//unsigned char Address[5] = {0x00,0x00,0x00,0x00,0x01};
unsigned char SendBuf[5] = {0x01,0x02,0x03,0x04,0x05};
unsigned char Address[4] = {0x00,0x00,0x01,0xFF};
const unsigned char SoundData[32] = {
0xFF,0xFF,0x6D,0x00,0x2D,0x01,0x3D,0x01,0x43,0x00,0x08,0xFF,0xAA,0xFE,0x5E,0xFF,
0x0F,0x00,0x68,0xFF,0x76,0xFD,0x17,0xFC,0x8C,0xFD,0x29,0x02,0x61,0x07,0x45,0x09,
};
/*****************************************************************************************
//函数名: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()
{
// ET0 = 0;
UartInit();
TimerInit();
nRF2401_Initial(); // 初始化nRF2401A
nRF2401_Mode(1); // 设置nRF2401A工作方式:发送
nRF2401_SetAddress(Address,4); // 设置接收端地址 00 00 00 00 01
Puts("nRF2401_TX 设置nRF2401A工作方式:发送");
while(1)
{
//nRF2401_Str("123456789");
// nRF2401_WriteByte(0xf5);
//nRF2401_SendByte(0x02); // 发送键号"2"
if(key1==0)
{
nRF2401_SendBuffer_Byte(SoundData,26);
//nRF2401_SendByte(0x01); // 发送键号"1"
Delay_Us(500);
}
if(key2==0)
{
//nRF2401_SendByte(0x02); // 发送键号"2"
nRF2401_SendBuffer_Byte(SoundData,26);
Delay_Us(500);
}
if(key3==0)
{
nRF2401_SendBuffer_Byte(SoundData,27);
//nRF2401_SendByte(0x03); // 发送键号"3"
Delay_Us(500);
}
if(key4==0)
{
nRF2401_SendBuffer_Byte(SoundData,28);
//nRF2401_SendByte(0x04); // 发送键号"4"
Delay_Us(500);
}
if(key5==0)
{
nRF2401_SendByte(0x05); // 发送键号"5"
Delay_Us(500);
}
if(key6==0)
{
nRF2401_SendByte(0x06); // 发送键号"6"
Delay_Us(500);
}
if(key7==0)
{
nRF2401_SendByte(0x07); // 发送键号"7"
Delay_Us(500);
}
if(key8==0)
{
nRF2401_SendByte(0x08); // 发送键号"8"
Delay_Us(500);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -