你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32 智能防酒驾装置

[复制链接]
STMCU 发布时间:2020-9-11 12:54
一、总体设计方案

  智能防酒驾装置由一个主机设备从和三个从机(酒精传感器)组成,主机采用STM32F407作为主控设备,从机采用STM32F030作为主控芯片,主机和从机之间通过NRF24L01进行无线通信。主机负责酒驾情况的判定、通过GPS进行定位、通过GSM向用户发送短信、控制车辆状态、显示设备当前状态并向驾驶员发出语音提示等工作。从机主要负责采集车辆内各处的酒精浓度、向主机报告车辆各处的酒精浓度。酒精传感器采用TGS2620高灵敏度乙醇气体传感器对气体信号进行检测,当空气中有乙醇气体存在时,该气体的浓度越高传感器的电导率也会越高。将电导率的变化转换成与该气体浓度相对应的电压信号输出,根据电压信号进行酒精含量的判断。

  该装置的工作流程为:驾驶司机进入车内后,会有一个装有气体传感器的装置强行检测司机的喝酒情况。当酒精浓度较低时,系统无反应并进行语音提示,车子可以正常行驶;但是当酒精浓度过高,超过安全驾驶的酒精浓度范围时,检测装置断开点火开关,使汽车无法发动,并发出语音提示驾驶员请勿酒驾。另外,系统会通过GPS对车辆进行定位,将司机醉酒驾驶的情况和当前位置以短信的形式发送给司机的家属、朋友,或向第三方发送位置,请求代驾。


二、硬件电路

1.主机

  主机由STM32F407VET6作为主控芯片,将GPS模块、GSM模块、语音模块、NRF24L01无线通信模块、OLED显示模块集成在一起,对外提供继电器、热释电红外模块、酒精传感器和矩阵键盘的接口。主设备采用12V电源进行供电,首先经过MP1584en稳压模块将电压降至5V,为GSM模块、语音模块、继电器进行供电(原定采用AMS1117-5.0进行5V稳压,但由于AMS1117的最大输出电流只有1A,无法满足设备的供电需求)。然后在经过AMS1117-3.3将电压稳压到3.3V,为GPS模块、OLED显示屏、NRF24L01无线通信模块、热释电红外模块进行供电。


20200911125526.19a5966287a6415602e11d615c611bde.png

20200911125551.8719143ed6a85976e3d5f2e14ccee351.png


2.从机

  从机由STM32F030F4P6作为主控芯片,主要负责对酒精传感器的信号进行采集,并通过NRF24L01将采集到的信号发送给主机做酒驾判断。为了减小从机的体积,从机部分没有采用外部的晶振提供时钟信号,只保留了稳压电路和滤波电路。

20200911125610.670127be52c0fe9206aa1ff83431e1a8.png


20200911125621.d6ee35ce3a04c0af9222fed41ba1f83c.png


3.酒精传感器

  酒精传感器由TGS2620作为敏感元件,首先让TGS2620输出的电压信号经过惠斯通电桥,形成一组差分信号,然后经过由LM358组成的差分运算放大器,从而获得到空气中酒精浓度的相对变化值。


5.png

20200911125654.3ba28eacb3f3ad543a45da431bb51eb5.png

6.png



三、程序设计
7.jpg



主机部分


