找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 978|回復(fù): 1
收起左側(cè)

51單片機(jī)溫度檢測

[復(fù)制鏈接]
ID:1109679 發(fā)表于 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";          //定義數(shù)組
uchar  code table2[] = "abcdefghABCDEFGH";

uchar *str1 = table1;        //定義指針,把數(shù)組的首地址賦給它
uchar *str2 = table2;          //數(shù)組名表示數(shù)組首元素的地址
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; //數(shù)據(jù)傳輸線接單片機(jī)的相應(yīng)的引腳

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)                   //延時(shí)子程序//
{
   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個(gè)位置開始顯示
    while(*Temperature_Disp1 !='\0')           //判斷是否顯示完字符串
                {                       
                        LcdWrDat(*Temperature_Disp1++) ;                //         把字符串一個(gè)一個(gè)放到相應(yīng)位置
                }

                Lcd_Show(0,1);        //定位         從2行的第1個(gè)位置開始顯示
                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();
        }

}
回復(fù)

使用道具 舉報(bào)

ID:402383 發(fā)表于 2024-3-26 09:12 | 顯示全部樓層
建議LZ補(bǔ)充附上原理圖
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表