找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3996|回复: 1
收起左侧

基于SIM900A的电池自动满电提醒及实时查询电量装置 附单片机源码与仿真

[复制链接]
ID:372961 发表于 2018-10-10 16:32 | 显示全部楼层 |阅读模式
作品:基于SIM900A的电池自动满电提醒及实时查询电量装置,附件中含有SIM900A参考资料,源码,仿真图等

proteus仿真原理图:仿真图中不包含sim900A模块,7805模块可以将输入电压在6-36V之间的电压稳定输出为5V,作为ADC0832的电压源以及参考电压。
微信图片_20181010160548.png


实物图:测量的电池(核定电压在12V)电量为10V左右,14.4V为满电浮充状态(以14.4V为满电标准,信息显示电满),此时10V,则显示少于90%电量。
微信图片_20181010162209.jpg

微信图片_20181010162205.jpg
微信图片_20181010162352.png


单片机主函数代码:
  1. /*******************************************************************************
  2. *  标题:        基于SIM900A的自动充电提醒以及实时查询电量装置                  *
  3. *  时间                      2018年10月10日16:12:42                            *
  4. *                                                                              *
  5. *  实验说明:本装置的核心是SIM900A模块,需要调用SIM900A指令。本原理图中ADC0832 *
  6. 的参考电压为5V,由7805稳压源提供7805的电源以及参考电压。检测电池的电压在9-15V之 *
  7. 间,采用分压方式,一个30K,一个10K电阻进行分压。假如检测电压为15V,则输入ADC0832 *
  8. 的电压为5V。依次类推。                                                                                                                   *
  9.                                                                                *
  10. ********************************************************************************
  11. * 实验心得:SIM900A是个难点,需要在电脑上进行调试。ADC0832进行AD转化的时候,需 *
  12. 要研究下时序图,在进行数据检测的时候需要进行两次检测,如果前后两次数据相等,那 *
  13. 么再返回得到的值,减少了测量误差。7805稳压源是个重要的部件,为了提供精确稳定的 *
  14. 5V参考电压                                                                                                                                       *
  15. ********************************************************************************/

  16. #include <reg51.h>
  17. #include "UART.h"
  18. #include "DELAY.h"
  19. #include "define.h"
  20. #include "ADC0832.h"

  21. //全局变量
  22. #define Yes                1
  23. #define No                0
  24. typedef unsigned int u16;
  25. typedef unsigned char u8;
  26. unsigned char number[11]={'0','0','0','0','0','0','0','0','0','0','0'};
  27. unsigned char Voltage_bit;                //对电压进行AD转化后的数字信号,5V为参考电压,此时Voltage_bit=256;
  28. float Percentage;                //电量百分比,Percentage = Voltage/256;

  29. /********测试GSM是否启动**********/
  30. int test_boot;
  31. /********测试GSM是否注册网络**********/
  32. int test_net_register;
  33. /********测试是否有电话打入**********/
  34. int test_have_call;
  35. /********测试是否有信息发入**********/
  36. int test_have_SM;
  37. /********GSM串口接收数据缓存**********/
  38. unsigned char GSM_receive[60];      
  39. /********GSM串口接收计数器**********/
  40. unsigned char GSMDATA_count;
  41. /********号码缓存*******************/
  42. /********短信发送程序********/

  43. //IO口定义
  44. sbit K1 = P2^0;
  45. sbit K2 = P2^1;
  46. sbit Powerkey = P2^2;
  47. sbit beep = P1^5;

  48. //短信发送程序
  49. void message()
  50. {
  51.         Send_String("AT+CMGF=1\r\n");//指定信息的输入输出格式为文本格式
  52.         Delay_Ms(100);
  53.         
  54.         Send_String("AT+CSMP=17,167,2,25\r\n");                                 //
  55.         Delay_Ms(100);
  56.         
  57.         Send_String("AT+CSCS=\"UCS2\"\r\n");                                 //设置为 UCS2 字符集编码
  58.         Delay_Ms(100);
  59.         
  60.         Send_String("AT+CMGS=\"00310038003800380038003600340035003000300033\"\r\n");           //号码18888645003的Unicode编码,0031 0038 0038 0038 0038 0036 0034 0035 0030 0030 0033
  61.         Delay_Ms(100);
  62.         
  63.         Send_String("4E3B4EBAFF0C53C889C197625566\r\n");                                                                   //主人,又见面啦          4E3B 4EBA FF0C 53C8 89C1 9762 5566
  64.         Delay_Ms(100);        
  65.         Send_Char(0x1A);                                                                                                                                   //结束命令符号
  66.         Delay_Ms(200);
  67. }

  68. void Phonemessage()
  69. {
  70.         Send_String("AT+CMGF=1\r\n");//指定信息的输入输出格式为文本格式
  71.         Delay_Ms(100);
  72.         
  73.         Send_String("AT+CSMP=17,167,2,25\r\n");                                 //
  74.         Delay_Ms(100);
  75.         
  76.         Send_String("AT+CSCS=\"UCS2\"\r\n");                                 //设置为 UCS2 字符集编码
  77.         Delay_Ms(100);
  78.         
  79.         Send_String("AT+CMGS=\"00310038003800380038003600340035003000300033\"\r\n");           //号码18888645003的Unicode编码,0031 0038 0038 0038 0038 0036 0034 0035 0030 0030 0033
  80.         Delay_Ms(100);
  81.         
  82.         Send_String("4E3B4EBAFF0C62116536523075358BDD5566\r\n");                                                                   //主人,我收到电话啦 4E3B 4EBA FF0C 6211 6536 5230 7535 8BDD 5566
  83.         Delay_Ms(100);        
  84.         Send_Char(0x1A);                                                                                                                                   //结束命令符号
  85.         Delay_Ms(200);
  86. }

  87. void othermessage()
  88. {
  89.         Send_String("AT+CMGF=1\r\n");//指定信息的输入输出格式为文本格式
  90.         Delay_Ms(100);
  91.         
  92.         Send_String("AT+CSMP=17,167,2,25\r\n");                                 //
  93.         Delay_Ms(100);
  94.         
  95.         Send_String("AT+CSCS=\"UCS2\"\r\n");                                 //设置为 UCS2 字符集编码
  96.         Delay_Ms(100);
  97.         
  98.         Send_String("AT+CMGS=\"00310038003800380038003600340035003000300033\"\r\n");           //号码18888645003的Unicode编码,0031 0038 0038 0038 0038 0036 0034 0035 0030 0030 0033
  99.         Delay_Ms(100);
  100.         
  101.         /*Send_String("00560065006E00630065002C6211653652304F60768477ED4FE15566\r\n");                                                                   //主人,我收到电话啦 4E3B 4EBA FF0C 6211 6536 5230 7535 8BDD 5566
  102.         Delay_Ms(100);        
  103.         Send_Char(0x1A);                                                                                                                                   //结束命令符号
  104.         Delay_Ms(200);*/
  105. }

  106. void delay(u16 i)
  107. {
  108.         while(i--);      
  109. }

  110. void main()
  111. {
  112.         unsigned char j=0;
  113.         test_boot = No;
  114.         Powerkey = 0;
  115.         test_have_call=No;
  116.         test_have_SM = No;
  117.         //GSM启动
  118.         Powerkey=1;
  119.         Delay_Ms(1000);                                //系统重启在开机和关机之间的间隔要大于800ms.
  120.         Powerkey=0;
  121.         Uart_Init();
  122.         while(test_boot==No)
  123.         {
  124.                 Send_String("AT\r\n");                 //如果开机成功,则会返回一个OK,进入串口中断
  125.                 Delay_Ms(1000);
  126.         }
  127.         Send_String("AT+CLIP=1\r\n");
  128.         Delay_Ms(100);
  129.          while(1)
  130.          {
  131.                 Voltage_bit = ADC0832(0);                        //从ADC转化通道0进入,模数转化后的值为Voltage_bit;
  132.                 Percentage = (float)Voltage_bit/256;        //电量百分比转化语句
  133.                 j = 0;
  134.                 if(K1==0)
  135.                 {
  136.                         while(j==0)
  137.                         {
  138.                                 message();
  139.                                 j=1;
  140.                         }
  141.                         
  142.                 }

  143.                 if(K2==0)
  144.                 {
  145.                         while(j==0)
  146.                         {
  147.                                 othermessage();
  148.                                 j=1;
  149.                         }        
  150.                 }
  151.                 while(test_have_call==Yes)
  152.                 {
  153.                         if(number[10]=='3'&&number[9]=='0'&&number[8]=='0'&&number[7]=='5')
  154.                         Phonemessage();
  155.                         break;
  156.                 }
  157.                
  158.                 if(test_have_SM == Yes)
  159.                 {
  160.                          //Send_String("AT+CMGR=1\r\n");
  161.                          //if(GSM_receive[69]=='7'&&GSM_receive[70]=='5'&&GSM_receive[71]=='3'&&GSM_receive[72]=='5')
  162.                          //{
  163.                                   Send_String("AT+CMGD=1\r\n");
  164.                                  if(Percentage<=0.1)
  165.                                  {
  166.                                          othermessage();
  167.                                          Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003100300025\r\n");                                                                   //Vence,电量少于10%
  168.                                          Delay_Ms(100);        
  169.                                          Send_Char(0x1A);                                                                                                                                   //结束命令符号
  170.                                          Delay_Ms(200);
  171.                                          test_have_SM = No;
  172.                                          
  173.                                  }
  174.         
  175.                                  if(Percentage>0.1&&Percentage<=0.2)
  176.                                  {
  177.                                           othermessage();
  178.                                          Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003200300025\r\n");                                                                   //Vence,电量少于20%
  179.                                          Delay_Ms(100);        
  180.                                          Send_Char(0x1A);                                                                                                                                   //结束命令符号
  181.                                          Delay_Ms(200);
  182.                                          test_have_SM = No;
  183.                                          Send_String("AT+CMGD=1,4\r\n");
  184.                                  }
  185.         
  186.                                   if(Percentage>0.2&&Percentage<=0.3)
  187.                                  {
  188.                                           othermessage();
  189.                                          Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003300300025\r\n");                                                                   //Vence,电量少于30%
  190.                                          Delay_Ms(100);        
  191.                                          Send_Char(0x1A);                                                                                                                                   //结束命令符号
  192.                                          Delay_Ms(200);
  193.                                          test_have_SM = No;
  194.                                          Send_String("AT+CMGD=1,4\r\n");
  195.                                  }
  196.         
  197.                                   if(Percentage>0.3&&Percentage<=0.4)
  198.                                  {
  199.                                           othermessage();
  200.                                          Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003400300025\r\n");                                                                   //Vence,电量少于40%
  201.                                          Delay_Ms(100);        
  202.                                          Send_Char(0x1A);                                                                                                                                   //结束命令符号
  203.                                          Delay_Ms(200);
  204.                                          test_have_SM = No;
  205.                                          Send_String("AT+CMGD=1,4\r\n");
  206.                                  }
  207.         
  208.                                   if(Percentage>0.4&&Percentage<=0.5)
  209.                                  {
  210.                                           othermessage();
  211.                                          Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003500300025\r\n");                                                                   //Vence,电量少于50%
  212.                                          Delay_Ms(100);        
  213.                                          Send_Char(0x1A);                                                                                                                                   //结束命令符号
  214.                                          Delay_Ms(200);
  215.                                          test_have_SM = No;
  216.                                          Send_String("AT+CMGD=1,4\r\n");
  217.                                  }
  218.         
  219.                                   if(Percentage>0.5&&Percentage<=0.6)
  220.                                  {
  221.                                           othermessage();
  222.                                          Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003600300025\r\n");                                                                   //Vence,电量少于60%
  223.                                          Delay_Ms(100);        
  224.                                          Send_Char(0x1A);                                                                                                                                   //结束命令符号
  225.                                          Delay_Ms(200);
  226.                                          test_have_SM = No;
  227.                                          Send_String("AT+CMGD=1,4\r\n");
  228.                                  }
  229.         
  230.                                   if(Percentage>0.6&&Percentage<=0.7)
  231.                                  {
  232.                                           othermessage();
  233.                                          Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003700300025\r\n");                                                                   //Vence,电量少于70%
  234.                                          Delay_Ms(100);        
  235.                                          Send_Char(0x1A);                                                                                                                                   //结束命令符号
  236.                                          Delay_Ms(200);
  237.                                          test_have_SM = No;
  238.                                          Send_String("AT+CMGD=1,4\r\n");
  239.                                  }
  240.         
  241.                                   if(Percentage>0.7&&Percentage<=0.8)
  242.                                  {
  243.                                           othermessage();
  244.                                          Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003800300025\r\n");                                                                   //Vence,电量少于80%
  245.                                          Delay_Ms(100);        
  246.                                          Send_Char(0x1A);                                                                                                                                   //结束命令符号
  247.                                          Delay_Ms(200);
  248.                                          test_have_SM = No;
  249.                                          Send_String("AT+CMGD=1,4\r\n");
  250.                                  }
  251.         
  252.                                   if(Percentage>0.8&&Percentage<=0.9)
  253.                                  {
  254.                                           othermessage();
  255.                                          Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003900300025\r\n");                                                                   //Vence,电量少于90%
  256.                                          Delay_Ms(100);        
  257.                                          Send_Char(0x1A);                                                                                                                                   //结束命令符号
  258.                                          Delay_Ms(200);
  259.                                          test_have_SM = No;
  260.                                          Send_String("AT+CMGD=1,4\r\n");
  261.                                  }
  262.         
  263.                                   if(Percentage>0.9&&Percentage<1)
  264.                                  {
  265.                                           othermessage();
  266.                                          Send_String("00560065006E00630065002C73B05728753591CF5C114E8E0031003000300025\r\n");                                                                   //Vence,电量少于100%
  267.                                          Delay_Ms(100);        
  268.                                          Send_Char(0x1A);                                                                                                                                   //结束命令符号
  269.                                          Delay_Ms(200);
  270.                                          test_have_SM = No;
  271.                                          Send_String("AT+CMGD=1,4\r\n");
  272.                                  }
  273.         
  274.                                  if(Percentage==1)
  275.                                  {
  276.                                           othermessage();
  277.                                          Send_String("00560065006E00630065002C73B05728753591CF4E3A0031003000300025\r\n");                                                                   //Vence,电量为100%
  278.                                          Delay_Ms(100);        
  279.                                          Send_Char(0x1A);                                                                                                                                   //结束命令符号
  280.                                          Delay_Ms(200);
  281.                                          test_have_SM = No;
  282.                                          Send_String("AT+CMGD=1,4\r\n");
  283.                                  }
  284.                          //}
  285.                         

  286.                 }               
  287.          }
  288. }

  289. /****************************************************************************/
  290. void uart(void) interrupt 4
  291. {
  292.         unsigned char UART_data;
  293.         unsigned char i;

  294.         if(RI)
  295.         {
  296.                 UART_data=SBUF;
  297.                 if(UART_data=='\n')
  298.                 {
  299.                         if(GSM_receive[0]=='O'&&GSM_receive[1]=='K')
  300.                                 test_boot=Yes;
  301.                         else
  302.                                 test_boot=No;
  303.                         
  304.                         if(GSM_receive[12]=='C')
  305.                                 test_net_register=Yes;
  306.                         else
  307.                                 test_net_register=No;     
  308.                         
  309.                         if(GSM_receive[0]=='R'&&GSM_receive[1]=='I'&&GSM_receive[2]=='N'&&GSM_receive[3]=='G')
  310.                         {
  311.                                 test_have_call=Yes;                                
  312.                         }
  313.                         else
  314.                         {
  315.                                 test_have_call=No;
  316.                         }
  317.                         
  318.                         if(GSM_receive[0]=='+'&&GSM_receive[1]=='C'&&GSM_receive[2]=='L'&&GSM_receive[3]=='I'&&GSM_receive[4]=='P')
  319.                         {
  320.                                 for(i=0;i<11;i++)
  321.                                 {
  322.                                         number[i]=GSM_receive[i+8];
  323.                                 }
  324.                                 //display_number(line_2,0x00);        
  325.                                 
  326.                                 
  327.                         }
  328.                         if(GSM_receive[0]=='+'&&GSM_receive[1]=='C'&&GSM_receive[2]=='M'&&GSM_receive[3]=='T'&&GSM_receive[4]=='I')
  329.                         {
  330.                                  test_have_SM = Yes;
  331.                         }
  332.                         GSMDATA_count=0;
  333.                 }
  334.                 else
  335.                 {
  336.                         GSM_receive[GSMDATA_count]=UART_data;
  337.                         GSMDATA_count++;
  338.                 }
  339.         }
  340.         RI=0;
  341. }
复制代码

0.png 0.png

全部资料51hei下载地址:
SIM900A自动满电提醒装置.zip (8.4 MB, 下载次数: 49)

评分

参与人数 1黑币 +100 收起 理由
admin + 100 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:1 发表于 2018-10-10 17:08 | 显示全部楼层
好资料,51黑有你更精彩!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表