1.GPS

  GPS通过USART3与主机进行通信,波特率为9600。使用NMEA-0183协议进行通信,获取GPS返回的GPGGA数据,然后进行相关的数据解析。

  1. <font size="3" color="#000000">//********************************************************************
  2. //得到GPS的经纬度
  3. //数据为char型数组
  4. //若没有得到数据则无返回结果
  5. //********************************************************************
  6. void GPS_GetData()
  7. {
  8.         u8 i;
  9.         if(USART3_RX_STA&0X8000)                //接收到一次数据了
  10.         {
  11.                 USART3_RX_STA=0;                                   //启动下一次接收
  12.                
  13.                 GPS_Analysis(&gpsx,(u8*)USART3_RX_BUF);//分析字符串
  14.                
  15.                 if(gpsx.gpssta==1 ||gpsx.gpssta==2 )        //GPS定位成功
  16.                 {
  17.                         u8 i;
  18.                         
  19.                         GPS_positioning = 1;

  20.                         Transform_double_to_char(((double)(gpsx.longitude)/10000000-0.003466), longitude_char, 6, 1);
  21.                         Transform_double_to_char(((double)(gpsx.latitude)/10000000+0.323707), latitude_char, 6, 1);        
  22.                         
  23.                         i =strlen(longitude_char);
  24.                         longitude_char[i] =gpsx.ewhemi;
  25.                         longitude_char[i+1] ='\0';
  26.                         
  27.                         i =strlen(latitude_char);
  28.                         latitude_char[i] =gpsx.nshemi;
  29.                         latitude_char[i+1] ='\0';
  30.                         
  31.                         printf("GPS定位成功\r\n");        
  32.                 }
  33.                 else if(gpsx.gpssta==0)
  34.                 {        
  35.                         if(GPS_positioning == 1 )
  36.                         {
  37.                                 GPS_positioning = 2;
  38.                                 printf("GPS信号弱\r\n");
  39.                         }
  40.                         else if(GPS_positioning == 0 )
  41.                         {
  42.                                 printf("GPS无信号\r\n");
  43.                         }
  44.                 }
  45.                 gpsx.gpssta = 0;
  46.         }
  47. }
  48. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END



  49. //********************************************************************
  50. //浮点型/整型转字符型
  51. //Decimal需转换小数
  52. //Character字符型储存的地址,在主函数定义
  53. //precision保留的精度
  54. //round是否四舍五入,1是,0否
  55. //注意可以转换的长度
  56. //********************************************************************
  57. void Transform_double_to_char(double Decimal, char *Character, short int precision, char round)
  58. {
  59.         int i,x=1;
  60.         int integer ,integer_decimals;
  61.         char temporary_char[10];
  62.         for(i=0;i<precision;i++)
  63.         {
  64.                 x *=10;
  65.         }
  66.         integer = (int)Decimal;                                                                                                                        //整数部分
  67.         integer_decimals = (int)((Decimal - integer) * x + 0.5*round);
  68.         for(i=0 ;integer>0 ;i++)
  69.         {
  70.                 temporary_char[i]= (integer % 10) + '0';
  71.                 integer = (int)(integer/10);
  72.         }
  73.         if( i==0)
  74.         {
  75.                 temporary_char[i]= '0';
  76.                 i++;
  77.         }
  78.         for(x=0;x<i;x++)
  79.         {
  80.                 Character[x] =temporary_char[i-x-1];
  81.         }
  82.         memset(temporary_char,NULL,sizeof(temporary_char));
  83.         if(precision!=0)
  84.         {
  85.                 Character[x]= '.';
  86.                 x++;
  87.                 for(i=0 ;i<precision ;i++)
  88.                 {
  89.                         temporary_char[i]= (integer_decimals % 10) + '0';
  90.                         integer_decimals = (int)(integer_decimals/10);
  91.                 }
  92.                 for(i=0 ;i<precision ;x++,i++)
  93.                 {
  94.                         Character[x] = temporary_char[precision-i-1];        
  95.                 }
  96.         }
  97.         Character[x] ='\0';
  98. }
  99. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END</font>
复制代码

