什么单片机?我的是430
#include
#include
#define uint unsigned int
#define uchar unsigned char
#define data_in P1DIR&=~BIT0//设置输入
#define data_out P1DIR|=BIT0//设置输出
#define data0 P1OUT&=~BIT0//输出0
#define data1 P1OUT|=BIT0//输出1
//#define responds P1IN&BIT0//输入判断
#define RS BIT5
#define RW BIT6
#define EC BIT7
uchar data[]="0123456789";
void temperature();
void write_byte_to_18b20(uchar);
void delayms(uint h)//延时函数
{
uint i,j;
for(i=h;i>0;i--)
for(j=1000;j>0;j--)
{
_NOP();
_NOP();
}
}
void write_data(uchar data)//写入一个数据
{
P5OUT|=RS;
delayms(10);
P5OUT&=~RW;
delayms(10);
P5OUT&=~EC;
delayms(10);
P5OUT|=EC;
delayms(10);
P4OUT=data;
P5OUT&=~EC;
delayms(10);
}
void write_conmand(uchar conmand)//写入一条指令
{
P5OUT&=~RS;
delayms(10);
P5OUT&=~RW;
delayms(10);
P5OUT&=~EC;
delayms(10);
P5OUT|=EC;
delayms(10);
P4OUT=conmand;
P5OUT&=~EC;
delayms(10);
}
void init_lcd()//初始化液晶
{
P5DIR=0xe0;
P5OUT=0;
P4DIR=0xff;
P4OUT=0;
write_conmand(0x38);
delayms(100);
write_conmand(0x38);
delayms(10);
write_conmand(0x01);
delayms(100);
write_conmand(0x06);
delayms(100);
write_conmand(0x0e);
delayms(100);
write_conmand(0x80);
delayms(200);
write_conmand(0x80);
delayms(20);
}
void init_clk()//初始化时钟
{
uchar i;
BCSCTL1&=~XT2OFF;
BCSCTL2|=SELM1+SELS;
do{
IFG1&=~OFIFG;
for(i=0;i<100;i++)
_NOP();
}
while((IFG1&OFIFG))
;
IFG1&=~OFIFG;
}
void delayNus(uint n)//延时n微秒
{
uchar i;
for(i=n;i>0;i--)
{
_NOP(); _NOP(); _NOP(); _NOP(); _NOP();
}
}
void init_ds18b20()//初始化DS18B20
{
data_out;
data0;
delayNus(500);
data1;
delayNus(50);
data_in;
while(P1IN&0x01);
while(!(P1IN&0x01));
}
void write_byte_to_18b20(uchar data)//向ds18b20写一个字节
{
uchar i;
data_out;
for(i=0;i<8;i++)
{
data0;
delayNus(10);
if(data&0x01)
data1;
else
data0;
delayNus(40);
data1;
delayNus(2);
data>>=1;
}
}
uchar read_byte_from_ds18b20()//从ds18b20里读一个字节
{
uchar byte1=0,i;
for(i=0;i<8;i++)
{
byte1>>=1;
data_out;
data0;
data1;
data_in;
if(0x01&P1IN)
byte1|=0x80;
delayNus(40);
}
return byte1;
}
void temperature()//读取字节数据并用液晶显示
{
uint a=0,b=0;
double wd=0;
uint temp=0,temperature1=0;
init_ds18b20();
write_byte_to_18b20(0xCC);
write_byte_to_18b20(0x44);
delayms(800);
init_ds18b20();
write_byte_to_18b20(0xCC);
write_byte_to_18b20(0xee);
write_byte_to_18b20(0x07);
write_byte_to_18b20(0xde);
write_byte_to_18b20(0x60);
init_ds18b20();
write_byte_to_18b20(0xCC);
write_byte_to_18b20(0xbe);
a=read_byte_from_ds18b20();
b=read_byte_from_ds18b20();
init_ds18b20();
temperature1=b;
temperature1<<=8;
temperature1|=a;
temperature1&=0x0fff;
wd=temperature1*0.625;
temp=wd+0.5;
write_conmand(0x83);
delayms(20);
write_data(data[temp/10000]);
write_data(data[temp/1000]);
write_data(data[(temp/100)%10]);
write_data(data[(temp/10)%10]);
write_data('.');
write_data(data[temp%10]);
}
void main()
{
WDTCTL=WDTPW+WDTHOLD;
init_clk();
init_lcd();
while(1)
temperature();
}