thy110 发表于 2014-1-14 23:12:34

我一直在用串口给nrf24l01发送数据进行通信还是不行求助

这几天,我在用ATmage16的串口给NRF12l01发数据和另一款单片机通信,但是不行,请大神帮忙。

#include "iom16v.h"
#include "macros.h"
#include "NRF24L01.h"       
#include "AVR_HJ-2G.h"       
#define uint unsigned int
#define uchar unsigned char
#define TX_ADR_WIDTH    5   // 5 bytes TX(RX) address width
#define TX_PLOAD_WIDTH20// 16 bytes TX payload
uchar TX_ADDRESS={0x34,0x43,0x10,0x10,0x01}; // Define a static TX address;
uchar rx_buf;
uchar tx_buf={0X02,0X02,0X03,0X05,0X09};
//**************//UART//**********************************
//波特率,晶振
#define BAUD 19200                                       
#define FXTAL 12000000

//串口接收完毕中断触发声明
#pragma interrupt_handler USART_Received_Ir:12
#pragma interrupt_handler Increase_INT0_Ir:2
//中断初始化函数,在使用IRQ时认为的在pd2上加一个低电平
void Interrupt_Init(void)
{
   MCUCR&=0xF0; //INT0,低电平触发
   GICR|=BIT(6); //GICR|=0xC0; INT0中断允许位为1
   GIFR|=BIT(6); //GIFR|=0xC0; INTO清中断标志位
   SREG|=BIT(7); //全局中断使能位置一
}
/**************************************************/

//变量定义:接收缓冲变量,接收标志位
unsigned char RX_Buffer=0x00,RX_Flag=0;

//串口IO初始化函数
void USART_IO_Init(void)
{
                DDRD|=BIT(PD1); //PD1:TX为输出状态
}

//串口初始化函数
void USART_Init(void)
{
                unsigned int Temp;       
               
                USART_IO_Init();                //串口IO初始化函数调用
       
                UCSRA=0x00;                                                                        //串口控制器A清零
                UCSRB=0x00;                                                                        //串口控制器B清零
                UCSRC|=BIT(URSEL)|BIT(UCSZ1)|BIT(UCSZ0);        //选择USCRC,异步操作,禁止检验危,1个停止位,八位数据
       
                Temp=(FXTAL/BAUD/16)-1;                        //求出19200波特率的赋值
                UBRRH=((Temp>>8)&0x00ff);                //波特率寄存器高八位赋值
                UBRRL=(Temp&0x00ff);                        //波特率寄存器低八位赋值
       
                //UBRR=71;        //从手册中直接取得赋值,9600kbps
               
                UCSRB|=BIT(TXEN)|BIT(RXEN)|BIT(RXCIE);        //发送使能,接收使能,接收完毕中断使能
                SREG|=BIT(7);                //全局中断使能
}



//串口发送函数
void USART_Send(unsigned char Data)
{
                while(!(UCSRA&(BIT(UDRE))));                //数据寄存器UDR是否为空?
                UDR=Data;                                                        //UDR赋值
                while(!(UCSRA&(BIT(TXC))));                        //数据是否已经发送完毕?
                UCSRA|=BIT(TXC);                                        //清除发送完毕标志位
}

//串口接收完毕中断函数
void USART_Received_Ir(void)
{
   
                UCSRB&=~BIT(RXCIE);                //接收完毕中断不使能
                RX_Buffer=UDR;                        //读取UDR的数据
                RX_Flag=1;                                //接收标志位置一
                UCSRB|=BIT(RXCIE);                //接收完毕中断使能
}
//********************************************************************************






//NRF24L01和SPI
//******************************************************************************


void SPI_init(void)
{
   DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_CSN)|(1 << PB3);      //设置 MOSI,SCK,SS.CE为 OUTPUT,其它为 INPUT
   SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR0);         //使能 SPI 接口,主机模式,MSB 低位在前,模式0,16分频,SPI 时钟1MHZ
   SPSR=0;
}


uchar SPI_RW(uchar date)
{
   SPDR=date;
   while(!(SPSR&(1<<SPIF)));
   return SPDR;
}

uchar SPI_Read(uchar reg)
{
    uchar reg_val;
    nRF24L01_CSNL;                // CSN low, initialize SPI communication...
    SPI_RW(reg);               // Select register to read from..
    reg_val = SPI_RW(0);      // ..then read registervalue
    nRF24L01_CSNH;            // CSN high, terminate SPI communication
    return(reg_val);          // return register value
}

uchar SPI_RW_Reg(uchar reg, uchar value)
{
    uchar status;
    nRF24L01_CSNL;          // CSN low, init SPI transaction
    status = SPI_RW(reg);   // select register
    SPI_RW(value);          // ..and write value to it..
    nRF24L01_CSNH;          // CSN high again
    return(status);         // return nRF24L01 status uchar

}

uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar bites)
{
    uint status1,uchar_ctr;

    nRF24L01_CSNL;               // Set CSN low, init SPI tranaction
    status1 = SPI_RW(reg);       // Select register to write to and read status uchar

    for(uchar_ctr=0;uchar_ctr<bites;uchar_ctr++)
    pBuf = SPI_RW(0);             //

   nRF24L01_CSNH;

    return(status1);             // return nRF24L01 status uchar
}
uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar bites)
{

    uchar status1,uchar_ctr;

    nRF24L01_CSNL;                              //SPI 使能
    status1 = SPI_RW(reg);
    for(uchar_ctr=0; uchar_ctr<bites; uchar_ctr++) //
    SPI_RW(*pBuf++);
    nRF24L01_CSNH;                              //关闭 SPI
    return(status1);                            //

}
void RX_Mode(void)
{
      nRF24L01_CEL;
      SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);
      SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);         // Enable Auto.Ack:Pipe0
      SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);    // Enable Pipe0
      SPI_RW_Reg(WRITE_REG + RF_CH, 40);         // Select RF channel 40
      SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH);
      SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);
      SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);       // Set PWR_UP bit,enable CRC(2 bytes)& Prim:RX. RX_DR enabled..
      nRF24L01_CEH;
      Delayms(20);                                    
}
void TX_Mode(void)
{
   nRF24L01_CEL;
   SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);
   SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);
   SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload
   SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
   SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
   SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
   SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40
   SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
   SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit,enable CRC(2 bytes)& Prim:TX. MAX_RT & TX_DS enabled..
   nRF24L01_CEH;
   Delayms(20);
}
void NRF24l01INIT(void)
{
       SPI_init();
   nRF24L01_CEL; // 芯片使能
   nRF24L01_CSNH; // 禁止SPI
   nRF24L01_SCKL; // 初始化SPI时钟线为高
}
void SPI_CLR_Reg(uchar R_T)
{
   nRF24L01_CSNL;
   if(R_T==1) // CSN low, init SPI transaction
       SPI_RW(FLUSH_TX); // ..and write value to it..
   else
       SPI_RW(FLUSH_RX); // ..and write value to it..
    nRF24L01_CSNH; // CSN high again
}
void NRF_CLERN_ALL(void)
{
      SPI_CLR_Reg(0);
      SPI_CLR_Reg(1);
      SPI_RW_Reg(WRITE_REG+STATUS,0xff);
      nRF24L01_IRQH;
}

//******************************************************************************************/

void Increase_INT0_Ir(void)
{
   GIFR|=BIT(6); //GIFR|=0xC0; INTO清中断标志位
}
void main(void)
{

uint lock_key=0;
uchar sta;
USART_Init();
NRF24l01INIT();
Interrupt_Init();
    DDRA=0xff;                     
    PORTA=0xfb;
    DDRB=0xff;
    PORTB=0XFF;


while(1)
{
         if(RX_Flag)
                        {   
                          RX_Flag=0;
                                PORTB=0xfe;
                                Delayms(100);
                                PORTB=0xff;
                    Delayms(100);
                                tx_buf=RX_Buffer;
                        }//如果串口接受大数据则点亮一个LED灯
            nRF24L01_IRQH;
            SPI_RW_Reg(WRITE_REG+STATUS,0xff);
                  TX_Mode();
                  Delayms(10);
                        while(nRF24L01_IRQ);
                  sta=SPI_Read(STATUS);
                        SPI_RW_Reg(WRITE_REG+STATUS,sta);   
                  if(sta&TX_DS)
         {
                        PORTB=0XFD;
                        Delayms(100);       
                        PORTB=0XFF;
                        Delayms(100);//如果有数据从nrf24l01发送则点亮一个LED灯
         }
         else
         {
            NRF_CLERN_ALL();
          }
         lock_key=1;
       } //发送       
          
       
    if(lock_key)
    {
      lock_key=0;
      RX_Mode();
      nRF24L01_IRQH;
      while(nRF24L01_IRQ==0);
       Delayus(400);
    }
    nRF24L01_IRQH;
        if(nRF24L01_IRQ==0)       
        {
          sta=SPI_Read(STATUS);
      SPI_RW_Reg(WRITE_REG+STATUS,sta);
      if(sta&RX_DR)
      {
         SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);
         USART_Send(*rx_buf);
             }       
    else
    {
      NRF_CLERN_ALL();
      RX_Mode();
      nRF24L01_IRQH;
      while(nRF24L01_IRQ==0);
    }          
          
        }   
}
页: [1]
查看完整版本: 我一直在用串口给nrf24l01发送数据进行通信还是不行求助