久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 6785|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

51單片機溫度顯示的實現(xiàn)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:312820 發(fā)表于 2018-5-21 20:30 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
分享一下單片機溫度顯示的實現(xiàn),傳感器用的DS18B20,很簡單,直接看代碼就行
兩個文件:first.c代碼
#include<stdio.h>
#include<reg52.h>
#include<temp.h>
#include<stdlib.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
u8 num=0,Data[8];
u8 t[5];
u8 code datachuan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};     //共陰極數(shù)碼管
void delay(u16 m){
    while(m--);
}                                                       //設(shè)置delay延遲
int datareceive(int temp){
    float tp;
        if(temp>0){
        Data[0]=0x00;
                tp=temp;
                temp=tp*0.0625*100+0.5;
        }
        else {
                Data[0]=0x40;
                temp--;
                temp=~temp;
                tp=temp;
                temp=tp*0.0625*100;       
                temp=temp+0.5;                            //四舍五入的作用
        }
        return temp;
}
                                                                                                   //數(shù)碼管打印
void dataprintf(int temp){
     int temp1;
         u8 m;
         temp1=datareceive(temp);
         Data[1]=datachuan[temp1/10000];
         t[0]=temp1%10000/1000+0x30;
         Data[2]=datachuan[temp1%10000/1000];
         t[1]=temp1%10000%1000/100+0x30;
         t[2]='.';
         t[3]=temp1%10000%1000%100/10+0x30;
         t[4]=temp1%10000%1000%100%10+0x30;
         m=datachuan[temp1%10000%1000/100];
         Data[3]=m|0x80;
         Data[4]=datachuan[temp1%10000%1000%100/10];
         Data[5]=datachuan[temp1%10000%1000%100%10];                   //計算每個數(shù)碼管的數(shù)值
}



void DigDisplay(){
    u8 i;
        for(i=0;i<6;i++){
            switch(i){
                    case (0) :
                            LSA=0;LSB=0;LSC=0;break;
                        case (1) :                                                                                                                 
                                LSA=1;LSB=0;LSC=0;break;
                        case (2) :
                                LSA=0;LSB=1;LSC=0;break;
                        case (3) :
                                LSA=1;LSB=1;LSC=0;break;
                        case (4) :
                                LSA=0;LSB=0;LSC=1;break;
                        case (5) :
                                LSA=1;LSB=0;LSC=1;break;
                }
                P0=Data[i];
                delay(100);
                P0=0x00;
        }
}


void UsartInit(){
    TMOD=0x20;      //選擇工作方式二
    TH1=0xFD;                //9600波特率
        TL1=0xFD;
        //PCON=0x80;
        TR1=1;
        SCON=0x50;
        ES=1;
        EA=1;
}

void SerialSendchar(u8 dat)  
{  
    SBUF = dat;      
    while (TI == 0);  
    TI = 0;  
}  

void SerialSendString(u8 *p)
{  
   while(*p != '\0')
   {
    SerialSendchar(*p);
        p++;  
   }
}

void main(){
        int temp;
    while(1){
             temp=Ds18b20ReadTemp();
                    dataprintf(temp);
                 DigDisplay();
                 UsartInit();
                 SerialSendString("temperature is ");
                 SerialSendString(t);
                  SerialSendString("\n");
                 }
}


temp.c代碼

#include<temp.h>

void Delay1ms(uint y){
    uint x;
        for(;y>0;y--){
            for(x=110;x>0;x--);
        }
}
//Ds18b20初始化
uchar Ds18b20(){
    uchar i=70;
        DS=0;
        while(i--);    //延時642us
        DS=1;
        i=0;           //將i置為0
        while(DS){
            Delay1ms(1);
                i++;
                if(i>5){
                    return 0;
                }
        }
                                //延時超過5ms,return0
        return 1;
}
//寫數(shù)據(jù)
void Ds18b20write(uchar dat){
    uchar i,j;
        for(j=0;j<8;j++){
            DS=0;
                i++;
                DS=dat&0x01;
                i=6;
                while(i--);   //延時68ms
                 DS=1;
                 dat>>=1;
        }
}
//讀數(shù)據(jù)
uchar Ds18b20read(){
    uchar i,j;
        uchar bi,byte;
        for(j=0;j<8;j++){
            DS=0;
                i++;              //延時1ms
                DS=1;
        i++;
                i++;
                bi= DS;
                byte=(byte>>1)|(bi<<7);
                i=4;
                while(i--);
        }
        return byte;
}
//最后的溫度數(shù)據(jù)
int Ds18b20ReadTemp(){
    int temp=0;
    uchar temph,templ;
    Ds18b20();
        Delay1ms(1);
        Ds18b20write(0xcc);
        Ds18b20write(0x44);
    Ds18b20();
        Delay1ms(1);
        Ds18b20write(0xcc);            //跳過64位rom
        Ds18b20write(0xbe);            //寫入ram
        templ=Ds18b20read();
        temph=Ds18b20read();
        temp=temph;
        temp<<=8;
        temp|=templ;
        return temp;
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂1 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:644401 發(fā)表于 2019-11-18 17:38 | 只看該作者
有沒有仿真圖
回復(fù)

使用道具 舉報

板凳
ID:678168 發(fā)表于 2019-12-31 10:31 | 只看該作者
大神,求助,源程序怎么編


回復(fù)

使用道具 舉報

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

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 成人免费视频一区二区 | 中文字幕一级 | 毛片视频免费观看 | 国产精品精品3d动漫 | 一区二区三区电影网 | 麻豆a级片| 成人黄色a | 九一在线 | 国产麻豆乱码精品一区二区三区 | 久久伊人影院 | 国产欧美精品一区二区 | 国产色婷婷精品综合在线手机播放 | 国产一区二区三区在线 | www97影院 | 在线看亚洲 | 国产一区二区在线视频 | 国产精品亚洲成在人线 | 国产亚洲一区二区三区 | 手机看片在线播放 | 性高湖久久久久久久久aaaaa | 一级毛片在线播放 | 色综合欧美 | 青青草社区 | 99精品视频在线观看免费播放 | 日韩在线视频免费观看 | 日韩av看片| 国产精品一区二区欧美 | 中文字幕精品一区 | 国产精品毛片一区二区三区 | 欧美日高清| 国产伦精品一区二区 | 色综合视频 | 免费的色网站 | 日中文字幕在线 | 国产精品免费观看视频 | 成年无码av片在线 | 日韩欧美在线视频 | 国产在线97 | 一级视频黄色 | 欧美日韩在线一区二区三区 | av在线亚洲天堂 |