找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1356|回复: 1
收起左侧

51单片机温度报警程序:电机正转并点亮LED

[复制链接]
ID:683130 发表于 2020-10-12 11:35 | 显示全部楼层 |阅读模式


#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define delayNOP() {_nop_();_nop_();_nop_();_nop_();}



sbit DQ = P1^0;
sbit dn= P1^1;
sbit cn = P1^2;
sbit led = P3^0;
sbit f1=P3^1;
sbit f2=P3^2;
sbit z3=P3^3;
sbit z4=P3^4;


unsigned  char  biao[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
uchar code df_Table[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};  


        uchar Temp_Value[]={0x00,0x00};                 

uchar Display_Digit[]={0,0,0,0};               
uchar y=25,CurrentT = 0;
bit DS18B20_IS_OK = 1;       
uchar ng=0;


//延时子函数
void DelayXus(uint x)
{
        uchar i;
        while(x--)
        {
                 for(i=0;i<200;i++);
        }
}

//延时子函数
void Delay(uint x)
{
        while(--x);
}
//延时子函数
void delay()
{
        unsigned  int  i;
          for(i=100;i>0;i--);

}
//-------------------------------------------------------------------------
//        显示子程序
//------------------------------------------------------------------------
void xianshi()
{

       
        {
        P2=0X01;if(ng==1)P0=biao[10];else P0=biao[11];
        delay();
        P2=0X02;P0=biao[Display_Digit[3]];
        delay();
        P2=0X04;P0=biao[Display_Digit[2]];
        delay();
        P2=0X08;P0=biao[Display_Digit[1]];
        delay();
        P2=0X10;if(ng==1)P0=biao[10];else P0=biao[11];
        delay();
        P2=0X20;P0=biao[y/100%10];
        delay();
        P2=0X40;P0=biao[y/10];
        delay();
        P2=0X80;P0=biao[y%10];
        delay();
                }
               
}

//------------------------------------------------------------------------
//        DS18B20初始化
//------------------------------------------------------------------------
uchar Init_DS18B20()
{
        uchar status;
        DQ = 1;                               
        Delay(8);                         
        DQ = 0;                                 
        Delay(90);                         //时间(480~960us)
        DQ = 1;                                 
        Delay(15);                       
        status=DQ;                         
        Delay(100);                         
        DQ = 1;                                 
        return status;                 
}

//-----------------------------------------------------------------------
//        DS18B20读取
//------------------------------------------------------------------------
uchar ReadOneByte()
{
        uchar i,dat=0;
        DQ = 1;                                  
        _nop_();                          
        for(i=0;i<8;i++)          
        {
                 DQ = 0;                         
                _nop_();                 
                _nop_();                  
                dat >>= 1;               
                DQ = 1;                          
                if(DQ)                          
                        dat |= 0X80;  
                Delay(30);                  
                DQ = 1;                       
        }
        return dat;                          
}

//---------------------------------------------------------------------
//        DS18B20操作命令
//--------------------------------------------------------------------
uchar WriteOneByte(uchar dat)
{
        uchar i;
        for(i=0;i<8;i++)          
        {
                 DQ = 0;                          
                DQ = dat& 0x01;          
                Delay(15);                 
                DQ = 1;                          
                dat >>= 1;               
        }
        return dat;                         
}

//------------------------------------------------------------------------------
//        DS18B20读取写入
//------------------------------------------------------------------------------
void Read_Temperature()
{
        if(Init_DS18B20()==1)                     
                {DS18B20_IS_OK=0;}                       
        else
        {
                WriteOneByte(0xcc);                       
                WriteOneByte(0x44);                       
                Init_DS18B20();                               
                WriteOneByte(0xcc);               
                WriteOneByte(0xbe);                       
                Temp_Value[0] = ReadOneByte();
                Temp_Value[1] = ReadOneByte();
                DS18B20_IS_OK=1;                          
        }
}

//---------------------------------------------------------------------------
//         LCD温度显示
//---------------------------------------------------------------------------
void Display_Temperature()
{        uchar i;
ng = 0;                  
        if((Temp_Value[1]&0xf8)==0xf8)                       
        {
                 Temp_Value[1] = ~Temp_Value[1];               
                Temp_Value[0] = ~Temp_Value[0]+1;
                if(Temp_Value[0]==0x00)                               
                        Temp_Value[1]++;               
                ng = 1;       
        }
        Display_Digit[0] = df_Table[Temp_Value[0]&0x0f];
        CurrentT = ((Temp_Value[0]&0xf0)>>4) | ((Temp_Value[1]&0x07)<<4);
        Display_Digit[3] = CurrentT/100;
        Display_Digit[2] = CurrentT%100/10;
        Display_Digit[1] = CurrentT%10;
       

       
}

//---------------------------------------------------------------------
//        ???
//----------------------------------------------------------------------
void main()
{
        Read_Temperature();

        while(1)
        {
                 Read_Temperature();
                if(DS18B20_IS_OK)
                        Display_Temperature();
                xianshi();       
        if(dn==0)
                {
                        delay();
                        if(dn==0)
                        {y--;
                        if(y<1)
                        {y=999;}
                        }
                        while(dn==0);
                        }
                if(cn==0)
                {
                        delay();
                        if(cn==0)
                        {y++;
                        if(y>999)
                        {y=1;}
                        }
                        while(cn==0);
                        }
       

       
       
        if(CurrentT<y)
        {led=1;
        z4=1;f1=1;        z3=0;f2=0;
        }
else {led=0;
        z4=0;f1=0;z3=1;f2=1;}
       
}}
回复

使用道具 举报

ID:1 发表于 2020-10-12 22:22 | 显示全部楼层
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表