辜渝傧

13037102709

027-87870986

教学实训

您当前的位置:首页 > 教学资源 > 实训方案 >

RFID实训|433M无线通信之电路搭建和程序实现

发布者:唯众    布时间:2020-12-02 14:58:15    点击量:

433M无线通信433M无线通信







#include "hardware.h"
#include "uart1.h"
u8 Line1[16];
u8 Line2[16];
u8 TempBuf[50];
u8 StartStep=0;
u8 LcdDispEnable=0;
u8 SensorChangeFlag=0;
extern u8 flag_rev1_finish;
extern u8 flag_rev2_finish;
SensorData Sensor;
NodeData Node,NodeTemp;
SysParam Sys={3,0};
RfPacket_t BR433_TX,BR433_RX;
u8 Ser_num;  //协调器编号
u8 Str2[16];
u8 Send_Buf[40];
u8 Uart1Data[USART1_MAX_RECV_LEN];
u8 Uart2Data[USART2_MAX_RECV_LEN];
u8 CheckResetMode(void);
void SetBR433Pack(void);
void GetSensorData(u8 id,u32 count);
void AppCmdDeal(void);
void SensorDetect(void);
void BR433_Setport(u8 ch);
void EXTI1_IRQHandler(void);
void Boot_A()
{
     u32 i;
     clear_screen();
     disp_string_8x16_16x16(1,1,(const u8 *)"Button to Set!!");
     for(i=0;i<6553600;i++)
     {
              IWDG_Feed();
              if(KEY_Scan(0)==1)
              {
                       clear_screen();
                       disp_string_8x16_16x16(1,1,(const u8 *)"Set the web site");
                       disp_string_8x16_16x16(3,1,(const u8 *)"A: 0");
                       for(i=0;i<6553600;i++)
                       {
                                IWDG_Feed();
                                 if(KEY_Scan(0))
                                 {                
                                                   Ser_num=Ser_num+1;
                                                   if(Ser_num==10) Ser_num=0;         
                                                   sprintf((char*)Str2,"%2d",Ser_num);               
                                                   disp_string_8x16_16x16(3,18,(const u8 *)Str2);
                                 }
                       }
                       memset((char*)&NodeTemp,0,sizeof(NodeData));
                       NodeTemp.server[0]=Ser_num;
                       STMFLASH_Write(FLASH_SAVE_ADDR,(u16*)&NodeTemp,sizeof(NodeData));              
                       Ser_num=0;
              }
     }
}
 
u8 BOOT_B()
{
     IWDG_Feed();
     memset((char*)&NodeTemp,0,sizeof(NodeData));
     STMFLASH_Read(FLASH_SAVE_ADDR,(u16*)&NodeTemp,sizeof(NodeData));
     memset((char*)Send_Buf,0,sizeof(Send_Buf));
     Send_Buf[0]='@';
     Send_Buf[1]=NodeTemp.server[0];
     Send_Buf[9]=NodeTemp.ip[0];
     Send_Buf[11]=Sensor.Id;
     Send_Buf[39]='$';   
     USART1->CR1 &= ~(1<<5);
     IWDG_Feed();
     uart1_send_bytes(40,Send_Buf);
     USART1->CR1 |= 1<<5;
     delay_ms(500);
     if(flag_rev1_finish)
     {
              flag_rev1_finish=0;
              if(!( USART1_RX_BUF[0]=='@' && USART1_RX_BUF[39]=='$'))
              {
                       return 1;
              }
              if(USART1_RX_BUF[11]==Sensor.Id)
              {
                        if(USART1_RX_BUF[13]=='S' && USART1_RX_BUF[14]=='E')
                       {
                                 NodeTemp.server[0]=USART1_RX_BUF[1];
                                 NodeTemp.ip[0]=USART1_RX_BUF[9];
                                 STMFLASH_Write(FLASH_SAVE_ADDR,(u16*)&NodeTemp,sizeof(NodeData));                       
                       }
                       return 0;
              }       
     }
     delay_ms(100);
     return 1;
}
 