2.GSM

  GSM采用USART2进行通信,向模块发送相应的AT指令既可完成对模块相应的操作。

  1. <font size="3" color="#000000">//********************************************************************
  2. //SIM执行命令
  3. //0~3发送短信,0~3号联系人
  4. //********************************************************************
  5. char SIM900A_Executive_Command( char number )
  6. {        
  7.         if( number == 0 ||number == 1 ||number == 2 ||number == 3 )
  8.         {
  9.                 int i ,Length;
  10.                 char Message_content[280];
  11.                 char AT_CMGS[4],Data_Length[4];
  12.                 char PUD_latitude[50],PUD_longitude[50];
  13.                 char PUD_longitude_latitude[100];
  14.                 char PUD_comma[5]={"002C"};
  15.                
  16.                 PDU_Transform__Phone_number(Phone_number[number]);
  17.                         
  18.                 if(GPS_positioning != 0)
  19.                 {
  20.                         PDU_Transform__Message_content(Message_content,Message_content_original_1);
  21.                         
  22.                         Char_to_Unicode(latitude_char, PUD_latitude );
  23.                         Char_to_Unicode(longitude_char, PUD_longitude );
  24.                         
  25.                         memset(PUD_longitude_latitude,NULL,strlen(PUD_longitude_latitude));
  26.                         String_addition(PUD_longitude_latitude ,PUD_latitude ,0);
  27.                         String_addition(PUD_longitude_latitude ,PUD_comma ,strlen(PUD_longitude_latitude));
  28.                         String_addition(PUD_longitude_latitude ,PUD_longitude ,strlen(PUD_longitude_latitude));
  29.                         
  30.                         String_addition(Message_content ,PUD_longitude_latitude ,15*4);
  31.                 }
  32.                 else
  33.                 {
  34.                         PDU_Transform__Message_content(Message_content,Message_content_original_2);
  35.                 }
  36.                 Length = strlen(Message_content)/2;
  37.                 Transform_int_to_hexadecimal(Length ,Data_Length ,2);
  38.                 Transform_double_to_char( Length+15, AT_CMGS, 0, 0);
  39.                
  40.                 printf("%s\r\n",PDU_Phone_number);
  41.                 printf("%s\r\n",Data_Length);
  42.                 printf("%s\r\n",Message_content);
  43.                
  44.                 for(i=0;i<Number_retries;i++)
  45.                 {
  46.                         SIM900A_Send_AT( "AT+CMGS=" ,0 );
  47.                         SIM900A_Send_AT( AT_CMGS ,1 );               
  48.                         
  49.                         if(USART2_Data_seek(USART2_Data ,">" ,500))
  50.                         {
  51.                                 SIM900A_Send_AT( PDU_Phone_number ,0 );
  52.                                 SIM900A_Send_AT( Data_Length ,0 );
  53.                                 SIM900A_Send_AT( Message_content ,0 );

  54.                                 USART_SendData( USART2, 0x1A );
  55.                                 SIM900A_Send_AT( 0 ,1 );
  56.                                 if(USART2_Data_seek(USART2_Data ,"ERROR" ,2000))
  57.                                 {
  58.                                         printf("发送失败\r\n");
  59.                                         return 0xFF;
  60.                                 }
  61.                                 printf("发送成功\r\n");
  62.                                 return 0x00;
  63.                         }
  64.                         else printf("AT+CMGS指令错误\r\n");
  65.                 }        
  66.         }
  67.         printf("指令错误\r\n");
  68.         return 0xFF;
  69. }
  70. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END



  71. //********************************************************************
  72. //通过串口2发送AT指令
  73. //可以直接发字符串或发数组
  74. //********************************************************************
  75. void SIM900A_Send_AT( char *AT_command ,char over )
  76. {
  77.         for(;*AT_command!='\0';AT_command++)
  78.         {
  79.                 while( USART_GetFlagStatus( USART2 , USART_FLAG_TXE ) == 0 );
  80.                 USART_SendData( USART2, *AT_command );
  81.         }
  82.         if( over )
  83.         {
  84.                 while( USART_GetFlagStatus( USART2 , USART_FLAG_TXE ) == 0 );
  85.                 USART_SendData( USART2, 0x0d );//  /r,0x0d,回车的作用只是移动光标至该行的起始位置
  86.                 while( USART_GetFlagStatus( USART2 , USART_FLAG_TXE ) == 0 );
  87.                 USART_SendData( USART2, 0x0a );//  /n,0x0a,换行至下一行行首起始位置;
  88.         }
  89.         while( USART_GetFlagStatus( USART2 , USART_FLAG_TXE ) == 0 );
  90. }
  91. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END



  92. //*******************************************************************
  93. //将手机号码转换为PDU格式
  94. //*******************************************************************
  95. void PDU_Transform__Phone_number(char *Phone_number_input)
  96. {
  97.         u8 i;
  98.         Phone_number_input[11] = 'F';
  99.         for(i =0 ;i<12 ;i++)
  100.         {
  101.                 if( i%2 == 0)
  102.                 {
  103.                         PDU_Phone_number[i+12] = Phone_number_input[i+1];
  104.                 }
  105.                 else
  106.                 {
  107.                         PDU_Phone_number[i+12] = Phone_number_input[i-1];
  108.                 }
  109.         }
  110.         Phone_number_input[11] = '\0';
  111. }
  112. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END



  113. //*******************************************************************
  114. //字符串内添加字符串
  115. //原字符串String
  116. //需要添加的字符串Add_String
  117. //第Add_location字之后添加
  118. //*******************************************************************
  119. void String_addition(char *String ,char *Add_String ,int Add_location)
  120. {
  121.         u8 i;
  122.         int length_String ,length_Add;
  123.         length_String = strlen(String);
  124.         length_Add = strlen(Add_String);
  125.         
  126.         String[ length_String+length_Add ] ='\0';
  127.         
  128.         for( ;length_String> Add_location ; length_String--)
  129.         {
  130.                 String[ length_String-1+length_Add ] = String[ length_String-1 ];
  131.         }
  132.         
  133.         for( ;length_Add>0 ;length_Add--)
  134.         {
  135.                 String[ Add_location-1+length_Add ] = Add_String[ length_Add-1 ];
  136.         }
  137. }
  138. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END</font>
复制代码

3.NRF24L01

  NRF24L01采用SPI进行通信,由从机发送信息,主机只进行接收信息,并对信息进行解析。

  1. <font size="3" color="#000000">**************************************
  2. //NRF24L01数据解析
  3. //********************************************************************
  4. void NRF24L01_Data_Parse(const char *data)
  5. {
  6.         u8 i,x;
  7.         char cache[8],slave;
  8.         if( data[0]=='K' )
  9.         {
  10.                 for(i=1,x=0; data[i]!='#'; i++)
  11.                 {
  12.                         switch(data[i])
  13.                         {        
  14.                                 case 'H' :                        for(i++,x=0 ;data[i]!='&' ;i++)        //没有结束
  15.                                                                         {
  16.                                                                                 cache[x] = data[i];
  17.                                                                                 x++;
  18.                                                                         };
  19.                                                                         cache[x]='\0';
  20.                                                                         slave = Transform_char_to_double(cache);
  21.                                                                         slave_disconnect[slave-1] = 0;
  22. //                                                                        printf("slave=%d\r\n",slave);
  23.                                                                         break;
  24.                                                                         
  25.                                 case 'V' :               
  26.                                                                         for(i++,x=0 ;data[i]!='&' ;i++)        //没有结束
  27.                                                                         {
  28.                                                                                 cache[x] = data[i];
  29.                                                                                 x++;
  30.                                                                         };
  31.                                                                         cache[x]='\0';
  32.                                                                         slave_unit_voltage[slave-1] = Transform_char_to_double(cache);
  33. //                                                                        printf("voltage=%lf\r\n",slave_unit_voltage[slave-1]);
  34.                                                                         break;
  35.                                 case 'A' :                        for(i++,x=0 ;data[i]!='&' ;i++)        //没有结束
  36.                                                                         {
  37.                                                                                 cache[x] = data[i];
  38.                                                                                 x++;
  39.                                                                         };
  40.                                                                         cache[x]='\0';
  41.                                                                         slave_unit_alcohol_concentration[slave-1] = Transform_char_to_double(cache);
  42. //                                                                        printf("alcohol_concentration=%d\r\n",slave_unit_alcohol_concentration[slave-1]);
  43.                                                                         break;
  44.                         }
  45.                                 memset(cache,NULL,strlen(cache));
  46.                 }        
  47.         }
  48. }
  49. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END



  50. //********************************************************************
  51. //接收到数据进入中断
  52. //分析接收到的数据
  53. //********************************************************************
  54. void EXTI15_10_IRQHandler()
  55. {
  56.         if(EXTI_GetITStatus(EXTI_Line12))
  57.         {
  58.                 char NRF_RX_Data[32];
  59.                 NRF24L01_RxPacket(NRF_RX_Data);
  60. //                printf("%s\r\n",NRF_RX_Data);
  61.                 NRF24L01_Data_Parse(NRF_RX_Data);
  62.         }
  63.         EXTI_ClearITPendingBit(EXTI_Line12);
  64. }
  65. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END</font>
