新闻中心

EEPW首页>嵌入式系统>设计应用> MSP430与DS18B20数码管显示(中断法)

MSP430与DS18B20数码管显示(中断法)

作者: 时间: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)
/*****数码管部分的接口定义********/
#define wei_h P5OUT|= BIT5
#define wei_l P5OUT&= ~BIT5
#define duan_l P6OUT &= ~BIT6
#define duan_h P6OUT |= BIT6
//数码管七段码;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部分程序******************/
/*******************************************
函数名称:DelayNus
功 能:实现N个微秒的延时
参 数:n--延时长度
返回值 :无
说明 :定时器A的计数时钟是1MHz,CPU主频8MHz
所以通过定时器延时能够得到极为精确的
us级延时
********************************************/
void DelayNus(uint n)
{
CCR0 = n;
TACTL |= MC_1; //增计数到CCR0
while(!(TACTL & BIT0)); //等待
TACTL &= ~MC_1; //停止计数
TACTL &= ~BIT0; //清除中断标志
}
/*******************************************
函数名称:Init_18B20
功 能:对DS18B20进行复位操作
参 数:无
返回值 :初始化状态标志: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,后面就会出现死循环,表示18B20可能坏了
}
/*******************************************
函数名称:Write_18B20
功 能:向DS18B20写入一个字节的数据
参 数:wdata--写入的数据
返回值 :无
********************************************/
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 下一页

评论


技术专区

关闭