龙空技术网

51/52单片机14课:温控开关

十月华笑 149

前言:

目前看官们对“单片机for语句延时”大约比较注重,小伙伴们都需要剖析一些“单片机for语句延时”的相关资讯。那么小编也在网络上网罗了一些关于“单片机for语句延时””的相关资讯,希望看官们能喜欢,姐妹们一起来学习一下吧!

#include <reg51.h>//头文件

#define uint unsigned int//宏定义

#define uchar unsigned char //宏定义

sbit DQ=P3^2;//18B20的2脚与单片机P3.2口相接

sbit P10=P1^0;//数码管位选引脚P1.0

sbit P11=P1^1;//数码管位选引脚P1.1

sbit P14=P1^4;//继电器控制引脚

uchar temp;

uchar htemp=30;//动作温度值

code unsigned char sz []={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0-9数组

void delay (uint t);//延时函数声明

void delayms(uint a);//for语句延时函数声明

void init();//18B20初始化程序声明

void write (uchar dat);//写1字节程序声明

uchar read ();//读1字节程序声明

void display();//显示程序声明

uchar readtemp();//读温度程序声明

void delay (uint t)//延时函数

{

while(t--);

}

void delayms(uint a) //for语句延时函数

{

uint x,y;

for(x=a;x>0;x--)

for(y=110;y>0;y--);

}

void init ()//18B20初始化程序

{

uchar n;

DQ=1;

delay(8);

DQ=0;

delay(80);

DQ=1;

delay(8);

n=DQ;

delay(4);

}

void write (uchar dat)//写1字节程序

{

uchar i;

for (i=0;i<8;i++)

{

DQ=0;

DQ=dat&0x01;

delay (4);

DQ=1;

dat>>=1;//dat=dat>>1

}

delay(4);

}

uchar read ()//读1字节程序

{

uchar i,value;

for (i=0;i<8;i++)

{

DQ=0;

value>>=1;

DQ=1;

if(DQ)///

value|=0x80;

delay(4);

}

return value;

}

uchar readtemp()//读温度程序

{

uchar a,b;

init();

write (0xcc);

write (0x44);

delay (300);

init ();

write (0xcc);

write (0xbe);

a=read();

b=read();

b<<=4;

b+=(a&0xf0)>>4;

return b;

}

void display ()//显示程序

{

P10=0;

P0=sz[temp/10];

delayms(2);

P10=1;

P11=0;

P0=sz[temp%10];

delayms(2);

P11=1;

}

void main()//主程序

{

while(1)

{

temp=readtemp();

display ();

if (temp>=htemp)

{

P14=0;

}

else

{

P14=1;

}

}

}

标签: #单片机for语句延时