复制代码

4.OLED显示

  OLED显示屏采用软件IIC进行数据通信,每200ms更新一次显示信息,每4s进行一次翻页,显示不同的设备状态信息。

  1. <font size="3" color="#000000">//********************************************************************
  2. //200ms进入一次中断
  3. //更新oled显示
  4. //********************************************************************
  5. void TIM1_TRG_COM_TIM11_IRQHandler()
  6. {
  7.         if(TIM_GetITStatus(TIM11, TIM_IT_Update))
  8.         {
  9.                 extern char PILOT;
  10.                 static int Enter_number =0,Page =0 ,Canned_format=0,aaa=0;
  11.                 Page = Enter_number/20 ;//控制翻页时间200*20ms
  12.                
  13.                 //********************************************************************
  14.                 //第一页信息
  15.                 //GPS状态
  16.                 //********************************************************************
  17.                 if( Page ==0 )        
  18.                 {
  19.                         extern char GPS_positioning;
  20.                         extern char longitude_char[15],latitude_char[15];

  21.                         if( Canned_format/10 != 1)
  22.                         {
  23.                                 OLED_CLS();
  24.                                 OLED_P8x16Str(36,0,"GPS",F8x16);
  25.                                 OLED_P16x16Str(60,0,"状态",F16x16_Idx,F16x16);
  26.                                 OLED_P6x8Str(122,0,"1",F6x8);
  27.                                 
  28.                                 Canned_format = 10;
  29.                         }

  30.                         if( GPS_positioning == 0)
  31.                         {
  32.                                 if(Canned_format%10 != 1 )
  33.                                 {
  34.                                         OLED_P8x16Str(0,24,"GPS",F8x16);
  35.                                         OLED_P16x16Str(24,24,"定位失败",F16x16_Idx,F16x16);
  36.                                         Canned_format = 11;                                
  37.                                 }
  38.                         }
  39.                         else if( GPS_positioning == 1)
  40.                         {
  41.                                 if(Canned_format%10 != 2 )
  42.                                 {
  43.                                         OLED_CLS_y(16);
  44.                                         OLED_CLS_y(24);
  45.                                         OLED_CLS_y(32);
  46.                                         OLED_P8x16Str(0,16,"GPS",F8x16);
  47.                                         OLED_P16x16Str(24,16,"定位成功",F16x16_Idx,F16x16);        
  48.                                         Canned_format = 12;
  49.                                 }
  50.                                 OLED_P8x16Str(0,32,longitude_char,F8x16);
  51.                                 OLED_P8x16Str(0,48,latitude_char,F8x16);
  52.                         }
  53.                         else if( GPS_positioning == 2)
  54.                         {
  55.                                 if(Canned_format%10 != 3 )
  56.                                 {
  57.                                         OLED_CLS_y(16);
  58.                                         OLED_CLS_y(24);
  59.                                         OLED_P8x16Str(0,16,"GPS",F8x16);
  60.                                         OLED_P16x16Str(24,16,"信号弱",F16x16_Idx,F16x16);
  61.                                         OLED_P8x16Str(0,32,longitude_char,F8x16);
  62.                                         OLED_P8x16Str(0,48,latitude_char,F8x16);
  63.                                         Canned_format = 13;
  64.                                 }
  65.                         }
  66.                 }
  67.                 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END
  68.                
  69.                
  70.                
  71.                 //********************************************************************
  72.                 //第二页信息
  73.                 //设备连接情况
  74.                 //********************************************************************
  75.                 else if( Page ==1 )
  76.                 {
  77.                         extern u8 slave_disconnect[3];
  78.                         
  79.                         if( Canned_format/10 != 2)
  80.                         {
  81.                                 
  82.                                 OLED_CLS();
  83.                                 OLED_P16x16Str(16,0,"设备连接",F16x16_Idx,F16x16);
  84.                                 OLED_P16x16Str(80,0,"状态",F16x16_Idx,F16x16);
  85.                                 OLED_P6x8Str(122,0,"2",F6x8);

  86.                                 OLED_P16x16Str(0,16,"设备",F16x16_Idx,F16x16);
  87.                                 OLED_P8x16Str(32,16," 1 ",F8x16);
  88.                                 OLED_P16x16Str(0,32,"设备",F16x16_Idx,F16x16);
  89.                                 OLED_P8x16Str(32,32," 2 ",F8x16);
  90.                                 OLED_P16x16Str(0,48,"设备",F16x16_Idx,F16x16);
  91.                                 OLED_P8x16Str(32,48," 3 ",F8x16);
  92.                                 
  93.                                 Canned_format = 20;
  94.                         }
  95.                         
  96.                         if( slave_disconnect[0] == 100)
  97.                         {
  98.                                 if(Canned_format%10 != 1 )
  99.                                 {
  100.                                         OLED_P16x16Str(56,16,"断开连接",F16x16_Idx,F16x16);
  101.                                         Canned_format = 21;                                
  102.                                 }
  103.                         }
  104.                         else
  105.                         {
  106.                                 if(Canned_format%10 != 2 )
  107.                                 {
  108.                                         OLED_P16x16Str(56,16,"连接成功",F16x16_Idx,F16x16);
  109.                                         Canned_format = 22;        
  110.                                 }
  111.                         }
  112.                         
  113.                         if( slave_disconnect[1] == 100)
  114.                         {
  115.                                 if(Canned_format%10 != 3 )
  116.                                 {
  117.                                         OLED_P16x16Str(56,32,"断开连接",F16x16_Idx,F16x16);
  118.                                         Canned_format = 23;                                
  119.                                 }
  120.                         }
  121.                         else
  122.                         {
  123.                                 if(Canned_format%10 != 4 )
  124.                                 {
  125.                                         OLED_P16x16Str(56,32,"连接成功",F16x16_Idx,F16x16);
  126.                                         Canned_format = 24;        
  127.                                 }
  128.                         }
  129.                         
  130.                         if( slave_disconnect[2] == 100)
  131.                         {
  132.                                 if(Canned_format%10 != 5 )
  133.                                 {
  134.                                         OLED_P16x16Str(56,48,"断开连接",F16x16_Idx,F16x16);
  135.                                         Canned_format = 25;                                
  136.                                 }
  137.                         }
  138.                         else
  139.                         {
  140.                                 if(Canned_format%10 != 6 )
  141.                                 {
  142.                                         OLED_P16x16Str(56,48,"连接成功",F16x16_Idx,F16x16);
  143.                                         Canned_format = 26;        
  144.                                 }
  145.                         }        
  146.                 }
  147.                 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END
  148.                
  149.                
  150.                
  151.                 //********************************************************************
  152.                 //第三页信息
  153.                 //电量
  154.                 //********************************************************************
  155.                 else if( Page ==2 )
  156.                 {
  157.                         extern double voltage;
  158.                         extern double slave_unit_voltage[3];
  159.                         
  160.                         char OLED_voltage[4][6];
  161.                         
  162.                         if( Canned_format/10 != 3)
  163.                         {
  164.                                 OLED_CLS();
  165.                                 OLED_P16x16Str(48,0,"电量",F16x16_Idx,F16x16);
  166.                                 OLED_P6x8Str(122,0,"3",F6x8);

  167.                                 OLED_P8x16Str(0,24,        "M:    V",F8x16);                                
  168.                                 OLED_P8x16Str(72,24,"S1:   V",F8x16);
  169.                                 OLED_P8x16Str(0,48,        "S2:   V",F8x16);
  170.                                 OLED_P8x16Str(72,48,"S3:   V",F8x16);        
  171.                                 
  172.                                 Canned_format = 30;
  173.                         }
  174.                         Transform_double_to_char( voltage , OLED_voltage[0], 1, 1);
  175.                         Transform_double_to_char( slave_unit_voltage[0] , OLED_voltage[1], 1, 1);
  176.                         Transform_double_to_char( slave_unit_voltage[1] , OLED_voltage[2], 1, 1);
  177.                         Transform_double_to_char( slave_unit_voltage[2] , OLED_voltage[3], 1, 1);
  178.                         
  179.                         OLED_P8x16Str(16,24,OLED_voltage[0],F8x16);
  180.                         OLED_P8x16Str(94,24,OLED_voltage[1],F8x16);
  181.                         OLED_P8x16Str(24,48,OLED_voltage[2],F8x16);
  182.                         OLED_P8x16Str(94,48,OLED_voltage[3],F8x16);
  183.                 }
  184.                 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END
  185.                
  186.                
  187.                
  188.                 //********************************************************************
  189.                 //第四页信息
  190.                 //酒精浓度
  191.                 //********************************************************************
  192.                 else if( Page ==3 )
  193.                 {
  194.                         extern short int alcohol_concentration;
  195.                         extern short int slave_unit_alcohol_concentration[3];
  196.                         
  197.                         char OLED_alcohol_concentration[4][6];
  198.                         
  199.                         if( Canned_format/10 != 4)
  200.                         {
  201.                                 OLED_CLS();
  202.                                 OLED_P16x16Str(32,0,"酒精浓度",F16x16_Idx,F16x16);
  203.                                 OLED_P6x8Str(122,0,"3",F6x8);
  204.                                 
  205.                                 OLED_P8x16Str(0,24,"A1:",F8x16);
  206.                                 OLED_P8x16Str(64,24,"A2:",F8x16);
  207.                                 OLED_P8x16Str(0,48,"A3:",F8x16);
  208.                                 OLED_P8x16Str(64,48,"A4:",F8x16);
  209.                                 
  210.                                 Canned_format = 40;
  211.                         }
  212.                         
  213.                         Transform_double_to_char( alcohol_concentration , OLED_alcohol_concentration[0], 0, 1);
  214.                         Transform_double_to_char( slave_unit_alcohol_concentration[0] , OLED_alcohol_concentration[1], 0, 1);
  215.                         Transform_double_to_char( slave_unit_alcohol_concentration[1] , OLED_alcohol_concentration[2], 0, 1);
  216.                         Transform_double_to_char( slave_unit_alcohol_concentration[2] , OLED_alcohol_concentration[3], 0, 1);
  217.                         
  218.                         
  219.                         OLED_P8x16Str(48,24," ",F8x16);
  220.                         OLED_P8x16Str(112,24," ",F8x16);
  221.                         OLED_P8x16Str(48,48," ",F8x16);
  222.                         OLED_P8x16Str(112,48," ",F8x16);
  223.                         
  224.                         OLED_P8x16Str(24,24,OLED_alcohol_concentration[0],F8x16);
  225.                         OLED_P8x16Str(88,24,OLED_alcohol_concentration[1],F8x16);
  226.                         OLED_P8x16Str(24,48,OLED_alcohol_concentration[2],F8x16);
  227.                         OLED_P8x16Str(88,48,OLED_alcohol_concentration[3],F8x16);
  228.                         
  229.                 }
  230.                 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END
  231.                
  232.                
  233.                
  234.                 //********************************************************************
  235.                 //第五页信息
  236.                 //
  237.                 //********************************************************************               
  238.                 else if( Page ==4 )
  239.                 {
  240.                         extern char Phone_number[4][12];
  241.                         if( Canned_format/10 != 5)
  242.                         {
  243.                                 OLED_CLS();
  244.                                 OLED_P16x16Str(44,0,"联系人",F16x16_Idx,F16x16);        
  245.                                 OLED_P6x8Str(122,0,"5",F6x8);
  246.                                 
  247.                                 OLED_P8x16Str(0,16,"C1:",F8x16);
  248.                                 OLED_P8x16Str(24,16,Phone_number[0],F8x16);
  249.                                 OLED_P8x16Str(0,32,"C2:",F8x16);
  250.                                 OLED_P8x16Str(24,32,Phone_number[1],F8x16);
  251.                                 OLED_P8x16Str(0,48,"C3:",F8x16);
  252.                                 OLED_P8x16Str(24,48,Phone_number[2],F8x16);
  253.                                 
  254.                                 Canned_format = 50;
  255.                         }
  256.                 }
  257.                 //********************************************************************
  258.                 //第六页信息
  259.                 //车辆状态
  260.                 //********************************************************************
  261.                 else if( Page ==5 )//第一页
  262.                 {
  263.                         extern char DRUNK_DRIVING ;
  264.                         if( Canned_format/10 != 6)
  265.                         {
  266.                                 OLED_CLS();
  267.                                 OLED_P16x16Str(32,0,"车辆状态",F16x16_Idx,F16x16);        
  268.                                 OLED_P6x8Str(122,0,"4",F6x8);
  269.                                 
  270.                                 Canned_format = 60;        
  271.                         }
  272.                         
  273.                         if(DRUNK_DRIVING==0)
  274.                         {
  275.                                 if( Canned_format%10 != 1)
  276.                                 {
  277.                                         OLED_CLS_y(16);
  278.                                         OLED_CLS_y(24);
  279.                                         OLED_CLS_y(32);
  280.                                         OLED_CLS_y(48);
  281.                                         OLED_CLS_y(56);
  282.                                         OLED_P16x16Str(16,32,"车辆正常行驶",F16x16_Idx,F16x16);        
  283.                                         Canned_format = 61;
  284.                                 }
  285.                         }
  286.                         else
  287.                         {
  288.                                 if( Canned_format%10 != 2)
  289.                                 {
  290.                                         OLED_CLS_y(16);
  291.                                         OLED_CLS_y(24);
  292.                                         OLED_CLS_y(32);
  293.                                         OLED_CLS_y(48);
  294.                                         OLED_CLS_y(56);
  295.                                         OLED_P16x16Str(8,24,"发现驾驶员酒驾",F16x16_Idx,F16x16);
  296.                                         OLED_P16x16Str(16,48,"车辆禁止行驶",F16x16_Idx,F16x16);        
  297.                                         Canned_format = 62;
  298.                                 }
  299.                         }        
  300.                 }               
  301.                
  302.                 //********************************************************************
  303.                 //重新开始显示第一页
  304.                 //********************************************************************
  305.                 else if(Page==6)
  306.                 {
  307.                         Enter_number =0;
  308.                         Canned_format=0;
  309.                 }
  310.                 Enter_number++;
  311.         }
  312.         TIM_ClearITPendingBit(TIM11, TIM_IT_Update);
  313. }
  314. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END</font>
