|
程序能正常顯示溫度,當(dāng)溫度負(fù)數(shù)時(shí)變量閃爍,請(qǐng)問(wèn)怎樣在主函數(shù)里顯示“—”號(hào)?
#include <STC32G.H>
#include <intrins.h>
typedef unsigned char u8;
typedef unsigned int u16;
#define MAIN_Fosc 24000000UL
#if (MAIN_Fosc >= 24000000L)
#define usrNOP() _nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
#endif
#define LCD_data P2 //數(shù)據(jù)口
sbit LCD_RS = P4^1; //寄存器選擇輸入
sbit LCD_RW = P4^2; //液晶讀/寫(xiě)控制
sbit LCD_EN = P4^4; //液晶使能控制
sbit LCD_PSB = P4^5; //串/并方式控制
sbit LCD_RST = P3^7;
sbit DQ_1= P1^0; //18B20氣溫?cái)?shù)據(jù)口
bit MinusFlag=0; //負(fù)數(shù)標(biāo)志,0:正數(shù),1:負(fù)數(shù)
u16 QW; //氣溫變量
u8 mun_char_table[]={"0123456789"};
u8 code dis1[] = {"氣溫: ℃"};
u8 code dis2[] = {"氣溫: - ℃"}; //零下氣溫
void delay_ms(u16 z) //延時(shí)ms
{
u16 x,y;
for(x=z;x>0;x--)
for(y=614;y>0;y--);
}
void delay_us(u8 us)
{
do{
usrNOP();
usrNOP();
}
while(--us);
}
/**************************************
復(fù)位DS18B20,并檢測(cè)設(shè)備是否存在
**************************************/
void DS18B20_Reset()
{
CY = 1;
while (CY)
{
DQ_1 = 0; //送出低電平復(fù)位信號(hào)
delay_us(240); //延時(shí)至少480us
delay_us(240);
DQ_1 = 1; //釋放數(shù)據(jù)線
delay_us(60); //等待60us
CY = DQ_1; //檢測(cè)存在脈沖
delay_us(240); //等待設(shè)備釋放數(shù)據(jù)線
delay_us(240);
}
}
/**************************************
從DS18B20讀1字節(jié)數(shù)據(jù)
**************************************/
u8 DS18B20_ReadByte()
{
u8 i;
u8 dat = 0;
for (i=0; i<8; i++) //8位計(jì)數(shù)器
{
dat >>= 1;
DQ_1 = 0; //開(kāi)始時(shí)間片
delay_us(1); //延時(shí)等待
DQ_1 = 1; //準(zhǔn)備接收
delay_us(1); //接收延時(shí)
if (DQ_1) dat |= 0x80; //讀取數(shù)據(jù)
delay_us(60); //等待時(shí)間片結(jié)束
DQ_1 = 1;
}
return dat;
}
/**************************************
向DS18B20寫(xiě)1字節(jié)數(shù)據(jù)
**************************************/
void DS18B20_WriteByte(u8 dat)
{
char i;
for (i=0; i<8; i++) //8位計(jì)數(shù)器
{
DQ_1 = 0; //開(kāi)始時(shí)間片
delay_us(1); //延時(shí)等待
dat >>= 1; //送出數(shù)據(jù)
DQ_1 = CY;
delay_us(60); //等待時(shí)間片結(jié)束
DQ_1 = 1; //恢復(fù)數(shù)據(jù)線
delay_us(1); //恢復(fù)延時(shí)
}
}
//========================================================================
// 函數(shù): u16 ReadTemperature()
// 描述: 讀取溫度函數(shù)。
// 參數(shù): none.
// 返回: 溫度值.
//========================================================================
u16 ReadTemperature() //氣溫
{
u16 TempH, TempL, Temperature;
DS18B20_Reset_1(); //設(shè)備復(fù)位
DS18B20_WriteByte_1(0xCC); //跳過(guò)ROM命令
DS18B20_WriteByte_1(0x44); //開(kāi)始轉(zhuǎn)換命令
while (!DQ_1); //等待轉(zhuǎn)換完成
DS18B20_Reset_1(); //設(shè)備復(fù)位
DS18B20_WriteByte_1(0xCC); //跳過(guò)ROM命令
DS18B20_WriteByte_1(0xBE); //讀暫存存儲(chǔ)器命令
TempL = DS18B20_ReadByte_1(); //讀溫度低字節(jié)
TempH = DS18B20_ReadByte_1(); //讀溫度高字節(jié)
if(TempH & 0xf8) //判斷是否位負(fù)數(shù)
{
MinusFlag = 1; //設(shè)置負(fù)數(shù)標(biāo)志
Temperature = (TempH<<8) | TempL;
Temperature = ~Temperature + 1;
Temperature *= 0.625; //0.0625 * 10,保留1位小數(shù)點(diǎn)
}
else
{
MinusFlag = 0; //清除負(fù)數(shù)標(biāo)志
Temperature = (((TempH<<8) | TempL) * 0.625); //0.0625 * 10,保留1位小數(shù)點(diǎn)
}
return Temperature;
}
//////////////以下是LCD12864驅(qū)動(dòng)程序////////////////
/*******************************************************************/
/* */
/*檢查L(zhǎng)CD忙狀態(tài) */
/*lcd_busy為1時(shí),忙,等待。lcd-busy為0時(shí),閑,可寫(xiě)指令與數(shù)據(jù)。 */
/* */
/*******************************************************************/
bit lcd_busy()
{
bit result;
P2=0X00; //關(guān)掉數(shù)據(jù)口信號(hào)。阻止LCD受到P2口信號(hào)的影響
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
delay_ms(1);
result = (bit)(P0&0x80);
LCD_EN = 0;
return(result);
}
/*******************************************************************/
/* */
/*寫(xiě)指令數(shù)據(jù)到LCD */
/*RS=L,RW=L,E=高脈沖,D0-D7=指令碼。 */
/* */
/*******************************************************************/
void lcd_wcmd(u8 cmd)
{
while(lcd_busy());
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
_nop_();
_nop_();
P2 = cmd;
delay_ms(1);
LCD_EN = 1;
delay_ms(1);
LCD_EN = 0;
}
/*******************************************************************/
/* */
/*寫(xiě)顯示數(shù)據(jù)到LCD */
/*RS=H,RW=L,E=高脈沖,D0-D7=數(shù)據(jù)。 */
/* */
/*******************************************************************/
void lcd_wdat(u8 dat)
{
while(lcd_busy());
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
P2 = dat;
delay_ms(1);
LCD_EN = 1;
delay_ms(1);
LCD_EN = 0;
}
/*******************************************************************/
/* */
/* LCD初始化設(shè)定 */
/* */
/*******************************************************************/
void lcd_init()
{
LCD_PSB = 1; //并口方式
lcd_wcmd(0x34); //擴(kuò)充指令操作
delay_ms(5);
lcd_wcmd(0x30); //基本指令操作
delay_ms(5);
lcd_wcmd(0x0C); //顯示開(kāi),關(guān)光標(biāo)
delay_ms(5);
lcd_wcmd(0x01); //清除LCD的顯示內(nèi)容
delay_ms(5);
}
/*********************************************************/
/* */
/* 設(shè)定顯示位置 */
/* */
/*********************************************************/
void lcd_pos(u8 X,u8 Y)
{
u8 pos;
if (X==0)
{X=0x80;}
else if (X==1)
{X=0x90;}
else if (X==2)
{X=0x88;}
else if (X==3)
{X=0x98;}
pos = X+Y ;
lcd_wcmd(pos); //顯示地址
}
/*---------------------以上是LCD12864驅(qū)動(dòng)程序-----------------------*/
void main()//主函數(shù)模塊//
{
u8 i=0;
P1M0 = 0x00; P1M1 = 0x00;
P2M0 = 0x00; P2M1 = 0x00;
P3M0 = 0x00; P3M1 = 0x00;
P4M0 = 0x00; P4M1 = 0x00;
lcd_init(); //LCD初始化
LCD_RST=1;
delay_ms(100);
LCD_RST=0;
/*-------------------------------------------------------------------*/
if(MinusFlag==0)
{
lcd_pos(0,0); //設(shè)置顯示位置為第1行的第1個(gè)字符
i = 0;
while(dis1 != '\0')
{ //顯示字符
lcd_wdat(dis1);
i++;
}
}
else
{
lcd_pos(0,0); //設(shè)置顯示位置為第1行的第1個(gè)字符
i = 0;
while(dis2 != '\0')
{ //顯示字符
lcd_wdat(dis2);
i++;
}
}
while(1)
{
QW=ReadTemperature();
lcd_pos(0,4);
lcd_wdat(mun_char_table[QW/1000%10]);
lcd_wdat(mun_char_table[QW/100%10]);
lcd_wdat(mun_char_table[QW/10%10]);
//lcd_wdat(mun_char_table[QW%10]);
}
}
|
|