|
#include <avr/io.h>
#include<util/delay.h>
#define uchar unsigned char
#define MOSI 5
#define MISO 6
#define SCK 7
#define IRQ 2
#define CSN 4
#define CE 3
#define PWR_H PORTB |= (1 << PWR)
#define PWR_L PORTD &= ~(1 << PWR)
#define CSN_H PORTB |= (1 << CSN)
#define CSN_L PORTB&= ~(1 << CSN)
#define CE_H PORTB|= (1 << CE)
#define CE_L PORTB &= ~(1 << CE)
void SPI_Init(void);//SPI初始化
uchar SPI_RW(unsigned char dat);//读写SPI
void IO_Init(void);//io口初始化
uchar SPI_RW(uchar byte);
uchar SPI_RW_Reg(uchar reg, uchar value);
uchar SPI_Read(uchar reg);
uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar bytes);
uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar bytes);
void RX_Mode(void);
void TX_Mode(void);
uchar TX_ADDRESS[5]={0,0,0,0,0};//发送数据地址
uchar tx_buf[100]={0x55,0xff,0x00,0xC0,0xF9,0xA4};
uchar t[10]={0x5,0,0};//发送数据内容
uchar TX_ADR_WIDTH;//发送地址宽度
uchar TX_PLOAD_WIDTH;//数据长度
#define READ_REG 0x00 // 读配置寄存器AAAAA 指出读操作的寄存器地址 Define read command to register
#define WRITE_REG 0x20 // 写配置寄存器AAAAA 指出写操作的寄存器地址只有在掉电模式和待机模式下可操作 Define write command to register
#define RD_RX_PLOAD 0x61 // 读RX 有效数据1-32 字节读操作全部从字节0 开始当读RX有效数据完成后FIFO 寄存器中有效数据被清除应用于接收模式下Define RX payload register address
#define WR_TX_PLOAD 0xA0 //写TX 有效数据1-32 字节写操作从字节0 开始应用于发射模式下 Define TX payload register address
#define FLUSH_TX 0xE1 // 清除TX FIFO 寄存器应用于发射模式下Define flush TX register command
#define FLUSH_RX 0xE2 //清除RX FIFO 寄存器应用于接收模式下在传输应答信号过程中不应执行此指令也就是说若传输应答信号过程中执行此指令的话将使得应答信号不能被完整的传输 Define flush RX register command
#define REUSE_TX_PL 0xE3 // 重新使用上一包有效数据当CE 为高过程中数据包被不断的重新发射在发射数据包过程中必须禁止数据包重利用功能Define reuse TX payload register command
#define NOP 0xFF // 空操作可以用来读状态寄存器 Define No Operation, might be used to read status register
#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 //数据通道0 接收地址,最大长度:5 个字节先写低字节所写字节数量由SETUP_AW 设定 'RX address pipe0' register address
#define RX_ADDR_P1 0x0B // 数据通道1 接收地址.最大长度:5 个字节先写低字节所写字节数量由SETUP_AW 设定 RX address pipe1' register address
#define RX_ADDR_P2 0x0C // 数据通道2接收地址.最低字节可设置高字节部分必须与RX_ADDR_P1[39:8]相等''RX address pipe2' register address
#define RX_ADDR_P3 0x0D // 数据通道3 接收地址 最低字节可设置高字节部分必须与RX_ADDR_P1[39:8]相等'RX address pipe3' register address
#define RX_ADDR_P4 0x0E // 数据通道4 接收地址.最低字节可设置高字节部分必须与RX_ADDR_P1[39:8]相等''RX address pipe4' register address
#define RX_ADDR_P5 0x0F // 数据通道5 接收地址 .最低字节可设置高字节部分必须与RX_ADDR_P1[39:8]相等''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 状态寄存器'FIFO Status Register' register address
void IO_Init(void)
{
//DDRB = (1 << MOSI)|(1 << SCK)|(1 <<CSN);
//DDRB = (1 << CSN);
DDRB = (1 << CE)|(1<<PB3);
PORTB = (1 << IRQ);
PORTB= (1 << CE);
}
/*
函数功能:SPI初始化
入口参数:无
返 回:无
备 注:无
void SPI_Init(void)
{
SPI_PORT = (1 << SPI_SS) | (1 << SPI_MISO);//将SS置位输出拉高,MISO输入带上拉
SPI_DDR = (1 << SPI_SS) | (1 << SPI_MOSI) | (1 << SPI_SCK);
//将SS SCK MOSI置为输出
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);//根据实际情况修改
//使能SPI接口,主机模式,MSB低位在前,模式0,16分频,SPI时钟1MHZ
}
函数功能:SPI读写一个字节函数
入口参数:需要发送的1字节数据
返 回:无
备 注:无
uchar SPI_RW(uchar data)
{
SPDR = data; //传送数据到SPI寄存器,并启动一次SPI传输
while(!(SPSR & (1 << SPIF)))//等待数据传送完
;
return (SPDR); //返回接收的数据
}*/
void SPI_Init(void)
{
PORTB |= (1<<PB4) | (1<<PB5) | (1<<PB6) | (1<<PB7); //
DDRB |= (1<<DDB5) | (1<<DDB7) | (1<<DDB4); //设置 MOSI, SCK 和 SS as 输出
SPCR = 0x73; //SPI使能,低位首发,主机模式,时钟模式00,上升沿采样;时钟速率Fosc/128
SPSR = 0x00; //SPI2x=0
}
unsigned char SPI_RW(unsigned char dat)//读写SPI
{
SPSR &= ~(1 << SPIF);
SPDR = dat;
while((SPSR & (1 << SPIF)) == 0);
return SPDR;
}
uchar SPI_RW_Reg(uchar reg, uchar value)//寄存器访问函数
{
uchar status;
CSN_L; // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
CSN_H; // CSN high again
return(status); // return nRF24L01 status byte
}
uchar SPI_Read(uchar reg)//读取寄存器值的函数
{
uchar reg_val;
CSN_L; // CSN low, initialize SPI communication...
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read registervalue
CSN_H; // CSN high, terminate SPI communication
return(reg_val); // return register value
}
uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar bytes)//接收缓冲区访问函数
{
uchar status,byte_ctr;
CSN_L; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status byte
for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
pBuf[byte_ctr] = SPI_RW(0); // Perform SPI_RW to read byte from nRF24L01
CSN_H; // Set CSN high again
return(status); // return nRF24L01 status byte
}//主要用来在接收时读取FIFO 缓冲区中的值。基本思路就是通过READ_REG 命令把数据从接收FIFO(RD_RX_PLOAD)中读出并存到数组里面去。
uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar bytes)//发射缓冲区访问函数
{
uchar status,byte_ctr;
CSN_L; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status byte
_delay_us(10);
for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf)
SPI_RW(*pBuf++);
CSN_H; // Set CSN high again
return(status); // return nRF24L01 status byte
}//发射缓冲区访问函数:主要用来把数组里的数放到发射FIFO 缓冲区中。基本思路就是通过WRITE_REG 命令把数据存到发射FIFO(WR_TX_PLOAD)中去。
void RX_Mode(void)//设定24L01 为接收方式
{
CE_L;
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..
CE_H; // Set CE pin high to enable RX device
// This device is now ready to receive one packet of 16 bytes payload from a TX devicesending to address
// '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 anddatarate = 2Mbps.
}
void TX_Mode(void)//设定24L01 为发送方式
{
CE_L;
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);
//SPI_Read_Buf(WRITE_REG + TX_ADDR, t, 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);//PORTA=0x55;
//if(SPI_Read(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..
CE_H;
}
//设置SPI为主模式,以及设置SPI时钟频率
int main(void)
{
//uchar i;
DDRA=0xff;
DDRC=0xff;
PORTC=0xff;
SPI_Init();
IO_Init();
// Insert code
//SPI_RW_Reg(WRITE_REG);
while(1)
{
TX_ADR_WIDTH=5;
TX_PLOAD_WIDTH=1;
TX_Mode();
//for(i=0;i<5;i++)
//{
//PORTA=t[0];
//}
//_delay_ms(1000);
}
return 0;
}
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|