搜索
bottom↓
回复: 6

请各位老师帮忙看一下这些测试24L01的代码是否有问题?

[复制链接]

出0入0汤圆

发表于 2009-7-11 19:56:30 | 显示全部楼层 |阅读模式
调试了一下午了,快要崩溃了。。。

编译器用的是IAR AVR的。

点击此处下载 ourdev_460378.rar(文件大小:33K) (原文件名:Test24L01.rar)

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入0汤圆

 楼主| 发表于 2009-7-11 19:59:05 | 显示全部楼层
补充:单片机用的是M88,晶振7372800

接线情况如下:

CE ---〉PB1
CSN--〉PB2
MOSI--〉PB3
MISO--〉PB4
SCK-->PB5
IRQ--〉PC0
LED---〉PD7


KEY_IN-->PC5 将PC5脚拉低就是发射端,发射时LED亮,接收端若收到数据LED也亮。

出0入0汤圆

 楼主| 发表于 2009-7-11 20:15:36 | 显示全部楼层
共4个文件:

IO_Def.h

#ifndef IO_DEF_H
#define IO_DEF_H

#define  ENABLE_BIT_DEFINITIONS
#define SYSTEM_CLOCK 7372800
//#include "def_AVR_IAR.h"
#include <iom88.h>
#include <ina90.h>

#define _NOP_us(i)  __delay_cycles((i*(unsigned long long)SYSTEM_CLOCK)/1000000)
#define _NOP_ms(i)  __delay_cycles((i*(unsigned long long)SYSTEM_CLOCK)/1000)

//Extern


//LED
#define KEY_DDR DDRC_Bit5
#define KEY_OUT PORTC_Bit5
#define KEY_IN  PINC_Bit5


//LED
#define LED_DDR DDRD_Bit7
#define LED_OUT PORTD_Bit7

#endif //IO_DEF_H

出0入0汤圆

 楼主| 发表于 2009-7-11 20:16:20 | 显示全部楼层
NRF24L01.h

// nRF24L01.h
#ifndef _NRF_24L01_
#define _NRF_24L01_

#include "../IO_def.h"

/*********************** IO Mapping *****************************/
#define   nRF24L01_MISO_DDR        DDRB_Bit4
#define   nRF24L01_MOSI_DDR        DDRB_Bit3
#define   nRF24L01_SCK_DDR      DDRB_Bit5   
#define   nRF24L01_CE_DDR        DDRB_Bit1  
#define   nRF24L01_CSN_DDR      DDRB_Bit2               
#define   nRF24L01_IRQ_DDR      DDRC_Bit0       

#define   nRF24L01_MISO          PORTB_Bit4
#define   nRF24L01_MOSI          PORTB_Bit3
#define   nRF24L01_SCK          PORTB_Bit5   
#define   nRF24L01_CE          PORTB_Bit1  
#define   nRF24L01_CSN    PORTB_Bit2               
#define   nRF24L01_IRQ    PORTC_Bit0               

#define   nRF24L01_MISO_PIN         PINB_Bit4
#define   nRF24L01_IRQ_PIN         PINC_Bit0       


// Define nRF24L01 interrupt flag's
#define IDLE            0x00  // Idle, no interrupt pending
#define MAX_RT          0x10  // Max #of TX retrans interrupt
#define TX_DS           0x20  // TX data sent interrupt
#define RX_DR           0x40  // RX data received


#define TX_ADR_WIDTH    5   // 5 bytes TX address width
#define RX_ADR_WIDTH    5   // 5 bytes RX address width
#define TX_PLOAD_WIDTH  32  // 20 bytes TX payload
#define RX_PLOAD_WIDTH  32  // 20 bytes TX payload

//****************************************************************//
// Instruction set for the nRF24L01 SPI interface
#define READ_REG        0x00  // Define read command to register
#define WRITE_REG       0x20  // Define write command to register
#define RD_RX_PLOAD     0x61  // Define RX payload register address
#define WR_TX_PLOAD     0xA0  // Define TX payload register address
#define FLUSH_TX        0xE1  // Define flush TX register command
#define FLUSH_RX        0xE2  // Define flush RX register command
#define REUSE_TX_PL     0xE3  // Define reuse TX payload register command
#define NOP             0xFF  // Define No Operation, might be used to read status register


