STM8L单片机SI4463无线芯片IAR例程 厂家资料 好移植

#include 
#include 
#include 
#include 
#include "gpio.h"
#include "uart.h"
#include "oled.h"
#include "function.h"
#include "sensor.h"
#include "rf.h"

//SI4463 RF @STM8L151
void frameProcess( u8 *buf, u8 size )
{
  u8 cmd = buf[0];
  u8 len = buf[1];
  
  LED_RX=1;
  
  switch ( cmd )
  {
  case CMD_TEMP_HUM:
    
    if ( rfAck )
    { //收到协议数据,立即发送应答
      sendBuf[0]        = CMD_ACK;
      sendBuf[1]        = 0;
      RFSendData( sendBuf, 2 );
      msg( "send reply..." );
    }

    if ( len == sizeof(float) )
    { //拿到温湿度数据,更新显示
      memcpy( &temperature, buf + 2, 2 );
      memcpy( &humidity, buf + 4, 2 );
      //发送串口显示
      rf2uart( temperature, humidity);
      msg( "get sensor dat" );
    }
    break;
    
  case CMD_ACK:
    //发送后收到应答
    msg( "get reply" );
    break;
    
  default:
    //无线接收转串口透传
    UartSend( buf, size);
    msg( "get rf data" );
    break;
  }
  
  LED_RX=0;
}

int main( void )
{ int len;
  
    asm("sim");   //全局中断关闭
    
    GPIOInit();   //IO初始化
    Soft_I2C_Int();
    GpioFicker(); //上电两灯闪
    
    UartInit();   //UART初始化
    
    LED_Init();   //OLED屏初始化
    
    getSensor();  //获取初始温湿度值
    
    RFInit();     //射频模块初始化
    
    RFRxMode();   //设置RF接收模式
    
    asm("rim");   //全局中断开启
  
    msg( "system start" );//显示开机信息

    /*infinite loop begin*/
    while(1)
    {
        OledDisplay();      //OLED屏幕刷新显示
    
        keyProcess();       //按键监测处理
    
        timerTask();        //定期更新传感器读数
    
        uart2rf();          //串口转无线透传
    
        len = RFRevData( revBuf );  //接收无线数据
        if ( len > 0 )
        {
            frameProcess( revBuf, len );//对收到的数据进行处理
        }
    
        if ( rfContinueSend() )     //连续发送处理
        {
            delay_ms( 500 );
        }
    }/*infinite loop end*/
}

#include 
#include 
#include 
#include 
#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 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 );
}

 

你可能感兴趣的:(stm8)