中级会员
- 积分
- 279
- 威望
- 206
- 贡献
- 39
- 兑换币
- 14
- 注册时间
- 2010-7-29
- 在线时间
- 17 小时
|
我写的程序无法实现通信,不知道为什么,读STATUS的值一直是1E,求高手帮助。给点意见亦可。- #include <mc9s12db128.h>
- #include <hidef.h>
- #include <math.h>
- #include "2401.h"
-
- void lcd_writeBCD(unsigned char ) ;
- void lcd_pos(uchar );
- void delay(uint i)
- {
- while(i--);
- asm(nop;);
- }
- ReadByte(uchar com)
- { //读一个字节
- uchar value;
- CSN0=0;
- delay(30);
- while(!(SPI0SR&0X20));
- SPI0DR=com;
- delay(30);
- while(!(SPI0SR&0X20));
- if(com==0x07)
- {
- value=SPI0DR;
- }
- else
- {
- value=SPI0DR;
- asm("nop");
- //SPI0DR=0XFF;
- SPI0DR=0X00;
- while(!(SPI0SR&0X20));
- delay(30);
- value=SPI0DR;
- }
- CSN0=1;
- return(value);
- }
- void WriteByte(uchar com,uchar data)
- { //写一个字节
- unsigned int i;
- CSN0=0;
- delay(20);
- while(!(SPI0SR&0X20));
- SPI0DR=com;
- delay(20);
- while(!(SPI0SR&0X20));
- i=SPI0DR;
- delay(20);
- while(!(SPI0SR&0X20));
- SPI0DR=data;
- delay(20);
- while(!(SPI0SR&0X20));
- delay(20);
- CSN0=1;
- }
-
- void ReadBytes(uchar com,uchar table[],uchar count)
- {
- uchar i;
- CSN0=0;
- delay(20);
- while(!(SPI0SR&0X20));
- SPI0DR=com;
- delay(20);
- while(!(SPI0SR&0X20));
- i=SPI0DR; //read SPIDR clear data
- delay(20);
- while(!(SPI0SR&0X20));
- for(i=0;i<count;i++)
- {
- //SPI0DR=0XFF;
- SPI0DR=0X00;
- delay(20) ;
- while(!(SPI0SR&0X20));
- delay(20);
- table[i]=SPI0DR;
- delay(20);
- }
- CSN0=1;
- }
- void WriteBytes(uchar com,uchar table[],uchar count)
- {
- uchar i;
- CSN0=0;
- delay(20);
- while(!(SPI0SR&0X20));
- SPI0DR=com;
- delay(20);
- while(!(SPI0SR&0X20));
- i=SPI0DR;
- delay(20);
- while(!(SPI0SR&0X20));
- for(i=0;i<count;i++){
- while(!(SPI0SR&0X20));
- SPI0DR=table[i];
- delay(20);
- }
- CSN0=1;
- }
-
- void RFinit(void)
- {
- //uchar temp;
- uchar TX_ADDRESS[]={0xE7,0xE7,0xE7,0xE7};
- ce0=0;
- CSN0=1;//
- //WriteBytes(WRITE_REG+TX_ADDR,TX_ADDRESS,4);//发送地址
- //WriteBytes(WRITE_REG+RX_ADDR_P0,TX_ADDRESS,4);//接收地址
- WriteByte(WRITE_REG+STATUS,0xfe); //清除标志位
- WriteByte(WRITE_REG+CONFIG,0X0F);//CRC使能/16位CRC校验上电/接收模式
- WriteByte(WRITE_REG+EN_AA,0X01);//数据通道0自动应答允许
- WriteByte(WRITE_REG+EN_RXADDR,0X01);//接收数据通道0允许
- WriteByte(WRITE_REG+SETUP_AW,0X03);//接收发送地址宽度 5字节宽度
- WriteByte(WRITE_REG+SETUP_RETR,0X00);//接收模式重发设置
- WriteByte(WRITE_REG+RF_CH,0X02);//设置NRF工作通道频率为默认值
- WriteByte(WRITE_REG+RF_SETUP,0X0a);//数据传输率和发送功率设置
- WriteByte(WRITE_REG+RX_PW_P0,4);//接收数据通道0有效数据宽度为4字节
- delay(10000);
- }
- void RFTx(void)
- { uchar TX_ADDRESS[]={0xE7,0xE7,0xE7,0xE7};
- ce0=0;
- WriteBytes(WRITE_REG+TX_ADDR,TX_ADDRESS,4);
- WriteBytes(WRITE_REG+RX_ADDR_P0,TX_ADDRESS,4);//装载接收端地址
- WriteBytes(WR_TX_PLOAD,Txtable,1);//装载数据
- WriteByte(WRITE_REG+CONFIG,0X0E);//发送模式
- ce0=1;
- delay(1000);
- //ce0=0;
- }
- void RFRx(void)
- { //extern unsigned char flag;
- uchar TX_ADDRESS[]={0xE7,0xE7,0xE7,0xE7};
- //uchar status;
- WriteBytes(WRITE_REG+RX_ADDR_P0,TX_ADDRESS,4);//接收地址
- WriteByte(WRITE_REG+CONFIG,0X0F);//接收模式
- ce0=1;
- delay(1000);
- /******************************888
- status=ReadByte(READ_REG+STATUS);
- while(status&0x40)
- { flag=1;
- ce0=0;
- ReadBytes(RD_RX_PLOAD,Rxtable,4);
- WriteByte(WRITE_REG+STATUS,status);
- status=0x00;
- }
- ***************************************/
- }
- #include <hidef.h> /* common defines and macros */
- #include <mc9s12db128.h> /* derivative information */
- #include "2401.h"
- #include "MCUInit.h"
- #include "SPIinit.h"
- #include "LcdDisplay.h"
- #pragma LINK_INFO DERIVATIVE "mc9s12db128b"
- //uchar TX_ADDRESS[]={0xE7,0xE7,0xE7,0xE7};
- uchar Txtable[1];
- uchar Rxtable[4];
- uchar sta,sta1;
- unsigned char flag;
- uint aa;
- unsigned char t;
- unsigned int j;
- void lcd_writeBCD(unsigned char value)
- {
- t=value>>4;
- if(t>9)t+=7;
- lcd_wdat(t+0x30);
- t=value&0x0f;
- if(t>9)t+=7;
- lcd_wdat(t+0x30);
-
- }
- //void A(void){
- // PORTA_BIT3=0;delay(10);PORTA_BIT3=1;delay(10);}
-
- void main(void)
- {
- /* put your own code here */
- MCUInit();
- SPIinit();
- EnableInterrupts;
- /*************LCD_I/O_INIT*****************/
- DDRK=0XFF;
- DDRM=0XFF;
- PTM=0XFF;
- lcd_init(); // 初始化LCD
- delayms(1000);
- DDRA_BIT0=1; //CE
- DDRA_BIT1=1; //CSN output
- DDRA_BIT2=0;//PTS2口为输入、2401IRQ引脚\
- DDRA_BIT3=1;
- DDRB=0XFF;
- PORTB=0XFF;
- //PORTB=0X00;
-
-
- //DDRM=0XFF;
- //PPSJ_PPSJ0=0;//下降沿
- //PIEJ_PIEJ0=1;//中断使能
- //Txtable[0]=0Xaa;
- //LcdDisplay(0);
- //delay(1000);
- //lcd_wcmd(0x01); //清除LCD的显示内容
- //delay(1000);
- RFinit();//初始化2401
- delay(1000);
-
- /****************************
- aa=0;
- for(j=0;j<9;j++) {
-
- sta1= ReadByte(READ_REG+CONFIG+aa);
- lcd_pos(0+aa*2);
- lcd_writeBCD(sta1);
- aa++;
- }
- ****************************/
- RFRx();//是成为接收模式
- //delayms(100);
-
- for(;;)
- { /* sta= ReadByte(READ_REG+CONFIG);//READ CONFIG
- lcd_pos(0);
- lcd_writeBCD(sta); */
-
- //*
-
- sta=ReadByte(READ_REG+STATUS);
- lcd_pos(0X40);
- lcd_writeBCD(sta);//*/
-
- // RFRx();//是成为接收模式
- /*delay(100);
- sta=ReadByte(READ_REG+STATUS);
- lcd_pos(0X40+2);
- lcd_writeBCD(sta);*/
-
- while(sta&0x40)
- {
- ce0=0;
- ReadBytes(RD_RX_PLOAD,Rxtable,4);
- PORTB=Rxtable[0];//send data to PORTB
- WriteByte(WRITE_REG+STATUS,sta);
- sta=0x00;
- RFRx();
- delay(1000);
- }
-
- RFRx();//是成为接收模式
-
- }
-
- }
复制代码 |
|