//***************************************************//
// SPI(nRF24L01) registers(addresses)
#define CONFIG          0x00  // 'Config' register address
#define EN_AA           0x01  // 'Enable Auto Acknowledgment' register address
#define EN_RXADDR       0x02  // 'Enabled RX addresses' register address
#define SETUP_AW        0x03  // 'Setup address width' register address
#define SETUP_RETR      0x04  // 'Setup Auto. Retrans' register address
#define RF_CH           0x05  // 'RF channel' register address
#define RF_SETUP        0x06  // 'RF setup' register address
#define STATUS          0x07  // 'Status' register address
#define OBSERVE_TX      0x08  // 'Observe TX' register address
#define CD              0x09  // 'Carrier Detect' register address
#define RX_ADDR_P0      0x0A  // 'RX address pipe0' register address
#define RX_ADDR_P1      0x0B  // 'RX address pipe1' register address
#define RX_ADDR_P2      0x0C  // 'RX address pipe2' register address
#define RX_ADDR_P3      0x0D  // 'RX address pipe3' register address
#define RX_ADDR_P4      0x0E  // 'RX address pipe4' register address
#define RX_ADDR_P5      0x0F  // 'RX address pipe5' register address
#define TX_ADDR         0x10  // 'TX address' register address
#define RX_PW_P0        0x11  // 'RX payload width, pipe0' register address
#define RX_PW_P1        0x12  // 'RX payload width, pipe1' register address
#define RX_PW_P2        0x13  // 'RX payload width, pipe2' register address
#define RX_PW_P3        0x14  // 'RX payload width, pipe3' register address
#define RX_PW_P4        0x15  // 'RX payload width, pipe4' register address
#define RX_PW_P5        0x16  // 'RX payload width, pipe5' register address
#define FIFO_STATUS     0x17  // 'FIFO Status Register' register address


//
/*
#define  RX_DR         (0x01<<6)
#define         TX_DS         (0x01<<5)
#define         MAX_RT         (0x01<<4)
#define  TX_FULL (0x01<<0)

union STATUS_24L01
{
  unsigned char sta;
  struct
  {
    unsigned char TX_FULL:1;
    unsigned char Bit1:1;
    unsigned char Bit2:1;
    unsigned char Bit3:1;
    unsigned char MAX_RT:1;
    unsigned char TX_DS:1;
    unsigned char RX_DR:1;
    unsigned char Bit7:1;
  }bits;
};
*/

//***************************************************************//
//                   FUNCTION's PROTOTYPES  //
/****************************************************************/
// void SPI_Init(BYTE Mode);     // Init HW or SW SPI
unsigned char SPI_RW(unsigned char byte);                               // Single SPI read/write
unsigned char SPI_Read(unsigned char reg);                              // Read one byte from nRF24L01
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char byte);        // Write one byte to register 'reg'
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char const *pBuf, unsigned char bytes);  // Writes multiply bytes to one register
unsigned char SPI_Read_Buf(unsigned char reg,  unsigned char *pBuf, unsigned char bytes);   // Read multiply bytes from one register
//*****************************************************************/

// 常用函数
void nRF24L01_Config(void);
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);
void nRF24L01_TxPacket(unsigned char * tx_buf);



#endif   //_NRF_24L01_

出0入0汤圆

 楼主| 发表于 2009-7-11 20:17:16 | 显示全部楼层
NRF24L01.c

//
// nRF24L01.c

#include "nRF24L01.h"

unsigned char const TX_ADDRESS[TX_ADR_WIDTH]  = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address
unsigned char const RX_ADDRESS[RX_ADR_WIDTH]  = {0x34,0x43,0x10,0x10,0x01}; // Define a static RX address
//unsigned char const RX_ADDRESS[RX_ADR_WIDTH]  = {0x1,0x1,0x1,0x1,0x1};

//union STATUS_24L01 sta_2401;