复制代码


5.酒精传感器

  酒精传感器使用ADC+DMA进行数据采集,每50ms进行一次ADC采集,每次采集20个数据,对这20个数据取平均值,获得一次酒精传感器的测量值。每100ms进入一次定时器中断,将主机采集的酒精传感器测量值与其余从机采集的酒精传感器测量值进行比较,从而判断驾驶员是否酒驾。

  1. <font size="3" color="#000000">//********************************************************************
  2. //100ms进入一次中断
  3. //判断是否酒驾
  4. //********************************************************************
  5. void TIM1_BRK_TIM9_IRQHandler()
  6. {
  7.         extern short int alcohol_concentration;
  8.         extern short int slave_unit_alcohol_concentration[3];
  9.         extern char DRUNK_DRIVING;
  10.         extern u8 voice_prompt[15] ;
  11.         static u8 Number_of_drunk_driving= 0;
  12.         if(TIM_GetITStatus(TIM9, TIM_IT_Update))
  13.         {
  14.                 double Weight_M=1 ,Weight_S1=1 ,Weight_S2=1 ,Weight_S3=1 ;
  15.                 if(        alcohol_concentration > 2000 || DRUNK_DRIVING == 1)//当主传感器大于规定值开始判断
  16.                 {
  17.                         //*****插入算法判断是否酒驾,之后需要修改
  18.                         if(Weight_M * alcohol_concentration > (Weight_S1*slave_unit_alcohol_concentration[0]+
  19.                                                                                                                                     Weight_S2*slave_unit_alcohol_concentration[1] +
  20.                                                                                                                                    Weight_S3*slave_unit_alcohol_concentration[2] +500) /3;
  21.                           )
  22.                         {
  23.                                 Number_of_drunk_driving++;
  24.                         }
  25.                         else if(Number_of_drunk_driving!=0)
  26.                         {
  27.                                 Number_of_drunk_driving--;
  28.                         }
  29.                         //>>>>>>>>>>>>>>>>>>>>>>>END
  30.                         
  31.                         //*****是否连续10次判定为酒驾
  32.                         if(Number_of_drunk_driving>10)
  33.                         {
  34.                                 Number_of_drunk_driving = 10;
  35.                                 DRUNK_DRIVING = 1;
  36.                                 if( voice_prompt[3] == 0 )
  37.                                 {
  38.                                         JQ6500_play(4);
  39.                                         voice_prompt[3] = 1;
  40.                                         voice_prompt[4] = 0;
  41.                                 }
  42.                         }
  43.                         else if(DRUNK_DRIVING == 1 && Number_of_drunk_driving ==0)
  44.                         {
  45.                                 DRUNK_DRIVING = 0;
  46.                                 if( voice_prompt[4] == 0 && voice_prompt[3] == 1)
  47.                                 {
  48.                                         JQ6500_play(5);
  49.                                         voice_prompt[4] = 1;
  50.                                         voice_prompt[3] = 0;
  51.                                 }
  52.                         }
  53.                         //>>>>>>>>>>>>>>>>>>>>>>>END
  54.                 }               
  55.         }
  56.         TIM_ClearITPendingBit(TIM9, TIM_IT_Update);
  57. }
  58. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END</font>
