#include
#include
#include "lcd12864.h"
#define uint unsigned int
#define uchar unsigned char
#define MISO PINB.6
#define SCK PORTB.7
#define MOSI PORTB.5
#define CSN PORTC.0
#define CE PORTC.1
#define IRQ PORTC.2
#define CONFIG 0x00 // 配置收发状态,CRC校验模式以及收发状态响应方式
#define EN_AA 0x01//自动应答使能
#define EN_RXADDR 0x02 //接收地址允许
#define SETUP_AW 0x03 //设置地址宽度
#define SETUP_RETR 0x04 //建立自动重发
#define RF_CH 0x05 //射频通道
#define RF_SETUP 0x06 //射频寄存器
#define STATUS 0x07 //状态寄存器
#define OBSTERVE_TX 0x08 //发送检测寄存器
#define CD 0x09 //载波检测
#define RX_ADDR_P0 0x0a //数据通道0接收地址
#define RX_ADDR_P1 0x0b //数据通道0接收地址
#define RX_ADDR_P2 0x0c //数据通道0接收地址
#define RX_ADDR_P3 0x0d //数据通道0接收地址
#define RX_ADDR_P4 0x0e //数据通道0接收地址
#define RX_ADDR_P5 0x0f //数据通道0接收地址
#define TX_ADDR 0x10 //发送地址
#define RX_PW_P0 0x11 //
#define RX_PW_P1 0x12 //
#define RX_PW_P2 0x13 //
#define RX_PW_P3 0x14 //
#define RX_PW_P4 0x15 //
#define RX_PW_P5 0x16 //
#define FIFO_STATUS 0x17 //FIFO状态寄存器
#define READ_REG 0x00 // 读寄存器指令
#define WRITE_REG 0x20 // 写寄存器指令
#define RD_RX_PLOAD 0x61 // 读取接收数据指令
#define WR_TX_PLOAD 0xA0 // 写待发数据指令
#define FLUSH_TX 0xE1 // 冲洗发送 FIFO指令
#define FLUSH_RX 0xE2 // 冲洗接收 FIFO指令
#define REUSE_TX_PL 0xE3 // 定义重复装载数据指令
#define NOP 0xFF // 保留
#define TX_ADR_WIDTH 5 // 5 uints TX address width
#define RX_ADR_WIDTH 5 // 5 uints RX address width
#define TX_PLOAD_WIDTH 20 // 20 uints TX payload
#define RX_PLOAD_WIDTH 20 // 20 uints TX payload
uchar status;
uchar sta;
uchar str[30];
uchar TX_ADDRESS[TX_ADR_WIDTH]= {0x05,0x04,0x03,0x02,0x01}; //本地地址
uchar RX_ADDRESS[RX_ADR_WIDTH]= {0x05,0x04,0x03,0x02,0x01}; //接收地址
uchar TxBuf[5]={0x65,0x98,0xf3,0x02,0x08};
uchar SPI_RW(uchar uuchar)//SPI写时序
{
uchar bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++)
{
MOSI=(uuchar&0x80);
uuchar=(uuchar<<1);
SCK=1;
uuchar|=MISO;
SCK=0;
}
return (uuchar);
}
uchar SPI_Read(uchar reg)//SPI读取一个字节
{
uchar reg_val;
CSN=0;
SPI_RW(reg);
delay_ms(1);
reg_val=SPI_RW(0);
delay_ms(1);
CSN=1;
return (reg_val);
}
uchar SPI_RW_Reg(uchar reg,uchar value)//写一个自接到寄存器函数
{
uchar status;
CSN=0;
status=SPI_RW(reg);
delay_ms(1);
SPI_RW(value);
delay_ms(1);
CSN=1;
return (status);
}
uchar *ucharostr(uchar num)
{
uchar x00,xx,x0,x,n;
x00=num/100;
xx=num0;
x0=xx/10;
x=xx;
n=0;
if(x00!=0)
{
str[n]=x00+48;
n++;
}
if(!(x00==0&x0==0))
{
str[n]=x0+48;
n++;
}
str[n]=x+48;
n++;
str[n]='\0';
return str;
}
uchar SPI_Read_Buf(uchar reg,uchar *pBuf,uchar uchars)//用于读数据,reg:寄存器地址,pBuf:带度数数据地址,uchars数据个数
{
uchar status,uchar_ctr;
CSN=0;
status=SPI_RW(reg);
for(uchar_ctr=0;uchar_ctr
{
pBuf[uchar_ctr]=SPI_RW(0);
}
CSN=1;
return (status);
}
uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
{
uint status,uchar_ctr;
CSN = 0; //SPI使能
status = SPI_RW(reg);
for(uchar_ctr=0; uchar_ctr
SPI_RW(*pBuf++);
CSN = 1; //关闭SPI
return(status); //
}
void init_NRF24L01(void)
{
delay_us(100);
CE=0; //射频停止工作
CSN=1;
SCK=0;
IRQ=1;//中断复位
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+EN_AA,0x00);//频道0自动,ACK应答禁止
SPI_RW_Reg(WRITE_REG+EN_RXADDR,0x01);//允许接收地址只有频道0
SPI_RW_Reg(WRITE_REG+RF_CH,1);//设置信道工作2.4GHZ,收发必须一致
SPI_RW_Reg(WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH);//设置接收数据长度,32字节
SPI_RW_Reg(WRITE_REG+RF_SETUP,0x07);//发射速率2MHZ,发射功率最大为0dB
}
void SetRX_Mode(void)//设置数据接收模式
{
CE=0;
SPI_RW_Reg(WRITE_REG+CONFIG,0x0f);//IRQ中断响应,16位CRC,接收模式
CE=1;
delay_us(130);
}
uchar nRF24L01_RxPacket(uchar *rx_buf)
{
uchar revale=0;
sta=SPI_Read(STATUS);
if(sta&0x40)//判断是否接受到数据RX_DR
{
CE=0;
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);//读取接收数据
revale=1;
SPI_RW_Reg(WRITE_REG+STATUS,0xff);//写1清楚中断标志
}
return revale;
}
void main(void)
{
uchar i;
uchar RxBuf[32];
uchar Buf0[5]={0,0,0,0,0};
i=0;
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0xB0;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0xff;
DDRC=0xff;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
lcd12864_init();
delay_ms(10);
init_NRF24L01();
IRQ=1;
SPI_Read_Buf(RX_ADDR_P0,Buf0,5);//读取0通道接收地址
delay_ms(100);
lcd12864_puts(ucharostr(Buf0[0]));
lcd12864_puts(ucharostr(Buf0[1]));
lcd12864_puts(ucharostr(Buf0[2]));
lcd12864_puts(ucharostr(Buf0[3]));
lcd12864_puts(ucharostr(Buf0[4]));
delay_ms(20);
SetRX_Mode();
delay_ms(50);
while (1)
{
SetRX_Mode();
delay_ms(20);
i=SPI_Read(CD);
if(i)//如果检测到中断产生
{ nRF24L01_RxPacket(RxBuf);
delay_ms(10);
lcd12864_gotoxy(0,0);
for(i=0;i<5;i++)
{
lcd12864_putchar(RxBuf[i]);
delay_ms(2); }
for(i=5;i<10;i++)
{
lcd12864_putchar(RxBuf[i]);
delay_ms(2);
}
for(i=10;i<16;i++)
{
lcd12864_putchar(RxBuf[i]);
delay_ms(2);
}
i=0;
}
};
}
上一篇:NRF24L01无线模块发送-AVR程序代码
下一篇:超声波测距-AVR程序代码
推荐阅读最新更新时间:2024-03-16 15:46