標(biāo)題:
STM32單片機(jī)+SX1278官方源碼分享
[打印本頁(yè)]
作者:
shw7610
時(shí)間:
2019-5-17 16:52
標(biāo)題:
STM32單片機(jī)+SX1278官方源碼分享
自帶收發(fā)功能,支持SX1272/1276/1278
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "platform.h"
#include "led.h"
#if USE_UART
#include "uart.h"
#endif
#include "radio.h"
#define BUFFER_SIZE 9 // Define the payload size here
static uint16_t BufferSize = BUFFER_SIZE; // RF buffer size
static uint8_t Buffer[BUFFER_SIZE]; // RF buffer
static uint8_t EnableMaster = true; // Master/Slave selection
tRadioDriver *Radio = NULL;
const uint8_t PingMsg[] = "PING";
const uint8_t PongMsg[] = "PONG";
/*
* Manages the master operation
*/
void OnMaster( void )
{
uint8_t i;
switch( Radio->Process( ) )
{
case RF_RX_TIMEOUT:
// Send the next PING frame
Buffer[0] = 'P';
Buffer[1] = 'I';
Buffer[2] = 'N';
Buffer[3] = 'G';
for( i = 4; i < BufferSize; i++ )
{
Buffer[i] = i - 4;
}
Radio->SetTxPacket( Buffer, BufferSize );
break;
case RF_RX_DONE:
Radio->GetRxPacket( Buffer, ( uint16_t* )&BufferSize );
if( BufferSize > 0 )
{
if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 )
{
// Indicates on a LED that the received frame is a PONG
LedToggle( LED_GREEN );
// Send the next PING frame
Buffer[0] = 'P';
Buffer[1] = 'I';
Buffer[2] = 'N';
Buffer[3] = 'G';
// We fill the buffer with numbers for the payload
for( i = 4; i < BufferSize; i++ )
{
Buffer[i] = i - 4;
}
Radio->SetTxPacket( Buffer, BufferSize );
}
else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
{ // A master already exists then become a slave
EnableMaster = false;
LedOff( LED_RED );
}
}
break;
case RF_TX_DONE:
// Indicates on a LED that we have sent a PING
LedToggle( LED_RED );
Radio->StartRx( );
break;
default:
break;
}
}
/*
* Manages the slave operation
*/
void OnSlave( void )
{
uint8_t i;
switch( Radio->Process( ) )
{
case RF_RX_DONE:
Radio->GetRxPacket( Buffer, ( uint16_t* )&BufferSize );
if( BufferSize > 0 )
{
if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
{
// Indicates on a LED that the received frame is a PING
LedToggle( LED_GREEN );
// Send the reply to the PONG string
Buffer[0] = 'P';
Buffer[1] = 'O';
Buffer[2] = 'N';
Buffer[3] = 'G';
// We fill the buffer with numbers for the payload
for( i = 4; i < BufferSize; i++ )
{
Buffer[i] = i - 4;
}
Radio->SetTxPacket( Buffer, BufferSize );
}
}
break;
case RF_TX_DONE:
// Indicates on a LED that we have sent a PONG
LedToggle( LED_RED );
Radio->StartRx( );
break;
default:
break;
}
}
/*
* Main application entry point.
*/
int main( void )
{
BoardInit( );
Radio = RadioDriverInit( );
Radio->Init( );
Radio->StartRx( );
while( 1 )
{
if( EnableMaster == true )
{
OnMaster( );
}
else
{
OnSlave( );
}
#if( PLATFORM == SX12xxEiger ) && ( USE_UART == 1 )
UartProcess( );
{
uint8_t data = 0;
if( UartGetChar( &data ) == UART_OK )
{
UartPutChar( data );
}
}
#endif
}
#ifdef __GNUC__
return 0;
#endif
}
復(fù)制代碼
全部資料51hei下載地址:
semtech驅(qū)動(dòng)源碼SX12xxDrivers-2.0.0.7z
(577.25 KB, 下載次數(shù): 109)
2019-5-18 03:54 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
zailushang...
時(shí)間:
2020-12-17 11:08
這是stm32的程序
作者:
993347045
時(shí)間:
2021-11-3 00:04
十分感謝分享代碼,學(xué)習(xí)了~~~
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1