首页资源分类嵌入式开发单片机 > 利尔达无线模块

利尔达无线模块

已有 460162个资源

下载专区


TI最新应用解决方案

工业电子 汽车电子 个人消费电子

文档信息举报收藏

标    签: 无线

分    享:

文档简介

无线收发已经调试完成,可以直接使用,利尔达无线模块

文档预览

/****************************************************** * @file hal_Lsd187xx.c * * @brief for 470MHZ 2Kbps 20dBm * * @date 20140312 *******************************************************/ /*===INCLUDE====================================================================*/ #include "include.h" #include "hal_Lsd187xxreg.h" #include "hal_Lsd187xx.h" #include "LED.h" /*===VARIABLE===================================================================*/ uint16_t temp_Crystal_Reg; extern uint8_t master_flag; const uint8_t ID_Tab[4]={0x54, 0x75, 0xC5, 0x2A}; //const uint8_t ID_Tab[4]={0xA4, 0xA5, 0xA5, 0xAA}; //ID code Byte 0建议为Axh或者5xh //const uint8_t ID_Tab[4]={0xA1, 0x45, 0x89, 0x77}; /*===SPI WRITE 1 BYTE===========================================================*/ void ByteSend(uint8_t src) { uint8_t i; for(i = 0; i < 8; i++) { if(src & 0x80) TRXEM_SDIO_OH; else TRXEM_SDIO_OL; TRXEM_DELAY; //空指令NOP TRXEM_SCK_H; TRXEM_DELAY; TRXEM_SCK_L; src = src << 1; } } /*===SPI READ 1 BYTE============================================================*/ uint8_t ByteRead(void) { uint8_t i,tmp; TRXEM_SDIO_OH;// SDIO pull high TRXEM_SDIO_MI;// change SDIO input for(i = 0; i < 8; i++)// Read one byte data { if(TRXEM_SDIO_IN) tmp = (tmp << 1) | 0x01; else tmp = tmp << 1; TRXEM_DELAY; TRXEM_SCK_H; TRXEM_DELAY; TRXEM_SCK_L; } return tmp; } /*===RF SPI初始化=================================================================*/ void trxRfSpiInterfaceInit(void) { TRXEM_SCS_PORT_DIR |= TRXEM_SCS_PIN; TRXEM_SCK_PORT_DIR |= TRXEM_SCK_PIN; TRXEM_SDIO_PORT_DIR |= TRXEM_SDIO_PIN; TRXEM_INT_PORT_DIR &=~ TRXEM_INT_PIN; TRXEM_INT_PORT_IFG&=~TRXEM_INT_PIN; TRXEM_INT_PORT_IES |=BIT1; TRXEM_INT_PORT_IE|=TRXEM_INT_PIN; TRXEM_SCS_H; TRXEM_SCK_L; // CPOL=0,CPAH=0 TRXEM_SDIO_MO; TRXEM_SDIO_OH; } /*===LSD187-A7108初始化==========================================================*/ void trxRfInit(void) { trxRfSpiInterfaceInit(); //spi init Lsd187xx_Reset();// reset Lsd187xx chip StrobeCmd(CMD_PLL);// Enable crystal oscillator delay_ms(1);// for crystal stabilized Lsd187xx_Config();// config Lsd187xx chip Lsd187xx_WriteID(); // write ID code Lsd187xx_Cal();// calibration IF,vco if (master_flag) { Lsd187xx_WriteReg(RX1_REG, 0x1897); //bit ULS=1(low side band) } else { Lsd187xx_WriteReg(RX1_REG, 0x1895); //bit ULS=0(high side band) } //参照数据手册P23 08H StrobeCmd(CMD_STANDBY); //初始化进入待机模式,用函数StrobeCmd()进行各状态切换 } /*===初始化RF寄存器=================================================================*/ void Lsd187xx_Config(void) { uint8_t i; for (i=0; i<16; i++) Lsd187xx_WriteReg(i, Lsd187xxConfig[i]); // Write initial value into Lsd187xx register. temp_Crystal_Reg = Lsd187xxConfig[CRYSTAL_REG];// Save the setting of CRYSTAL register to global variable temp_Crystal_Reg for future use for (i=0; i<13; i++) Lsd187xx_WritePage(i, Lsd187xxConfig_Page[i]);// Write initial value into Lsd187xx page register. } /*===写RF寄存器======================================================================*/ void Lsd187xx_WriteReg(uint8_t address, uint16_t dataWord) { uint8_t i; TRXEM_SCS_L;// Set SCS=0 to Enable SPI interface TRXEM_SDIO_MO;// change SDIO output address |= CMD_CTRLW;// Enable write operation of control registers for(i = 0; i < 8; i++)// Assign control register’s address by input variable addr { if(address & 0x80) TRXEM_SDIO_OH;//bit=1 else TRXEM_SDIO_OL;//bit=0 TRXEM_DELAY; TRXEM_SCK_H; TRXEM_DELAY; TRXEM_SCK_L; address = address << 1; } TRXEM_DELAY; for(i = 0; i < 16; i++) // send data word { if(dataWord & 0x8000) TRXEM_SDIO_OH; else TRXEM_SDIO_OL; TRXEM_SCK_H; TRXEM_DELAY; TRXEM_SCK_L; dataWord = dataWord << 1; } TRXEM_SCS_H;// Set SCS=1 to disable SPI interface } /*===读RF控制寄存器===================================================================*/ uint16_t Lsd187xx_ReadReg(uint8_t address) { uint8_t i; uint16_t tmp; TRXEM_SCS_L;// Set SCS=0 to enable SPI write function. TRXEM_SDIO_MO;// change SDIO output address |= CMD_CTRLR;// Enable read operation of control registers. for(i = 0; i < 8; i++)// Write address { if(address & 0x80) TRXEM_SDIO_OH; else TRXEM_SDIO_OL; TRXEM_SCK_H; TRXEM_DELAY; TRXEM_SCK_L; address = address << 1; } TRXEM_SDIO_OH; TRXEM_SDIO_MI; TRXEM_DELAY; for(i = 0; i < 16; i++) // read data word { if(TRXEM_SDIO_IN) tmp = (tmp << 1) | 0x01; else tmp = tmp << 1; TRXEM_DELAY; TRXEM_SCK_H; TRXEM_DELAY; TRXEM_SCK_L; } TRXEM_SCS_H;// Set SCS=1 to disable SPI interface. return tmp;// Return 16 bit value } /*===写RF PAGE寄存器===============================================================*/ void Lsd187xx_WritePage(uint8_t address, uint16_t dataWord) { uint16_t tmp; tmp = address; tmp = ((tmp << 12) | temp_Crystal_Reg); Lsd187xx_WriteReg(CRYSTAL_REG, tmp);// Set the writing-intended page register address to register CRYSTAL bit[15:12] Lsd187xx_WriteReg(TX1_REG, dataWord);// Write data to dedicated page register. (By address CRYSTAL bit[15:12]) Lsd187xx_WriteReg(CRYSTAL_REG, temp_Crystal_Reg);// Restore the page register address to 0. (Set to default) } /*===读RF PAGE寄存器===============================================================*/ uint16_t Lsd187xx_ReadPage(uint8_t address) { uint16_t tmp; tmp = address; tmp = ((tmp << 12) | temp_Crystal_Reg); Lsd187xx_WriteReg(CRYSTAL_REG, tmp);// Set the writing-intended page register address to register CRYSTAL bit[15:12] tmp = Lsd187xx_ReadReg(TX1_REG);// Read data from dedicated page register. (By address CRYSTAL bit[15:12]) Lsd187xx_WriteReg(CRYSTAL_REG, temp_Crystal_Reg);// Restore the page register address to 0. (Set to default) return tmp;// Return 16 bit value } /*===Reset_RF======================================================================*/ void Lsd187xx_Reset(void) { TRXEM_SCS_L; ByteSend(CMD_RFRST); TRXEM_SCS_H; } /*===ResetTXFIFO===================================================================*/ void Lsd187xx_ResetTXFIFO(void) { TRXEM_SCS_L; ByteSend(CMD_TFR); TRXEM_SCS_H; } /*===ResetRXFIFO===================================================================*/ void Lsd187xx_ResetRXFIFO(void) { TRXEM_SCS_L; ByteSend(CMD_RFR); TRXEM_SCS_H; } /*===WRITE ID======================================================================*/ void Lsd187xx_WriteID(void) { uint8_t i; uint8_t d1,d2,d3,d4; TRXEM_SCS_L; ByteSend(CMD_IDW); for (i=0; i < 4; i++) ByteSend(ID_Tab[i]); TRXEM_SCS_H; TRXEM_SCS_L; // for check ID ERROR ByteSend(CMD_IDR);//read mode d1 = ByteRead(); d2 = ByteRead(); d3 = ByteRead(); d4 = ByteRead(); TRXEM_SCS_H; if(d1 != ID_Tab[0]) { WDTCTL = WDTHOLD; } else { if(d2 != ID_Tab[1]) { WDTCTL = WDTHOLD; } else { if(d3 != ID_Tab[2]) { WDTCTL = WDTHOLD; } else { if(d4 != ID_Tab[3]) { WDTCTL = WDTHOLD; } else { } } } } } /*===Lsd187xx_Cal VCO Band 校正 ===================================================*/ void Lsd187xx_Cal(void) { uint16_t tmp; uint8_t fbcf,vbcf; Lsd187xx_WriteReg(MODE_REG, 0x18F2); do { tmp = Lsd187xx_ReadReg(MODE_REG); delay_ms(10); tmp &= 0x0002; } while (tmp); tmp = Lsd187xx_ReadReg(CALIBRATION_REG); //fb = tmp & 0x0F; fbcf = (tmp >> 4) & 0x01; if (fbcf) { WDTCTL = WDTHOLD; } Lsd187xx_WriteReg(PLL1_REG, MasterFreq_Tab[0]); Lsd187xx_WriteReg(PLL2_REG, MasterFreq_Tab[1]);//SET 470MHZ Lsd187xx_WriteReg(MODE_REG, 0x00C4); //vco calibration 470MHz do { tmp = Lsd187xx_ReadReg(MODE_REG); delay_ms(10); tmp &= 0x0004; } while (tmp); tmp = Lsd187xx_ReadReg(CALIBRATION_REG); //vb = (tmp >>5) & 0x07; // unused vbcf = (tmp >>8) & 0x01; if (vbcf) { WDTCTL = WDTHOLD; } } /*===LSD187 FREQSET================================================================*/ //主从机频率设置不匹配会严重影响传输距离 void FreqSet(void) { if (master_flag) { Lsd187xx_WriteReg(PLL1_REG, MasterFreq_Tab[0]);//setting PLL1 Lsd187xx_WriteReg(PLL2_REG, MasterFreq_Tab[1]);//setting PLL2 } else { Lsd187xx_WriteReg(PLL1_REG, SlaveFreq_Tab[0]);//setting PLL1 Lsd187xx_WriteReg(PLL2_REG, SlaveFreq_Tab[1]);//setting PLL2 } } /*===Strobe Command写入命令========================================================*/ /*切换不同工作状态 CMD_SLEEP CMD_IDLE CMD_STANDBY CMD_PLL CMD_RX CMD_TX CMD_DEEP_SLEEP */ void StrobeCmd(uint8_t cmd) { uint8_t i; TRXEM_SCS_L; TRXEM_SDIO_MO; for(i = 0; i < 8; i++)// Write one byte data of strobe command procedure. { if(cmd & 0x80) TRXEM_SDIO_OH; else TRXEM_SDIO_OL; TRXEM_DELAY; TRXEM_SCK_H; TRXEM_DELAY; TRXEM_SCK_L; cmd = cmd << 1; } TRXEM_SCS_H; } /*===WriteFIFO=====================================================================*/ void Lsd187xx_WriteTXFIFO(uint8_t *packet) { uint8_t i; Lsd187xx_ResetTXFIFO();// Call subroutine of Lsd187xx_ResetTXFIFO to reset write pointer of TX FIFO. TRXEM_SCS_L;// Set SCS=0 to enable SPI interface. // Call subroutine of ByteSend for one byte write operation. ByteSend(CMD_DATAW);// here send write fifo cmmmand for(i=0; i <3; i++)// Write PN9_Tab into TX FIFO, total 64 bytes. ByteSend(packet[i]); TRXEM_SCS_H;// Set SCS=1 to disable SPI interface. } /*===SendPacket====================================================================*/ void SendPacket(uint8_t* packet) { Lsd187xx_WriteTXFIFO(packet); //write data to tx fifo StrobeCmd(CMD_TX); LED1_ON; while(!GIO2); while(GIO2);//wait transmit completed LED1_OFF; } /*===RxPacket======================================================================*/ void RxPacket(uint8_t *packet) { uint8_t i; uint8_t recv; Lsd187xx_ResetRXFIFO(); TRXEM_SCS_L; ByteSend(CMD_DATAR);//send read fifo command for(i=0; i < 3; i++)//3为数据长度,可根据发送数据长度更改 { recv = ByteRead(); packet[i]=recv; } TRXEM_SCS_H; } /*===SET TO SLEEP===================================================================*/ void SET_TO_SLEEP(void) { StrobeCmd(CMD_SLEEP); P1DIR = 0xFF; P1SEL = 0x00; P1OUT = 0x00; P2DIR = 0xFF; P2SEL = 0x00; P2OUT = 0x00; P3DIR = 0xFF; P3SEL = 0x00; P3OUT = 0x00; TRXEM_SCS_PORT_OUT|=TRXEM_SCS_PIN;//1 TRXEM_SCK_PORT_OUT&=(~TRXEM_SCK_PIN); //1 TRXEM_SDIO_PORT_OUT|=TRXEM_SDIO_PIN;//1 TRXEM_INT_PORT_OUT &=(~TRXEM_INT_PIN);//1 }

Top_arrow
回到顶部
EEWORLD下载中心所有资源均来自网友分享,如有侵权,请发送举报邮件到客服邮箱bbs_service@eeworld.com.cn 或通过站内短信息或QQ:273568022联系管理员 高进,我们会尽快处理。