?? 24l01_driver.h
字號:
#include <iom16v.h>
#include <macros.h>
#include <bit.h>
#define start 's'
#define wat 'w'
#define heal 'h'
#define cyc 'c'
#define rep 'r'
#define TX_ADR_WIDTH 5
#define TX_PLOAD_WIDTH 2 //數(shù)據(jù)緩沖區(qū)大小(2字節(jié))
uchar TX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; // Define a static TX address
#define CE_0 do {PORTA&=0xFD;} while(0)
#define CE_1 do {PORTA|=0x02;} while(0)
#define CSN_0 do {PORTB&=0XEF;} while(0)
#define CSN_1 do {PORTB|=0X10;} while(0)
uchar rx_buf[TX_PLOAD_WIDTH]; //接收緩沖區(qū)
uchar tx_buf[TX_PLOAD_WIDTH]; //發(fā)送緩沖區(qū)
uchar flag,flag1;
uchar date;
uchar sta;
char Reciv_data;
char num_buff;//數(shù)字緩沖器
char flagg;//數(shù)據(jù)屬性標(biāo)志位
char fir_bit;
char sec_bit;
char i;
//-----------------------------------------------------------
//串口初始化子程序
//-----------------------------------------------------------
void Uart_Init(void);
//-----------------------------------------------------------
//串口接收字節(jié)子程序
//-----------------------------------------------------------
unsigned char Getchar(void);
//-----------------------------------------------------------
//串口發(fā)送字節(jié)子程序
//-----------------------------------------------------------
void Putchar(unsigned char c);
//-----------------------------------------------------------
//串口發(fā)送字符串子程序
//-----------------------------------------------------------
void Putstr(char *s);
//-----------------------------------------------------------
//串口發(fā)送字符串子程序(帶有換行符)
//-----------------------------------------------------------
void Puts(char *s);
//-----------------------------------------------------------
//-----------------------------------------------------------
//串口初始化子程序
//字符長度:8位
//奇偶校驗:禁止
//通訊模式:異步
//-----------------------------------------------------------
void Uart_Init(void)
{ /*
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00; //Bit1為1則倍速發(fā)送
UCSRC = 0x86;
UBRRL = 0x33; //波特率:9600 Bps
UBRRH = 0x00; //誤差率:0.156%
UCSRB = 0x18;
*/
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00; //Bit1為1則倍速發(fā)送
UCSRC = BIT(URSEL) |0x06;
UBRRL = 0x33; //波特率:9600 Bps
UBRRH = 0x00; //誤差率:0.156%
UCSRB = 0x98;
SEI();
}
//-----------------------------------------------------------
//串口接收字節(jié)子程序
//-----------------------------------------------------------
unsigned char Getchar(void)
{
while(!(UCSRA& (1<<RXC)));
return UDR;
}
//-----------------------------------------------------------
//串口發(fā)送字節(jié)子程序
//-----------------------------------------------------------
void Putchar(char c)
{
while (!(UCSRA&(1<<UDRE)));
UDR=c;
}
//-----------------------------------------------------------
//串口發(fā)送字符串子程序
//-----------------------------------------------------------
void Putstr(char *s)
{
while (*s)
{
Putchar(*s);
s++;
}
}
//-----------------------------------------------------------
//串口發(fā)送字符串子程序(帶有換行符)
//-----------------------------------------------------------
void Puts(char *s)
{
while (*s)
{
Putchar(*s);
s++;
}
Putchar(0x0a); //回車換行
Putchar(0x0d);
}
void close_rx()
{
UCSRB&=~BIT(RXCIE);
}
void open_rx()
{
UCSRB|=BIT(RXCIE);
}
void PORT_Init()
{
DDRA=0XFF;
}
void delay(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1000;j++);
}
}
void spi_init()
{
DDRB|=0XB0;//SCK、SS和MOSI設(shè)置為輸出,其他為輸入
SPCR = (1 << SPE)|(1 << MSTR)|(1 << SPR0);
// SPCR = (1 << SPE)|(1 << MSTR);//2Mbps
SPSR = 0;
}
uchar send_spi(uchar date)
{
SPSR &= ~(1 << SPIF);
SPDR=date;
while(!(SPSR&(1 << SPIF)));
return SPDR;
}
void interr_0()
{
MCUCR=0X02;
SREG|=0X80;
GICR=0X40;
}
void init_sys()
{
DDRD|=0X02;//串口發(fā)送TX為輸出
CE_0; // 清零CE,處于待機(jī)狀態(tài)
CSN_0; //清零CSN,
}
/**************************************************
Function: SPI_RW_Reg();
Description:
Writes value 'value' to register 'reg'
/**************************************************/
uchar SPI_RW_Reg(BYTE reg, BYTE value)
{
uchar a;
CSN_0;
a=send_spi(reg);
send_spi(value); // ..and write value to it..
CSN_1;
return(a); // return nRF24L01 status byte
}
/**************************************************/
/**************************************************/
BYTE SPI_Read(BYTE reg)
{
BYTE reg_val;
CSN_0;//CSN IS LOW
send_spi(reg); // Select register to read from..
reg_val=send_spi(0); // ..then read registervalue
CSN_1;
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
/**************************************************/
uchar SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
uchar status,byte_ctr;
CSN_0;//CSN IS LOW
status=send_spi(reg); // Select register to write to and read status byte
for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
pBuf[byte_ctr] = send_spi(0); // Perform SPI_RW to read byte from nRF24L01
CSN_1;
return(status);
}
/**************************************************/
/**************************************************
Function: SPI_Write_Buf();
Description:
Writes contents of buffer '*pBuf' to nRF24L01
Typically used to write TX payload, Rx/Tx address
/**************************************************/
uchar SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
uchar status,byte_ctr;
CSN_0;//CSN IS LOW
status=send_spi(reg); // Select register to write to and read status byte寫的過程順便讀讀寄存器狀態(tài)
for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf)
send_spi(*pBuf++);
CSN_1;
return(status);
}
/**************************************************/
void init_24l01()
{
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0(數(shù)據(jù)通道0自動應(yīng)答允許)
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0(接收數(shù)據(jù)通道0允許)
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40(設(shè)置工作通道頻率為40)
SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width RX0通道有效數(shù)據(jù)寬度與TX設(shè)置相同
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR數(shù)據(jù)傳輸率2Mbps發(fā)射功率0dbm低噪聲放大增益
}
//CRC使能、8位CRC校驗、模式為掉電、不屏蔽任何中斷reserved
//接收發(fā)射地址寬度:5字節(jié)SET-UP_AW
//自動重發(fā)延時:250+86US,自動重發(fā)次數(shù):3次SET-UP_RETR
//發(fā)送地址與數(shù)據(jù)通道0接收地址初始值均相同
/**************************************************
Function: RX_Mode();
Description:
This function initializes one nRF24L01 device to
RX Mode, set RX address, writes RX payload width,
select RF channel, datarate & LNA HCURR.
After init, CE is toggled high, which means that
this device is now ready to receive a datapacket.
/**************************************************/
void RX_Mode(void)
{
CE_0;
SPI_Write_Buf((WRITE_REG + RX_ADDR_P0), TX_ADDRESS, TX_ADR_WIDTH); //Use the same address on the RX device as the TX device
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..
CE_1; // Set CE
}
/**************************************************/
/**************************************************
Function: TX_Mode();
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 TX_Mode(void)
{
CE_0;
SPI_Write_Buf((WRITE_REG + TX_ADDR), TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01
SPI_Write_Buf((WRITE_REG + RX_ADDR_P0), TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX_DS enabled..
delay(1);
CE_1;
}
/**************************************************/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -