当前位置:首页 > 单片机 > 单片机
[导读]#include#include#include"lcd12864.h"#defineuintunsignedint#defineucharunsignedchar#defineMISOPINB.6#defineSCKPORTB.7#defineMOSIPORTB.5#defineCSNPORTC.0#defineCEPORTC.1#defineIRQPORTC.2#defineCONFIG0x0

#include

#include

#include"lcd12864.h"

#defineuintunsignedint

#defineucharunsignedchar

#defineMISOPINB.6

#defineSCKPORTB.7

#defineMOSIPORTB.5

#defineCSNPORTC.0

#defineCEPORTC.1

#defineIRQPORTC.2

#defineCONFIG0x00//配置收发状态,CRC校验模式以及收发状态响应方式

#defineEN_AA0x01//自动应答使能

#defineEN_RXADDR0x02//接收地址允许

#defineSETUP_AW0x03//设置地址宽度

#defineSETUP_RETR0x04//建立自动重发

#defineRF_CH0x05//射频通道

#defineRF_SETUP0x06//射频寄存器

#defineSTATUS0x07//状态寄存器

#defineOBSTERVE_TX0x08//发送检测寄存器

#defineCD0x09//载波检测

#defineRX_ADDR_P00x0a//数据通道0接收地址

#defineRX_ADDR_P10x0b//数据通道0接收地址

#defineRX_ADDR_P20x0c//数据通道0接收地址

#defineRX_ADDR_P30x0d//数据通道0接收地址

#defineRX_ADDR_P40x0e//数据通道0接收地址

#defineRX_ADDR_P50x0f//数据通道0接收地址

#defineTX_ADDR0x10//发送地址

#defineRX_PW_P00x11//

#defineRX_PW_P10x12//

#defineRX_PW_P20x13//

#defineRX_PW_P30x14//

#defineRX_PW_P40x15//

#defineRX_PW_P50x16//

#defineFIFO_STATUS0x17//FIFO状态寄存器

#defineREAD_REG0x00//读寄存器指令

#defineWRITE_REG0x20//写寄存器指令

#defineRD_RX_PLOAD0x61//读取接收数据指令

#defineWR_TX_PLOAD0xA0//写待发数据指令

#defineFLUSH_TX0xE1//冲洗发送FIFO指令

#defineFLUSH_RX0xE2//冲洗接收FIFO指令

#defineREUSE_TX_PL0xE3//定义重复装载数据指令

#defineNOP0xFF//保留

#defineTX_ADR_WIDTH5//5uintsTXaddresswidth

#defineRX_ADR_WIDTH5//5uintsRXaddresswidth

#defineTX_PLOAD_WIDTH20//20uintsTXpayload

#defineRX_PLOAD_WIDTH20//20uintsTXpayload

ucharstatus;

ucharstr[30];

ucharTX_ADDRESS[TX_ADR_WIDTH]={0x05,0x04,0x03,0x02,0x01};//本地地址

ucharRX_ADDRESS[RX_ADR_WIDTH]={0x05,0x04,0x03,0x02,0x01};//接收地址

ucharTxBuf[17]="anywhereyouare";

ucharSPI_RW(ucharuuchar)//SPI写时序

{

ucharbit_ctr;

for(bit_ctr=0;bit_ctr<8;bit_ctr++)

{

MOSI=(uuchar&0x80);

uuchar=(uuchar<<1);

SCK=1;

uuchar|=MISO;

SCK=0;

}

return(uuchar);

}

ucharSPI_Read(ucharreg)//SPI读取一个字节

{

ucharreg_val;

CSN=0;

SPI_RW(reg);

delay_ms(1);

reg_val=SPI_RW(0);

delay_ms(1);

CSN=1;

return(reg_val);

}

ucharSPI_RW_Reg(ucharreg,ucharvalue)//写一个自接到寄存器函数

