#include <REGX52.H>
#include "LCD1602.h"
#include "DS18B20.h"
#include "Delay.h"
#include "Timer0.h"
float T;
void main()
{
UsartConfiguration();
Timer0_Init();
DS18B20_ConvertT(); //上电先转换一次温度,防止第一次读数据错误
Delay(1000); //等待转换完成
LCD_Init();
LCD_ShowString(1,1,"Temperature:");
P1_0=0;
while(1)
{
DS18B20_ConvertT(); //转换温度
T=DS18B20_ReadT(); //读取温度
if(T<0) //如果温度小于0
{
LCD_ShowChar(2,1,'-'); //显示负号
T=-T; //将温度变为正数
}
else //如果温度大于等于0
{
LCD_ShowChar(2,1,'+'); //显示正号
}
LCD_ShowNum(2,2,T,3); //显示温度整数部分
LCD_ShowChar(2,5,'.'); //显示小数点
LCD_ShowNum(2,6,(unsigned long)(T*10000)%10000,4);//显示温度小数部分
if(T>=24)
{P1_0=1;
}
else
P1_0=0;
}
}
sbit Motor=P1^0;
unsigned char Counter,Compare; //???????,????PWM
unsigned char KeyNum,Speed;
void forward()
{
Compare=0 ;
}
void back()
{
Compare=50 ;
}
void left()
{
Compare=100;
}
void Com_Int(void) interrupt 4
{
unsigned char receive_data;
EA = 0;
if(RI == 1)
{
RI=0;
receive_data = SBUF;
switch(receive_data)
{
case ('1'):
forward();break;
case ('2'):
back();break;
case ('3'):
left();break;
}
}
EA = 1;
}
void UsartConfiguration()
{
SCON=0X50; //SCON ??????????,???????????????/????????????
TMOD=0X20;
PCON=0X00;
TH1=0Xfd;
TL1=0Xfd; //????9600????fd
TR1= 1;
ES = 1;
EA = 1;
PS=1;
}
void Timer0_Routine() interrupt 1
{
TL0 = 0x9C; //??????
TH0 = 0xFF; //??????
Counter++;
Counter%=100; //??????????0~99
if(Counter<Compare) //????????
{
Motor=1; //??1
}
else //????????
{
Motor=0; //??0
}
}
|