找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

帖子
查看: 2530|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

MC96F6508A測(cè)溫C程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:62214 發(fā)表于 2020-4-5 02:39 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式

#include <mc96f6508a.h>
#include <intrins.h>
#include        "MC96F6508A.h"
#include        "func_def.h"
#include"lcd.h"
#include"temp.h"

#define uchar unsigned char
#define uint unsigned int


void LcdDisplay(int);
/*******************************************************************************
* 函數(shù)名         : main
* 函數(shù)功能                   : 主函數(shù)
* 輸入           : 無(wú)
* 輸出                  : 無(wú)
*******************************************************************************/

main()
{
        P1IO                = 0xff;                        // 輸出P1 Direction Register
    P1OD                = 0xff;                        //漏極開(kāi)路輸出 P1 Open-drain Selection Register
    P1PU                = 0xff;                        // 內(nèi)部上拉電阻P1 Pull-up Resistor Selection Register
    P1DB                = 0xff;                        //消抖
       
        P2IO                = 0xf0;                        // 輸出P1 Direction Register
    P2OD                = 0x00;                        //漏極開(kāi)路輸出 P1 Open-drain Selection Register
    P2PU                = 0xff;                        // 內(nèi)部上拉電阻P1 Pull-up Resistor Selection Register
    P2FSR                = 0x00;                        //


        P3IO                = 0xff;                        // 輸出P1 Direction Register
    P3OD                = 0x00;                        //漏極開(kāi)路輸出 P1 Open-drain Selection Register
    P3PU                = 0xff;                        // 內(nèi)部上拉電阻P1 Pull-up Resistor Selection Register
    P3FSR                = 0x00;                        //消抖
       
       
        P4IO                = 0xff;                        // 輸出P1 Direction Register
    P4OD                = 0xff;                        //漏極開(kāi)路輸出 P1 Open-drain Selection Register
    P4PU                = 0xff;                        // 內(nèi)部上拉電阻P1 Pull-up Resistor Selection Register
    P4FSR                = 0x00;                        //消抖


        P5IO                = 0xff;                        // 輸出P1 Direction Register
    P5OD                = 0xff;                        //漏極開(kāi)路輸出 P1 Open-drain Selection Register
    P5PU                = 0xf0;                        // 內(nèi)部上拉電阻P1 Pull-up Resistor Selection Register
    P5FSRH                = 0xff;                        //
        P5FSRL                = 0xff;

                P6IO                = 0xff;                        // 輸出P1 Direction Register
    P6OD                = 0xff;                        //漏極開(kāi)路輸出 P1 Open-drain Selection Register
    P6PU                = 0xff;                        // 內(nèi)部上拉電阻P1 Pull-up Resistor Selection Register
    P6FSR                = 0x0f;                        // 設(shè)置為Xin/Xout
       



        P0IO                = 0x00;                        // 輸出P1 Direction Register
    P0OD                = 0xff;                        //漏極開(kāi)路輸出 P1 Open-drain Selection Register
    P0PU                = 0xff;                        // 內(nèi)部上拉電阻P1 Pull-up Resistor Selection Register
    P0DB                = 0xff;                        //消抖
       
        P4=0xff;
                // internal RC clock (16.000000MHz)
        OSCCR = 0x08;           // Set Int. OSC
        SCCR = 0x00;            // Use Int. OSC
        //P4=0x00;
        //DSPORT=0;
        //OSCCR = 0x28;           // 外部主時(shí)鐘
        //SCCR = 0x00;            // 打開(kāi)外部主時(shí)鐘
        LcdInit();                         //初始化LCD1602
        LcdWriteCom(0x88);        //寫地址 80表示初始地址
        LcdWriteData('C');
        while(1)
        {
                LcdDisplay(Ds18b20ReadTemp());
                //Delay1ms(1000);//1s鐘刷一次
        }            
}

/*******************************************************************************
* 函數(shù)名         : LcdDisplay()
* 函數(shù)功能                   : LCD顯示讀取到的溫度
* 輸入           : v
* 輸出                  : 無(wú)
*******************************************************************************/

void LcdDisplay(int temp)          //lcd顯示
{

          unsigned char datas[] = {0, 0, 0, 0, 0}; //定義數(shù)組
        float tp;  
        if(temp< 0)                                //當(dāng)溫度值為負(fù)數(shù)
          {
                  LcdWriteCom(0x80);                //寫地址 80表示初始地址
            LcdWriteData('-');                  //顯示負(fù)
                //因?yàn)樽x取的溫度是實(shí)際溫度的補(bǔ)碼,所以減1,再取反求出原碼
                temp=temp-1;
                temp=~temp;
                tp=temp;
                temp=tp*0.0625*100+0.5;       
                //留兩個(gè)小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語(yǔ)言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時(shí)候把小數(shù)點(diǎn)
                //后面的數(shù)自動(dòng)去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進(jìn)1了,小于0.5的就
                //算由?.5,還是在小數(shù)點(diǎn)后面。

          }
        else
          {                       
                  LcdWriteCom(0x80);                //寫地址 80表示初始地址
            LcdWriteData('+');                 //顯示正
                tp=temp;//因?yàn)閿?shù)據(jù)處理有小數(shù)點(diǎn)所以將溫度賦給一個(gè)浮點(diǎn)型變量
                //如果溫度是正的那么,那么正數(shù)的原碼就是補(bǔ)碼它本身
                temp=tp*0.0625*100+0.5;       
                //留兩個(gè)小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語(yǔ)言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時(shí)候把小數(shù)點(diǎn)
                //后面的數(shù)自動(dòng)去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進(jìn)1了,小于0.5的就
                //算加上0.5,還是在小數(shù)點(diǎn)后面。
        }
        datas[0] = temp / 10000;
        datas[1] = temp % 10000 / 1000;
        datas[2] = temp % 1000 / 100;
        datas[3] = temp % 100 / 10;
        datas[4] = temp % 10;

        LcdWriteCom(0x82);                  //寫地址 80表示初始地址
        LcdWriteData('0'+datas[0]); //百位

       
        LcdWriteCom(0x83);                 //寫地址 80表示初始地址
        LcdWriteData('0'+datas[1]); //十位

        LcdWriteCom(0x84);                //寫地址 80表示初始地址
        LcdWriteData('0'+datas[2]); //個(gè)位

        LcdWriteCom(0x85);                //寫地址 80表示初始地址
        LcdWriteData('.');                 //顯示 ‘.’

        LcdWriteCom(0x86);                 //寫地址 80表示初始地址
        LcdWriteData('0'+datas[3]); //顯示小數(shù)點(diǎn)  

        LcdWriteCom(0x87);                 //寫地址 80表示初始地址
        LcdWriteData('0'+datas[4]); //顯示小數(shù)點(diǎn)  
}


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

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

使用道具 舉報(bào)

沙發(fā)
ID:62214 發(fā)表于 2020-4-5 20:22 | 只看該作者
補(bǔ)全電路原理圖,源碼,

temperature.pdf

56.3 KB, 下載次數(shù): 5, 下載積分: 黑幣 -5

temperature.zip

76.23 KB, 下載次數(shù): 3, 下載積分: 黑幣 -5

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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