void nRF24L01_Config(void)
{
  //initial io
  nRF24L01_MISO_DDR =0;
  nRF24L01_MOSI_DDR =1;       
  nRF24L01_SCK_DDR  =1;            
  nRF24L01_CE_DDR   =1;       
  nRF24L01_CSN_DDR  =1;                           
  nRF24L01_IRQ_DDR  =0;       
  
  nRF24L01_CE       =0;       
  nRF24L01_CSN      =1;       
  nRF24L01_SCK      =1;       
  nRF24L01_MISO     =0;
  nRF24L01_MOSI     =0;                       
  nRF24L01_IRQ      =0;       
  
  _NOP_ms(10);
  nRF24L01_CE       =0;       
  SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);   
  SPI_RW_Reg(WRITE_REG + EN_AA, 0x00);      
  SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);
  SPI_RW_Reg(WRITE_REG + SETUP_AW, 0x03);   
  SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x00);
  SPI_RW_Reg(WRITE_REG + RF_CH, 0);         
  SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f);  
  // STATUS
  // OBSERVE_TX
  // CD
  SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH);
  SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);   
  SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH);
  
  SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);     // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..
  nRF24L01_CE=1;  //
  _NOP_us(130);
  nRF24L01_CE       =0;       
}


/**************************************************
Function: SPI_RW();

  Description:
  Writes one byte to nRF24L01, and return the byte read
  from nRF24L01 during write, according to SPI protocol
**************************************************/
unsigned char SPI_RW(unsigned char byte)
{
  unsigned char i;
  nRF24L01_SCK = 0;   
  for(i=0;i<8;i++) // output 8-bit
  {
    nRF24L01_MOSI = (byte & 0x80)>>7;  // output 'byte', MSB to MOSI
    byte = byte<<1;                        // shift next bit into MSB..
    nRF24L01_SCK = 1;                           // Set SCK high..
    byte |=  (nRF24L01_MISO_PIN&0X01);        // capture current MISO bit
    nRF24L01_SCK = 0;                              // ..then set SCK low again
  }
  return(byte);                             // return read byte
}
/**************************************************/

/**************************************************
Function: SPI_RW_Reg();

  Description:
  Writes value 'value' to register 'reg'
**************************************************/
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value)
{
  unsigned char status=0;
  nRF24L01_CSN = 0;                   // CSN low, init SPI transaction
  status = SPI_RW(reg);      // select register
  SPI_RW(value);             // ..and write value to it..
  nRF24L01_CSN = 1;                   // CSN high again
  
  return(status);            // return nRF24L01 status byte
}
/**************************************************/

/**************************************************
Function: SPI_Read();

  Description:
  Read one byte from nRF24L01 register, 'reg'
**************************************************/
unsigned char SPI_Read(unsigned char reg)
{
  unsigned char reg_val;
  
  nRF24L01_CSN = 0;                // CSN low, initialize SPI communication...
  SPI_RW(reg);            // Select register to read from..
  reg_val = SPI_RW(0);    // ..then read registervalue
  nRF24L01_CSN = 1;                // CSN high, terminate SPI communication
  
  return(reg_val);        // return register value
}
/**************************************************/

/**************************************************
Function: SPI_Read_Buf();

  Description:
  Reads 'bytes' #of bytes from register 'reg'
  Typically used to read RX payload, Rx/Tx address
**************************************************/
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
  unsigned char status=0;
  unsigned char i=0;
  nRF24L01_CSN = 0;                                    // Set CSN low, init SPI tranaction
  status = SPI_RW(reg);                       // Select register to write to and read status byte
  for(i=0;i<bytes;i++)
  {
    pBuf = SPI_RW(0);    // Perform SPI_RW to read byte from nRF24L01
  }
  nRF24L01_CSN = 1;                           // Set CSN high again
  
  return(status);                    // return nRF24L01 status byte
}
/**************************************************/

/**************************************************
Function: SPI_Write_Buf();

  Description:
  Writes contents of buffer '*pBuf' to nRF24L01
  Typically used to write TX payload, Rx/Tx address
**************************************************/
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char const *pBuf, unsigned char bytes)
{
  unsigned char status=0;
  unsigned char i=0;
  
  nRF24L01_CSN = 0;                   // Set CSN low, init SPI tranaction
  status = SPI_RW(reg);    // Select register to write to and read status byte
  for(i=0; i<bytes; i++) // then write all byte in buffer(*pBuf)
  {
    SPI_RW(*pBuf++);
  }
  nRF24L01_CSN = 1;                 // Set CSN high again
  return(status);          // return nRF24L01 status byte
}


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

unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
  unsigned char revale=0;
  unsigned char tmp=0;
  
  if(nRF24L01_IRQ_PIN==1)
    return 0;


    tmp=SPI_Read(STATUS);        // read register STATUS's value
    if(tmp&RX_DR)
    {
      nRF24L01_CE = 0;   // stand by mode
      SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
      revale =1;//we have receive data
    }

  SPI_RW_Reg(WRITE_REG+STATUS,tmp);
  nRF24L01_CE =0;
  
  SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);     // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..
  nRF24L01_CE = 1;
  _NOP_us(130);
  return revale;
}

/**************************************************
Function: nRF24L01_TxPacket();

  Description:
  This function initializes one nRF24L01 device to
  TX mode, set TX address, set RX address for auto.ack,
  fill TX payload, select RF channel, datarate & TX pwr.
  PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX.
  
        ToDo: One high pulse(>10us) on CE will now send this
        packet and expext an acknowledgment from the RX device.
**************************************************/
void nRF24L01_TxPacket(unsigned char * tx_buf)
{

  nRF24L01_CE =0;

  SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH);
  SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);     
  nRF24L01_CE=1;
  _NOP_us(160);
  nRF24L01_CE=0;
  
  SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);     // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..
  nRF24L01_CE = 1;
  _NOP_us(130);
}
/**************************************************/

出0入0汤圆

 楼主| 发表于 2009-7-11 20:17:53 | 显示全部楼层
main.c

#include ".\\nRF24L01\\nRF24L01.h"

unsigned char tf =0;
unsigned char txbuf[32]; //
unsigned char rxbuf[32];
unsigned char counter=0;
unsigned char counter2=0;


void USART0_DEFAULT_SETTING( unsigned int ubrr)
{
  /*Set baud rate */
  UBRR0=(0x0fff&(SYSTEM_CLOCK/(16*(unsigned long long)ubrr)-1));
  /*Enable receiver and transmitter */
  UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  /* Set frame format: 8data, 2stop bit */
  UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}

#define USART0_PRINTF(message) \
while(!((UCSR0A>>UDRE0)&1));\
(UDR0 = message)

void main(void)
{
  unsigned char i=0;
  KEY_DDR =0;
  KEY_OUT =1;
  LED_DDR =1;
  LED_OUT =1;
  
  USART0_DEFAULT_SETTING(9600);
  
  nRF24L01_Config();
  

  _NOP_ms(10);
  
  LED_OUT =0;
  USART0_PRINTF(0xaa);
  
  //SET_INT0_MODE_ON_LOW_LEVEL();
  //CLEAR_INT0_INTFLAG();
  //NEABLE_INT0();
  
  //__enable_interrupt();
  
  while(1)
  {
    if(KEY_IN ==0)
    {
      
      _NOP_ms(2);
     
      
      LED_OUT = 1;
      counter =25;
      for( i=0;i<32;i++)
      {
        txbuf=i+1;
      }
      nRF24L01_TxPacket(txbuf);
      for( i=0;i<32;i++)
      {
        txbuf=0;
      }
    }
   
   
    //POWER_OFF();
   

    if(nRF24L01_RxPacket(rxbuf))
    {
      LED_OUT = 1;
      counter =25;
      for(i=0;i<32;i++)
      {
       USART0_PRINTF(rxbuf);
      }
      _NOP_ms(5);
    }

    if(counter>0)
    {
      _NOP_ms(5);
      counter--;
    }
    else
    {
       LED_OUT = 0;
    }
  }

}
  
/*

#pragma vector = INT0_vect
__interrupt void WirelessReceive()
{
    if(nRF24L01_RxPacket(RxBuf)==1)
    {
      if(RxBuf[1]==1)
      {                
         LED1 = 1;
         counter1=48;
      }
      if(RxBuf[2]==1)
      {
         LED2 = 1;
         counter2=48;
      }
      RxBuf[1]=0;
      RxBuf[2]=0;
    }
}
*/

出0入0汤圆

发表于 2009-9-26 18:46:01 | 显示全部楼层
mark
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-6-7 03:44

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表