|
#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ù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
main()
{
P1IO = 0xff; // 輸出P1 Direction Register
P1OD = 0xff; //漏極開路輸出 P1 Open-drain Selection Register
P1PU = 0xff; // 內(nèi)部上拉電阻P1 Pull-up Resistor Selection Register
P1DB = 0xff; //消抖
P2IO = 0xf0; // 輸出P1 Direction Register
P2OD = 0x00; //漏極開路輸出 P1 Open-drain Selection Register
P2PU = 0xff; // 內(nèi)部上拉電阻P1 Pull-up Resistor Selection Register
P2FSR = 0x00; //
P3IO = 0xff; // 輸出P1 Direction Register
P3OD = 0x00; //漏極開路輸出 P1 Open-drain Selection Register
P3PU = 0xff; // 內(nèi)部上拉電阻P1 Pull-up Resistor Selection Register
P3FSR = 0x00; //消抖
P4IO = 0xff; // 輸出P1 Direction Register
P4OD = 0xff; //漏極開路輸出 P1 Open-drain Selection Register
P4PU = 0xff; // 內(nèi)部上拉電阻P1 Pull-up Resistor Selection Register
P4FSR = 0x00; //消抖
P5IO = 0xff; // 輸出P1 Direction Register
P5OD = 0xff; //漏極開路輸出 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; //漏極開路輸出 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; //漏極開路輸出 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; // 外部主時鐘
//SCCR = 0x00; // 打開外部主時鐘
LcdInit(); //初始化LCD1602
LcdWriteCom(0x88); //寫地址 80表示初始地址
LcdWriteData('C');
while(1)
{
LcdDisplay(Ds18b20ReadTemp());
//Delay1ms(1000);//1s鐘刷一次
}
}
/*******************************************************************************
* 函數(shù)名 : LcdDisplay()
* 函數(shù)功能 : LCD顯示讀取到的溫度
* 輸入 : v
* 輸出 : 無
*******************************************************************************/
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;
//留兩個小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時候把小數(shù)點(diǎn)
//后面的數(shù)自動去掉,不管是否大于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)所以將溫度賦給一個浮點(diǎn)型變量
//如果溫度是正的那么,那么正數(shù)的原碼就是補(bǔ)碼它本身
temp=tp*0.0625*100+0.5;
//留兩個小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時候把小數(shù)點(diǎn)
//后面的數(shù)自動去掉,不管是否大于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]); //個位
LcdWriteCom(0x85); //寫地址 80表示初始地址
LcdWriteData('.'); //顯示 ‘.’
LcdWriteCom(0x86); //寫地址 80表示初始地址
LcdWriteData('0'+datas[3]); //顯示小數(shù)點(diǎn)
LcdWriteCom(0x87); //寫地址 80表示初始地址
LcdWriteData('0'+datas[4]); //顯示小數(shù)點(diǎn)
}
|
評分
-
查看全部評分
|