基于msp430单片机1602 DS1802温度显示
2016年09月13日 11:21 发布者:designapp
最近调好了DS1802温度传感器,用的是430G2553单片机,温度保留两位小数。把源代码分享给大家。/*****************************************
msp430g2553实现1602温度显示
来源:汪中原的博客
******************************************/
#include
#define uchar unsigned char
#define uint unsigned int
#define CPU_F ((double)1000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0)) //延时x us
#define ds_1 P2OUT |=BIT3 //DS18B20数据端高电平输入(P2.3端口)
#define ds_0 P2OUT &=~BIT3 //DS18B20数据端低电平输入(P2.3端口)
#define lcdrs_1 P2OUT |=BIT1; //1602 RS写数据(P2.1端口)
#define lcdrs_0 P2OUT &=~BIT1; //1602 RS写指令(P2.1端口)
#define lcden_1 P2OUT |=BIT0; //1602使能信号开(P2.0端口)
#define lcden_0 P2OUT &=~BIT0; //1602使能信号关(P2.0端口)
uchar table[]="wzy,come on"; //开机第一行显示
uchar table1[]="believe yourself"; //开机第二行显示
uint temp,T;
float temp_f;
uchar shi,ge,dian1,dian2,num; //定义温度十位、各位、小数点后一位、后二位
/*************************************************************
* 名 称:void init(void)
* 功 能:1602显示配置端口初始化
* 入口参数:无
* 出口参数:无
* 说 明: P1、P2端口设为输出低电平
*************************************************************/
void init(void)
{
WDTCTL=WDTPW + WDTHOLD;
P1DIR=0xff;
P2DIR=0x1f;
P1OUT=0x00;
P2OUT=0x00;
}
/*************************************************************
* 名 称:void delay(uint a)
* 功 能:简单的延时
* 入口参数:uint a
* 出口参数:无
* 说 明: 可作为1602显示简单的延时
*************************************************************/
void delay(uint a)
{
uint i,j;
for(i=a;i>0;i--)
for(j=110;j>0;j--);
}
/*************************************************************
* 名 称:void write_date(uchar date)
* 功 能:1602写数据
* 入口参数:date
* 出口参数:无
* 说 明: 用P1口输入数据
*************************************************************/
void write_date(uchar date)
{
lcdrs_1;
P1OUT=date;
delay(5);
lcden_1;
delay(5);
lcden_0;
}
/*************************************************************
* 名 称:void write_com(uchar com)
* 功 能:1602写指令
* 入口参数:com
* 出口参数:无
* 说 明: 用P1口输入指令
*************************************************************/
void write_com(uchar com)
{
lcdrs_0;
P1OUT=com;
delay(5);
lcden_1;
delay(5);
lcden_0;
}
/*************************************************************
* 名 称:void uinit()
* 功 能:初始化及其他显示设置指令
* 入口参数:无
* 出口参数:无
* 说 明: 1602初始化设置
*************************************************************/
void uinit()
{
lcden_0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
/*************************************************************
* 名 称:void LCD_display()
* 功 能:1602显示
* 入口参数:无
* 出口参数:无
* 说 明: 将前几个函数综合起来,显示字符内容及区域
*************************************************************/
void LCD_display()
{
init();
uinit();
write_com(0x80);
for(num=0;num>= 1;
ds_0;
delay_us(6);
ds_1;
delay_us(8);
P2DIR &= ~BIT3;
_NOP();
if(P2IN & BIT3)
temp |= 0x80;
delay_us(45);
P2DIR |=BIT3;
ds_1;
delay_us(10);
}
return temp;
}
/*************************************************************
* 名 称:void write_temp(uchar data)
* 功 能:DS18B20写入数据
* 入口参数:data
* 出口参数:无
* 说 明: 写入一个字节
*************************************************************/
void write_temp(uchar data)
{
uchar i;
for(i = 0; i >= 1;
ds_1;
delay_us(10);
}
}
/*************************************************************
* 名 称:void temp_change(void)
* 功 能:DS18B20温度转化指令
* 入口参数:无
* 出口参数:无
* 说 明: 跳过rom并温度转化
*************************************************************/
void temp_change(void)
{
write_temp(0xcc);
write_temp(0x44);
}
/*************************************************************
* 名 称:uint temp_get()
* 功 能:DS18B20温度获取
* 入口参数:无
* 出口参数:T
* 说 明: 获取温度并四舍五入为保留两位小数
*************************************************************/
uint temp_get()
{
uchar a,b;
DS18B20_init();
delay(1);
write_temp(0xcc);
write_temp(0xbe);
a=read_temp(); //读低8位
b=read_temp(); //读高8位
T=b;
T 0;a--)
delay_us(60000);
do
{
a = DS18B20_init();
}
while(a);
dis_temp(temp_get());
init();
uinit();
write_com(0x80+0x0b);
write_date('0'+shi);
delay(5);
write_date('0'+ge);
delay(5);
write_date('.');
delay(5);
write_date('0'+dian1);
delay(5);
write_date('0'+dian2);
delay(5);
}
}
效果图:上面的数字即为当前温度