void EXTI1_IRQHandler(void)
{
     u8 temp=0;              
     delay_ms(5);   
     temp=1;
     sprintf((char*)Str2,"Val:%d",temp);
     disp_string_8x16_16x16(3,1,(const u8*)Str2);
     BR433_TX.Pack.Sid[0] = 20;
 BR433_TX.Pack.Sid[1] = ' ';
 BR433_TX.Pack.Cmd[0] = 'S';
 BR433_TX.Pack.Cmd[1] = 'N';
 BR433_TX.Pack.Len = 1;
 BR433_TX.Pack.Data[0] = temp;       
     uart1_send_bytes(40,BR433_TX.Buf);   
     EXTI->PR=1<<1;  //清除LINE0上的中断标志位 
}
 
int main(void)
{
 
     u32 count=0;
     u32 temp8=0;
//1.各项硬件初始化
 Stm32_Clock_Init(9);
     delay_init(72);          
     fg_BuzzerInit();       
     fg_KeyInit();
     fg_OledInit();           
     fg_BuzzerOn(200);
     IWDG_Init(4,1250);
     fg_SensorIdInit();
     clear_screen();        
    
     if(CheckResetMode()==1){                 //软件复位
              disp_string_8x16_16x16(1,1,(u8*)"User Reset!");
     }else{                                    
              disp_string_8x16_16x16(1,1,(u8*)"Power on!");
     }                
#if(ESP8266_IS_STA==0)     
     Boot_A();
     disp_string_8x16_16x16(1,1,(const u8*)"武汉唯众智创科技");
     disp_string_8x16_16x16(3,1,(const u8*)"有限公司");
     fg_SetKeyReset();                      
     memset((char*)&NodeTemp,0,sizeof(NodeData));
     STMFLASH_Read(FLASH_SAVE_ADDR,(u16*)&NodeTemp,sizeof(NodeData));
     clear_screen();
     disp_string_8x16_16x16(1,1,(const u8*)"A: 0");
     sprintf((char*)Str2,"%2d",NodeTemp.server[0]);            
     disp_string_8x16_16x16(1,18,(const u8*)Str2);
     uart1_init(72,115200);
     uart2_init(36,9600);
     while(1)
     {
                       u8 i;
                       delay_ms(10);
                       sprintf((char*)Str2,"Received: %2d",temp8);         
                       disp_string_8x16_16x16(3,1,(const u8*)Str2);
                       IWDG_Feed();
                       if(flag_rev1_finish )
                       {       
                                 IWDG_Feed();
                                 temp8++;
                                 if(USART1_RX_BUF[0]=='@'&&USART1_RX_BUF[39]=='$')
                                 {       
                                          for(i=0;i<16;i++)
                                          {
                                                             if(NodeTemp.ip[i]==USART1_RX_BUF[9])
                                                             {
                                                                      break;
                                                             }                                                                        
                                          }
                                          if(NodeTemp.server[0]==USART1_RX_BUF[1] && (i<15))
                                          {
                                                   Send_Buf[0]='@';
                                                   Send_Buf[11]=USART1_RX_BUF[11];
                                                   Send_Buf[13]='O';
                                                   Send_Buf[14]='K';
                                                   Send_Buf[39]='$';             
                                                   IWDG_Feed();
                                                   USART1->CR1 &= ~(1<<5);
                                                   uart1_send_bytes(40,Send_Buf);
                                                  
                                          }
                                          else
                                          {
                                                   for(i=0;i<16;i++)
                                                   {
                                                             if(NodeTemp.ip[i]==0)
                                                             {
                                                                      break;
                                                             }                                                                        
                                                   }
                                                   NodeTemp.ip[i]=i*3+USART1_RX_BUF[11]+13;                                           
                                                   Send_Buf[0]='@';
                                                   Send_Buf[11]=USART1_RX_BUF[11];
                                                   Send_Buf[13]='S';
                                                   Send_Buf[14]='E';
                                                   Send_Buf[1]=NodeTemp.server[0];
                                                   Send_Buf[9]=NodeTemp.ip[i];
                                                   Send_Buf[39]='$';                                         
                                                   STMFLASH_Write(FLASH_SAVE_ADDR,(u16*)&NodeTemp,sizeof(NodeData));    
                                                   IWDG_Feed();
                                                   USART1->CR1 &= ~(1<<5);
                                                   uart1_send_bytes(40,Send_Buf);             
                                                  
                                          }
                                         
                                 }
                                 else if(USART1_RX_BUF[0]=='#' && USART1_RX_BUF[39]=='$' && USART1_RX_BUF[1]==NodeTemp.server[0])
                                 {
                                                             u2_send_data(USART1_RX_BUF,40);
                                 }
                                 USART1->CR1 |= 1<<5;
                                 flag_rev1_finish=0;
                       }
                       if(flag_rev2_finish)
                       {
                                 uart1_send_bytes(40,USART2_RX_BUF);
                                 flag_rev2_finish=0;
                       }
              }
 
#else
              uart1_init(72,115200);
              clear_screen();
              disp_string_8x16_16x16(1,1,(const u8*)"武汉唯众智创科技");
              disp_string_8x16_16x16(3,1,(const u8*)"有限公司");
              fg_SetKeyReset();             
              IWDG_Feed();
              Sensor.Id = fg_GetSensorId();
              clear_screen();
              sprintf((char*)Str2,"%2d: %2d: SID:%2d",NodeTemp.server[0],NodeTemp.ip[0],Sensor.Id);                  
              disp_string_8x16_16x16(1,1,(const u8*)Str2);
              SetBR433Pack();
              fg_SensorDataInit(Sensor.Id);
              if(!(Sensor.Id==S2_YYSB || Sensor.Id==S15_YYBF))
              {
                       fg_Timer3Init(9999,7199);//1s触发一次
              }
              if(Sensor.Id==S20_SYJC)
              {
                       RCC->APB2ENR|=1<<2;
                       GPIOA->CRL&=0XFFFFFF0F;
                       GPIOA->CRL|=0X00000080;
                       Ex_NVIC_Config(GPIO_A,1,RTIR);
                       MY_NVIC_Init(2,2,EXTI1_IRQn,2);
              }                
              while(1)
              {
                       count++;
                       GetSensorData(Sensor.Id,count);
                       IWDG_Feed();
                       if(flag_rev1_finish)
                       {
                                 if(USART1_RX_BUF[0]=='#'&&USART1_RX_BUF[39]=='$'&&USART1_RX_BUF[11]==Sensor.Id)
                                 {
                                                   BR433_Setport(Sensor.Id);      
                                 }
                                 flag_rev1_finish=0;
                       }
              }
 
#endif
}
void BR433_Setport(u8 ch)
{
     switch(ch)
     {
              case S1_JDQ:
                       if(USART1_RX_BUF[13]==0)
                       {
                                 PAout(1)=0;
                       }
                       else PAout(1)=1;
                       break;
                       default:
                                 break;
     }
}
void SensorDetect(void)
{
     if(SensorChangeFlag == 1){
              SensorChangeFlag = 0;            
              delay_ms(500);
              if(SensorChangeFlag == 0){
                       Sensor.Id = fg_FreshSensor(Sensor.Id);                      
                       BR433_TX.Pack.Sid[0] = Sensor.Id;
              }
     }
}
 
