作品:基于SIM900A的电池自动满电提醒及实时查询电量装置,附件中含有SIM900A参考资料,源码,仿真图等
proteus仿真原理图:仿真图中不包含sim900A模块,7805模块可以将输入电压在6-36V之间的电压稳定输出为5V,作为ADC0832的电压源以及参考电压。
实物图:测量的电池(核定电压在12V)电量为10V左右,14.4V为满电浮充状态(以14.4V为满电标准,信息显示电满),此时10V,则显示少于90%电量。
单片机主函数代码:
- /*******************************************************************************
- * 标题: 基于SIM900A的自动充电提醒以及实时查询电量装置 *
- * 时间 2018年10月10日16:12:42 *
- * *
- * 实验说明:本装置的核心是SIM900A模块,需要调用SIM900A指令。本原理图中ADC0832 *
- 的参考电压为5V,由7805稳压源提供7805的电源以及参考电压。检测电池的电压在9-15V之 *
- 间,采用分压方式,一个30K,一个10K电阻进行分压。假如检测电压为15V,则输入ADC0832 *
- 的电压为5V。依次类推。 *
- *
- ********************************************************************************
- * 实验心得:SIM900A是个难点,需要在电脑上进行调试。ADC0832进行AD转化的时候,需 *
- 要研究下时序图,在进行数据检测的时候需要进行两次检测,如果前后两次数据相等,那 *
- 么再返回得到的值,减少了测量误差。7805稳压源是个重要的部件,为了提供精确稳定的 *
- 5V参考电压 *
- ********************************************************************************/
- #include <reg51.h>
- #include "UART.h"
- #include "DELAY.h"
- #include "define.h"
- #include "ADC0832.h"
- //全局变量
- #define Yes 1
- #define No 0
- typedef unsigned int u16;
- typedef unsigned char u8;
- unsigned char number[11]={'0','0','0','0','0','0','0','0','0','0','0'};
- unsigned char Voltage_bit; //对电压进行AD转化后的数字信号,5V为参考电压,此时Voltage_bit=256;
- float Percentage; //电量百分比,Percentage = Voltage/256;
- /********测试GSM是否启动**********/
- int test_boot;
- /********测试GSM是否注册网络**********/
- int test_net_register;
- /********测试是否有电话打入**********/
- int test_have_call;
- /********测试是否有信息发入**********/
- int test_have_SM;
- /********GSM串口接收数据缓存**********/
- unsigned char GSM_receive[60];
- /********GSM串口接收计数器**********/
- unsigned char GSMDATA_count;
- /********号码缓存*******************/
- /********短信发送程序********/
- //IO口定义
- sbit K1 = P2^0;
- sbit K2 = P2^1;
- sbit Powerkey = P2^2;
- sbit beep = P1^5;
- //短信发送程序
- void message()
- {
- Send_String("AT+CMGF=1\r\n");//指定信息的输入输出格式为文本格式
- Delay_Ms(100);
-
- Send_String("AT+CSMP=17,167,2,25\r\n"); //
- Delay_Ms(100);
-
- Send_String("AT+CSCS=\"UCS2\"\r\n"); //设置为 UCS2 字符集编码
- Delay_Ms(100);
-
- Send_String("AT+CMGS=\"00310038003800380038003600340035003000300033\"\r\n"); //号码18888645003的Unicode编码,0031 0038 0038 0038 0038 0036 0034 0035 0030 0030 0033
- Delay_Ms(100);
-
- Send_String("4E3B4EBAFF0C53C889C197625566\r\n"); //主人,又见面啦 4E3B 4EBA FF0C 53C8 89C1 9762 5566
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);
- }
- void Phonemessage()
- {
- Send_String("AT+CMGF=1\r\n");//指定信息的输入输出格式为文本格式
- Delay_Ms(100);
-
- Send_String("AT+CSMP=17,167,2,25\r\n"); //
- Delay_Ms(100);
-
- Send_String("AT+CSCS=\"UCS2\"\r\n"); //设置为 UCS2 字符集编码
- Delay_Ms(100);
-
- Send_String("AT+CMGS=\"00310038003800380038003600340035003000300033\"\r\n"); //号码18888645003的Unicode编码,0031 0038 0038 0038 0038 0036 0034 0035 0030 0030 0033
- Delay_Ms(100);
-
- Send_String("4E3B4EBAFF0C62116536523075358BDD5566\r\n"); //主人,我收到电话啦 4E3B 4EBA FF0C 6211 6536 5230 7535 8BDD 5566
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);
- }
- void othermessage()
- {
- Send_String("AT+CMGF=1\r\n");//指定信息的输入输出格式为文本格式
- Delay_Ms(100);
-
- Send_String("AT+CSMP=17,167,2,25\r\n"); //
- Delay_Ms(100);
-
- Send_String("AT+CSCS=\"UCS2\"\r\n"); //设置为 UCS2 字符集编码
- Delay_Ms(100);
-
- Send_String("AT+CMGS=\"00310038003800380038003600340035003000300033\"\r\n"); //号码18888645003的Unicode编码,0031 0038 0038 0038 0038 0036 0034 0035 0030 0030 0033
- Delay_Ms(100);
-
- /*Send_String("00560065006E00630065002C6211653652304F60768477ED4FE15566\r\n"); //主人,我收到电话啦 4E3B 4EBA FF0C 6211 6536 5230 7535 8BDD 5566
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);*/
- }
- void delay(u16 i)
- {
- while(i--);
- }
- void main()
- {
- unsigned char j=0;
- test_boot = No;
- Powerkey = 0;
- test_have_call=No;
- test_have_SM = No;
- //GSM启动
- Powerkey=1;
- Delay_Ms(1000); //系统重启在开机和关机之间的间隔要大于800ms.
- Powerkey=0;
- Uart_Init();
- while(test_boot==No)
- {
- Send_String("AT\r\n"); //如果开机成功,则会返回一个OK,进入串口中断
- Delay_Ms(1000);
- }
- Send_String("AT+CLIP=1\r\n");
- Delay_Ms(100);
- while(1)
- {
- Voltage_bit = ADC0832(0); //从ADC转化通道0进入,模数转化后的值为Voltage_bit;
- Percentage = (float)Voltage_bit/256; //电量百分比转化语句
- j = 0;
- if(K1==0)
- {
- while(j==0)
- {
- message();
- j=1;
- }
-
- }
- if(K2==0)
- {
- while(j==0)
- {
- othermessage();
- j=1;
- }
- }
- while(test_have_call==Yes)
- {
- if(number[10]=='3'&&number[9]=='0'&&number[8]=='0'&&number[7]=='5')
- Phonemessage();
- break;
- }
-
- if(test_have_SM == Yes)
- {
- //Send_String("AT+CMGR=1\r\n");
- //if(GSM_receive[69]=='7'&&GSM_receive[70]=='5'&&GSM_receive[71]=='3'&&GSM_receive[72]=='5')
- //{
- Send_String("AT+CMGD=1\r\n");
- if(Percentage<=0.1)
- {
- othermessage();
- Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003100300025\r\n"); //Vence,电量少于10%
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);
- test_have_SM = No;
-
- }
-
- if(Percentage>0.1&&Percentage<=0.2)
- {
- othermessage();
- Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003200300025\r\n"); //Vence,电量少于20%
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);
- test_have_SM = No;
- Send_String("AT+CMGD=1,4\r\n");
- }
-
- if(Percentage>0.2&&Percentage<=0.3)
- {
- othermessage();
- Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003300300025\r\n"); //Vence,电量少于30%
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);
- test_have_SM = No;
- Send_String("AT+CMGD=1,4\r\n");
- }
-
- if(Percentage>0.3&&Percentage<=0.4)
- {
- othermessage();
- Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003400300025\r\n"); //Vence,电量少于40%
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);
- test_have_SM = No;
- Send_String("AT+CMGD=1,4\r\n");
- }
-
- if(Percentage>0.4&&Percentage<=0.5)
- {
- othermessage();
- Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003500300025\r\n"); //Vence,电量少于50%
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);
- test_have_SM = No;
- Send_String("AT+CMGD=1,4\r\n");
- }
-
- if(Percentage>0.5&&Percentage<=0.6)
- {
- othermessage();
- Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003600300025\r\n"); //Vence,电量少于60%
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);
- test_have_SM = No;
- Send_String("AT+CMGD=1,4\r\n");
- }
-
- if(Percentage>0.6&&Percentage<=0.7)
- {
- othermessage();
- Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003700300025\r\n"); //Vence,电量少于70%
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);
- test_have_SM = No;
- Send_String("AT+CMGD=1,4\r\n");
- }
-
- if(Percentage>0.7&&Percentage<=0.8)
- {
- othermessage();
- Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003800300025\r\n"); //Vence,电量少于80%
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);
- test_have_SM = No;
- Send_String("AT+CMGD=1,4\r\n");
- }
-
- if(Percentage>0.8&&Percentage<=0.9)
- {
- othermessage();
- Send_String("00560065006E00630065002C73B05728753591CF5C114E8E003900300025\r\n"); //Vence,电量少于90%
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);
- test_have_SM = No;
- Send_String("AT+CMGD=1,4\r\n");
- }
-
- if(Percentage>0.9&&Percentage<1)
- {
- othermessage();
- Send_String("00560065006E00630065002C73B05728753591CF5C114E8E0031003000300025\r\n"); //Vence,电量少于100%
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);
- test_have_SM = No;
- Send_String("AT+CMGD=1,4\r\n");
- }
-
- if(Percentage==1)
- {
- othermessage();
- Send_String("00560065006E00630065002C73B05728753591CF4E3A0031003000300025\r\n"); //Vence,电量为100%
- Delay_Ms(100);
- Send_Char(0x1A); //结束命令符号
- Delay_Ms(200);
- test_have_SM = No;
- Send_String("AT+CMGD=1,4\r\n");
- }
- //}
-
- }
- }
- }
- /****************************************************************************/
- void uart(void) interrupt 4
- {
- unsigned char UART_data;
- unsigned char i;
- if(RI)
- {
- UART_data=SBUF;
- if(UART_data=='\n')
- {
- if(GSM_receive[0]=='O'&&GSM_receive[1]=='K')
- test_boot=Yes;
- else
- test_boot=No;
-
- if(GSM_receive[12]=='C')
- test_net_register=Yes;
- else
- test_net_register=No;
-
- if(GSM_receive[0]=='R'&&GSM_receive[1]=='I'&&GSM_receive[2]=='N'&&GSM_receive[3]=='G')
- {
- test_have_call=Yes;
- }
- else
- {
- test_have_call=No;
- }
-
- if(GSM_receive[0]=='+'&&GSM_receive[1]=='C'&&GSM_receive[2]=='L'&&GSM_receive[3]=='I'&&GSM_receive[4]=='P')
- {
- for(i=0;i<11;i++)
- {
- number[i]=GSM_receive[i+8];
- }
- //display_number(line_2,0x00);
-
-
- }
- if(GSM_receive[0]=='+'&&GSM_receive[1]=='C'&&GSM_receive[2]=='M'&&GSM_receive[3]=='T'&&GSM_receive[4]=='I')
- {
- test_have_SM = Yes;
- }
- GSMDATA_count=0;
- }
- else
- {
- GSM_receive[GSMDATA_count]=UART_data;
- GSMDATA_count++;
- }
- }
- RI=0;
- }
复制代码
全部资料51hei下载地址:
SIM900A自动满电提醒装置.zip
(8.4 MB, 下载次数: 49)
|