微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > MSP430与DS18B20之1602显示

MSP430与DS18B20之1602显示

时间:12-02 来源:互联网 点击:
#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;

#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];

/*******************************************
函数名称:Delay5ms
功 能:延时约5ms
参 数:无
返回值 :无
********************************************/
void Delay5ms(void)
{
uint i=40000;
while (i != 0)
{
i--;
}
}
/*******************************************
函数名称: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; //清除中断标志
}
/*******************************************
函数名称:WaitForEnable
功 能:等待1602液晶完成内部操作
参 数:无
返回值 :无
********************************************/
void WaitForEnable(void)
{
P4DIR &= 0x00; //将P4口切换为输入状态
CLR_RS;
SET_RW;
_NOP();
SET_EN;
_NOP();
_NOP();

while((P4IN & Busy)!=0); //检测忙标志
CLR_EN;
P4DIR |= 0xFF; //将P4口切换为输出状态
}
/*******************************************
函数名称:write_com
功 能:向液晶模块写入命令
参 数:cmd--命令,
chk--是否判忙的标志,1:判忙,0:不判
返回值 :无
********************************************/
void write_com(uchar cmd)
{
WaitForEnable(); // 检测忙信号?

CLR_RS;
CLR_RW;
_NOP();
DataPort = cmd; //将命令字写入数据端口
_NOP();

SET_EN; //产生使能脉冲信号
_NOP();
_NOP();
CLR_EN;
}

/*******************************************
函数名称:write_data
功 能:向液晶显示的当前地址写入显示数据
参 数:data--显示字符数据
返回值 :无
********************************************/
void write_data( uchar data )
{
WaitForEnable(); //等待液晶不忙
SET_RS;
CLR_RW;
_NOP();
DataPort = data; //将显示数据写入数据端口
_NOP();
SET_EN; //产生使能脉冲信号
_NOP();
_NOP();
CLR_EN;
}

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

/*******************************************
函数名称:LcdReset
功 能:对1602液晶模块进行复位操作
参 数:无
返回值 :无
********************************************/
void LcdReset(void)
{
CtrlDir |= 0x07; //控制线端口设为输出状态
DataDir = 0xFF; //数据端口设为输出状态

write_com(0x38); //规定的复位操作
Delay5ms();
write_com(0x38);
Delay5ms();
write_com(0x38);
Delay5ms();
write_com(0x38); //显示模式设置
write_com(0x08); //显示关闭
write_com(0x01); //显示清屏
write_com(0x06); //写字符时整体不移动
write_com(0x0c); //显示开,不开游标,不闪烁
}

/*******************************************
函数名称: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;
}
/*******************************************
函数名称: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();
}
/*******************************************
函数名称:Read_18B20
功 能:从DS18B20读取一个字节的数据
参 数:无
返回值 :读出的一个字节数据
********************************************/
uchar Read_18B20(void)
{
uchar i;
uchar temp = 0;

_DINT();
for(i = 0;i < 8;i++)
{
temp >>= 1;
DQ0;
DelayNus(6); //延时6us
DQ1;
DelayNus(8); //延时9us
DQ_in;
_NOP();
if(DQ_val) temp |= 0x80;
DelayNus(45); //延时45us
DQ_out;
DQ1;
DelayNus(10); //延时10us
}
_EINT();

return temp;
}
/*******************************************
函数名称:Skip
功 能:发送跳过读取产品ID号命令
参 数:无
返回值 :无
********************************************/
void Skip(void)
{
Write_18B20(0xcc);
}
/*******************************************
函数名称:Convert
功 能:发送温度转换命令
参 数:无
返回值 :无
********************************************/
void Convert(void)
{
Write_18B20(0x44);
}
/*******************************************
函数名称:Read_SP
功 能:发送读ScratchPad命令
参 数:无
返回值 :无
********************************************/
void Read_SP(void)
{
Write_18B20(0xbe);
}
/*******************************************
函数名称:ReadTemp
功 能:从DS18B20的ScratchPad读取温度转换结果
参 数:无
返回值 :读取的温度数值
********************************************/
uint ReadTemp(void)
{
uchar temp_low;
uint temp;

temp_low = Read_18B20(); //读低位
temp = Read_18B20(); //读高位
temp = (temp<8) | temp_low;

return temp;
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top