void GetSensorData(u8 id,u32 count)
{
     u8* ptr=NULL;
     u8 temp8=0;
     u16 delay=12000;
     SetBR433Pack();
     switch(id){
     case S0_NULL:
              sprintf((char*)Line2,"No sensor!");                  
              break;
     case S1_JDQ:            
              Sensor.Ivalue = PAin(0);
              Sensor.Ovalue = PAout(1);
              break;
     case S2_YYSB:
              sprintf((char*)Line2,"YYSB");
              break;
     case S10_WNHW:            
              temp8 = uart2_recv(2);
              if(temp8>0){
    
              }
              sprintf((char*)Line2,"WNHW");
              break;
     case S11_CK:   
              break;
     case S20_SYJC:
              temp8=0;
              break;
     case S18_WSD:
              delay = 100;
              if(count%delay==0){
                       DHT11(&Sensor.Tvalue,&Sensor.Hvalue);
              }
              break;
     case S19_HWSF:
    
              break;      
     case S4_YD:              
     case S8_KQZL:
     case S17_RTHW:
     case S25_YW:
     case S26_GM:          
              ptr = fg_GetAoutDout();
              Sensor.Dvalue = ptr[0];
              Sensor.Avalue = ptr[2];
            
              break;
     default:
              sprintf((char*)Line2,"Untreated!");
              break;
     }
     if(count%delay==0){
     }
}
void TIM3_IRQHandler(void)
{
     u8* ptr=NULL;
     if(TIM3->SR&0X01)
     {                                     
              Sys.counter++;                  
              if(Sys.counter%UPLOAD_TIME==0){
                       SetBR433Pack();
                       BR433_TX.Pack.Sid[0]=Sensor.Id;
                       BR433_TX.Pack.Sid[1] = ' ';     
                       BR433_TX.Pack.Cmd[0] = 'S';
                       BR433_TX.Pack.Cmd[1] = 'N';
                       switch(Sensor.Id){
                       case S1_JDQ:
                                 BR433_TX.Pack.Len = 2;
                                 BR433_TX.Pack.Data[0] = PAin(0);
                                 BR433_TX.Pack.Data[1] = PAout(1);
             
                                 sprintf((char*)Line2,"In:%d Out:%d",Sensor.Ivalue,Sensor.Ovalue);
                                 break;
                       case S18_WSD:
                                 BR433_TX.Pack.Len = 2;
                                 BR433_TX.Pack.Data[0] = Sensor.Tvalue;
                                 BR433_TX.Pack.Data[1] = Sensor.Hvalue;
             
                                 sprintf((char*)Line2,"Tem:%2d Hum:%2d",Sensor.Tvalue,Sensor.Hvalue);
                                 break;               
                       case S19_HWSF:
                                 break;
                       case S3_HE:
                                 break;
                       case S20_SYJC:         
                                 BR433_TX.Pack.Len = 1;
                                 BR433_TX.Pack.Data[0] = PAin(1);
                                 sprintf((char*)Line2,"Val:%d ",BR433_TX.Pack.Data[0]);
    
                                 break;
                       case S14_485:
    
                                 break;
                      
                       case S4_YD:                                
                       case S8_KQZL:
                       case S17_RTHW:                        
                       case S25_YW:
                       case S26_GM:                   
                                 ptr = fg_GetAoutDout();
                                 BR433_TX.Pack.Len = 3;
                                 BR433_TX.Pack.Data[0] = ptr[0];
                                 BR433_TX.Pack.Data[1] = ptr[1];
                                 BR433_TX.Pack.Data[2] = ptr[2];             
                                 sprintf((char*)Line2,"Sig:%d Val:%d",ptr[0],ptr[2]);
                                 break;
                      
                       default:
                                 break;
                       }
                       uart1_send_bytes(40,BR433_TX.Buf);
    
                       oled_clear_vertical(2);
                       disp_string_8x16_16x16(3,1,Line2);
              }           
              TIM3->SR&=~(1<<0);
     }
}
 
void SetBR433Pack(void)
{                                                          
     BR433_TX.Pack.Head = '#';
     BR433_TX.Pack.Cmd[0] = 'S';
     BR433_TX.Pack.Cmd[1] = 'N';
     BR433_TX.Pack.Laddr[0] = NodeTemp.server[0];
     BR433_TX.Pack.Saddr[0] = NodeTemp.ip[0];
     BR433_TX.Pack.Tail = '$';
}
 
u8 CheckResetMode(void)
{
     u8 temp8 = 0;
     if(RCC->CSR & 0x10000000)
     {//????
              temp8 = 1;
     }
     RCC->CSR |= 0x01000000;//RCC->CSR ?????
     return temp8;
}


上一篇:RFID实训|433M无线通信之技术介绍

下一篇:RFID实训|Zigbee无线通信