//-----------------------------------------------------------------------------
#include <c8051f020.h> // SFR声明
#include <stdio.h>
#include <string.h>
//-----------------------------------------------------------------------------
// sfr定义及全局常量
//-----------------------------------------------------------------------------
#define SYSCLK 22118400 // 系统时钟为22118400HZ
#define BAUDRATE 9600 // UART0 波特率为19200HZ
//#define TX_LENGTH 16 // 接收缓冲器长度为16
sfr16 DP = 0x82; // 数据指针
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
sfr16 RCAP4 = 0xe4; // Timer4 capture/reload
sfr16 T4 = 0xf4; // Timer4
sbit PWR= P1^3; //P0.2 =1 为工作模式;P0.2=0为待机模式
sbit CS = P1^2; //P0.3=0为频道1,载波频率为433MHZ
//P0.3=1为频道2,载波频率为315HZ
sbit TXEN = P1^4; //P0.4=0为接收模式,P0.4=1为发射模式
sbit LED = P1^6 ; //LED显示发射状态
int i=0;
int m=0;
int j=0;
int k=0;
int n=0;
int p=0;
//bit TI1=0;
//bit RI1=0;
xdata char sendhead[]={0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa};
xdata char senddata[100]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,
0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,
0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,
0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,
0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,
0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
0x50,0x51,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,
0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,0x60,0x61,0x62}; //-----------------------------------------------------------------------------
// 全局变量
//-----------------------------------------------------------------------------
//unsigned char xdata sentdata[108]={
//存储空间定义成idata使用内部RAM
//unsigned char code *read; //读FLASH
//char idata TX_Buf[TX_LENGTH]; //发送缓冲区长度
//-----------------------------------------------------------------------------
// 函数声明
//-----------------------------------------------------------------------------
void SYSCLK_Init (void); //系统时钟初始化函数声明
void PORT_Init (void); //端口初始化函数声明
void UART_Init (void); //UART初始化函数声明
void UART0_ISR (void); //UART中断服务程序声明
//void SENT_Head (void);
void SENT_Datapacket (void);
//---------------------------------------------------------------------------
//发送函数
//---------------------------------------------------------------------------
//----------------------------------------------------------------------------
//主函数
//----------------------------------------------------------------------------
void main(void)
{
WDTCN = 0xde; //禁止看门狗定时器
WDTCN = 0xad;
SYSCLK_Init ();
PORT_Init ();
UART_Init ();
PWR = 1; //置nRF401为工作模式
CS = 0;
for(k=0;k<1000;k++)
{for(i=0;i<100;i++);} //选择载波频率为433MHZ
TXEN = 1;
i=0;
k=0;
for(k=0;k<1000;k++)
{for(i=0;i<100;i++);}
EA = 1 ;
ES0=1;
LED=1;
//绿灯亮表示开始发送数据 //允许全局中断
// RI=0 ;
//TI1=0;
//SBUF0=0xaa;
// TI0=1 ;
while (1) //TI0=1;
{
for(p=0;p<30;p++)
{SCON1=0x50;
SBUF1=sendhead[p];
while(SCON1==0x50);
SCON1=0x50;
}
for(m=0;m<100;m++)
{SCON1=0x50;
SBUF1=senddata[m];
while(SCON1==0x50);
SCON1=0x50;
//for(n=0;n<50000;n++);
}
//开始发送
// if(m==20)
// m=0; //死循环,等待中断防止程序逻辑执行到此跳出,
} //接收不到中断,中断到来时根据中断向量表直
//接跳到中断服务程序来执行,执行完再返回到
//-----------------------------------------------------------------------------
//时钟初始化
//-----------------------------------------------------------------------------
//while(1);这条语句上来继续等待下一个中断。
}
void SYSCLK_Init (void)
{
int i; // 延时计数
OSCXCN = 0x67; // 使用外部时钟22.1184MHz
for (i=0; i < 256; i++) ; // 等侍晶振起振
while (!(OSCXCN & 0x80)) ; // 等待晶振稳定
OSCICN = 0x88; // 选择外部晶振作为系统时钟
//CKCON = 0x20; //定时器2使用系统时钟
}
//-----------------------------------------------------------------------------
// 交叉开关配置
//-----------------------------------------------------------------------------
//
void PORT_Init(void)
{
XBR0 = 0x07; //使能UART0
XBR1 = 0x00;
XBR2 = 0x44; //使能UART1,使能交叉开关
P0MDOUT |= 0xFD; //RX,即P0.1为漏极开路方式,其它为推挽方式
P1MDOUT |= 0xFD; //p1.6为推挽方式
P1MDIN=0xFF; //非复用方式67页
}
//--------------------------------------------------------------------------------------
//UART初始化
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
//UART初始化
//--------------------------------------------------------------------------------------
void UART_Init (void)
{
SCON0=0x50;
SCON1=0x50;
TCON = 0x40;
TMOD=0x20;
TH1=-(SYSCLK/BAUDRATE/16);
TR1=1;
CKCON |=0x10;
PCON |=0x80;
//TI0=1;
// T2CON|=0x30;
//RCAP2H=0xFF;
//RCAP2L=0xB8;// 波特率为19200 使用系统时钟二分频为时钟源,65536-22118400/2/32/19200=18
//SCON1=0x50;
//TI1=0;
//TR2=1;
}
//------------------------------------------------------------------------------------
//UART中断服务程序,中断向量4
//------------------------------------------------------------------------------------
//void UART_ISR (void) interrupt 4 //using 3
//{
// if(TI1==1)
//}
评论0