|
代码我放到下面,求大佬帮忙看看该怎么改!谢谢!红色字体即为我加的定时器代码- #include <reg52.h>
- #include <intrins.h>
- #include "common.h"
- #include "lcd1602.h"
- #include "DS18B20_One.h"
- #include "uart.h"
- #include "gsm.h"
- #define uint unsigned int
- #define uchar unsigned char //宏定义
- const char * xdata phonenum= "17630966319"; //在此处更换号码
- sbit SET= P1^0;//设置按键引脚
- sbit ADD= P1^1;//按键加引脚
- sbit DEC= P1^2;//按键减引脚
- sbit SEND= P1^3;//手动发送短信按键引脚
- sbit BEEP=P2^3;//蜂鸣器引脚
- sbit ADC0832_CS = P2^2;//ADC0832 CS引脚
- sbit ADC0832_CLK = P2^0;//ADC0832 CLK引脚
- sbit ADC0832_DIO = P2^1;//ADC0832 DO引脚
- sbit LED_Y=P1^5;//温度报警指示灯
- sbit LED_R=P1^6;//温度报警指示灯
- uchar th=30;//温度上限初始值
- uchar setn = 0;//进入设置标志位,setn等于0时退出设置
- bit shoudong = 0;//手动发送短信标志位,等于1时,进入手动发送短信模式
- uchar xdata send_data[]="Temp:00C";//短信发送内容
- <font color="#ff0000">uchar timer50msCount = 0; //定时器定义
- void Timer0Init(void) //[email protected]
- {
- TMOD &= 0xF0; // 设置定时器模式,这里为T0
- TMOD |= 0x01; // 设置定时器工作方式1,为16为定时器
- TL0 = 0x00; // 设置定时器低位初值
- TH0 = 0x4C; // 设置定时器高位初值
- TF0 = 0; // 清楚TF0溢出标志位
- TR0 = 1; // 启动定时器0开始计时
- }
- void timer0Interrupt() interrupt 1
- {
- timer50msCount++;
- // 每次中断时,定时器初值为0,需重新设置定时器初值,保持50ms
- // 时间不变
- TL0 = 0x00;
- TH0 = 0x4C;
- }</font>
- void keyscan()//按键扫描
- {
- if(SET == 0)//设置键按下
- {
- delay_ms(10);//延时消抖
- if(SET == 0)//判断是否按下
- {
- while(!SET);//等待松开
- setn++;//设置标志位自加1
- if(setn > 1)//总共设置,温度上限
- {
- setn = 0;//退出设置模式
- lcd_write_com(0x0c);//关闭光标
- }
- switch(setn)
- {
- case(1):lcd_write_com(0x80+14);lcd_write_com(0x0f);break;//在温度上限值的位置光标闪烁
- }
- }
- }
- if(ADD == 0)//按键加按下
- {
- delay_ms(10);//延时消抖
- if(ADD == 0)//按键加按下
- {
- while(!ADD);//等待松开
- if(setn == 1)//设置标志位等于1时,为温度上限设置
- {
- if(th < 99)//温度上限值最大只能加到99
- {
- th++;//值加1
- }
- lcd_write_char(13,0,th/10+0x30);//显示温度上限值十位
- lcd_write_char(14,0,th%10+0x30);//显示温度上限值个位
- }
-
- }
- switch(setn)
- {
- case(1):lcd_write_com(0x80+14);lcd_write_com(0x0f);break;//在温度上限值的位置光标闪烁
- }
- }
-
- if(DEC == 0)//按键减按下
- {
- delay_ms(10);//延时消抖
- if(DEC == 0)//按键减按下
- {
- while(!DEC);//等待松开
-
- if(setn == 1)//设置标志位等于1时,为温度上限设置
- {
- if(th > 0)//温度上限值最小只能减到0
- {
- th--;//值减1
- }
- lcd_write_char(13,0,th/10+0x30);//显示温度上限值十位
- lcd_write_char(14,0,th%10+0x30);//显示温度上限值个位
- }
-
- }
- switch(setn)
- {
- case(1):lcd_write_com(0x80+14);lcd_write_com(0x0f);break;//在温度上限值的位置光标闪烁
- }
- }
- if(SEND == 0)//手动发送键按下
- {
- delay_ms(10);//延时消抖
- if(SEND == 0)//手动发送键按下
- {
- while(!SEND);//等待松开
- shoudong = 1;//手动发送位置1
- lcd_write_str(0,0, " Send Sms... ");//显示发送短信中
- lcd_write_str(0,1, " Waiting... ");//第二行清屏
- gsm_send_englishmsg(phonenum,send_data);//发送短信
- delay_ms(2000);//延时2秒
- delay_ms(2000);//延时2秒
- delay_ms(2000);//延时2秒
- lcd_write_str(0,0, " Send OK... ");//显示发送成功
- lcd_write_str(0,1, " ");
- delay_ms(2000);//延时2秒
- delay_ms(2000);//延时2秒
- delay_ms(2000);//延时2秒
- lcd_write_str(0,0, "NOW: C SET: C");//返回主页面显示
- lcd_write_str(0,1, " IN OPERATION ");//第二行显示内容随意
- lcd_write_char(13,0,th/10+0x30);//显示温度上限值十位
- lcd_write_char(14,0,th%10+0x30);//显示温度上限值个位
- shoudong = 0;
- }
- }
- }
- void main()
- {
- uint count;
- short temperature;//温度
- bit send_flag=0;
-
- <font color="#ff0000"> EA = 1; // 开总中断
- ET0 = 1; // 开定时器 0 中断
- Timer0Init();</font>
-
- lcd_init();//1602初始化
- temperature = DS18B20_TF();//温度DS18B20初始化
- lcd_write_str(0,0," GSM init... ");//1602显示GSM初始化中
- uart_init(9600);//串口初始化,波特率9600
- gsm_init();//gsm初始化
- delay_ms(500);//延时
- lcd_write_str(0,0, "NOW: C SET: C");//进入主页面,第一行显示的内容,温度值和温度上限值
- lcd_write_str(0,1, " IN OPERATION ");//第二行显示内容随意
- lcd_write_char(13,0,th/10+0x30);//显示温度上限值十位
- lcd_write_char(14,0,th%10+0x30);//显示温度上限值个位
- while(1)
- {
- keyscan();//按键扫描
- if(count++ > 150 && setn == 0)//延时一段时间,并且设置位等于0时,进入以下函数
- {
- count = 0;//累加变量清零
- if(shoudong == 0)
- {
-
- temperature = DS18B20_TF();//读取温度值
- lcd_write_char(4,0,temperature/10+0x30);//温度值十位显示
- lcd_write_char(5,0,temperature%10+0x30);//温度值个位显示
-
- send_data[5]=temperature/10+'0';//短信内容值,温度十位
- send_data[6]=temperature%10+'0';//短信内容值,温度个位
-
-
- LED_R = 1;//LED灭
- //if(temperature >= th)
- // LED_Y = 0;//温度超标,LED亮
- //else
- // LED_Y = 1;//LED灭
- if(temperature >= th)
- LED_R = 0;//温度超标,LED亮
- else
- LED_R = 1;//LED灭
-
- if(temperature >= th)//温度超标,蜂鸣器报警,并且发送短信
- {
- BEEP = 0;
- if(send_flag == 0)//为了避免报警时,短信频繁发送,这里超标时只发一次短信
- {
- send_flag = 1;
- gsm_send_englishmsg(phonenum,send_data);//发送短信
- }
- delay_ms(100);//延时
-
- }
- else
- {
- BEEP = 1;//关闭蜂鸣器
- send_flag = 0;//短信发送标志位清零
- }
- }
- }
- delay_ms(1);
- //定时发送程序
- // 每5000ms允许if语句块中的程序
- <font color="#ff0000"> if (timer50msCount == 100)
- {
- SEND =0;
- delay_ms(500);//延时0.5秒
- SEND =1;
- timer50msCount = 0;
- }</font>
- delay_ms(1);
- }
- }
复制代码
|
|