- #include
- #include
- #include
- #include
- #define uint unsigned int
- #define uchar unsigned char
- //------------------------------------------------------位表示-----------------------------------------------
- #define BYTE_BIT0 0x01
- #define BYTE_BIT1 0x02
- #define BYTE_BIT2 0x04
- #define BYTE_BIT3 0x08
- #define BYTE_BIT4 0x10
- #define BYTE_BIT5 0x20
- #define BYTE_BIT6 0x40
- #define BYTE_BIT7 0x80
- //--------------------------------------------------------------------------------------------------------------
- ***it KEY0 = P3^6;
- ***it KEY1 = P3^7;
- ***it BELL = P3^4;
- //----------------------------------------------------------------------------------------------------------------
- bdata unsigned DATA_BUF;
- #define DATA7 ((DATA_BUF&BYTE_BIT7) != 0)
- #define DATA0 ((DATA_BUF&BYTE_BIT0) != 0)
- ***it flag =DATA_BUF^7;
- ***it flag1 =DATA_BUF^0;
- //-------------------------------------------------发送数据缓冲区-------------------------------------------------------
- #define TxRxBuf_Len 32
- unsigned char TxRxBuf[TxRxBuf_Len];
- //------------------------------------------------NRF905配置口定义------------------------------------------------
- ***it TXEN=P1^0;
- ***it TRX_CE=P3^2;
- ***it PWR=P1^1;
- //----------------------------------------------------------------------------------------------------------------
- ***it MISO=P1^6;
- ***it MOSI=P1^5;
- ***it SCK=P1^7;
- ***it CSN=P1^3;
- //----------------------------------------------------------------------------------------------------------------
- ***it AM=P1^4;
- ***it DR=P3^3;
- ***it CD=P1^2;
- //--------------------------------------------------NRF905的SPI控制指令-------------------------------------------
- #define WC 0x00
- #define RC 0x10
- #define WTP 0x20
- #define RTP 0x21
- #define WTA 0x22
- #define RTA 0x23
- #define RRP 0x24
- //-----------------------------------------------------------------
- unsigned char flag_rec =0;
- unsigned char num_rec =0;
-
- unsigned char code kaijihuamian[]="HLJU-505";
- unsigned char code receiving[]="Receiving!";
- unsigned char code nodata[]="No GPS data!";
- char code TIME_AREA= 8; //时区
- unsigned char flag_data; //数据标志位
- //GPS数据存储数组
- unsigned char JD[10]; //经度
- unsigned char JD_a; //经度方向
- unsigned char WD[9]; //纬度
- unsigned char WD_a; //纬度方向
- unsigned char date[6]; //日期
- unsigned char time[6]; //时间
- unsigned char time1[6]; //时间
- unsigned char speed[5]={'0','0','0','0','0'}; //速度
- unsigned char high[6]; //高度
- unsigned char angle[5]; //方位角
- unsigned char use_sat[2]; //使用的卫星数
- unsigned char total_sat[2]; //天空中总卫星数
- unsigned char lock; //定位状态
- //串口中断需要的变量
- unsigned char seg_count; //逗号计数器
- unsigned char dot_count; //小数点计数器
- unsigned char byte_count; //位数计数器
- unsigned char cmd_number; //命令类型
- unsigned char mode; //0:结束模式,1:命令模式,2:数据模式
- unsigned char buf_full; //1:整句接收完成,相应数据有效。0:缓存数据无效。
- unsigned char cmd[5]; //命令类型存储数组
- //-----------------------------------------------------------------------------------------------
- //-----------------------------------------------NRF905寄存器配置------------------------------------------------
- unsigned char idata RFConf[11]=
- {
- 0x00, //配置命令//
- 0x4c, //CH_NO,配置频段在430MHZ
- 0x0C, //输出功率为10dbm,不重发,节电为正常模式
- 0x44, //地址宽度设置,为4字节
- 0x20,0x20, //接收发送有效数据长度为4字节
- 0xCC,0xCC,0xCC,0xCC, //接收地址
- 0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
- };
- code TxAddress[4]={0xcc,0xcc,0xcc,0xcc};
- //-----------------------------------------------延时--------------------------------------------------------------
- static void delay(uchar n)
- {
- uint i;
- while(n--)
- for(i=0;i<80;i++);
- }
- //----------------------------------------------SPI写函数---------------------------------------------------------
- void SpiWrite(unsigned char send)
- {
- unsigned char i;
- DATA_BUF=send;
- for (i=0;i<8;i++)
- {
- if (DATA7) //总是发送最高位
- {
- MOSI=1;
- }
- else
- {
- MOSI=0;
- }
- SCK=1;
- DATA_BUF=DATA_BUF<<1;
- SCK=0;
- }
- }
- //---------------------------------------------初始化nRF905状态-----------------------------------------------------
- void nRF905Init(void)
- {
- CSN=1; // Spi disable
- SCK=0; // Spi clock line init low
- DR=0; // Init DR for input
- AM=0; // Init AM for input
- CD=0; // Init CD for input
- PWR=1; // nRF905 power on
- TRX_CE=0; // Set nRF905 in standby mode
- TXEN=0; // set radio in Rx mode
- }
- //----------------------------------------------NRF905初始化寄存器------------------------------------------------
- void Config905(void)
- {
- uchar i;
- CSN=0; // Spi enable for write a spi command
- //SpiWrite(WC); // Write config command写放配置命令
- for (i=0;i<11;i++) // Write configration words 写放配置字
- {
- SpiWrite(RFConf[i]);
- }
- CSN=1; // Disable Spi
- }
- //----------------------------------------打包待发送的数据--------------------------------------------------------
- void TxPacket(void)
- {
- uchar i;
- //Config905();
- CSN=0;
- SpiWrite(WTP); // Write payload command
- for (i=0;i<32;i++)
- {
- SpiWrite(TxRxBuf[i]); // Write 32 bytes Tx data
- }// Spi enable for write a spi command
- CSN=1;
- delay(1); // Spi disable
- CSN=0; // Spi enable for write a spi command
- SpiWrite(WTA); // Write address command
- for (i=0;i<4;i++) // Write 4 bytes address
- {
- SpiWrite(TxAddress[i]);
- }
- CSN=1; // Spi disable
- TRX_CE=1; // Set TRX_CE high,start Tx data transmission
- delay(1); // while (DR!=1);
- TRX_CE=0; // Set TRX_CE low
- }
- //--------------------------------------------设置发送模式----------------------------------------------------------
- void SetTxMode(void)
- {
- TRX_CE=0;
- TXEN=1;
- delay(1); // delay1 for mode change(>=650us)
- }
- //---------------------------------------------------------------------------------------------------------------
- unsigned char CheckCD(void) //Pin->检查是否已存在 同频率载波
- {
- if (CD==1)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- //---------------------------------------------------数据发送--------------------------------------------------------------
- void TX(void)
- {
- SetTxMode();// Set nRF905 in Tx mode
- TxPacket();// Send data by nRF905
- CheckCD(); // 返回CD的当前电平
- }
- void InitUART(void) //9600 11.05926MHz
- {
- SCON = 0x50; //REN=1允许串行接受状态,串口工作模式1
- TMOD|= 0x20; //定时器工作方式2
- PCON|= 0x80;
- TH1 = 0xFa; //baud*2 /* reload value 9600、数据位8、停止位1。效验位无 (11.0592)
- TL1 = 0xF3;
- TR1 = 1;
- ES = 1; //开串口中断
- EA = 1; // 开总中断
- }
- /**************************************
- //将UTC时间转成BJ时间
- void trans_time(void)
- {
- unsigned char temp,hour_shi,hour_ge;
- temp=(time[0]-'0')*10+(time[1]-'0');
- if(temp<=16)
- {
- temp=temp+8;
- hour_shi=temp/10;
- hour_ge=temp%10;
- time[0]=hour_shi+'0';
- time[1]=hour_ge+'0';
-
- }
- else
- {
- temp=temp+8-24;
- time[0]='0';
- time[1]=temp%10+'0';
-
- }
- }
- ***********************************/
- /**********************************
- //判断是否有GPS数据 有1,无0
- bit gps_data(void)
- {
- if(buf_full&0x01!=0)
- return 1;
- else
- return 0;
- }
- **********************************/
- //--------------------------------------------------------------------
- //串口中断程序
- void ser_int (void) interrupt 4 using 1
- {
-
- unsigned char tmp;
- if(RI)
- {
- RI=0;
- tmp=SBUF;
- switch(tmp)
- {
- case '程序有点长了,这是能够接收到数据的程序
- :
- cmd_number=0; //命令类型清空
- mode=1; //接收命令模式
- byte_count=0; //接收位数清空
- flag_data=1;
- flag_rec=1; //数据标志位置一
- break;
- case ',':
- seg_count++; //逗号计数加1
- byte_count=0;
- break;
- case '*':
- switch(cmd_number)
- {
- case 1:
- buf_full|=0x01;
- break;
- case 2:
- buf_full|=0x02;
- break;
- case 3:
- buf_full|=0x04;
- break;
- }
- mode=0;
- break;
- default:
- if(mode==1) //命令种类判断
- {
- cmd[byte_count]=tmp; //接收字符放入类型缓存
- if(byte_count>=4)
- { //如果类型数据接收完毕,判断类型
- if(cmd[0]=='G')
- {
- if(cmd[1]=='N')
- {
- if(cmd[2]=='G')
- {
- if(cmd[3]=='G')
- {
- if(cmd[4]=='A')
- {
- cmd_number=1; //data type
- mode=2;
- seg_count=0;
- byte_count=0;
- }
- }
- else if(cmd[3]=='S')
- {
- if(cmd[4]=='V')
- {
- cmd_number=2;
- mode=2;
- seg_count=0;
- byte_count=0;
- }
- }
- }
- else if(cmd[2]=='R')
- {
- if(cmd[3]=='M')
- {
- if(cmd[4]=='C')
- {
- cmd_number=3;
- mode=2;
- seg_count=0;
- byte_count=0;
- }
- }
- }
- }
- }
- }
- }
- else if(mode==2)
- {
- //接收数据处理
- switch (cmd_number)
- {
- case 1: //类型1数据接收。GPGGA
- switch(seg_count)
- {
- case 2: //纬度处理
- if(byte_count<9)
- {
- WD[byte_count]=tmp;
- }
- break;
- case 3: //纬度方向处理
- if(byte_count<1)
- {
- WD_a=tmp;
- }
- break;
- case 4: //经度处理
- if(byte_count<10)
- {
- JD[byte_count]=tmp;
- }
- break;
- case 5: //经度方向处理
- if(byte_count<1)
- {
- JD_a=tmp;
- }
- break;
- case 6: //定位判断
- if(byte_count<1)
- {
- lock=tmp;
- }
- break;
- case 7: //定位使用的卫星数
- if(byte_count<2)
- {
- use_sat[byte_count]=tmp;
- }
- break;
- case 9: //高度处理
- if(byte_count<6)
- {
- high[byte_count]=tmp;
- }
- break;
- }
- break;
- case 2: //类型2数据接收。GPGSV
- switch(seg_count)
- {
- case 3: //天空中的卫星总数
- if(byte_count<2)
- {
- total_sat[byte_count]=tmp;
- }
- break;
- }
- break;
- case 3: //类型3数据接收。GPRMC
- switch(seg_count)
- {
- case 1: //时间处理
- if(byte_count<6)
- {
- time[byte_count]=tmp;
- }
- break;
- case 2: //定位判断
- if(byte_count<1)
- {
- if (tmp=='V') {lock=0;}
- else
- {
- lock=1;
- }
- }
- break;
- case 3: //纬度处理
- if(byte_count<9)
- {
- WD[byte_count]=tmp;
- }
- break;
- case 4: //纬度方向处理
- if(byte_count<1)
- {
- WD_a=tmp;
- }
- break;
- case 5: //经度处理
- if(byte_count<10)
- {
- JD[byte_count]=tmp;
- }
- break;
- case 6: //经度方向处理
- if(byte_count<1)
- {
- JD_a=tmp;
- }
- break;
- case 7: //速度处理
- if(byte_count<5)
- {
- speed[byte_count]=tmp;
- }
- break;
- case 8: //方位角处理
- if(byte_count<5)
- {
- angle[byte_count]=tmp;
- }
- break;
- case 9: //方位角处理
- if(byte_count<6)
- {
- date[byte_count]=tmp;
- }
- break;
- }
- break;
- }
- }
- byte_count++; //接收数位加1
- break;
- }
- }
- }
- //------------------------------------主函数-------------------------------------
- void main(void)
- {
- uchar i;
- nRF905Init();
- Config905();
- InitUART();
- while(1)
- {
- if(JD[0]=='1')
- {
-
- for(i=0;i<10;i++)
- {
- TxRxBuf[i] = JD[i]; //把数据送给发送缓冲
- }
-
- TX(); //发送数据
- }
- }
- }
复制代码
程序有点长了,这是能够接收到数据的程序
|
|
2014-7-16 14:21:51
评论
举报
|
|
|