MSP430與DS18B20數(shù)碼管顯示(中斷法)
typedef unsigned char uchar;
typedef unsigned int uint;
/*****18B20部分的接口定義********/
#define DQ1 P1OUT |= BIT6
#define DQ0 P1OUT &= ~BIT6
#define DQ_in P1DIR &= ~BIT6
#define DQ_out P1DIR |= BIT6
#define DQ_val (P1IN & BIT6)
/*****數(shù)碼管部分的接口定義********/
#define wei_h P5OUT|= BIT5
#define wei_l P5OUT&= ~BIT5
#define duan_l P6OUT &= ~BIT6
#define duan_h P6OUT |= BIT6
//數(shù)碼管七段碼;0--f
uchar table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar table1[16] = {0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1};//有點(diǎn)
uchar tflag,num=0 ;
int tvalue;
uchar disdata[4];
/***********18B20部分程序******************/
/*******************************************
函數(shù)名稱:DelayNus
功 能:實(shí)現(xiàn)N個(gè)微秒的延時(shí)
參 數(shù):n--延時(shí)長度
返回值 :無
說明 :定時(shí)器A的計(jì)數(shù)時(shí)鐘是1MHz,CPU主頻8MHz
所以通過定時(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ù)名稱:Init_18B20
功 能:對(duì)DS18B20進(jìn)行復(fù)位操作
參 數(shù):無
返回值 :初始化狀態(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;//此處如果 Error = 1,后面就會(huì)出現(xiàn)死循環(huán),表示18B20可能壞了
}
/*******************************************
函數(shù)名稱:Write_18B20
功 能:向DS18B20寫入一個(gè)字節(jié)的數(shù)據(jù)
參 數(shù):wdata--寫入的數(shù)據(jù)
返回值 :無
********************************************/
void Write_18B20(uchar wdata)
{
uchar i;
_DINT();
for(i = 0; i < 8;i++)
{
DQ0;
DelayNus(6); //延時(shí)6us
if(wdata & 0X01) DQ1;
else DQ0;
wdata >>= 1;
DelayNus(50); //延時(shí)50us
DQ1;
DelayNus(10); //延時(shí)10us
}
_EINT();
}
評(píng)論