完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
本文记录在SX1278 在STM32F103 驱动,实现收发数据功能。(SPI 驱动参考 https://www.cnblogs.com/T0213-ZH/articles/10573067.html)
编译环境:MDK5.25 STM32标准库:V3.5.0 SX1278驱动:sx12xxDrivers-V2.1.0 (https://www.semtech.com/uploads/documents/sx12xxDrivers-V2.1.0.zip) 相关文档 https://www.semtech.com/products/wireless-rf/lora-transceivers/sx1278 1、MDK工程目录(创建工程方式略) 收发数据由宏“ SX1276_RX_DEF" 区别,定义了为接收数据,未定义为发送数据 2、修改默认sx1276-LoRa.c 里面默认的频率与带宽 编译时出现bool 没定义,增加 bool类型头文件,sx1276.h文件里 main.c 函数实现如下, 发送功能:每间隔1s 发送一组数据 发送完成后打印一串数据“lora send ok”, 接收功能:收到数据后串口打印(本全程打印 ”lora send data“) #include "platform.h"static uint32_t TimeingDelay = 0; static uint32_t timeout_cnt = 1000; void Delayms(uint32_t ms){ TimeingDelay = ms; while(TimeingDelay != 0){};}void uart_recv_data(uint8_t *p, uint16_t len){ UART_ApiStructure.send_str(p, len);}extern void SX1276Init( void );extern void SX1276SetTxPacket( const void *buffer, uint16_t size );extern uint32_t SX1276Process( void );static uint16_t BufferSize = 30; // RF buffer sizestatic uint8_t Buffer[30]; // RF bufferconst uint8_t send_data_buf[] = "lora send datarn";extern void SpiInit( void );extern void Soft_delay_ms(u16 time);int main(void){ uint16_t count = 0, i; static uint32_t state = 0; //TIM_Configuration(); SYSTEM_tick_config_1ms(); Delayms(10); SX1276Init(); UART_ApiStructure.config(&UART_TypeDef_param, uart_recv_data); UART_ApiStructure.send_str("uart init okrn", 14); #if defined(SX1276_RX_DEF) SX1276StartRx();#else SX1276SetTxPacket( send_data_buf, 18 );#endif while (1){ if(!timeout_cnt){ timeout_cnt = 1000; count++; #if !defined(SX1276_RX_DEF) SX1276SetTxPacket( send_data_buf, strlen(send_data_buf));#endif } #if defined(SX1276_RX_DEF) if(SX1276Process( ) == RF_RX_DONE){ SX1276GetRxPacket( Buffer, ( uint16_t* )&BufferSize ); UART_ApiStructure.send_str(Buffer, BufferSize); for(i=0;i<30;i++) Buffer = 0; SX1276StartRx(); } #else if(SX1276Process( ) == RF_TX_DONE){ UART_ApiStructure.send_str("lora send okrn", 14); } #endif }}volatile uint32_t TickCounter = 0;void TimeingDelay_Decrement(void){ if (TimeingDelay > 0){ TimeingDelay--; } if(timeout_cnt){ timeout_cnt--; } TickCounter++;} 3、SX1276 spi 抽象层接口修改 sx1276_hal.c, sx1276-Hal.h SX1276-Hal.h 增加结构体,结构体实现在platform.c 文件中 sx1276_hal.c 实现如下 #include "platform.h"void SX1276InitIo( void ){ sx1278_ApiStructre.init();}void SX1276SetReset( uint8_t state ){ if( state == RADIO_RESET_ON ){ sx1278_ApiStructre.reset(0); }else{ sx1278_ApiStructre.reset(1); }}void SX1276Write( uint8_t addr, uint8_t data ){ SX1276WriteBuffer( addr, &data, 1 );}void SX1276Read( uint8_t addr, uint8_t *data ){ SX1276ReadBuffer( addr, data, 1 );}void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size ){ SPI_ApiStructure.write_buf(&sx1278_ApiStructre.spi, addr | 0x80, buffer, size);}void SX1276ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size ){ SPI_ApiStructure.read_buf(&sx1278_ApiStructre.spi, addr & 0x7F, buffer, size);}void SX1276WriteFifo( uint8_t *buffer, uint8_t size ){ SX1276WriteBuffer( 0, buffer, size );}void SX1276ReadFifo( uint8_t *buffer, uint8_t size ){ SX1276ReadBuffer( 0, buffer, size );}/*inline*/ uint8_t SX1276ReadDio0( void ){ //return GPIO_ReadInputDataBit( DIO0_IOPORT, DIO0_PIN ); return sx1278_ApiStructre.rDio0();}/*inline*/ uint8_t SX1276ReadDio1( void ){ return 0;//GPIO_ReadInputDataBit( DIO1_IOPORT, DIO1_PIN );}/*inline*/ uint8_t SX1276ReadDio2( void ){ return 0;//GPIO_ReadInputDataBit( DIO2_IOPORT, DIO2_PIN );}/*inline*/ uint8_t SX1276ReadDio3( void ){ return 0;//GPIO_ReadInputDataBit( DIO3_IOPORT, DIO3_PIN );}/*inline*/ uint8_t SX1276ReadDio4( void ){ return 0;//GPIO_ReadInputDataBit( DIO4_IOPORT, DIO4_PIN );}/*inline*/ uint8_t SX1276ReadDio5( void ){ return 0;//GPIO_ReadInputDataBit( DIO5_IOPORT, DIO5_PIN );}/*inline*/ void SX1276WriteRxTx( uint8_t txEnable ){// if( txEnable != 0 )// {// Set_RF_Switch_TX(); // IoePinOn( FEM_CTX_PIN );// IoePinOff( FEM_CPS_PIN );// }// else// {// Set_RF_Switch_RX(); // IoePinOff( FEM_CTX_PIN );// IoePinOn( FEM_CPS_PIN );// }} 4、编译、烧录、运行 |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1602 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1536 浏览 1 评论
967 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
680 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1579 浏览 2 评论
1860浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
641浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
513浏览 3评论
527浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
500浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-19 14:43 , Processed in 0.881855 second(s), Total 79, Slave 62 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号