【proteus经典实战】显示温度和电压测量值在一个LCD屏幕上
一、简介
为基于微控制器的系统设计的,旨在实现温度和电压的实时测量并将结果显示在LCD屏幕上。核心部件包括DS1621数字温度传感器和MAX1241 A/D转换器:DS1621用于温度测量,而MAX1241则负责电压测量。通过精确读取这些传感器的数据,处理这些数据,并以易于读取的格式在LCD上展示出来,定义相关接口用于初始化和设置一些与LCD及可能的I2C设备相关的硬件接口。
二、头文件选择,变量定义
在8051微控制器上基于嵌入式C语言的程序片段。定义一系列的宏定义、全局变量和特殊功能位(sbit),用于简化寄存器操作和提高代码可读性。
2.1 头文件:
这是8051微控制器编程中常用的头文件,包括了特定于8051的寄存器定义和sfr(特殊功能寄存器)的宏定义。
2.2 宏定义:
uchar 和 uint 分别为 unsigned char 和 unsigned int 的简化形式,用于声明变量。
High 和 Low 分别定义了逻辑高电平(1)和低电平(0),用于提高代码的可读性。
_nop 定义了一个空操作,通常对应于汇编语言中的NOP指令,这里被定义为空花括号。
2.3 LCD外设地址:
PAGEADD、TIERADD 和 DIS_STARADD 是LCD显示相关的地址,用于LCD内存映射或寄存器操作。
2.4 全局变量:
key_value: 用于存储按键扫描的结果或类似功能的数值。
voltage_display: 一个数组,用来存储要在LCD上显示的电压值。
2.5 特殊功能位:
DI, E, CS1, CS2, RW, cs, sclk 和 dout 是与LCD显示和其它外设通信的控制线,对应于端口1的不同位。
I2C_SCL 和 I2C_SDA 是与I2C总线通信的串行时钟和数据线,这显示了程序可能包含与I2C兼容设备的通信。
2.6 代码:
A
三、定义字符表
定义一个字符表,其中包含了ASCII字符和一些中文字符的显示数据。通过使用这个字符表,可以在液晶模块上显示出对应的字符,字符表中的每个字符对应一个8x8的字模,每个字模使用8个字节来表示。
其中,table1定义了ASCII字符的字模,table2定义了一些中文字符的字模。
每个字符在字符表中的索引位置,以及对应的字符,使用这个字符表时,可以根据需要从字符表中取出相应的字模数据,然后根据液晶模块的显示方式进行显示。以下是字符数组
/***************************** 字符表 显示"Welcome Use" ******************************/ uchar code table1[]={ 0x00,0x3e,0x51,0x49,0x45,0x3e,0x00,0x00,//0(0) 0x00,0x00,0x42,0x7f,0x40,0x00,0x00,0x00,//1 0x00,0x42,0x61,0x51,0x49,0x46,0x00,0x00,//2 0x00,0x21,0x41,0x45,0x4b,0x31,0x00,0x00,//3 0x00,0x18,0x14,0x12,0x7f,0x10,0x00,0x00,//4 0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00,//5 0x00,0x3c,0x4a,0x49,0x49,0x30,0x00,0x00,//6 0x00,0x01,0x01,0x79,0x05,0x03,0x00,0x00,//7 0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00,//8 0x00,0x06,0x49,0x49,0x29,0x1e,0x00,0x00,//9 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// (10) 0x00,0x00,0x00,0x47,0x00,0x00,0x00,0x00,//! 0x00,0x23,0x13,0x08,0x64,0x62,0x00,0x00,//% 0x00,0x36,0x49,0x55,0x22,0x50,0x00,0x00,//& 0x00,0x14,0x08,0x3e,0x08,0x14,0x00,0x00,//* 0x00,0x08,0x08,0x3e,0x08,0x08,0x00,0x00,//+ 0x00,0x08,0x08,0x08,0x08,0x08,0x00,0x00,//- 0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x00,//. 0x00,0x20,0x10,0x08,0x04,0x02,0x00,0x00,/// 0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00,//:(19) 0x00,0x14,0x14,0x14,0x14,0x14,0x00,0x00,//= 0x00,0x02,0x01,0x51,0x09,0x06,0x00,0x00,//? 0x00,0x32,0x49,0x79,0x41,0x3E,0x00,0x00,//@(22) 0x00,0x7e,0x11,0x11,0x11,0x7f,0x00,0x00,//A 0x00,0x41,0x7f,0x49,0x49,0x36,0x00,0x00,//B 0x00,0x3e,0x41,0x41,0x41,0x22,0x00,0x00,//C 0x00,0x41,0x7f,0x41,0x41,0x3e,0x00,0x00,//D 0x00,0x7f,0x49,0x49,0x49,0x49,0x00,0x00,//E 0x00,0x7f,0x09,0x09,0x09,0x01,0x00,0x00,//F 0x00,0x3e,0x41,0x41,0x49,0x7a,0x00,0x00,//G(29) 0x00,0x7f,0x08,0x08,0x08,0x7f,0x00,0x00,//H 0x00,0x00,0x41,0x7f,0x41,0x00,0x00,0x00,//I 0x20,0x40,0x41,0x3f,0x01,0x01,0x00,0x00,//J 0x00,0x7f,0x08,0x14,0x22,0x41,0x00,0x00,//K 0x00,0x7f,0x40,0x40,0x40,0x40,0x00,0x00,//L 0x00,0x7f,0x02,0x0c,0x02,0x7f,0x00,0x00,//M 0x00,0x7f,0x06,0x08,0x30,0x7f,0x00,0x00,//N 0x00,0x3e,0x41,0x41,0x41,0x3e,0x00,0x00,//O 0x00,0x7f,0x09,0x09,0x09,0x06,0x00,0x00,//P 0x00,0x3e,0x41,0x51,0x21,0x5e,0x00,0x00,//Q(39) 0x00,0x7f,0x09,0x19,0x29,0x46,0x00,0x00,//R 0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x00,//S 0x00,0x01,0x01,0x7f,0x01,0x01,0x00,0x00,//T 0x00,0x3f,0x40,0x40,0x40,0x3f,0x00,0x00,//U 0x00,0x1f,0x20,0x41,0x20,0x1f,0x00,0x00,//V 0x00,0x7f,0x20,0x80,0x20,0x7f,0x00,0x00,//W 0x00,0x63,0x14,0x08,0x14,0x63,0x00,0x00,//X 0x00,0x07,0x08,0x70,0x08,0x07,0x00,0x00,//Y 0x00,0x61,0x51,0x49,0x45,0x43,0x00,0x00,//Z 0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00,//a(49) 0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00,//b 0x00,0x38,0x44,0x44,0x44,0x28,0x00,0x00,//c 0x00,0x38,0x44,0x44,0x48,0x7f,0x00,0x00,//d 0x00,0x38,0x54,0x54,0x54,0x18,0x00,0x00,//e 0x00,0x00,0x08,0x7e,0x09,0x02,0x00,0x00,//f 0x00,0x0c,0x52,0x52,0x4c,0x3e,0x00,0x00,//g 0x00,0x7f,0x08,0x04,0x04,0x78,0x00,0x00,//h 0x00,0x00,0x44,0x7d,0x40,0x00,0x00,0x00,//i 0x00,0x20,0x40,0x44,0x3d,0x00,0x00,0x00,//j 0x00,0x00,0x7f,0x10,0x28,0x44,0x00,0x00,//k(59) 0x00,0x00,0x41,0x7f,0x40,0x00,0x00,0x00,//l 0x00,0x7c,0x04,0x78,0x04,0x78,0x00,0x00,//m 0x00,0x7c,0x08,0x04,0x04,0x78,0x00,0x00,//n 0x00,0x38,0x44,0x44,0x44,0x38,0x00,0x00,//o 0x00,0x7e,0x0c,0x12,0x12,0x0c,0x00,0x00,//p 0x00,0x0c,0x12,0x12,0x0c,0x7e,0x00,0x00,//q 0x00,0x7C,0x08,0x04,0x04,0x08,0x00,0x00,//r 0x00,0x58,0x54,0x54,0x54,0x64,0x00,0x00,//s 0x00,0x04,0x3f,0x44,0x40,0x20,0x00,0x00,//t 0x00,0x3c,0x40,0x40,0x3c,0x40,0x00,0x00,//u(69) 0x00,0x1c,0x20,0x40,0x20,0x1c,0x00,0x00,//v 0x00,0x3c,0x40,0x30,0x40,0x3c,0x00,0x00,//w 0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00,//x 0x00,0x1c,0xa0,0xa0,0x90,0x7c,0x00,0x00,//y 0x00,0x44,0x64,0x54,0x4c,0x44,0x00,0x00,//z(74) }; /**************************** 中文字符表 显示"欢迎使用液晶模块" *****************************/ uchar code table2[]={ 0x14,0x24,0x44,0x84,0x64,0x1c,0x20,0x18, 0x0f,0xe8,0x08,0x08,0x28,0x18,0x08,0x00, 0x20,0x10,0x4c,0x43,0x43,0x2c,0x20,0x10, 0x0c,0x03,0x06,0x18,0x30,0x60,0x20,0x00,//欢(0) 0x40,0x41,0xce,0x04,0x00,0xfc,0x04,0x02, 0x02,0xfc,0x04,0x04,0x04,0xfc,0x00,0x00, 0x40,0x20,0x1f,0x20,0x40,0x47,0x42,0x41, 0x40,0x5f,0x40,0x42,0x44,0x43,0x40,0x00,//迎(1) 0x40,0x20,0xf0,0x1c,0x07,0xf2,0x94,0x94, 0x94,0xff,0x94,0x94,0x94,0xf4,0x04,0x00, 0x00,0x00,0x7f,0x00,0x40,0x41,0x22,0x14, 0x0c,0x13,0x10,0x30,0x20,0x61,0x20,0x00,//使(2) 0x00,0x00,0x00,0xfe,0x22,0x22,0x22,0x22, 0xfe,0x22,0x22,0x22,0x22,0xfe,0x00,0x00, 0x80,0x40,0x30,0x0f,0x02,0x02,0x02,0x02, 0xff,0x02,0x02,0x42,0x82,0x7f,0x00,0x00,//用(3) 0x10,0x61,0x06,0xe0,0x18,0x84,0xe4,0x1c, 0x84,0x65,0xbe,0x24,0xa4,0x64,0x04,0x00, 0x04,0x04,0xff,0x00,0x01,0x00,0xff,0x41, 0x21,0x12,0x0c,0x1b,0x61,0xc0,0x40,0x00,//液(4) 0x00,0x00,0x00,0x00,0x7e,0x2a,0x2a,0x2a, 0x2a,0x2a,0x2a,0x7e,0x00,0x00,0x00,0x00, 0x00,0x7f,0x25,0x25,0x25,0x25,0x7f,0x00, 0x00,0x7f,0x25,0x25,0x25,0x25,0x7f,0x00,//晶(5) 0x10,0xd0,0xff,0x50,0x90,0x04,0xf4,0x54, 0x5f,0x54,0x54,0x5f,0xf4,0x04,0x00,0x00, 0x03,0x00,0xff,0x00,0x00,0x84,0x85,0x45, 0x35,0x0f,0x15,0x25,0x65,0xc4,0x44,0x00,//模(6) 0x10,0x10,0xff,0x10,0x10,0x00,0x08,0x08, 0xff,0x08,0x08,0x08,0xf8,0x00,0x00,0x00, 0x08,0x18,0x0f,0x04,0x85,0x41,0x31,0x0d, 0x03,0x05,0x09,0x11,0x31,0x61,0x21,0x00,//块(7) 0x00,0x00,0xF8,0x48,0x48,0x48,0x48,0xFF, 0x48,0x48,0x48,0x48,0xF8,0x00,0x00,0x00, 0x00,0x00,0x0F,0x04,0x04,0x04,0x04,0x3F, 0x44,0x44,0x44,0x44,0x4F,0x40,0x70,0x00,//电(8) 0x00,0x00,0xFE,0x02,0x42,0x42,0x42,0x42, 0xFA,0x42,0x42,0x42,0x62,0x42,0x02,0x00, 0x20,0x18,0x27,0x20,0x20,0x20,0x20,0x20, 0x3F,0x20,0x21,0x2E,0x24,0x20,0x20,0x00,//压(9) 0x10,0x21,0x86,0x70,0x00,0x7E,0x4A,0x4A, 0x4A,0x4A,0x4A,0x7E,0x00,0x00,0x00,0x00, 0x02,0xFE,0x01,0x40,0x7F,0x41,0x41,0x7F, 0x41,0x41,0x7F,0x41,0x41,0x7F,0x40,0x00,//温(10) 0x00,0x00,0xFC,0x04,0x24,0x24,0xFC,0xA5, 0xA6,0xA4,0xFC,0x24,0x24,0x24,0x04,0x00, 0x80,0x60,0x1F,0x80,0x80,0x42,0x46,0x2A, 0x12,0x12,0x2A,0x26,0x42,0xC0,0x40,0x00,//度(11) 0x08,0x31,0x86,0x60,0x00,0xFE,0x02,0xF2, 0x02,0xFE,0x00,0xF8,0x00,0x00,0xFF,0x00, 0x04,0xFC,0x03,0x00,0x80,0x47,0x30,0x0F, 0x10,0x67,0x00,0x07,0x40,0x80,0x7F,0x00,//测(12) 0x40,0x40,0x40,0xDF,0x55,0x55,0x55,0xD5, 0x55,0x55,0x55,0xDF,0x40,0x40,0x40,0x00, 0x40,0x40,0x40,0x57,0x55,0x55,0x55,0x7F, 0x55,0x55,0x55,0x57,0x50,0x40,0x40,0x00,//量(13) };
四、各种函数介绍
4.1 延时函数
定义一个名为`Delay_nms`的函数,它用于延迟或休眠一段时间。这个函数接收一个无符号字符类型的参数`n`,表示需要延迟的次数。
Delay_nms(uchar n) { // 定义一个无符号字符类型的变量a,用于循环计数 uchar a; // 开始一个空循环,条件是n大于0 for(;n>0;n--) { // 开始另一个循环,循环次数为100次 for(a=0;a0;n--):一个空循环,只要n大于0就会继续循环,直到n小于或等于0时停止。这可以看作是一个等待或延迟的过程,其中n表示需要等待的次数。for(a=0;a