相同的程序烧到STC89C52上就可以用,STC15W408S的就不可以(端口有改动)
附源程序 求解答
#include <stc15f2k60s2.h>
//#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ = P2^4;
sbit DB7 = P1^7;
sbit lcd_rs = P2^5;
sbit lcd_rw = P2^6;
sbit lcd_e = P2^7;
uchar code table[] = "0123456789";
uint temp; //整型变量存储二进制16位温度值
float f_temp; //浮点型变量存储十进制温度值
void delay(uchar us) //延时(调用1次需要24us,执行1次需要16us.)
{
uchar s;
for (s = 0;s < us;s++);
}
uchar DS18B20_rest (void) //复位
{
uchar presence;
DQ = 0;
delay(45);
DQ = 1;
delay(3);
presence = DQ;
delay(25);
return presence;
}
uchar DS18B20_read_bit (void) // 读一位
{
uchar i;
DQ = 0;
DQ = 1;
for(i = 0;i < 3;i++);
return DQ;
}
uchar DS18B20_read_byte (void) // 读一个字节
{
uchar i;
uchar value = 0;
for(i = 0;i <8;i++)
{
if(DS18B20_read_bit())value |= 0x01<<i;
delay(6);
}
return value;
}
void DS18B20_write_bit (uchar bitval) // 写一位
{
DQ = 0;
if(bitval == 1)
DQ = 1;
delay(5);
DQ = 1;
}
void DS18B20_write_byte (uchar val) //写一个字节
{
uchar i;
uchar temp;
for(i = 0;i < 8;i++)
{
temp = val>>i;
temp &= 0x01;
DS18B20_write_bit(temp);
}
delay(5);
}
bit DS18B20_convert (void) //温度转换
{
DS18B20_rest ();
if(DS18B20_rest () == 0)
{
DS18B20_write_byte (0xcc);
DS18B20_write_byte (0x44);
return 1;
}
else
{
return 0;
}
}
void DS18B20_read_flash (void) //读取温度值
{
uchar Lsb,Msb;
if(DS18B20_rest () == 0)
{
DS18B20_write_byte (0xcc);
DS18B20_write_byte (0xbe);
Lsb = DS18B20_read_byte ();
Msb = DS18B20_read_byte ();
temp = Msb;
temp = (temp << 8)|Lsb;
f_temp = (float)temp*0.0625;
}
else
{
f_temp = 0;
}
temp = f_temp*100;
}
void delay1ms(uchar x) //延时
{
uint j,k;
for(j = x;j > 0; j--)
for(k = 110; k >0; k--);
}
void LCD1602_busy(void) //忙检测
{
uchar i;
DB7 = 1;
lcd_rs=0;
lcd_rw=1;
lcd_e=1;
while((DB7==1)|(i++ < 250));
lcd_e=0;
}
void LCD_write_command(uchar com) //写指令
{
lcd_rs = 0;
lcd_rw = 0;
P1 = com;
delay1ms(5);
lcd_e = 1;
lcd_e = 0;
}
void LCD_write_command_buf(uchar com) //写指令带忙检测
{
LCD1602_busy();
lcd_rs = 0;
lcd_rw = 0;
P1 = com;
delay1ms(5);
lcd_e = 1;
lcd_e = 0;
}
void LCD_write_data_buf(uchar dat) //写数据
{
LCD1602_busy();
lcd_rs = 1;
lcd_rw = 0;
P1 = dat;
delay1ms(5);
lcd_e = 1;
lcd_e = 0;
}
void LCD_init(void) //1602初始化
{
delay1ms(15);
LCD_write_command(0x38); // 设置16*2显示,5*7点阵,8位数据接口
delay1ms(5);
LCD_write_command(0x38);
delay1ms(5);
LCD_write_command(0x38);
LCD_write_command_buf(0x38);
LCD_write_command_buf(0x08);
LCD_write_command_buf(0x01);
LCD_write_command_buf(0x06);
LCD_write_command_buf(0x0c);
}
void LCD_dispal(void)
{
LCD_write_command_buf(0x80+0x4a);
LCD_write_data_buf(table[temp/1000]);
LCD_write_command_buf(0x80+0x4b);
LCD_write_data_buf(table[temp%1000/100]);
LCD_write_command_buf(0x80+0x4c);
LCD_write_data_buf('.');
LCD_write_command_buf(0x80+0x4d);
LCD_write_data_buf(table[temp%100/10]);
LCD_write_command_buf(0x80+0x4e);
LCD_write_data_buf(0xdf);
LCD_write_command_buf(0x80+0x4f);
LCD_write_data_buf('c');
}
void main (void)
{
LCD_init();
P1M0 = 0X00;
P1M1 = 0X00;
P2M0 = 0X00;
P2M1 = 0X00;
while (1)
{
if(DS18B20_convert () == 1)
{
DS18B20_read_flash ();
}
LCD_dispal();
}
} |