这个程序有错吗?
#include<iom16v.h>
#include<macros.h>
#define NULL 0
#define DDR DDRB
#define PORT PORTB
#define CE 3
#define SS 4
#define MOSI 5
#define MISO 6
#define SCK 7
#define CE_1 PORT|=(1<<CE)
#define CE_0 PORT&=~(1<<CE)
#define SS_1 PORT|=(1<<SS)
#define SS_0 PORT&=~(1<<SS)
#define R_REG 0x00
#define W_REG 0x20
#define R_DATA 0x61
#define W_DATA 0xA0
#define FLUSH_TX 0xE1
#define FLUSH_RX 0xE2
#define CONFIG 0x00
#define EN_AA 0x01
#define EN_RXADDR 0x02
#define RF_CH 0x05
#define RF_SETUP 0x06
#define STATUS 0x07
#define RX_ADDR_P0 0x0A
#define TX_ADDR 0x10
#define RX_PW_P0 0x11
void delay_us(unsigned int Xus)
{
if(Xus==0)
return;
while(--Xus);
}
void delay_ms(unsigned int Xms)
{
unsigned int j;
while(Xms--)
for(j=0;j<1140;j++);
}
int PORT_init()
{
DDRA=0xFF;
DDR=(1<<CE)|(1<<SS)|(1<<MOSI)|(1<<SCK); //Output:CE,SS,MOSI,SCK;input:MISO;
PORT=(1<<SS); //Stop tranmission;
}
void SPI_init()
{
SPSR=0x01; //Double speed;
SPCR=0x51; //Enable SPI,master;
}
unsigned char SPI_rw(unsigned char DATA)
{
SPDR=DATA;
while(!(SPSR&0x80));
delay_us(10);
return SPDR;
}
void SPI_regW(unsigned char REG,unsigned char DATA)
{
SS_0;
SPI_rw(REG|W_REG);
SPI_rw(DATA);
SS_1;
}
unsigned char SPI_regR(unsigned char REG)
{
unsigned char temp;
SS_0;
SPI_rw(REG|R_REG);
temp=SPI_rw(NULL);
SS_1;
return temp;
}
void SPI_bufW(unsigned char REG,unsigned char *BUF,unsigned char LONG)
{
SS_0;
SPI_rw(REG|W_REG);
while(LONG--)
{
SPI_rw(*BUF++);
}
SS_1;
}
void SPI_bufR(unsigned char REG,unsigned char *BUF,unsigned char LONG)
{
SS_0;
SPI_rw(REG|R_REG);
while(LONG--)
{
BUF[LONG]=SPI_rw(NULL);
}
SS_1;
}
void NRF24L01master_init(unsigned char *BUF)
{
CE_0;
SPI_bufW(RX_ADDR_P0,BUF,5); //Data channel 0 receive address;
SPI_bufW(TX_ADDR,BUF,5); //Send address;
SPI_regW(EN_AA,0x01); //Enable channel 0 ack;
SPI_regW(EN_RXADDR,0x01); //Enable channel 0;
SPI_regW(RF_CH,0x00); //working frequency,Must be consistent with the slave;
SPI_regW(RX_PW_P0,0x01); //One byte data width;
SPI_regW(CONFIG,0x7A); //Mask all interrupts,Power-on,Transmission mode;
CE_1;
}
void NRF24L01slave_init(unsigned char *BUF)
{
CE_0;
SPI_bufW(RX_ADDR_P0,BUF,5); //Data channel 0 receive address;
SPI_regW(EN_AA,0x01); //Enable channel 0 ack;
SPI_regW(EN_RXADDR,0x01); //Enable channel 0;
SPI_regW(RF_CH,0x00); //working frequency,Must be consistent with the master;
SPI_regW(RX_PW_P0,0x01); //One byte data width;
SPI_regW(CONFIG,0x7B); //Mask all interrupts,Power-on,Receive mode;
CE_1;
}
void NRF24L01_send(unsigned char DATA)
{
CE_0;
SPI_regW(W_DATA,DATA);
CE_1;
delay_us(300);
}
unsigned char NRF24L01_receive()
{
unsigned char temp=NULL;
if((SPI_regR(STATUS)&0x40))
{
temp=SPI_regR(R_DATA);
SPI_regW(STATUS,(SPI_regR(STATUS)&(~(0x40))));
}
return temp;
}
void main()
{
unsigned char address[5]={0x34,0x43,0x10,0x10,0x01};
PORT_init();
SPI_init();
NRF24L01master_init(address);
while(1)
{
unsigned char i=0;
for(i=0;i<0xFF;i++)
{
NRF24L01_send(i);
delay_ms(300);
}
}
}
|