LT8920是一超便宜的2.4GHz无线芯片,用来做小东西非常不错,废话不多说,直接上代码

#include "stc8_spi.h"
#include <intrins.h>
#include "lt8920.h"
#include "delay.h"
#include <stdio.h>
#include <string.h>
xdata u8 tx_buff[tx_buff_size];
data u8 rx_buff[rx_buff_size]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
xdata u8 RF_channel = 0;
xdata u8 address[8];
code u8 SyncAddr[8] = {'U','A','V','I','H','O','M','E'};
extern xdata float rcPitch, rcRoll , rcYaw,rcThrottle;
/*
struct LT8920_init_reg
{u8 reg_addr;u8 reg_data_h;u8 reg_data_l;
};
struct LT8920_init_reg const LT8920_reg[] =
{                   //Power-up reset value, Recommended value, Notes{ 0, 0x6f, 0xe0 },//0x6fef, 0x6fe0{ 1, 0x56, 0x81 },//0x5681, 0x5681{ 2, 0x66, 0x17 },//0x6619, 0x6617{ 4, 0x9c, 0xc9 },//0x5447, 0x9cc9{ 5, 0x66, 0x37 },//0xf000, 0x6637{ 7, 0x00, 0x30 },//0x0030, 0x0030,(2402MHz) Use for setting RF frequency, and to start/stop Tx/Rx packets;{ 8, 0x6c, 0x90 },//0x71af, 0x6c90{ 9, 0x48, 0x00 },//0x3000, 0x1840, Sets Tx power level, PA -12.2dbm,1840?{10, 0x7f, 0xfd },//0x7fdd, 0x7fdd, Crystal osc.enabled.{11, 0x00, 0x08 },//0x4008, 0x0008, RSSI enabled{12, 0x00, 0x00 },//0x0000, 0x0000{13, 0x48, 0xbd },//0x4855, 0x48bd{22, 0x00, 0xff },//0xcoff, 0x00ff{23, 0x80, 0x05 },//0x8005, 0x8005, Calibrate VCO before each and every Tx/Rx.{24, 0x00, 0x67 },//0x307b, 0x0067{25, 0x16, 0x59 },//0x1659, 0x1659{26, 0x19, 0xe0 },//0x1883, 0x19e0{27, 0x13, 0x00 },//0x9100, 0x1300, No crystal trim{28, 0x18, 0x00 },//0x1800, 0x1800//29, 0000x0, read-only, Stores p/n, version information//30, 0xf413, read-only, Stores p/n, version information//31, 0x1002, read-only, Stores p/n, version information{32, 0x1a, 0x00 },//0x1806, 0x4800, Packet data type: NRZ, no FEC, BRCKL = 12div.by4=3MHz //1 byte preamble_len,64bit addr,bit trailer{33, 0x3f, 0xc7 },//0x6307, 0x3fc7, Configures packet sequencing{34, 0x20, 0x00 },//0x030b, 0x2000, Configures packet sequencing{35, 0x05, 0x00 },//0x1300, 0x0300, AutoACK max Tx retries = 3//-- ReSend Timer=10?4?{36, 'U', 'A' },//Choose unique sync words{37, 'V', 'I' },//for each over-the-air{38, 'H', 'O' },//network. {39, 'M', 'E' },//Similar to a MAC address{40, 0x44, 0x02 },//0x2107, 0x2102, Configure FIFO flag, sync threshold.{41, 0xBC, 0x00 },//0xb800, 0xb000, CRC is ON, scramble is OFF, AUTO_ACK  Enable, PKT Low Active{42, 0xFD, 0xb0 },//0xfd6b, 0xfdb0, AutoACK off ??RX_ACK?? 176us{43, 0x00, 0x0f },//0x000f, 0x000f, Configure scan_rssi.{44, 0x01, 0x00 },//0x0100, 0x1000, Configure data rate {45, 0x05, 0x52 },//0x0080, 0x0552, -- 62.5 kpbs{50, 0x00, 0x00 }
};
*/void LT8920_WREG(u8 addr, u8 reg_h, u8 reg_l)
{SS = 0;_nop_();//send addressSPI(addr);//send dataSPI(reg_h);SPI(reg_l);SS = 1;
}
//u16 LT8920_RREG(u8 addr)
//{
//    u16 temp;
//    u16 dat;
//    SS = 0;
//    //send address
//    SPI(0x80 + addr);
//    //read data
//    temp = SPI(0xff);
//    dat = temp << 8;
//    temp = SPI(0xff);
//    dat |= temp;
//    SS = 1;
//    return dat;
//}
//void FIFO_Write(u8 dat[], u8 len)
//{
//    u8 n;
//    if((len == 0) || (len > 64)) return;
//    SS = 0;
//    SPI(TXRX_FIFO_REG & WRITE_REG);
//    SPI(len);
//    for(n = 0; n < len; n++)
//    {
//        SPI(dat[n]);
//    }
//    SS = 1;
//}u8 FIFO_Read(u8 dat[], u8 len)
{u8 temp;u8 n;if(len > 64) return 0;SS = 0;SPI(TXRX_FIFO_REG | READ_REG);temp = SPI(0xff);//lenfor(n = 0; n <len; n++){dat[n] = SPI(0xff);}SS = 1;return temp;
}
//set rx or rx and RF channel
#define TX_EN 0x01
#define RX_EN 0x80
//enter tx mode, and set RF channel
//void LT8920_Tx_Frq(u8 ch)
//{
//    if(ch > 0x3f)return;
//    LT8920_WREG(7 , TX_EN, ch);//f = 2402 + ch
//}
//enter tx mode, and set RF channel
void LT8920_Rx_Frq(u8 ch)
{if(ch > 0x3f)return;LT8920_WREG(7 , 0x00, RX_EN + ch);//f = 2402 + ch
}
void LT8920_Address(u8 mac[])
{LT8920_WREG(36, mac[0], mac[1]);LT8920_WREG(37, mac[2], mac[3]);LT8920_WREG(38, mac[4], mac[5]);LT8920_WREG(39, mac[6], mac[7]);
}
//void LT8920_TxRx_OFF()
//{
//    LT8920_WREG(7 , 0x00, 0x00);
//}
void LT8920_FIFO_CLP()//clear FIFO point
{LT8920_WREG(52, 0x80, 0x80);
}
#define RAW_RSSI_ADDR 6
//u16 Get_RSSI_Value()
//{
//    u16 rssi;
//    rssi = LT8920_RREG(RAW_RSSI_ADDR);
//    return (rssi >> 11);
//}
bit CRC_ERROR = 0;
bit FEC23_ERROR = 0;
u8 FRAMER_ST = 0;
bit SYNCWORD_RECV = 0;
bit PKT_FLAG = 0;
bit FIFO_FLAG = 0;
//void Get_RF_Status()
//{
//    u16 temp;
//    temp = LT8920_RREG(48);
//    CRC_ERROR = temp & 0x8000;
//    FEC23_ERROR = temp & 0x4000;
//    FRAMER_ST = (temp >> 8) && 0xc0;
//    SYNCWORD_RECV = temp & 0x0080;
//    PKT_FLAG = temp & 0x0040;
//    FIFO_FLAG = temp & 0x0020;
//}
//u16 Get_RF_ID_Ver() //address:29,30,31
//{
//    u16 idv;
//    idv = LT8920_RREG(30);
//    return idv;
//}
void LT8920_Init()
{SS = 1;SCLK = 0;RST_N = 0; //关闭芯片,电流<1uA,数字部分的值会失去delay_ms(2);RST_N = 1; //开启芯片,寄存器回复复位值delay_ms(6);LT8920_WREG(0, 0x6f, 0xe0);LT8920_WREG(1, 0x56, 0x81);LT8920_WREG(2, 0x66, 0x17);LT8920_WREG(4, 0x9c, 0xc9);LT8920_WREG(5, 0x66, 0x37);
//    LT8920_WREG(7, 0x00, 0x30);//use for setting RF frequency, and to start/stop Tx/Rx packetsLT8920_WREG(8, 0x6c, 0x90);LT8920_WREG(9, 0x18, 0x40);//set Tx power levelLT8920_WREG(10, 0x7f, 0xfd);//Crystal osc. enabledLT8920_WREG(11, 0x00, 0x08);//RSSI enabled.LT8920_WREG(12, 0x00, 0x00);LT8920_WREG(13, 0x48, 0xbd);LT8920_WREG(22, 0x00, 0xff);LT8920_WREG(23, 0x80, 0x05);//Calibrate VCO before each and every Tx/RxLT8920_WREG(24, 0x00, 0x67);LT8920_WREG(25, 0x16, 0x59);LT8920_WREG(26, 0x19, 0xe0);LT8920_WREG(27, 0x13, 0x00);//no crystal trimLT8920_WREG(28, 0x18, 0x00);LT8920_WREG(32, 0x48, 0x00);//3byte preabmle len,32bits MAC, 4bits trailer, Packet data type:NRZ, no FEC, BRCLK=12 div.by4=3MHzLT8920_WREG(33, 0x3f, 0xc7);//Configure packet sequencingLT8920_WREG(34, 0x20, 0x00);//Configure packet sequencingLT8920_WREG(35, 0x03, 0x00);//AutoAck max Tx retries = 3LT8920_WREG(36, address[0], address[1]);//MAC addressLT8920_WREG(37, address[2], address[3]);
//    LT8920_WREG(38, address[4], address[5]);
//    LT8920_WREG(39, address[6], address[7]);LT8920_WREG(40, 0x21, 0x02);//Configure FIFO flag, sync thresholdLT8920_WREG(41, 0xb0, 0x00);//CRC on. SCRAMBLE off. 1st byte is packet lengthLT8920_WREG(42, 0xfd, 0xb0);//AutoACK off.LT8920_WREG(43, 0x00, 0x0f);//Configure sacn_rssi LT8920_WREG(44, 0x04, 0x00);//Configure data rate: 1MbpsLT8920_WREG(45, 0x05, 0x52);LT8920_WREG(52, 0x80, 0x80);//清空FIFO读写指针,不清空数据memset(rx_data, 0, rx_buff_size);
}
xdata int rx_data[9];
bit SW1 = 1;
bit SW2 = 1;
bit SW3 = 1;
bit SW4 = 1;
bit SW5 = 1;
bit SW6 = 1;
bit SW7 = 1;
bit SW8 = 1;
bit key1 = 1;
bit key2 = 1;
bit key3 = 1;
bit key4 = 1;
bit key5 = 1;
bit key6 = 1;
bit ch5 = 0;
bit ch6 = 0;//code u8 signal_error_cnt = 0;
void rx_data_handle()
{if(rx_buff[0] == 0xdc)//帧头{u8 check = 0;u8 check2 = 0;rx_buff[0] = 0;check = (rx_buff[1]+rx_buff[2]+rx_buff[3]+rx_buff[4]+rx_buff[5]+rx_buff[6]+rx_buff[7]+rx_buff[8]+rx_buff[9]);check2 = (rx_buff[10]+rx_buff[11]+rx_buff[12]+rx_buff[13]+rx_buff[14]+rx_buff[17]+rx_buff[18]);if((check == rx_buff[16]) && (check2 == rx_buff[15]))//各通道和校验,每一个字节都要校验,不校验误码率高{rx_cnt ++;rx_data[0] = rx_buff[1] * 256 + rx_buff[2];  //rc adc0 batteryrx_data[1] = rx_buff[3] * 256 + rx_buff[4];  //rc adc1 right rocker  ↑↓rx_data[2] = rx_buff[5] * 256 + rx_buff[6];  //rc adc2 right rocker  ←→rx_data[3] = rx_buff[7] * 256 + rx_buff[8];  //rc adc3 left rocker  ↑↓ rx_data[4] = rx_buff[9] * 256 + rx_buff[10]; //rc adc4 left rocker  ←→rx_data[5] = rx_buff[11] * 256 + rx_buff[12];//rc adc5rx_data[6] = rx_buff[13] * 256 + rx_buff[14];//rc adc6rx_data[7] = rx_buff[15] * 256 + rx_buff[16];//rc adc7rx_data[8] = rx_buff[17] * 256 + rx_buff[18];//key & swSW1 = rx_buff[17] & 0x80;SW2 = rx_buff[17] & 0x40;SW3 = rx_buff[17] & 0x20;SW4 = rx_buff[17] & 0x10;SW5 = rx_buff[17] & 0x08;SW6 = rx_buff[17] & 0x04;SW7 = rx_buff[17] & 0x02;SW8 = rx_buff[17] & 0x01;key1 = rx_buff[18] & 0x80;key2 = rx_buff[18] & 0x40;key3 = rx_buff[18] & 0x20;key4 = rx_buff[18] & 0x10;key5 = rx_buff[18] & 0x08;key6 = rx_buff[18] & 0x04;ch5  = rx_buff[18] & 0x02;ch6  = rx_buff[18] & 0x01;rcPitch    = (float)(rx_data[1] - 2048)/104.8;//限制最大倾角为+-2048/102.4 = 10度rcRoll     = (float)(rx_data[2] - 2048)/104.8;rcThrottle = (float)rx_data[3] / 4.096;//1000rcYaw      = (float)(rx_data[4] - 2048)/104.8;if((rx_data[1] > 2000) && (rx_data[1] < 2100)) rcPitch = 0;//限制摇杆启动范围if((rx_data[2] > 2000) && (rx_data[2] < 2100)) rcRoll = 0;
//            if((rx_data[3] > 2000) && (rx_data[3] < 2100)) rcThrottle = 0;if((rx_data[4] > 2000) && (rx_data[4] < 2100)) rcYaw = 0;memset(rx_buff, 0, rx_buff_size);}}
}
void rx_data_out()
{
//    uart1_sendByte(0x03);
//    uart1_sendByte(0xfc);
//    uart1_sendByte(rx_buff[1]);
//    uart1_sendByte(rx_buff[0]);
//    uart1_sendByte(rx_buff[3]);
//    uart1_sendByte(rx_buff[2]);
//    uart1_sendByte(rx_buff[5]);
//    uart1_sendByte(rx_buff[4]);
//    uart1_sendByte(rx_buff[7]);
//    uart1_sendByte(rx_buff[6]);
//    uart1_sendByte(rx_buff[9]);
//    uart1_sendByte(rx_buff[8]);
//    uart1_sendByte(rx_buff[11]);
//    uart1_sendByte(rx_buff[10]);
//    uart1_sendByte(rx_buff[13]);
//    uart1_sendByte(rx_buff[12]);
//    uart1_sendByte(rx_buff[15]);
//    uart1_sendByte(rx_buff[14]);
//    uart1_sendByte(rx_buff[17]);
//    uart1_sendByte(rx_buff[17]);
//    uart1_sendByte(rx_buff[18]);
//    uart1_sendByte(0xfc);
//    uart1_sendByte(0x03);printf("Rx:%d R0:%u R1:%u R2:%u R3:%u R4:%u R5:%u R6:%u R7:%u R8:%x\n",(int)signal_cnt,rx_data[0],rx_data[1],rx_data[2],rx_data[3],rx_data[4],rx_data[5],rx_data[6],rx_data[7],rx_data[8]);
}    

