Proteus仿真
#include
#include
sbit RS = P3^6;
sbit LCDEN = P3^7;
sbit ds = P1^7;
sbit IN1=P2^0;
sbit IN2=P2^1;
sbit ENA=P2^4;
sbit led=P1^6;
sbit P1_0=P1^0;
float T,TS=5;
typedef unsigned int uint;
typedef unsigned char uchar;
unsigned char sec,Min; //定义秒分
unsigned int T0Count,KeyNum;
unsigned char Counter,Compare,Line,Column;
char*String;
uint num,z;
//延时函数
void delayMs(uint a)
{
uint i, j;
for(i = a; i > 0; i–)
for(j = 110; j > 0; j–);
}
//LCD1602写指令函数
void writeComm(uchar comm)
{
RS = 0;
P0 = comm;
delayMs(5);
LCDEN = 1;
delayMs(5);
LCDEN = 0;
delayMs(5);
}
//LCD1602写数据函数
void writeData(uchar dat)
{
RS = 1;
P0 = dat;
delayMs(5);
LCDEN = 1;
delayMs(5);
LCDEN = 0;
delayMs(5);
}
//LCD1602行列位置函数
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line1)
{
writeComm(0x80|(Column-1));
}
else if(Line2)
{
writeComm(0x80|(Column-1+0x40));
}
}
//LCD1602显示字符函数
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{
LCD_SetCursor(Line,Column);
writeData(Char);
}
//LCD1602显示函数
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!=’\0’;i++)
{
writeData(String[i]);
}
}
int LCD_Pow(int X,int Y)
{
unsigned char i;
int Result=1;
for(i=0;i
Result*=X;
}
return Result;
}
//LCD1602显示表达式数字函数
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i–)
{
writeData(Number/LCD_Pow(10,i-1)%10+‘0’);
}
}
//LCD1602初始化
void init()
{
writeComm(0x38);
writeComm(0x0c);
writeComm(0x06);
writeComm(0x01);
}
//DS18B20初始化函数
void dsInit()
{
uint i;
ds = 0;
i = 103;
while(i>0)
i--;
ds = 1;
i = 4;
while(i>0)
i--;
}
//读一位数据函数
bit tempreadBit()
{
uint i;
bit dat;
ds = 0;
i++;
ds = 1;
i++;
i++;
dat = ds;
i = 8;
while(i>0) i–;
return dat;
}
//读1字节
unsigned char tempreadByte()
{
unsigned int i;
unsigned char j, dat;
dat = 0;
for(i=0; i<8; i++)
{
j = tempreadBit();
dat = (j << 7) | (dat >> 1);
}
return dat;
}
//写1字节
void tempwriteByte(uchar dat)
{
unsigned int i;
unsigned char j;
bit b;
for(j = 0; j < 8; j++)
{
b = dat & 0x01;
dat >>= 1;
if(b)
{
ds = 0;
i++;
i++;
ds = 1;
i = 8;
while(i>0)
i--;
}
else
{
ds = 0;
i = 8;
while(i>0)
i--;
ds = 1;
i++;
i++;
}
}
}
//获取温度并转换
void tempchange()
{
dsInit();
delayMs(1);
tempwriteByte(0xcc);
tempwriteByte(0x44);
}
//读取寄存器中存储的温度数据
int getTemp()
{
unsigned int temp;
int t;
unsigned char a ,b;
dsInit();
delayMs(1);
tempwriteByte(0xcc);
tempwriteByte(0xbe);
a = tempreadByte();
b = tempreadByte();
t=(b<<8)+a;
if((t&0xf800)==0xf800)
{
t=(~t)+1;
}
else
{
temp=t*0.0625;
}
return temp ;
}
//计时器T0中断初始化
void Timer0Init()
{
TMOD|=0X01;
TH0=0XFC;
TL0=0X18;
ET0=1;
EA=1;
TR0=1;
}
//PWM调节转速函数
void PWM_Ctrl(void)
{
if(T
if((T-TS)>=0)
{Compare=30;}
if((T-TS)<=1&&(T-TS)>0)
{Compare=40;}
if((T-TS)<=2&&(T-TS)>1)
{Compare=50;}
if((T-TS)<=3&&(T-TS)>2)
{Compare=60;}
if((T-TS)<=4&&(T-TS)>3)
{Compare=70;}
if((T-TS)<=5&&(T-TS)>4)
{Compare=80;}
if((T-TS)<=6&&(T-TS)>5)
{Compare=90;}
if((T-TS)>6)
{Compare=100;}
}
//分秒累加函数
void Ctrl(void)
{
if((T-TS)>0)
{
if (T0Count>=590)
{
T0Count=0;
sec++;
if(sec>=60)
{
Min++;
sec=0;
}
if(Min>=3)
{
// ENA=0;
// a=0;
Compare=0;
EA=0;
if(Counter
IN1=1;
IN2=0;
ENA=1;
}
else
{
IN1=0;
IN2=0;
ENA=0;
}
LCD_ShowString(2,9,“Too Hot:”);
}
}
}
}
//LCD1602显示函数
void LCD_Display()
{
LCD_ShowNum(1,8,(unsigned long)(T10)%10 ,1);
LCD_ShowNum(1,13,TS,2);
LCD_ShowNum(1,5,T,2);
LCD_ShowNum(1,16,(unsigned long)(TS10)%10 ,1);
LCD_ShowNum(2,4,Min,2);
LCD_ShowNum(2,7,sec,2);
}
//显示温度正负函数
void Temp_Convert(void)
{
if(T<0)
{
LCD_ShowChar(1,4,’-’);
T=-T;
}
else
{
LCD_ShowChar(1,4,'+');
}
if(TS<0)
{
LCD_ShowChar(1,12,'-');
TS=-TS;
}
else
{
LCD_ShowChar(1,12,'+');
}
}
//独立按键重新启动电机函数
void Time_End(void)
{
if(P1_0==0)
{
IN1=1;
ENA=1;
IN2=0;
EA=1;
sec=0;
Min=0;
// a=1;
T0Count=0;
LCD_ShowString(2,9," ");
}
}
//主函数
void main()
{
init();
Timer0Init();
LCD_ShowString(1,1,“ET:”);
LCD_ShowString(1,9,“ST:”);
LCD_ShowString (2,1,“WT:”);
LCD_ShowString (2,6,":" );
LCD_ShowChar(1,7,’.’);
LCD_ShowChar(1,15,’.’);
while(1)
{
tempchange(); //获取温度并转换
T=getTemp(); //读取温度
Time_End(); //独立按键控制重新启动
Temp_Convert(); //温度正负
Ctrl(); //分秒
LCD_Display(); //LCD显示
PWM_Ctrl(); //PWM调节电机转速
}
}
//计时器T0中断函数
void Timer0_Routine() interrupt 1
{
TH0 = 0xFc;
TL0 = 0x18;
Counter++;
Counter%=100;
T0Count++;
if(Counter
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)