您的位置 首页 动态

根据MSP430单片机驱动NRF24L01无线模块发送C言语程序

MCU:MSP430F16X系列,RF无线模块:NRF24L01,液晶显示:ST7920串行控制128*64液晶,发送数据是把4X4矩阵键盘对应的键值发送出去。IRQ是采

MCU:MSP430F16X系列,RF无线模块:NRF24L01,液晶显示:ST7920串行操控128*64液晶,发送数据是把4X4矩阵键盘对应的键值发送出去。IRQ是选用中止查询法,详细的能够参阅下面代码:

//p4口为液晶屏//p1口中止//nRF24L01无线通信//p5.0  csn    //p5.1  mosi//p5.3  sck//p5.2  miso//p1.2  irq//p5.7  ce//ST7920串行操控128*64液晶//rst  p4.3   复位//rs   p4.0   片选//rw   p4.1    数据//E    p4.2    时钟#include #include #define uchar unsigned char#define uint unsigned int#define ulong unsigned long#define LCD_CLK_1   (P4OUT |=  BIT2)#define LCD_CLK_0   (P4OUT &=~ BIT2)#define LCD_D_1   (P4OUT |=  BIT1)#define LCD_D_0   (P4OUT &=~ BIT1)#define LCD_RST_1  (P4OUT |=  BIT3)#define LCD_RST_0  (P4OUT &=~ BIT3)#define LCD_CS_1 (P4OUT |= BIT0)#define LCD_CS_0 (P4OUT &= ~BIT0)#define TMP121_CS_1 (P5OUT |= BIT7)#define TMP121_CS_0 (P5OUT &= ~BIT7)#define TMP121_CLK_1 (P5OUT |= BIT3)#define TMP121_CLK_0 (P5OUT &= ~BIT3)#define send_1 (P2OUT |= BIT0)#define send_0 (P2OUT &= ~BIT0)#define CPU_F ((double)1000000) // MCLK=1MHZ主频#define Delayus_us(x) __Delayus_cycles((long)(CPU_F*(double)x/1000000.0))#define Delayus_ms(x) __Delayus_cycles((long)(CPU_F*(double)x/1000.0))#define Delayus_s(x) __Delayus_cycles((long)(CPU_F*(double)x/1.0))uint  alldata[32],keyflg;uchar char11[16]={0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f};uchar busy=A,hang,lie,flash_flag,bostart;uchar key0=0,key1=T,key2,boflg,flg,key2flg=0,tab1=1,newsflag1,newsflag2;uchar key11,key12;uint LCDL;uchar tu[32][32]={0x00};uint Rdata[8]={0},botu[98]={0x0000};uchar Tx_Payload[32]={4,1,9};uchar Tx_Address[5]={100,101,102,103,104};void Delayus(ulong ilinshi){ ulong ii;for(ii=0;ii<=ilinshi;ii++){key0=key0;}}#define nRF24L01_CE_1   (P5OUT|= BIT7)#define nRF24L01_CE_0   (P5OUT&=~BIT7)#define nRF24L01_CSN_1  (P5OUT|= BIT0)#define nRF24L01_CSN_0  (P5OUT&=~BIT0)#define nRF24L01_SCK_1  (P5OUT|= BIT3)#define nRF24L01_SCK_0  (P5OUT&=~BIT3)#define nRF24L01_MOSI_1 (P5OUT|= BIT1)#define nRF24L01_MOSI_0 (P5OUT&=~BIT1)//SPI Commands#define nRF24L01_R_REGISTER    0x00#define nRF24L01_W_REGISTER    0x20#define nRF24L01_R_RX_PAYLOAD  0x61#define nRF24L01_W_TX_PAYLOAD  0xA0#define nRF24L01_FLUSH_TX      0xE1#define nRF24L01_FLUSH_RX      0xE2#define nRF24L01_REUSE_TX_PL   0xE3#define nRF24L01_NOP           0xFF//NRF24L01寄存器地址#define nRF24L01_CONFIG        0x00#define nRF24L01_EN_AA         0x01#define nRF24L01_EN_RXADDR     0x02#define nRF24L01_SETUP_AW      0x03#define nRF24L01_SETUP_RETR    0x04#define nRF24L01_RF_CH         0x05#define nRF24L01_RF_SETUP      0x06#define nRF24L01_STATUS        0x07#define nRF24L01_OBSERVE_TX    0x08#define nRF24L01_CD            0x09#define nRF24L01_RX_ADDR_P0    0x0A#define nRF24L01_RX_ADDR_P1    0x0B#define nRF24L01_RX_ADDR_P2    0x0C#define nRF24L01_RX_ADDR_P3    0x0D#define nRF24L01_RX_ADDR_P4    0x0E#define nRF24L01_RX_ADDR_P5    0x0F#define nRF24L01_TX_ADDR       0x10#define nRF24L01_RX_PW_P0      0x11#define nRF24L01_RX_PW_P1      0x12#define nRF24L01_RX_PW_P2      0x13#define nRF24L01_RX_PW_P3      0x14#define nRF24L01_RX_PW_P4      0x15#define nRF24L01_RX_PW_P5      0x16#define nRF24L01_FIFO_STATUS   0x17void IO_Init(void)   //初始化端口{P2SEL = 0;P3SEL = 0;P4SEL = 0;P4DIR|=0x0f;              //P4.0,1,2,3=OUTP4OUT|=0x0f;P2DIR=0xfd;              //P2.1=in    qita=OUTP3DIR&=0xf7;              //p3.3=in}void Init_Port1(void)   //P1.0、P1.1为中止,下降沿触发{  P1SEL=0;                         //设置为一般I/O口功用P1DIR=0;                         //设置为输入方向P1IE=0;P1IES=0;                          P1IE|=BIT2;                           //翻开中止答应P1IES|=BIT2;                          //下降沿触发P1IFG=0;                              //P1IES的切换可能使P1IFG置位,需铲除}/*******************************************************************************///无线通信void Init_nRF24L01_IO(){nRF24L01_CE_0;nRF24L01_CSN_1;nRF24L01_SCK_0;nRF24L01_MOSI_0;}//NRF24L01的SPI写时序void nRF24L01SpiWrite(unsigned char byte){unsigned char i;nRF24L01_SCK_0;Delayus(1);for (i=0;i<8;i++) // Setup byte circulation bits{if ((byte&BIT7)==BIT7) // Put DATA_BUF.7 on data linenRF24L01_MOSI_1; //MOSI=1;elsenRF24L01_MOSI_0; //MOSI=0;nRF24L01_SCK_1; // Set clock line highDelayus(2);byte=byte<<1;nRF24L01_SCK_0; // Set clock line lowDelayus(2);}Delayus(1);}//NRF24L01的SPI读时序unsigned char nRF24L01SpiRead(void){unsigned char i;unsigned char temp=0;nRF24L01_SCK_0;Delayus(2);for (i=0;i<8;i++) // Setup byte circulation bits{nRF24L01_SCK_1; // Set clock line highDelayus(2);temp=temp<<1; // Right shift DATA_BUFif ((P5IN&BIT2==BIT2)){temp|=1;} // Read datanRF24L01_SCK_0; // Set clock line lowDelayus(2);}Delayus(2);return temp;}//void nRF24L01_Flush_TX_FIFO(void)//Clear TX FIFO{nRF24L01_CSN_1;nRF24L01_CSN_0;nRF24L01SpiWrite(nRF24L01_FLUSH_TX);nRF24L01_CSN_1;}//void nRF24L01_Flush_RX_FIFO(void)//Clear RX FIFO{nRF24L01_CSN_1;nRF24L01_CSN_0;nRF24L01SpiWrite(nRF24L01_FLUSH_RX);nRF24L01_CSN_1;}//void nRF24L01SpiWriteReg(unsigned char SpiCommand,unsigned char Content){nRF24L01_CSN_1;nRF24L01_CSN_0;nRF24L01SpiWrite(SpiCommand);nRF24L01SpiWrite(Content);nRF24L01_CSN_1;}//void nRF24L01_Set_Rx_Address(unsigned char RX_Address_Pipex,unsigned char *Address,unsigned char Length)//Local//Address{unsigned char i=0;nRF24L01_CSN_1;nRF24L01_CSN_0;nRF24L01SpiWrite(RX_Address_Pipex);for(i=0;i

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/dongtai/317961.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部