STM8L-SI4463 IAR例程 廠家資料 好移植
單片機源程序如下:
- #include <stdio.h>
- #include <stdarg.h>
- #include <stdlib.h>
- #include <string.h>
- #include "function.h"
- #include "oled.h"
- #include "uart.h"
- #include "gpio.h"
- #include "rf.h"
- #include "sensor.h"
- unsigned short continueSendCnt=0; //連續發包數
- unsigned short continueSendIndex=0;
- void delay_us(u16 t)
- {
- u16 i;
- for(i=0;i<t;i++)
- {
-
- asm("NOP");
-
- }
- }
- void delay_ms(u16 t)
- {
- u16 i;
- u16 j;
- for(i=0;i<t;i++)
- for(j=0;j<400;j++);
- }
- /* ------------------------------------------------------------------------ */
- void GpioFicker( void )
- {
- //上電兩燈閃
- LED_TX=1; //兩燈亮
- LED_RX=1;
- delay_ms(500);
- LED_TX=0;
- LED_RX=0;
- delay_ms(500);
- LED_TX=1; //兩燈亮
- LED_RX=1;
- delay_ms(500);
- LED_TX=0;
- LED_RX=0;
- }
- void keyProcess( void )
- {
- if ( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_4 ) == 0x00 ) /* S1 C13 */
- {
- delay_ms( 20 );
- if ( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_4 ) == 0x00 )
- {
-
- msg( "clear ...." );
- iSend = 0;
- iRev = 0;
- while ( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_4 ) == 0x00 )
- ; /* 等待按鍵釋放 */
-
- }
- }
-
- if ( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_6 ) == 0x00 ) /* S2 A0 */
- {
- delay_ms( 10 );
-
- if ( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_6 ) == 0x00 )
- {
- u8 len = 0;
- while ( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_6 ) == 0x00 )
- ; /* 等待按鍵釋放 */
- sendBuf[0] = CMD_TEMP_HUM; /* cmd: 1發送傳感器數據 2應答成功 */
- sendBuf[1] = 4;
- getSensor();
- /* 數據長度 */
- memcpy( &sendBuf[2], &temperature, 2 );
- memcpy( &sendBuf[4], &humidity, 2 );
-
-
- LedRed( On );
- len = RFSendData( sendBuf, 6 );
- if ( len > 0 )
- msg( "send ok" );
- else
- msg( "send fail" );
-
- LedRed( Off );
- }
- }
-
-
- if ( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_5 ) == 0x00 )
- {
- delay_ms( 10 );
- if ( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_5 ) == 0x00 )
- {
-
- while ( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_5 ) == 0x00 )
- ; /* 等待按鍵釋放 */
-
- continueSendCnt+=50;
-
-
- }
- }
- }
- char rfContinueSend(void)
- {
- if(continueSendIndex>=continueSendCnt)return 0;
- continueSendIndex++;
- LedRed( On );
- getSensor();
- sendBuf[0] = CMD_TEMP_HUM; /* cmd: 1發送傳感器數據 2應答成功 */
- sendBuf[1] = 4; /* 數據長度 */
- memcpy( &sendBuf[2], &temperature, 2 );
- memcpy( &sendBuf[4], &humidity, 2 );
-
-
- RFSendData( sendBuf, 6 );
- sprintf( (char *) sendBuf, "AutoSend:%d/%d", continueSendIndex,continueSendCnt );
- msg( (char *) sendBuf );
-
- LedRed( Off );
- OledDisplay();
- return 1;
-
-
- }
- /* ------------------------------------------------------------------------ */
- void uart2rf(void)
- {
- static u8 uart_rx_len_last=0;
- u8 i,tmp=0;
- if(uart_rx_len_last>0)
- {
- LED_TX=1;
- if(uart_rx_len_last==uart_rx_len)
- {
- while(uart_rx_len)
- {
- u8 len=uart_rx_len>32?32:uart_rx_len;
- RFSendData(uart_rx_buf+tmp,len);
- uart_rx_len-=len;
- tmp+=len;
- delay_ms( 10 );
- }
- LED_TX=0;
- }
- }
-
- uart_rx_len_last=uart_rx_len;
- }
- void rf2uart(s16 tem,s16 hum)
- {
- u8 sbuf[64];
- sprintf( sbuf, "Tem%.1f Hum%.1f%%\n", tem / 10.0, hum / 10.0 );
- UartSend( sbuf, 18);
- }
- void timerTask(void)
- {
- static u32 count;
- //static u8 flag = 0;
- static int lastRevByte=-1;
-
- static u8 delay=1;
-
- if ( ++count > 2 )
- {
- count = 0;
-
-
- //收到數據5秒內顯示收到的數據而不是顯示采集到的本地數據
- if(lastRevByte!=iRev)
- {
- lastRevByte=iRev;
- delay=10;
- }else{
- if(--delay==0)
- {
- if(delay==0)delay=5;
- getSensor();
- }
- }
- }
- }
- void debug( const char* fmt, ... )
- {
- va_list args;
- va_start( args, fmt );
- vprintf( fmt, args );
- va_end( args );
- }
- /*
- * ------------------------------------------------------------------------
- * ------------------------------------------------------------------------
- * -------------------- 私有函數定義 ------------------------------
- */
- void msg( char *msg ) /* 如果要顯示中文,OLED點陣字庫必須有相應數據,英文則不需要 */
- {
- static u8 count=0;
- char buf[8];
- LED_PXx16MixStr( 0, 3, " " ); /* 16個字母 */
- sprintf(buf,"%d:",++count%10);
- LED_PXx16MixStr(0,3,buf);
- LED_PXx16MixStr( 14, 3, msg );
- //debug( msg );
-
- }
- void OledDisplay( void )
- {
- char buf[64] = "";
- static char flag=0;
-
- if(!flag)
- {
- sprintf( buf, "%s + SI7021", rfName );
-
- LED_PXx16MixStr( 0, 0, buf );
-
- flag=1;
- }
-
- sprintf( buf, "Tem%.1f Hum%.1f%%", temperature / 10.0, humidity / 10.0 );
-
-
- LED_PXx16MixStr( 0, 1, buf );
-
- sprintf( buf, "Snd:%d Rev:%d", iSend, iRev );
-
- LED_PXx16MixStr( 0, 2, buf );
- }
復制代碼
所有資料51hei提供下載:
STM8L_4463.7z
(892.6 KB, 下載次數: 43)
2019-2-15 16:43 上傳
點擊文件名下載附件
|