复制代码

6.语音模块

  语音模块采用UART4进行通信,波特率为9600.通过UART4发送16进制指令对JQ6500语音模块进行控制。

  1. <font size="3" color="#000000">*****************************************************
  2. //JQ6500发送控制指令
  3. //********************************************************************
  4. void JQ6500_control_command(char command)
  5. {
  6.         
  7.         char JQ6500_instruct_1[5]={0x7E ,0x03 ,0x06 ,0x02 ,0xEF};                //音量00~1E
  8.         char JQ6500_instruct_2[5]={0x7E ,0x03 ,0x06 ,0x17 ,0xEF};                //音量00~1E
  9.         char JQ6500_instruct_3[5]={0x7E ,0x03 ,0x06 ,0x1E ,0xEF};                //音量00~1E
  10.         
  11.         char JQ6500_instruct_4[5]={0x7E ,0x03 ,0x11 ,0x04 ,0xEF};         //播放模式:0 1 2 3 4(ALL FOL ONE RAM ONE_STOP)
  12.         
  13.         char JQ6500_instruct_5[4]={0x7E ,0x02 ,0x0A ,0xEF};                                //低功耗
  14.         char JQ6500_instruct_6[4]={0x7E ,0x02 ,0x0C ,0xEF};                                //复位
  15.         

  16.         switch (command)
  17.         {
  18.                 case 1:Send_hexadecimal_UART4(JQ6500_instruct_1);break;
  19.                 case 2:Send_hexadecimal_UART4(JQ6500_instruct_2);break;
  20.                 case 3:Send_hexadecimal_UART4(JQ6500_instruct_3);break;
  21.                 case 4:Send_hexadecimal_UART4(JQ6500_instruct_4);break;
  22.                 case 5:Send_hexadecimal_UART4(JQ6500_instruct_5);break;
  23.                 case 6:Send_hexadecimal_UART4(JQ6500_instruct_6);break;
  24.                
  25.                 default: break;
  26.         }
  27.         delay_ms(50);
  28. }
  29. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END



  30. //********************************************************************
  31. //JQ6500语音模块播放指定曲目
  32. //********************************************************************
  33. void JQ6500_play(u16 number)
  34. {
  35.         char JQ6500_play_number[6]={0x7E ,0x04 ,0x03 ,0x00 ,0x00 ,0xEF};                //音量00~1E
  36.         JQ6500_play_number[4] = (u8)number;
  37.         JQ6500_play_number[3] = (u8)(number>>8);
  38.         
  39.         while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_12)==1);
  40.         while((GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_12)==0))
  41.         {
  42.                 Send_hexadecimal_UART4(JQ6500_play_number);delay_ms(100);
  43.         }
  44.         while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_12)==1);
  45. }
  46. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END



  47. //********************************************************************
  48. //UART4发送16进制数据
  49. //********************************************************************
  50. void Send_hexadecimal_UART4( char *array )
  51. {
  52.         for( ;*array!=0Xff ;array++)
  53.         {
  54.                 while( USART_GetFlagStatus( UART4 , USART_FLAG_TXE ) == 0 );
  55.                 USART_SendData( UART4, *array );
  56.                 while( USART_GetFlagStatus( UART4 , USART_FLAG_TXE ) == 0 );
  57.         }
  58. }
  59. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>END
  60. </font>
