新聞中心

MSP430與DS18B20之1602顯示

作者: 時(shí)間:2016-12-02 來(lái)源:網(wǎng)絡(luò) 收藏
#include

typedef unsigned char uchar;
typedef unsigned int uint;
/**************宏定義***************/
#define DataDir P4DIR
#define DataPort P4OUT
#define Busy 0x80
#define CtrlDir P3DIR
#define CLR_RS P3OUT&=~BIT0; //RS = P3.0
#define SET_RS P3OUT|=BIT0;
#define CLR_RW P3OUT&=~BIT1; //RW = P3.1
#define SET_RW P3OUT|=BIT1;
#define CLR_EN P3OUT&=~BIT2; //EN = P3.2
#define SET_EN P3OUT|=BIT2;

本文引用地址:http://butianyuan.cn/article/201612/324659.htm

#define DQ1 P1OUT |= BIT6
#define DQ0 P1OUT &= ~BIT6
#define DQ_in P1DIR &= ~BIT6
#define DQ_out P1DIR |= BIT6
#define DQ_val (P1IN & BIT6)
uint tvalue;
uchar tflag;
uchar disdata[4];

/*******************************************
函數(shù)名稱(chēng):Delay5ms
功 能:延時(shí)約5ms
參 數(shù):無(wú)
返回值 :無(wú)
********************************************/
void Delay5ms(void)
{
uint i=40000;
while (i != 0)
{
i--;
}
}
/*******************************************
函數(shù)名稱(chēng):DelayNus
功 能:實(shí)現(xiàn)N個(gè)微秒的延時(shí)
參 數(shù):n--延時(shí)長(zhǎng)度
返回值 :無(wú)
說(shuō)明 :定時(shí)器A的計(jì)數(shù)時(shí)鐘是1MHz,CPU主頻8MHz
所以通過(guò)定時(shí)器延時(shí)能夠得到極為精確的
us級(jí)延時(shí)
********************************************/
void DelayNus(uint n)
{
CCR0 = n;
TACTL |= MC_1; //增計(jì)數(shù)到CCR0
while(!(TACTL & BIT0)); //等待
TACTL &= ~MC_1; //停止計(jì)數(shù)
TACTL &= ~BIT0; //清除中斷標(biāo)志
}
/*******************************************
函數(shù)名稱(chēng):WaitForEnable
功 能:等待1602液晶完成內(nèi)部操作
參 數(shù):無(wú)
返回值 :無(wú)
********************************************/
void WaitForEnable(void)
{
P4DIR &= 0x00; //將P4口切換為輸入狀態(tài)
CLR_RS;
SET_RW;
_NOP();
SET_EN;
_NOP();
_NOP();

while((P4IN & Busy)!=0); //檢測(cè)忙標(biāo)志
CLR_EN;
P4DIR |= 0xFF; //將P4口切換為輸出狀態(tài)
}
/*******************************************
函數(shù)名稱(chēng):write_com
功 能:向液晶模塊寫(xiě)入命令
參 數(shù):cmd--命令,
chk--是否判忙的標(biāo)志,1:判忙,0:不判
返回值 :無(wú)
********************************************/
void write_com(uchar cmd)
{
WaitForEnable(); // 檢測(cè)忙信號(hào)?

CLR_RS;
CLR_RW;
_NOP();
DataPort = cmd; //將命令字寫(xiě)入數(shù)據(jù)端口
_NOP();

SET_EN; //產(chǎn)生使能脈沖信號(hào)
_NOP();
_NOP();
CLR_EN;
}

/*******************************************
函數(shù)名稱(chēng):write_data
功 能:向液晶顯示的當(dāng)前地址寫(xiě)入顯示數(shù)據(jù)
參 數(shù):data--顯示字符數(shù)據(jù)
返回值 :無(wú)
********************************************/
void write_data( uchar data )
{
WaitForEnable(); //等待液晶不忙
SET_RS;
CLR_RW;
_NOP();
DataPort = data; //將顯示數(shù)據(jù)寫(xiě)入數(shù)據(jù)端口
_NOP();
SET_EN; //產(chǎn)生使能脈沖信號(hào)
_NOP();
_NOP();
CLR_EN;
}

void zifuchuan(uchar *ch)
{
while(*ch!=0)
write_data(*ch++);
Delay5ms();
}

/*******************************************
函數(shù)名稱(chēng):LcdReset
功 能:對(duì)1602液晶模塊進(jìn)行復(fù)位操作
參 數(shù):無(wú)
返回值 :無(wú)
********************************************/
void LcdReset(void)
{
CtrlDir |= 0x07; //控制線端口設(shè)為輸出狀態(tài)
DataDir = 0xFF; //數(shù)據(jù)端口設(shè)為輸出狀態(tài)

write_com(0x38); //規(guī)定的復(fù)位操作
Delay5ms();
write_com(0x38);
Delay5ms();
write_com(0x38);
Delay5ms();
write_com(0x38); //顯示模式設(shè)置
write_com(0x08); //顯示關(guān)閉
write_com(0x01); //顯示清屏
write_com(0x06); //寫(xiě)字符時(shí)整體不移動(dòng)
write_com(0x0c); //顯示開(kāi),不開(kāi)游標(biāo),不閃爍
}

/*******************************************
函數(shù)名稱(chēng):Init_18B20
功 能:對(duì)DS18B20進(jìn)行復(fù)位操作
參 數(shù):無(wú)
返回值 :初始化狀態(tài)標(biāo)志:1--失敗,0--成功
********************************************/
uchar Init_18B20(void)
{
uchar Error;

DQ_out;
_DINT();
DQ0;
DelayNus(500);
DQ1;
DelayNus(55);
DQ_in;
_NOP();
if(DQ_val)
{
Error = 1; //初始化失敗
}
else
{
Error = 0; //初始化成功
}
DQ_out;
DQ1;
_EINT();

DelayNus(400);

return Error;
}


上一頁(yè) 1 2 下一頁(yè)

關(guān)鍵詞: MSP430DS18B201602顯

評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