STC15F2K60S2单片机使用ds18b20在1602显示屏显示温度
时间: 2023-12-10 19:05:10 浏览: 82
51单片机ds18b20和lcd1602显示温度.doc
好的,这是一个关于STC15F2K60S2单片机使用ds18b20在1602显示屏显示温度的问题。下面是一个简单的程序示例,供参考:
```c
#include <reg51.h>
#include <intrins.h>
#define DS18B20_PORT P2
#define DS18B20_DQ P2_0
#define LCD_RS P0_0
#define LCD_RW P0_1
#define LCD_E P0_2
#define LCD_DAT P0
unsigned char code LCD_INIT_TBL[] = {0x38, 0x0c, 0x06, 0x01, 0x80};
unsigned char code DS18B20_SKIP_ROM[] = {0xcc, 0x44};
unsigned char code DS18B20_READ_SCRATCHPAD[] = {0xcc, 0xbe};
void lcd_init();
void lcd_clear();
void lcd_write_cmd(unsigned char cmd);
void lcd_write_data(unsigned char dat);
void lcd_write_str(unsigned char *str);
void delay_ms(unsigned int t);
void ds18b20_reset();
void ds18b20_write_byte(unsigned char dat);
unsigned char ds18b20_read_byte();
int ds18b20_read_temp();
void main()
{
unsigned int temp;
lcd_init();
lcd_write_str("Temperature:");
while (1)
{
temp = ds18b20_read_temp();
lcd_write_cmd(0x80 + 0x0c);
lcd_write_data(temp / 10 + '0');
lcd_write_data(temp % 10 + '0');
lcd_write_data('C');
delay_ms(1000);
}
}
void lcd_init()
{
unsigned char i;
LCD_DAT = 0x00;
LCD_RW = 0;
LCD_RS = 0;
LCD_E = 0;
delay_ms(15);
for (i = 0; i < sizeof(LCD_INIT_TBL); i++)
{
lcd_write_cmd(LCD_INIT_TBL[i]);
}
}
void lcd_clear()
{
lcd_write_cmd(0x01);
delay_ms(2);
}
void lcd_write_cmd(unsigned char cmd)
{
LCD_DAT = cmd;
LCD_RW = 0;
LCD_RS = 0;
LCD_E = 1;
_nop_();
_nop_();
LCD_E = 0;
delay_ms(2);
}
void lcd_write_data(unsigned char dat)
{
LCD_DAT = dat;
LCD_RW = 0;
LCD_RS = 1;
LCD_E = 1;
_nop_();
_nop_();
LCD_E = 0;
delay_ms(2);
}
void lcd_write_str(unsigned char *str)
{
while (*str)
{
lcd_write_data(*str++);
}
}
void delay_ms(unsigned int t)
{
unsigned int i, j;
for (i = 0; i < t; i++)
{
for (j = 0; j < 110; j++);
}
}
void ds18b20_reset()
{
DS18B20_DQ = 1;
_nop_();
DS18B20_DQ = 0;
delay_ms(20);
DS18B20_DQ = 1;
delay_ms(20);
}
void ds18b20_write_byte(unsigned char dat)
{
unsigned char i;
for (i = 0; i < 8; i++)
{
DS18B20_DQ = 0;
_nop_();
DS18B20_DQ = dat & 0x01;
delay_ms(2);
DS18B20_DQ = 1;
dat >>= 1;
}
}
unsigned char ds18b20_read_byte()
{
unsigned char i, dat = 0;
for (i = 0; i < 8; i++)
{
DS18B20_DQ = 0;
_nop_();
DS18B20_DQ = 1;
_nop_();
dat |= DS18B20_DQ << i;
delay_ms(2);
}
return dat;
}
int ds18b20_read_temp()
{
unsigned char high, low;
int temp;
ds18b20_reset();
ds18b20_write_byte(DS18B20_SKIP_ROM[0]);
ds18b20_write_byte(DS18B20_SKIP_ROM[1]);
ds18b20_write_byte(DS18B20_READ_SCRATCHPAD[0]);
ds18b20_write_byte(DS18B20_READ_SCRATCHPAD[1]);
low = ds18b20_read_byte();
high = ds18b20_read_byte();
temp = high;
temp <<= 8;
temp |= low;
temp *= 625;
temp /= 100;
return temp;
}
```
这个程序使用的是STC15F2K60S2单片机,通过DS18B20温度传感器读取温度值,并在1602液晶显示屏上显示出来。程序中涉及到了LCD1602显示屏和DS18B20温度传感器的读写操作,需要根据具体的硬件连接进行修改。
阅读全文