找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 175|回复: 1
打印 上一主题 下一主题
收起左侧

51单片机温度检测

[复制链接]
跳转到指定楼层
楼主
ID:1109679 发表于 2024-3-12 16:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "reg51.h"
#include "LCD1602.h"
#include "stdio.h"
#include "DS18B20.h"
#include <intrins.h>
#define uchar unsigned char                //宏定义
#define uint unsigned int

uchar  code table1[] = "0123456789012345";          //定义数组
uchar  code table2[] = "abcdefghABCDEFGH";

uchar *str1 = table1;        //定义指针,把数组的首地址赋给它
uchar *str2 = table2;          //数组名表示数组首元素的地址
uchar  Temperature_Disp1[] = "abcdefghABCDEFGH";
uchar  Temperature_Disp2[] = "abcdefghABCDEFGH";

unsigned char Key_Value;

uint Temperature;
uint Temperature_Max = 40;
uint Temperature_Min = 20;
sbit L0=P1^0;
sbit L1=P1^1;
sbit L2=P1^2;
sbit L3=P1^3;
sbit Beep=P2^4;
sbit DQ=P2^3; //数据传输线接单片机的相应的引脚

sbit LED_Green=P2^5;
sbit LED_Red_Max=P2^6;
sbit LED_Red_Min=P2^7;

sbit motor=P3^0;

void delay_Key(unsigned int time)                   //延时子程序//
{
   unsigned int i;
   for(i=0;i<time;i++)
   {}
}
unsigned char Key_Scan(void)
{       
                unsigned char Key_Value = 11;
                P1=0xef;            //行扫描初值11101111//
                if (L0==0) Key_Value = 0;        
                if (L1==0) Key_Value = 1;     
                if (L2==0) Key_Value = 2;      
                if (L3==0) Key_Value = 3;      
                delay_Key(5000);
               
                P1=0xdf;           //行扫描初值11011111//
                if (L0==0) Key_Value = 4;      
                if (L1==0) Key_Value = 5;        
                if (L2==0) Key_Value = 6;         
                if (L3==0) Key_Value = 7;
                delay_Key(5000);
               
                P1=0xbf;             //行扫描初值10111111//
                if (L0==0) Key_Value = 8;      
                if (L1==0) Key_Value = 9;        
                if (L2==0) Key_Value = 10;      
                if (L3==0) Key_Value = 11;      
                delay_Key(5000);
               
                P1=0x7f;             //行扫描初值//
                if (L0==0) Key_Value = 12;        
                if (L1==0) Key_Value = 13;      
                if (L2==0) Key_Value = 14;      
                if (L3==0) Key_Value = 15;      
                delay_Key(5000);
               
                return Key_Value;

}



void Key_Proc()
{
        Key_Value = Key_Scan();
        switch(Key_Value)
        {
                case 0:
                        Temperature_Max++;
                        break;
                case 1:
                        Temperature_Max--;
                        break;
                case 2:
                        Temperature_Min++;
                        break;
                case 3:
                        Temperature_Min--;
                        break;
                case 4:
                        Temperature_Max = 40;
                        Temperature_Min = 20;
                        Beep = 1;
                        break;
        }
}

void Display(unsigned char *Temperature_Disp1,unsigned char *Temperature_Disp2)
{
                Lcd_Show(0,0);        //定位         从1行的第1个位置开始显示
    while(*Temperature_Disp1 !='\0')           //判断是否显示完字符串
                {                       
                        LcdWrDat(*Temperature_Disp1++) ;                //         把字符串一个一个放到相应位置
                }

                Lcd_Show(0,1);        //定位         从2行的第1个位置开始显示
                while(*Temperature_Disp2!='\0')
                {                       
                        LcdWrDat(*Temperature_Disp2++) ;       
                }
               
}
void Temperature_Proc()
{
        Temperature = (uint)ReadTemperature();
        sprintf(Temperature_Disp1,"Max:%dC Min:%dC",Temperature_Max,Temperature_Min);
        sprintf(Temperature_Disp2,"Now:%dC        ",Temperature);
        Display(Temperature_Disp1,Temperature_Disp2);
        if(Temperature > Temperature_Max)
        {
                Beep = 1;
                LED_Red_Max = 0;
                LED_Red_Min = 1;
                LED_Green = 1;
                motor = 1;
        }
        else if((Temperature <= Temperature_Max)&&(Temperature >= Temperature_Min))
        {
                LED_Red_Max = 1;
                LED_Red_Min = 1;
                Beep = 0;
                LED_Green = 0;
                motor = 0;
        }
        else if(Temperature < Temperature_Min)
        {
                Beep = 1;
                LED_Red_Max = 1;
                LED_Red_Min = 0;
                LED_Green = 1;
                motor = 0;
        }

}


void main(void)
{
        Beep = 0;
        Temperature = (uint)ReadTemperature();
        motor = 0;
        Delay500ms();
        Lcd_Init();
       
        while(1)
        {
                Key_Proc();
                Temperature_Proc();
        }

}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:402383 发表于 2024-3-26 09:12 | 只看该作者
建议LZ补充附上原理图
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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