- #include<reg51.h>
- void main()
- {
- int temp;
- int datas1[] = {0, 0, 0, 0, 0};
- UsartConfiguration();
- LcdInit(); //初始化LCD1602
- LcdWriteCom(0x85); //写地址 80表示初始地址
- LcdWriteData('C');
- SETTEMP=32;
- datas1[0]=SETTEMP/10;
- datas1[1]=SETTEMP%10;
- LcdWriteCom(0x80+0x40+9);
- LcdWriteData(datas1[0]+'0');
- LcdWriteCom(0x80+0x40+10);
- LcdWriteData(datas1[1]+'0');
- while(1)
- {
- temp=Ds18b20ReadTemp();
- LcdDisplay(temp);
- Delay1ms(1000);//1s钟刷一次
- if(k1==0)
- {
- Delay1ms(10);
- SETTEMP=SETTEMP+1;
- Delay1ms(1000);//1s钟刷一次
- datas1[0]=SETTEMP/10;
- datas1[1]=SETTEMP%10;
- LcdWriteCom(0x80+0x40+9);
- LcdWriteData(datas1[0]+'0');
- LcdWriteCom(0x80+0x40+10);
- LcdWriteData(datas1[1]+'0');
- }
- if(k2==0)
- {
- Delay1ms(10);
- SETTEMP=SETTEMP-1;
- datas1[0]=SETTEMP/10;
- datas1[1]=SETTEMP%10;
- LcdWriteCom(0x80+0x40+9);
- LcdWriteData(datas1[0]+'0');
- LcdWriteCom(0x80+0x40+10);
- LcdWriteData(datas1[1]+'0');
- }
- }
- }
- #include"temp.h"
- void Ds18b20WriteByte(unsigned char dat)
- {
- unsigned int i,j;
- for(j=0;j<8;j++)
- {
- DSPORT=0; //每写入一位数据之前先把总线拉低1us
- i++;
- DSPORT=dat&0x01; //然后写入一个数据,从最低位开始
- i=6;
- while(i--); //延时68us,持续时间最少60us
- DSPORT=1; //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
- dat>>=1;
- }
- }
- unsigned char Ds18b20ReadByte()
- {
- unsigned char byte,bi;
- unsigned int i,j;
- for(j=8;j>0;j--)
- {
- DSPORT=0;//先将总线拉低1us
- i++;
- DSPORT=1;//然后释放总线
- i++;
- i++;//延时6us等待数据稳定
- bi=DSPORT; //读取数据,从最低位开始读取
- /*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
- byte=(byte>>1)|(bi<<7);
- i=4; //读取完之后等待48us再接着读取下一个数
- while(i--);
- }
- return byte;
- }
- void Ds18b20ChangTemp()
- {
- Ds18b20Init();
- Delay1ms(1);
- Ds18b20WriteByte(0xcc); //跳过ROM操作命令
- Ds18b20WriteByte(0x44); //温度转换命令
- }
- void Ds18b20ReadTempCom()
- {
- Ds18b20Init();
- Delay1ms(1);
- Ds18b20WriteByte(0xcc); //跳过ROM操作命令
- Ds18b20WriteByte(0xbe); //发送读取温度命令
- }
- int Ds18b20ReadTemp()
- {
- int temp=0;
- unsigned char tmh,tml;
- Ds18b20ChangTemp(); //先写入转换命令
- Ds18b20ReadTempCom(); //然后等待转换完后发送读取温度命令
- tml=Ds18b20ReadByte(); //读取温度值共16位,先读低字节
- tmh=Ds18b20ReadByte(); //再读高字节
- temp=tmh;
- temp<<=8;
- temp|=tml;
- return temp;
- }
- void LcdDisplay(int temp) //lcd显示
- {
- unsigned char i, datas[] = {0, 0, 0, 0, 0}; //定义数组
- float tp;
- LcdWriteCom(0x80); //写地址 80表示初始地址
- tp=temp;
- temp=tp*0.0625*100+0.5;
- if((temp/100)>(SETTEMP-1))
- {
- beep=0;
- }
- else
- {
- beep=1;
- }
- //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
- //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
- datas[1] = temp % 10000 / 1000;
- datas[2] = temp % 1000 / 100;
- datas[3] = temp % 100 / 10;
- datas[4] = temp % 10;
-
-
- str[0]=datas[1]+'0';
- str[1]=datas[2]+'0';
- str[2]='.';
- str[3]=datas[3]+'0';
- str[4]='T';
- str[5]=SETTEMP/10+'0';
- str[6]=SETTEMP%10+'0';
- str[7]='U';
- LcdWriteCom(0x80); //写地址 80表示初始地址
- LcdWriteData('t');
- LcdWriteCom(0x81); //写地址 80表示初始地址
- LcdWriteData('e');
- LcdWriteCom(0x82); //写地址 80表示初始地址
- LcdWriteData('m');
- LcdWriteCom(0x83); //写地址 80表示初始地址
- LcdWriteData('p');
- LcdWriteCom(0x84); //写地址 80表示初始地址
- LcdWriteData(':');
- LcdWriteCom(0x85); //写地址 80表示初始地址
- LcdWriteData('0'+datas[1]); //十位
- LcdWriteCom(0x86); //写地址 80表示初始地址
- LcdWriteData('0'+datas[2]); //个位
- LcdWriteCom(0x87); //写地址 80表示初始地址
- LcdWriteData('.'); //显示 ‘.’
- LcdWriteCom(0x88); //写地址 80表示初始地址
- LcdWriteData('0'+datas[3]); //显示小数点
- LcdWriteCom(0x89); //写地址 80表示初始地址
- LcdWriteData('C'); //显示小数点
- LcdWriteCom(0x80+0x40); //写地址 80表示初始地址
- for(i=0;i<=8;i++)
- {
- LcdWriteData(CNCHAR[i]);
- }
- LcdWriteCom(0x80+0x40+13);
- LcdWriteData('C');
- // datas1[0]=SETTEMP/10;
- // datas1[1]=SETTEMP%10;
- // LcdWriteCom(0x80+0x40+9);
- // LcdWriteData(datas1[0]+'0');
- // LcdWriteCom(0x80+0x40+10);
- // LcdWriteData(datas1[1]+'0');
- LcdWriteCom(0x80+0x40+11);
- LcdWriteData('.');
- LcdWriteCom(0x80+0x40+12);
- LcdWriteData('0');
- }
- //串口配置
- SCON &= (uint8_t)((uint8_t)( ~( UART_MODE | UART_RX ))); //清SM0 SM1 REN
- SCON |= (uint8_t)( UART_8BAUDRATE_VOLATILE | UART_RX );
-
- //TIM1配置
- TMOD &= (uint8_t)((uint8_t)( ~TIM1_MODE ));
- TMOD |= TIM1_MODE_2; //8位自动重装
- PCON = 0x00;
- TH1 = 0xFD; //波特率默认配置为9600
- TL1 = 0xFD;
-
- TI = 1; //清发送标志
- TR1 = 1; //使能定时器
- }
- uint8_t drv_uart_rx_bytes( uint8_t* RxBuffer )
- {
- uint8_t l_RxLength = 0;
- uint16_t l_UartRxTimOut = 0x7FFF;
-
- while( l_UartRxTimOut-- ) //在超时范围内检测数据
- {
- if( 0 != RI ) //检测是否接收到数据
- {
- RI = 0; //清标志位
- *RxBuffer = SBUF; //读数据
- RxBuffer++;
- l_RxLength++;
- l_UartRxTimOut = 0x7FFF;//重置超时检测
- }
- }
- return l_RxLength; //等待超时,数据接收完成
- }
复制代码 |