复制代码


从机部分

  从机采用HAL库进行编程


1.NRF24L01

  每次完成一次DMA传输后进行一次数据打包,在主函数中进行检测,如果数据准备好了,就启动一次数据的传输。


  1. <font size="3" color="#000000">void DMA1_Channel1_IRQHandler(void)
  2. {
  3.         u8 i;
  4.         double voltage;
  5.         int alcohol_concentration;
  6.         char Transform[10],data_head[5]={"KH1&"};
  7.         u32 ADC_data1=0,ADC_data2=0;
  8.         for(i=0;i<20;i++)
  9.         {
  10.                 ADC_data1 += DMA_ADC_DATA[i][0];
  11.                 ADC_data2 += DMA_ADC_DATA[i][1];
  12.         }
  13.         alcohol_concentration = (double)(ADC_data1/20)+0.5;
  14.         voltage = (double)(ADC_data2/20)/4096*3.3*(3-0.025)+0.005;
  15.         
  16.         if(NRF24l01_write_TXdata == 0)
  17.         {
  18.                 memset(NRF_TX_Data,NULL,strlen(NRF_TX_Data));        //清空要发送的数据
  19.                 strcat(NRF_TX_Data, data_head);                                        //发送数据加数据头

  20.                 NRF_TX_Data[strlen(NRF_TX_Data)] = 'V';                        //添加第一段数据
  21.                 Transform_double_to_char(voltage, Transform, 2, 1);
  22.                 strcat(NRF_TX_Data, Transform);
  23.                 NRF_TX_Data[strlen(NRF_TX_Data)] = '&';
  24.                
  25.                 NRF_TX_Data[strlen(NRF_TX_Data)] = 'A';                        //添加第二段数据
  26.                 Transform_double_to_char(alcohol_concentration, Transform, 0, 1);
  27.                 strcat(NRF_TX_Data, Transform);
  28.                 NRF_TX_Data[strlen(NRF_TX_Data)] = '&';
  29.                
  30.                 NRF_TX_Data[strlen(NRF_TX_Data)] = '#';                        //添加结束符
  31.         }
  32. //        printf("%s\r\n",NRF_TX_Data);
  33.         
  34.         HAL_DMA_IRQHandler(&hdma_adc);

  35.         HAL_ADC_Stop_DMA(&hadc);
  36. }</font>