LT8920无线通讯程序相关推荐

  1. NB-Iot烟感07:NB-IOT 无线通讯程序开发

    一.搭建程序的开发环境: NB-Iot 无线通讯板我们选择的主控芯片是 我们选择的是华大半导体的HC32F005C6PA, 这个芯片是基于M0的32位低功耗单片机. 开发环境我们选择的是Keil MD ...

  2. 昆仑通态复制的程序可以用吗_三菱FX2n plc(编程口)与昆仑通态(MCGS)无线通讯案例...

    一.系统组成 昆仑通态使用三菱 PLC FX2N 编程口通过专用协议对 PLC 站点相应寄存器进行读写操作, 从而实现对站点工作状态的控制.这种情况下三菱PLC专用无线通讯终端--DTD435MC,可 ...

  3. c#rs232与三菱通讯_MCGS 与 FX3U PLC 之间的无线通讯实例

    本方案主要详述了如何实现 MCGS 触摸屏与 FX3UPLC 之间 Modbus 协议的无线通讯.方案中所用到的无线通讯终端是 DTD435MC--等日系 PLC 专用的无线通讯终端.1. 方案概述1 ...

  4. PLC若想实现以太网口无线通讯,需要具备哪些条件?

    PLC若想实现以太网口无线通讯,首先我们需要确定好PLC型号,然后选择一个合适的通讯设备.比如如果是西门子S7-200PLC,就需要使用欧美PLC无线通讯终端,并且这个设备需要支持以太网口通讯. 这种 ...

  5. PLC无线通讯方案,支持西门子,三菱,欧姆龙,台达等各品牌PLC

    在实际项目中,经常有多台PLC之间需要通讯,距离可能从几十米到几千米,也有的客户需要通过互联网来远距离监控PLC的状态. 在这里介绍一种PLC无线通讯的实现方法,采用巨控GRM200系列PLC专用无线 ...

  6. modbus tcp主站和从站_Modbus-RTU 一主多从PLC无线通讯经典案例

    一.无线方案简介 该方案可适用于 3 台以上西门子 PLC,S7-200 或 S7-200Smart 之间实现一主多从Modbus 通讯协议的无线通讯. 适用 PLC 型号:S7-200 和 S7-2 ...

  7. 三菱modbusRTU通讯实例_三菱PLC的无线通讯实例

     西门子博途的全套视频教程下载!  西门子S7-1500内部培训PPT下载! 一.案例简述 [PLC 型号]:三菱FX3U (配FX3U-485ADP-MB 模块) [无线模块]:DTD435M [实 ...

  8. 三菱PLC之间的N:N无线通讯的常见问题汇总【1】

    三菱PLC之间的N:N无线通讯的常见问题汇总如下:   [需求描述①:佛山陈工] 我有三个FX3U,一个主站,两个从站.要实现无线N:N通讯,应该怎么下单,也就是说要买几个发射器,几个接受器,可以告诉 ...

  9. NFC·(近距离无线通讯技术)

    这个技术由非接触式射频识别(RFID)演变而来,由飞利浦半导体(现恩智浦半体)诺基亚和索尼共同研制开发,其基础是RFID及互连技术.近场通信 (Near Field Communication,NFC ...

最新文章

  1. C++_动态绑定,再谈const,new,delete
  2. 修改单个viewcontroller的状态栏字体颜色
  3. php 主页子标题修改,关于有部分用户默认PC主页大标题标签修改无效的答疑.
  4. 列出本机安装的所有硬件设备
  5. 【文末赠书】漫画:什么是 “跳表” ?
  6. 《Dotnet9》系列-Google ProtoBuf在C#中的简单应用
  7. 前馈神经网络——深度学习之神经网络核心原理与算法
  8. centos删除gnome_自定义你的 GNOME 桌面主题 | Linux 中国
  9. CentOS 7 上搭建 Nacos Cluster 集群
  10. 新手该怎么独立做跨境电商?做之前需要准备什么?
  11. web开发敏捷之道pdf
  12. python的返回函数的作用_函数的返回值和作用域
  13. GD32与STM32区别
  14. 时间轮定时器-Timewheel
  15. 2021-01-19 :在pycharm中创建包含anaconda中各种库的环境
  16. [转载]视频格式介绍
  17. 快速实现安卓输入法!
  18. 台式计算机怎么开声音,台式机如何使用耳机说话
  19. Tomcat开启APR模式并设置Tomcat为开机自启动服务
  20. 74HC595 芯片详细介绍

热门文章

  1. jsp文件的请求是如何被服务器的JSP容器转换成静态网页的
  2. SAP CRM WebClient UI上note下拉列表里类型的过滤逻辑
  3. 微软的ppt现在可以直接导出成可以播放的mp4了,非常方便
  4. SAP UI5 walkthrough第一第二部分解析:data-sap-ui-libs=“sap.ui.commons,sap.ui.table“
  5. how does local sandbox parse navigation target from hash
  6. WebClient UI忽略所有增强的开关
  7. Percent encode in consulting package
  8. 使用命令行工具mc上传模型训练数据到SAP云平台Leonardo机器学习服务的AWS存储
  9. SAP Spartacus的PWA支持
  10. 如何提高后台服务应用问题的排查效率?日志 VS 远程调试