单片机lcd显示温度的代码
时间: 2023-08-06 20:24:59 浏览: 57
以下是一个基于单片机的LCD显示温度的代码,以ATmega16为例,使用了C语言编写:
```
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
#define LCD_DATA_PORT PORTC // 数据口
#define LCD_DATA_DDR DDRC
#define LCD_CTRL_PORT PORTD // 控制口
#define LCD_CTRL_DDR DDRD
#define LCD_RS PD2 // RS
#define LCD_RW PD3 // RW
#define LCD_EN PD4 // EN
// 温度传感器引脚
#define TEMP_SENSOR_PIN PA0
// 初始化LCD
void lcd_init() {
LCD_DATA_DDR = 0xFF;
LCD_CTRL_DDR |= (1 << LCD_RS) | (1 << LCD_RW) | (1 << LCD_EN);
LCD_CTRL_PORT &= ~(1 << LCD_EN);
_delay_ms(20);
LCD_DATA_PORT = 0x38; // 8位数据口模式,2行显示,5x7点阵
LCD_CTRL_PORT |= (1 << LCD_EN);
_delay_us(1);
LCD_CTRL_PORT &= ~(1 << LCD_EN);
_delay_ms(5);
LCD_DATA_PORT = 0x0C; // 显示器开,光标关,光标闪烁关
LCD_CTRL_PORT |= (1 << LCD_EN);
_delay_us(1);
LCD_CTRL_PORT &= ~(1 << LCD_EN);
_delay_ms(5);
LCD_DATA_PORT = 0x01; // 清屏
LCD_CTRL_PORT |= (1 << LCD_EN);
_delay_us(1);
LCD_CTRL_PORT &= ~(1 << LCD_EN);
_delay_ms(5);
LCD_DATA_PORT = 0x06; // 光标右移,字符不移动
LCD_CTRL_PORT |= (1 << LCD_EN);
_delay_us(1);
LCD_CTRL_PORT &= ~(1 << LCD_EN);
_delay_ms(5);
}
// 写命令到LCD
void lcd_write_cmd(uint8_t cmd) {
LCD_DATA_PORT = cmd;
LCD_CTRL_PORT &= ~((1 << LCD_RS) | (1 << LCD_RW));
LCD_CTRL_PORT |= (1 << LCD_EN);
_delay_us(1);
LCD_CTRL_PORT &= ~(1 << LCD_EN);
_delay_ms(5);
}
// 写数据到LCD
void lcd_write_data(uint8_t data) {
LCD_DATA_PORT = data;
LCD_CTRL_PORT &= ~(1 << LCD_RW);
LCD_CTRL_PORT |= (1 << LCD_RS) | (1 << LCD_EN);
_delay_us(1);
LCD_CTRL_PORT &= ~(1 << LCD_EN);
_delay_ms(5);
}
// 将温度值转换为字符串
void float_to_string(float val, char *str) {
int int_part = (int)val;
float decimal_part = val - int_part;
sprintf(str, "%d.%02d", int_part, (int)(decimal_part * 100));
}
int main() {
// 初始化温度传感器引脚
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // 设置ADC时钟分频
ADMUX |= (1 << REFS0); // 设置参考电压为AVCC
ADCSRA |= (1 << ADEN); // 打开ADC
char temp_str[10];
// 初始化LCD
lcd_init();
while(1) {
// 读取温度值
ADCSRA |= (1 << ADSC); // 开始ADC转换
while(ADCSRA & (1 << ADSC)); // 等待转换完成
float temp = ADC * 5.0 / 1024.0 * 100.0; // 将ADC值转换为电压
float_to_string(temp, temp_str);
// 在LCD上显示温度值
lcd_write_cmd(0x01); // 清屏
lcd_write_cmd(0x80); // 光标移动到第一行
lcd_write_data('T');
lcd_write_data('e');
lcd_write_data('m');
lcd_write_data('p');
lcd_write_data(':');
lcd_write_data(' ');
lcd_write_data(temp_str[0]);
lcd_write_data(temp_str[1]);
lcd_write_data(temp_str[2]);
lcd_write_data('.');
lcd_write_data(temp_str[4]);
lcd_write_data(temp_str[5]);
lcd_write_data(0xDF); // °符号
lcd_write_data('C');
_delay_ms(500);
}
return 0;
}
```
这个程序使用了AVR的ADC模块,读取温度传感器的值。然后将温度值转换为字符串,并在LCD显示屏上显示温度值。需要注意的是,这个程序使用了AVR的内置库,因此需要使用AVR开发环境进行编译和烧录。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)