{

ucharstatus;

CSN=0;

status=SPI_RW(reg);

delay_ms(1);

SPI_RW(value);

delay_ms(1);

CSN=1;

return(status);

}

uchar*ucharostr(ucharnum)

{

ucharx00,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]='';

returnstr;

}

ucharSPI_Read_Buf(ucharreg,uchar*pBuf,ucharuchars)//用于读数据,reg:寄存器地址,pBuf:带度数数据地址,uchars数据个数

{

ucharstatus,uchar_ctr;

CSN=0;

status=SPI_RW(reg);

for(uchar_ctr=0;uchar_ctr

{

pBuf[uchar_ctr]=SPI_RW(0);

}

CSN=1;

return(status);

}

uintSPI_Write_Buf(ucharreg,uchar*pBuf,ucharuchars)

{

uintstatus,uchar_ctr;

CSN=0;//SPI使能

status=SPI_RW(reg);

for(uchar_ctr=0;uchar_ctr

SPI_RW(*pBuf++);

CSN=1;//关闭SPI

return(status);//

}

voidinit_NRF24L01(void)

{

delay_ms(1);

CE=0;//射频停止工作

CSN=1;

SCK=0;

IRQ=1;//中断复位

SPI_RW_Reg(WRITE_REG+EN_AA,0x00);//频道0自动,ack应答禁止

SPI_RW_Reg(WRITE_REG+SETUP_RETR,0x00);//禁止自动发送

SPI_RW_Reg(WRITE_REG+EN_RXADDR,0x00);//允许接收地址只有频道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

}

voidnRF24L01_TxPacket(unsignedchar*tx_buf)//发送tx_buf总中数据

{

CE=0;

SPI_Write_Buf(WRITE_REG+TX_ADDR,TX_ADDRESS,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);//装在要发送的数据

SPI_RW_Reg(WRITE_REG+CONFIG,0x0e);//IRQ收发完成中断响应,16位CRC,发送模式

CE=1;//置高CE,激发数据发送

delay_ms(1);

}

voidmain(void)

{ucharfifo_s;

ucharstatus;

uchari;

//Declareyourlocalvariableshere

//Input/OutputPortsinitialization

//PortAinitialization

//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In

//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T

PORTA=0x00;

DDRA=0x00;

//PortBinitialization

//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In

//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T

PORTB=0x00;

DDRB=0xB0;

//PortCinitialization

//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In

//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T

PORTC=0x00;

DDRC=0xff;

//PortDinitialization

//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In

//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T

PORTD=0x00;

DDRD=0x00;

//Timer/Counter0initialization

//Clocksource:SystemClock

//Clockvalue:Timer0Stopped

//Mode:Normaltop=FFh

//OC0output:Disconnected

TCCR0=0x00;

TCNT0=0x00;

OCR0=0x00;

//Timer/Counter1initialization

//Clocksource:SystemClock

//Clockvalue:Timer1Stopped

//Mode:Normaltop=FFFFh

//OC1Aoutput:Discon.

//OC1Boutput:Discon.

//NoiseCanceler:Off

//InputCaptureonFallingEdge

//Timer1OverflowInterrupt:Off

//InputCaptureInterrupt:Off

//CompareAMatchInterrupt:Off

//CompareBMatchInterrupt:Off

TCCR1A=0x00;

TCCR1B=0x00;

TCNT1H=0x00;

TCNT1L=0x00;

ICR1H=0x00;

ICR1L=0x00;

OCR1AH=0x00;

OCR1AL=0x00;

OCR1BH=0x00;

OCR1BL=0x00;

//Timer/Counter2initialization

//Clocksource:SystemClock

//Clockvalue:Timer2Stopped

//Mode:Normaltop=FFh

//OC2output:Disconnected

ASSR=0x00;

TCCR2=0x00;

TCNT2=0x00;

OCR2=0x00;

//ExternalInterrupt(s)initialization

//INT0:Off

//INT1:Off

//INT2:Off

MCUCR=0x00;

MCUCSR=0x00;

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