新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > MSP430與DS18B20數(shù)碼管顯示(中斷法)

MSP430與DS18B20數(shù)碼管顯示(中斷法)

作者: 時間:2016-12-02 來源:網絡 收藏
#include
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};//有點
uchar tflag,num=0 ;
int tvalue;
uchar disdata[4];
/***********18B20部分程序******************/
/*******************************************
函數(shù)名稱:DelayNus
功 能:實現(xiàn)N個微秒的延時
參 數(shù):n--延時長度
返回值 :無
說明 :定時器A的計數(shù)時鐘是1MHz,CPU主頻8MHz
所以通過定時器延時能夠得到極為精確的
us級延時
********************************************/
void DelayNus(uint n)
{
CCR0 = n;
TACTL |= MC_1; //增計數(shù)到CCR0
while(!(TACTL & BIT0)); //等待
TACTL &= ~MC_1; //停止計數(shù)
TACTL &= ~BIT0; //清除中斷標志
}
/*******************************************
函數(shù)名稱:Init_18B20
功 能:對DS18B20進行復位操作
參 數(shù):無
返回值 :初始化狀態(tài)標志: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,后面就會出現(xiàn)死循環(huán),表示18B20可能壞了
}
/*******************************************
函數(shù)名稱:Write_18B20
功 能:向DS18B20寫入一個字節(jié)的數(shù)據(jù)
參 數(shù):wdata--寫入的數(shù)據(jù)
返回值 :無
********************************************/
void Write_18B20(uchar wdata)
{
uchar i;

_DINT();
for(i = 0; i < 8;i++)
{
DQ0;
DelayNus(6); //延時6us
if(wdata & 0X01) DQ1;
else DQ0;
wdata >>= 1;
DelayNus(50); //延時50us
DQ1;
DelayNus(10); //延時10us
}
_EINT();
}

上一頁 1 2 下一頁

評論


技術專區(qū)

關閉