根据泽耀官方的STM8L101的例程移植的STM8S103 CC1101接收发送程序引脚有所变化,具体参见CC1101.h 和 SPI.h 的引脚定义
适用于泽耀及亿佰特的CC1101 SPI 接口模块,同理,改改就能适用于24L01模块
单片机源程序如下:
- #define __CC1101_TX_TEST__ //**@@ 如果测试发送功能则需要定义该宏,如果测试接收则需要屏蔽该宏 **@@//
- //#define __USE_SOFT_SPI_INTERFACE__ //**@@ 如果使用软件SPI则需要定义该宏,反之如果使用硬件SPI则需要屏蔽该宏 **@@//
- #include "main.h" //main.h 中含有TX/RX、软件SPI/硬件SPI选择配置选项
- #include "stdio.h"
- const char *g_Ashining = "ashining";
- uint8_t g_TxMode = 0;
- uint8_t g_UartRxBuffer[ 100 ] = { 0 };
- uint8_t g_RF24L01RxBuffer[ 32 ] = { 0 };
- /**
- * @brief :主函数
- * @param :无
- * @note :无
- * @retval:无
- */
- int main( void )
- {
- uint8_t i = 0;
- //串口初始化
- drv_uart_init( 9600 );
-
- //LED初始化
- drv_led_init( );
-
- //SPI初始化
- drv_spi_init( );
-
- //CC1101初始化
- CC1101_Init( );
-
- for( i = 0; i < 3; i++ )
- {
- led_red_flashing( );
- led_green_flashing( );
- drv_delay_ms( 500 );
- }
-
- #ifdef __CC1101_TX_TEST__
- //=========================================================================================//
- //*****************************************************************************************//
- //************************************* 发送 **********************************************//
- //*****************************************************************************************//
- //=========================================================================================//
-
- //按键初始化
- drv_button_init( );
- while( 1 )
- {
- //模式切换
- if( BUTOTN_PRESS_DOWN == drv_button_check( ))
- {
- g_TxMode = 1 - g_TxMode; //模式会在 TX_MODE_1( 0 ),TX_MODE_2( 1 )之间切换
-
- //状态显示清零
- led_green_off( );
- led_red_off( );
-
- if( TX_MODE_1 == g_TxMode )
- {
- for( i = 0; i < 3; i++ )
- {
- led_red_flashing( ); //固定发送模式,红灯闪烁3次
- drv_delay_ms( 300 );
- }
- }
- else
- {
- for( i = 0; i < 6; i++ )
- {
- led_green_flashing( ); //串口发送模式,绿灯闪烁3次
- drv_delay_ms( 300 );
- }
- }
- }
-
- //发送
- if( TX_MODE_1 == g_TxMode )
- {
- CC1101_Tx_Packet( (uint8_t *)g_Ashining, 8 , ADDRESS_CHECK ); //模式1发送固定字符,1S一包
- drv_delay_ms( 1000 );
- led_red_flashing( );
- //drv_uart_tx_bytes("sending\r\n",9);
- printf("固定发送模式\r\n");
-
- }
- else
- {
- //查询串口数据
- //drv_uart_tx_bytes("uart...\r\n",9);
- printf("串口发送模式\r\n");
- i = drv_uart_rx_bytes( g_UartRxBuffer );
-
- if( 0 != i )
- {
- CC1101_Tx_Packet( g_UartRxBuffer, i , ADDRESS_CHECK );
- led_red_flashing( );
- }
- }
- }
-
- #else
- //=========================================================================================//
- //*****************************************************************************************//
- //************************************* 接收 **********************************************//
- //*****************************************************************************************//
- //=========================================================================================//
- {
-
- //printf("接收数据模式");
- drv_uart_tx_bytes("receive\r\n",9);
- while( 1 )
- {
- CC1101_Clear_RxBuffer( );
- CC1101_Set_Mode( RX_MODE );
- i = CC1101_Rx_Packet( g_RF24L01RxBuffer ); //接收字节
-
- if( 0 != i )
- {
- led_green_flashing( );
- drv_uart_tx_bytes( g_RF24L01RxBuffer, i ); //输出接收到的字节
- }
- drv_delay_ms(500);
-
- }
- }
- #endif
-
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /**
- * @}
- */
复制代码
所有资料51hei提供下载:
C1101.rar
(1.71 MB, 下载次数: 90)
|