错误提示:missing';'before'0000'
- #include<reg52.h>
- #include<intrins.h>
- sbit KEY1 = P1^0; // 上调报警温度
- sbit KEY2 = P1^1; // 下调报警温度
- sbit LED1 = P1^2; // 正常时绿灯亮
- sbit LED2 = P1^3; // 报警时红灯亮
- sbit BUZZ = P1^6;
- sbit IO_18B20 = P3^2; //DS18B20通信引脚
- #define LCD1602_DB P0
- sbit LCD1602_RS = P2^0;
- sbit LCD1602_RW = P2^1;
- sbit LCD1602_E = P2^2;
- bit enBuzz = 0; //蜂鸣器使能标志
- bit flag1s = 0; //1s定时标志
- unsigned int warnT = 30; //报警温度值
- unsigned char T0RH = 0; //T0重载值的高字节
- unsigned char T0RL = 0; //T0重载值的低字节
- void ConfigTimer0(unsigned int ms);
- unsigned char IntToString(unsigned char *str, int dat);
- extern bit Start18B20();
- extern bit Get18B20Temp(int *temp);
- extern void InitLcd1602();
- extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
- extern void LcdWriteCmd(unsigned char cmd);
- //DS18B20程序:
- /* 软件延时函数,延时时间(t*10)us */
- void DelayX10us(unsigned char t)
- {
- do {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- } while (--t);
- }
- bit Get18B20Ack()
- {
- bit ack;
- EA = 0;
- IO_18B20 = 0;
- DelayX10us(50);
- IO_18B20 = 1;
- DelayX10us(6);
- ack = IO_18B20;
- while(!IO_18B20);
- EA = 1;
- return ack;
- }
- void Write18B20(unsigned char dat)
- {
- unsigned char mask;
- EA = 0;
- for(mask=0x01; mask!= 0; mask<<=1)
- {
- IO_18B20 = 0;
- _nop_();
- _nop_();
- if((mask&dat) == 0)
- IO_18B20 = 0;
- else
- IO_18B20 = 1;
- DelayX10us(6);
- IO_18B20 = 1;
- }
- EA = 1;
- }
- unsigned char Read18B20()
- {
- unsigned char dat;
- unsigned char mask;
- EA = 0;
- for(mask=0x01; mask!=0; mask<<=1)
- {
- IO_18B20 = 0;
- _nop_();
- _nop_();
- IO_18B20 = 1;
- _nop_();
- _nop_();
- if(!IO_18B20)
- dat &= ~mask;
- else
- dat |= mask;
- DelayX10us(6);
- }
- EA = 1;
- return dat;
- }
- bit Start18B20()
- {
- bit ack;
- ack = Get18B20Ack();
- if(ack == 0)
- {
- Write18B20(0xCC);
- Write18B20(0x44);
- }
- return ~ack;
- }
- bit Get18B20Temp(int *temp)
- {
- bit ack;
- unsigned char LSB, MSB;
- ack = Get18B20Ack();
- if(ack == 0)
- {
- Write18B20(0xCC);
- Write18B20(0xBE);
- LSB = Read18B20();
- MSB = Read18B20();
- *temp = ((int)MSB<<8) + LSB;
- }
- return ~ack;
- }
- //LCD1602程序:
- /* 等待液晶准备好 */
- void LcdWaitReady()
- {
- unsigned char sta;
- LCD1602_DB = 0xFF;
- LCD1602_RS = 0;
- LCD1602_RW = 1;
- do {
- LCD1602_E = 1;
- sta = LCD1602_DB; //读取状态字
- LCD1602_E = 0;
- } while (sta & 0x80); //bit7等于1表示液晶正忙,重复检测直到其等于0为止
- }
- /* 向LCD1602液晶写入一字节命令,cmd-待写入命令值 */
- void LcdWriteCmd(unsigned char cmd)
- {
- LcdWaitReady();
- LCD1602_RS = 0;
- LCD1602_RW = 0;
- LCD1602_DB = cmd;
- LCD1602_E = 1;
- LCD1602_E = 0;
- }
- /* 向LCD1602液晶写入一字节数据,dat-待写入数据值 */
- void LcdWriteDat(unsigned char dat)
- {
- LcdWaitReady();
- LCD1602_RS = 1;
- LCD1602_RW = 0;
- LCD1602_DB = dat;
- LCD1602_E = 1;
- LCD1602_E = 0;
- }
- /* 设置显示RAM起始地址,亦即光标位置,(x,y)-对应屏幕上的字符坐标 */
- void LcdSetCursor(unsigned char x, unsigned char y)
- {
- unsigned char addr;
- if (y == 0) //由输入的屏幕坐标计算显示RAM的地址
- addr = 0x00 + x; //第一行字符地址从0x00起始
- else
- addr = 0x40 + x; //第二行字符地址从0x40起始
- LcdWriteCmd(addr | 0x80); //设置RAM地址
- }
- /* 在液晶上显示字符串,(x,y)-对应屏幕上的起始坐标,str-字符串指针 */
- void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
- {
- LcdSetCursor(x, y); //设置起始地址
- while (*str != '\0') //连续写入字符串数据,直到检测到结束符
- {
- LcdWriteDat(*str++);
- }
- }
- /* 初始化1602液晶 */
- void InitLcd1602()
- {
- LcdWriteCmd(0x38); //16*2显示,5*7点阵,8位数据接口
- LcdWriteCmd(0x0C); //显示器开,光标关闭
- LcdWriteCmd(0x06); //文字不动,地址自动+1
- LcdWriteCmd(0x01); //清屏
- }
- void main()
- {
- bit res;
- bit backup = 1;
- int temp; //读取到的当前温度值
- int intT, decT; //温度值的整数和小数部分
- unsigned char len,len1;
- unsigned char str[12],str1[12];
- EA= 1; //开总中断
- P1= 0XF3;
- ConfigTimer0(1); //T0定时1ms
- Start18B20(); //启动DS18B20
- InitLcd1602(); //初始化液晶
- while (1)
- {
-
- if (flag1s) //每秒更新一次温度
- {
- flag1s = 0;
- res = Get18B20Temp(&temp); //读取当前温度
- if (res) //读取成功时,刷新当前温度显示
- {
- intT = temp >> 4; //分离出温度值整数部分
- decT = temp & 0xF; //分离出温度值小数部分
- len = IntToString(str, intT); //整数部分转换为字符串
- len1 = IntToString(str1, warnT); //报警温度转换为字符串
- str[len++] = '.'; //添加小数点
- decT = (decT*10) / 16; //二进制的小数部分转换为1
- //位十进制位
- str[len++] = decT + '0'; //十进制小数位再转换为
- //ASCII字符
- str[len++] = 0xdf; //添加字符串"℃"
- str[len++] = 'C';
- str[len++] = ' ';
- str[len] = '\0'; //添加字符串结束符
- str1[len1++] = 0xdf; //添加字符串"℃"
- str1[len1++] = 'C';
- str1[len1] = '\0'; //添加字符串结束符
- if (intT < warnT) //超过报警温度时执行报警
- {
- enBuzz = 0; //关闭蜂鸣器
- LcdWriteCmd(0x01); //清屏
- LcdShowStr(2, 0, "Temperature"); //显示第一行
- LcdShowStr(2, 1, str); //显示实时温度
- LcdShowStr(9, 1, "<");
- LcdShowStr(11, 1, str1); //显示报警温度
- }
- else
- {
- enBuzz = 1; //启动蜂鸣器发声
- LcdWriteCmd(0x01); //清屏
- LcdShowStr(4, 0, "Warning!"); //显示警告
- LcdShowStr(0, 1, "Temp:");
- LcdShowStr(5, 1, str); //显示实时温度
- LcdShowStr(11, 1, ">");
- LcdShowStr(12, 1, str1); //显示报警温度
- }
- }
- else //读取失败时,提示错误信息
- {
- LcdShowStr(4, 1, "error!");
- }
- Start18B20(); //重新启动下一次转换
- }
- }
- }
- /* 按键动作函数 */
- void KeyAction ()
- {
- if (KEY1 == 0)
- {
- KEY1 = 1;
- warnT++;
- }
- else if (KEY2 == 0)
- {
- KEY2 = 1;
- warnT--;
- }
- }
- /* 整型数转换为字符串,str-字符串指针,dat-待转换数,返回值-字符串长度 */
- unsigned char IntToString(unsigned char *str, int dat)
- {
- signed char i = 0;
- unsigned char len = 0;
- unsigned char buf[6];
- if (dat < 0) //如果为负数,首先取绝对值,并在指针上添加负号
- {
- dat = -dat;
- *str++ = '-';
- len++;
- }
- do { //先转换为低位在前的十进制数组
- buf[i++] = dat % 10;
- dat /= 10;
- } while (dat > 0);
- len += i; //i最后的值就是有效字符的个数
- while (i-- > 0) //将数组值转换为ASCII码反向拷贝到接收指针上
- {
- *str++ = buf[i] + '0';
- }
- *str = '\0'; //添加字符串结束符
- return len; //返回字符串长度
- }
- /* 配置并启动T0,ms-T0定时时间 */
- void ConfigTimer0(unsigned int ms)
- {
- unsigned long tmp; //临时变量
- tmp = 11059200 / 12; //定时器计数频率
- tmp = (tmp * ms) / 1000; //计算所需的计数值
- tmp = 65536 - tmp; //计算定时器重载值
- tmp = tmp + 28; //补偿中断响应延时造成的误差
- T0RH = (unsigned char)(tmp>>8); //定时器重载值拆分为高低字节
- T0RL = (unsigned char)tmp;
- TMOD &= 0xF0; //清零T0的控制位
- TMOD |= 0x01; //配置T0为模式1
- TH0 = T0RH; //加载T0重载值
- TL0 = T0RL;
- ET0 = 1; //使能T0中断
- TR0 = 1; //启动T0
- }
- /* T0中断服务函数,完成250ms, 1s定时 */
- void InterruptTimer0() interrupt 1
- {
- static unsigned int tmr1s = 0;
- static unsigned int tmrms = 0;
- TH0 = T0RH; //重新加载重载值
- TL0 = T0RL;
- if (enBuzz) //蜂鸣器发声处理
- {
- BUZZ = ~BUZZ; //启动蜂鸣器发声
- LED1 = 0;
- LED2 = 1;
- }
- else
- {
- BUZZ = 1; //关闭蜂鸣器
- LED1 = 1;
- LED2 = 0;
- }
- tmr1s++;
- tmrms++;
- if (tmrms >= 250) //延时250ms
- {
- tmrms = 0;
- KeyAction ();
- }
- if (tmr1s >= 1000) //定时1s
- {
- tmr1s = 0;
- flag1s = 1;
- }
- }
复制代码 |