复制代码

四、实物展示
20200907145407605.jpg

20200907145419644.jpg



五、改进方向

1.酒精传感器

  刚开始使用MQ-3作为酒精传感器,测量灵敏度不足,所以采用电桥电路,并使用运放将信号进行放大。但之后更换TGS2620作为敏感元件,灵敏度大大增加,只使用简单的电阻分压电路就可以得到较为准确的测量值。目前的电桥电路设计也有一些问题,由于调零电阻的关系,导致同样酒精浓度变化下传感器测量值的变化灵敏度不一致,如果想使用电桥电路获得更精准的测量数据,需要改进电桥电路的调零部分。


2.短信

  当前的GSM模块只可以发送经纬度数据,需要用户自己使用谷歌地球进行位置查询。今后可以使用GPRS数据传输与阿里云等平台进行网络连接,通过一些高德地图提供的API进行对经纬度进行逆地理编码,从而获得当前具体的所在位置。也可以使用微信小程序,直接查看当前所在位置的地图。


3.加入键盘

  主机的PCB电路板已经预留了矩阵键盘的接口,加入键盘后可对联系人进行设置,OLED显示信息进行翻页等操作。如果使用了微信小程序,可在小程序上完成对联系人进行设置的功能,省略键盘。


4.加入人体检测

  开始计划使用红外热释电传感器检测是否车内有人,当有人驾驶时,系统开始检测酒驾行为。无人驾驶时进入低功耗模式。但由于红外热释电只能检测运动的物体,无法精准检测人体活动,容易造成误检测。之后可以通过判断是否有启动车辆行为来决定系统是否进入工作模式,或采用其他模块检测人体活动。







收藏 1 评论1 发布时间:2020-9-11 12:54

举报

1个回答
乐天乐 回答时间:2020-9-11 13:40:49
有完整项目更佳

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

相似技术帖

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
微信公众号二维码 微信公众号
手机版二维码 手机版