PIC单片机实例四:温度测量系统的设计与仿真
扫描二维码
随时随地手机看文章
引言
网上有很多一线总线的产品,我觉得这项技术的前景不错,自己用C也写了一个测温的程序.
并通过PROTEUS进行了仿真.
搞了很多天,终于搞出来了,心里高兴的很,拿出来晒一下!
一. 原理介绍
说明:
1.单片机采用PIC16F877A,主要是由于液晶显示需要自己写字库,所以需要大容量的EEPROM,而他有8K字节.
2.液晶使用的是KS0108控制器系列,他的指令比较简单,性价比较高.
3.温度传感器采用的是1-WIRE总线技术的DS18B20,测的温度分辨率高.
附另外两张效果图
二. 器件介绍
DS18B20的相关资料
读写时序复位时序操作ROM的指令内部框图读出数据说明
KS0108控制器相关资料:
三. 程序
/*************************************************************
* 标题:温度测量系统 *
* 作者:Wujieflash *
* 日期:2008年1月8日 *
* 版本:V1.0 *
*说明:①单片机:PIC16F877A*
* ②液晶; KS0108控制器*
* ③传感器:DS18B20 *
*******************************************************************/
#include
__CONFIG(0x1832);
//芯片配置字,看门狗关,上电延时开,掉电检测关,低压编程关,加密,4M晶体HS振荡
#define E RC0//液晶使能端
#define RW RC1//读写控制端
#define DI RC2//数据/指令通道
#define CSA RC3//片选
#define CSB RC4//片选
#define nop() asm("nop")
#define uch unsigned char //给unsigned char起别名 uch
# define DQ RA0 //定义18B20数据端口
# define DQ_DIR TRISA0 //定义18B20D口方向寄存器
# define DQ_HIGH() DQ_DIR =1 //设置数据口为输入
# define DQ_LOW() DQ = 0; DQ_DIR = 0 //设置数据口为输出
uch TLV=0 ; //采集到的温度高8位
uch THV=0; //采集到的温度低8位
uch TZ=0; //转换后的温度值整数部分
int val1=35,val2=10;
//字库
/*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
const unsigned char wen[]={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};//温
const unsigned char du[]={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};/*"度"*/
const unsigned char ce[]={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};/*"测"*/
const unsigned char liang[]={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};/*"量"*/
const unsigned char xi[]={0x00,0x00,0x02,0x22,0xB2,0xAA,0x66,0x62,0x22,0x11,0x4D,0x81,0x01,0x01,0x00,0x00,0x00,0x40,0x21,0x13,0x09,0x05,0x41,0x81,0x7F,0x01,0x05,0x09,0x13,0x62,0x00,0x00};/*"系"*/
const unsigned char tong[]={0x20,0x30,0x2C,0xA3,0x60,0x10,0x84,0xC4,0xA4,0x9D,0x86,0x84,0xA4,0xC4,0x84,0x00,0x20,0x22,0x23,0x12,0x12,0x92,0x40,0x30,0x0F,0x00,0x00,0x3F,0x40,0x41,0x70,0x00};/*"统"*/
const unsigned char zuo[]={0x80,0x40,0x20,0xF8,0x07,0x22,0x18,0x0C,0xFB,0x48,0x48,0x48,0x68,0x48,0x08,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0xFF,0x04,0x04,0x04,0x04,0x06,0x04,0x00};//作
const unsigned char zhe[]={0x00,0x20,0x24,0x24,0x24,0x24,0xA4,0xBF,0x64,0x24,0x34,0x28,0x26,0x24,0x20,0x00,0x04,0x04,0x04,0x02,0x02,0xFF,0x49,0x49,0x49,0x49,0x49,0x49,0xFF,0x00,0x00,0x00};//者
const unsigned char ban[]={0x00,0xFE,0x90,0x90,0x9E,0x10,0x10,0xFE,0xE2,0x22,0x22,0x21,0xE1,0x21,0x00,0x00,0x40,0x3F,0x00,0x00,0x5F,0x20,0x98,0x47,0x20,0x13,0x0C,0x1B,0x60,0xC0,0x40,0x00};//版
const unsigned char ben[]={0x08,0x08,0x08,0x08,0x08,0xC8,0x38,0xFF,0x18,0x68,0x88,0x08,0x08,0x08,0x08,0x00,0x08,0x08,0x04,0x02,0x05,0x04,0x04,0x7F,0x04,0x04,0x04,0x03,0x06,0x0C,0x04,0x00};//本
const unsigned char zheng[]={0x00,0x02,0x02,0xC2,0x02,0x02,0x02,0x02,0xFE,0x82,0x82,0x82,0x82,0x82,0x02,0x00,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x20,0x20,0x00};//正
const unsigned char zai[]={0x00,0x04,0x04,0xC4,0x64,0x9C,0x87,0x84,0x84,0xE4,0x84,0x84,0x84,0x84,0x04,0x00,0x04,0x02,0x01,0x7F,0x00,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x20,0x00};//在
const unsigned char chu[]={0x00,0x08,0x88,0xC9,0x2E,0x98,0x48,0x00,0x08,0x08,0xF8,0x08,0x08,0xFC,0x08,0x00,0x02,0x01,0x00,0x7F,0x01,0x02,0x44,0x20,0x10,0x0C,0x03,0x20,0x40,0x3F,0x00,0x00};//初
const unsigned char shi[]={0x10,0x90,0x70,0x1F,0x12,0xF0,0x00,0x20,0x70,0x28,0x27,0x22,0x28,0x70,0x20,0x00,0x40,0x21,0x12,0x0C,0x06,0x09,0x30,0x00,0x7F,0x21,0x21,0x21,0x21,0x7F,0x00,0x00};//使
const unsigned char hua[]={0x80,0x40,0x20,0xF8,0x07,0x02,0x00,0x00,0xFF,0xC0,0x60,0x30,0x1C,0x08,0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x04,0x02,0x01,0x3F,0x40,0x40,0x40,0x40,0x78,0x00,0x00};//化
const unsigned char shang[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x40,0x40,0x40,0x40,0x40,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x40,0x40,0x60,0x40,0x00};//上
const unsigned char xia[]={0x00,0x02,0x02,0x02,0x02,0x02,0x02,0xFE,0x22,0x62,0xC2,0x82,0x02,0x03,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00};//下
const unsigned char xian[]={0xFE,0x02,0x32,0x4E,0x82,0x00,0xFE,0x4A,0xCA,0x4A,0x4A,0x4A,0x7E,0x00,0x00,0x00,0xFF,0x00,0x02,0x04,0x03,0x00,0xFF,0x40,0x20,0x03,0x0C,0x12,0x21,0x60,0x20,0x00};//限
const unsigned char zhu[]={0x10,0x60,0x01,0x86,0x60,0x08,0x08,0x08,0x09,0xFE,0x08,0x08,0x08,0x08,0x08,0x00,0x04,0x04,0xFE,0x01,0x40,0x40,0x41,0x41,0x41,0x7F,0x41,0x41,0x41,0x41,0x40,0x00};//注
const unsigned char yi[]={0x10,0x10,0x12,0xD2,0x56,0x5A,0xD2,0x53,0x52,0x5A,0x56,0xD2,0x12,0x12,0x10,0x00,0x00,0x20,0x38,0x03,0x3A,0x42,0x42,0x4B,0x52,0x42,0x42,0x63,0x08,0x30,0x00,0x00};//意
const unsigned char she[]={0x40,0x41,0xCE,0x04,0x00,0x80,0x40,0xBE,0x82,0x82,0x82,0xBE,0xC0,0x40,0x40,0x00,0x00,0x00,0x7F,0x20,0x90,0x80,0x40,0x43,0x2C,0x10,0x10,0x2C,0x43,0xC0,0x40,0x00};//设
const unsigned char zhi[]={0x00,0x20,0x2F,0xA9,0xA9,0xAF,0xE9,0xB9,0xA9,0xAF,0xA9,0xA9,0x2F,0x20,0x00,0x00,0x80,0x80,0x80,0xFF,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xFF,0x80,0x80,0x80,0x00};//置
const unsigned char dang[]={0x00,0x00,0x40,0x42,0x5C,0x48,0x40,0x40,0x7F,0x40,0x50,0x4E,0x44,0xC0,0x00,0x00,0x00,0x00,0x20,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x7F,0x00,0x00};//当
const unsigned char qian[]={0x08,0x08,0xE8,0xA8,0xA9,0xAE,0xEA,0x08,0x08,0xC8,0x0C,0x0B,0xEA,0x08,0x08,0x00,0x00,0x00,0x7F,0x04,0x24,0x44,0x3F,0x00,0x00,0x1F,0x40,0x80,0x7F,0x00,0x00,0x00};//前
const unsigned char fang[]={0x00,0xFE,0x22,0x5A,0x86,0x02,0x08,0x08,0xF9,0x8E,0x88,0x88,0x88,0x08,0x08,0x00,0x00,0xFF,0x04,0x08,0x47,0x20,0x18,0x07,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00};//防
const unsigned char shu[]={0x00,0x00,0x40,0x5F,0x55,0x55,0xF5,0x55,0x55,0x55,0xD5,0x5F,0x40,0x00,0x00,0x00,0x11,0x11,0x09,0x09,0xFD,0x55,0x57,0x57,0x55,0x55,0x55,0xFD,0x01,0x01,0x01,0x00};//暑
const unsigned char bao[]={0x40,0x20,0xF8,0x17,0x82,0x80,0xBE,0xA2,0xA2,0xE2,0xA2,0xA2,0xBE,0x80,0x80,0x00,0x00,0x00,0x7F,0x00,0x20,0x10,0x08,0x06,0x01,0x7F,0x03,0x0C,0x18,0x30,0x10,0x00};//保
const unsigned char nuan[]={0x00,0xFC,0x44,0x44,0xFC,0x80,0xA6,0xAA,0xE2,0xA6,0xAA,0xA1,0xA9,0xA5,0x80,0x00,0x00,0x0F,0x04,0x04,0x4F,0x20,0x58,0x46,0x27,0x2A,0x12,0x1A,0x26,0x62,0x20,0x00};//暖
const unsigned char nononono[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//
const unsigned char danwei_c[]={0x00,0x02,0x05,0xE2,0x18,0x04,0x02,0x02,0x02,0x02,0x02,0x02,0x04,0x1E,0x00,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x40,0x40,0x40,0x40,0x40,0x20,0x18,0x00,0x00};//单位
const unsigned char maohao[]={0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00};//':'
const unsigned char dian[]={0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x03,0x00,0x00,0x00};//'.'
const unsigned chardw[]={0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00};//‘W'
const unsigned char uu[]={0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20};//'u'
const unsigned char jj[]={0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00};//'j'
const unsigned char ii[]={0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00};//'i'
const unsigned char ee[]={0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00};//'e'
const unsigned char ff[]={0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00};//'f'
const unsigned char ll[]={0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00};//'l'
const unsigned char aa[]={0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20};//'a'
const unsigned char ss[]={0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00};//'s'
const unsigned char hh[]={0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20};//'h'
const unsigned char vv[]={0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00};//'v'
const unsigned char zhenghao1[]={0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00};//'+'
const unsigned char fuhao1[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01};//'-'
const unsigned char xinghao[]={0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00};//"*'
const unsigned char s1[]={0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00};//'1'
const unsigned char s0[]={0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00};//'0'
const unsigned char s2[]={0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00};/*"2",0*/
const unsigned chars3[]={0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00};/*"3",1*/
const unsigned char s4[]={0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00};/*"4",2*/
const unsigned char s5[]={0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00};/*"5",3*/
const unsigned char s6[]={0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00};/*"6",4*/
const unsigned char s7[]={0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00};/*"7",5*/
const unsigned char s8[]={0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00};/*"8",6*/
const unsigned chars9[]={0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00};/*"9",7*/
const unsigned char nono[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//空的
const unsigned char juhao[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00};//'.'
const unsigned char zhenghao[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0xFF,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//'+'
const unsigned char fuhao[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//'-'
const unsigned char ss0[]={0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x40,0x40,0x40,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xFE,0x0F,0x01,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0xFE,0xF0,0x00,0x00,0x00,0x1F,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xFF,0x1F,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x06,0x04,0x04,0x04,0x06,0x03,0x01,0x00,0x00,0x00};//0
const unsigned char ss1[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x04,0x04,0x06,0x07,0x07,0x06,0x04,0x04,0x04,0x00,0x00,0x00};//1
const unsigned char ss2[]={0x00,0x00,0x00,0x00,0x80,0x40,0x40,0x40,0x40,0x40,0xC0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1E,0x19,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC1,0x7F,0x3E,0x00,0x00,0x00,0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x07,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x07,0x01,0x00,0x00};//2
const unsigned char ss3[]={0x00,0x00,0x00,0x80,0x80,0x40,0x40,0x40,0x40,0xC0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x0F,0x00,0x00,0x80,0x80,0x80,0xC0,0x61,0x3F,0x1E,0x00,0x00,0x00,0x00,0x00,0xE0,0xE0,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0xFE,0x78,0x00,0x00,0x00,0x00,0x01,0x03,0x02,0x04,0x04,0x04,0x04,0x04,0x02,0x03,0x01,0x00,0x00,0x00};//3
const unsigned char ss4[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x30,0x0C,0x02,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x10,0x1C,0x12,0x11,0x10,0x10,0x10,0x10,0xFF,0xFF,0xFF,0x10,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x08,0x0F,0x0F,0x0F,0x08,0x08,0x08,0x00};//4
const unsigned char ss5[]={0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0xF8,0x87,0x40,0x20,0x20,0x20,0x20,0x60,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0xE0,0x61,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xFF,0x7E,0x00,0x00,0x00,0x00,0x01,0x02,0x04,0x04,0x04,0x04,0x04,0x04,0x02,0x03,0x01,0x00,0x00,0x00};//5
const unsigned char ss6[]={0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x40,0x40,0x40,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0xE0,0xFC,0x0E,0x81,0x80,0x40,0x40,0x40,0x40,0xC0,0x83,0x03,0x00,0x00,0x00,0x00,0x3F,0xFF,0xC3,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0x7E,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x06,0x04,0x04,0x04,0x04,0x02,0x03,0x01,0x00,0x00};//6
const unsigned char ss7[]={0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x0F,0x03,0x01,0x00,0x00,0x00,0x80,0x60,0x18,0x06,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xFC,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//7
const unsigned char ss8[]={0x00,0x00,0x00,0x00,0x80,0xC0,0x40,0x40,0x40,0x40,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x1E,0x3F,0x71,0xE0,0xC0,0x80,0x80,0x80,0x80,0x61,0x3F,0x1E,0x00,0x00,0x00,0xF8,0xFC,0x06,0x03,0x01,0x00,0x01,0x01,0x03,0x07,0x0E,0xFC,0xF8,0x00,0x00,0x00,0x00,0x01,0x03,0x02,0x04,0x04,0x04,0x04,0x04,0x02,0x03,0x01,0x00,0x00,0x00};//8
const unsigned char ss9[]={0x00,0x00,0x00,0x80,0x80,0x40,0x40,0x40,0x40,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0xFF,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x83,0xFE,0xF8,0x00,0x00,0x00,0x00,0x81,0x83,0x06,0x04,0x04,0x04,0x04,0x02,0x83,0xF1,0x7F,0x0F,0x00,0x00,0x00,0x00,0x03,0x03,0x04,0x04,0x04,0x04,0x06,0x03,0x01,0x00,0x00,0x00,0x00,0x00};//9
const unsigned char nonono[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//9
const unsigned char danwei_c1[]={0x00,0x00,0x00,0x00,0x00,0xE0,0x10,0x10,0x10,0xF0,0x00,0x00,0x00,0x80,0xc0,0x60,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x00,0xfC,0xff,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0x7f,0xf0,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x03};//℃
const unsigned char danwei_c2[]={0x30,0x30,0x10,0x10,0x10,0x20,0x20,0x20,0x40,0x80,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x40,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x06,0x06,0x06,0x06,0x06,0x06,0x02,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00};
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
////////////////////////基本子函数///////////////////////
//延时
void delay(char x,char y)
{
char z;
do{
z=y;
do{;}while(--z);
}while(--x);
}
//其指令时间为:7+(3*(Y-1)+7)*(X-1)如果再加上函数调用的call 指令、页面设定、传递参数花掉的7 个指令。
//则是:14+(3*(Y-1)+7)*(X-1)。
//--------------------------------------------------
//系统初始化函数
void init()
{
ADCON1=0X07; //设置A口为普通数字口
TRISA=0X00; //设置A口方向为输出
TRISD=0X00; //设置D口方向为输出
}
//-----------------------------------------------
//复位DS18B20函数(*一定要注意延时,我曾经在这上差点搞晕*)
reset(void)
{
char presence=1;
while(presence)
{
DQ_LOW() ; //主机拉至低电平
delay(2,90); //延时>480503us
DQ_HIGH(); //释放总线等电阻拉高总线,并保持15~60us
delay(2,8); //延时>60us
if(DQ==1) presence=1; //没有接收到应答信号,继续复位
else presence=0; //接收到应答信号
delay(2,70); //延时>240us
}
}
//-----------------------------------------------
//写18b20写字节函数
void write_byte(uch val)
{
uch i;
uch temp;
for(i=8;i>0;i--)
{
temp=val&0x01; //最低位移出
DQ_LOW();
NOP();
NOP();
NOP();
NOP();
NOP(); //从高拉至低电平,产生写时间隙
if(temp==1) DQ_HIGH(); //如果写1,拉高电平
delay(2,7); //延时>60us
DQ_HIGH();
NOP();
NOP();
val=val>>1; //右移一位
}
}
//------------------------------------------------
//18b20读字节函数
uch read_byte(void)
{
uch i;
uch value=0; //读出温度
static bit j;
for(i=8;i>0;i--)
{
value>>=1;
DQ_LOW();
NOP();
NOP();
NOP();
NOP(); //6us
DQ_HIGH(); //拉至高电平
NOP();
NOP();
NOP(); //4us
j=DQ;
if(j) value|=0x80;
delay(2,7); //63us
}
return(value);
}
//-------------------------------------------------
//启动温度转换函数
void get_temp()
{
int i;
DQ_HIGH();
reset(); //复位等待从机应答
write_byte(0XCC); //忽略ROM匹配
write_byte(0X44); //发送温度转化命令
for(i=10;i>0;i--)
{
delay(201,132);
}
reset(); //再次复位,等待从机应答
write_byte(0XCC); //忽略ROM匹配
write_byte(0XBE); //发送读温度命令
TLV=read_byte(); //读出温度低8
THV=read_byte(); //读出温度高8位
DQ_HIGH(); //释放总线
TZ=(TLV>>4)|(THV<<4); //温度整数部分
}
//--------------------------------------------------
void LCDdelay()
{
unsigned int i=500;
while(i--)
{;}
}
/*----------------------------------------------------*/
//状态检查
void CheckState()
{
unsigned int dat;
RW=1; //读状态
DI=0;
TRISB=0xff; //设置口为输入
while(1)
{
E=1; //高电平读数据
dat=PORTB&0x90; //当4、7位为0时为正常状态
E=0;
if(dat==0x00)break;
}
TRISB=0x00; //恢复口为输出
}
/*----------------------------------------------------*/
//写显示数据
//dat:显示数据
void WriteData(unsigned char dat)
{
CheckState();
DI=1; //写数据
RW=0;
PORTB=dat;
E=1;
E=0;
}
/*---------------------------------------------------*/
//写指令
//command:操作LCD的指令
void WriteCommand(unsigned char dat)
{
CheckState();
RW=0; //写指令
PORTB=dat;
E=1;
E=0;
}
/*---------------------------------------------------*/
//设定页地址----X:0--7
void SetLine(unsigned char line)
{
line=line&0x07; //0<=line<=7
line=line|0xb8;
WriteCommand(line);
}
/*---------------------------------------------------*/
//设定列地址----Y:0--63
void SetColumn(unsigned char column)
{
column=column&0x3f; //0<=column<=63
column=column|0x40;
WriteCommand(column);
}
/*--------------------------------------------------*/
//设定开始行
void SetStartLine(unsigned char startline)
{
startline=startline&0x3f; //0<=startline<=63
startline=startline|0xc0;
WriteCommand(startline);
}
/*--------------------------------------------------*/
//开关显示
//1:开显示 0:关显示
void SetOnOff(unsigned char state)
{
state=state|0x3e;
WriteCommand(state);
}
/*--------------------------------------------------*/
//选择屏幕
//screen: 1:左屏 2:右屏
void SlectScreen(unsigned char screen)
{
switch(screen)
{
case 1: CSA=0;
CSB=1;//左屏
break;
case 2: CSA=1;
CSB=0;//右屏
break;
}
}
/*--------------------------------------------------*/
//清屏
//screen: 1:左屏 2:右屏
void ClearScreen(unsigned char screen)
{
unsigned char i,j;
SlectScreen(screen);
for(i=0;i<8;i++)
{
SetLine(i);
for(j=0;j<64;j++)
{
WriteData(0x00);
WriteData(0x00);
}
}
}
/*---------------------------------------------------*/
//显示一个16X16的汉字
void Show16X16(unsigned char line,unsigned char column,const unsigned char *pt)
{
unsigned char i,column_temp;
column_temp=column;
SlectScreen(1); //选通左屏
SetColumn(column); //设置起始列
SetLine(line); //设置行
for(i=0;i<16;i++)
{
if(column>=64) //如果列>=64,则选通右屏
{
SlectScreen(2);
column=column%64; //设置右屏起始列
SetColumn(column);
SetLine(line);
}
WriteData(*pt); //写数据
*pt++;
column++;
}
SlectScreen(1);
SetLine(line+1); //设置汉字下半部分显示页
column=column_temp;
SetColumn(column);
for(i=0;i<16;i++)
{
if(column>=64)
{
SlectScreen(2);
column=column%64;
SetColumn(column);
SetLine(line+1);
}
WriteData(*pt);
*pt++;
column++;
}
}
/*-------------------------------------------------------*/
//显示一个8X16的字符
void Show8X16(unsigned char line,unsigned char column,const unsigned char *pt)
{
unsigned char i,column_temp;
column_temp=column;
SlectScreen(1); //选通左屏
SetColumn(column); //设置起始列
SetLine(line); //设置行
for(i=0;i<8;i++)
{
if(column>=64) //如果列>=64,则选通右屏
{
SlectScreen(2);
column=column%64; //设置右屏起始列
SetColumn(column);
SetLine(line);
}
WriteData(*pt); //写数据
*pt++;
column++;
}
SlectScreen(1);
SetLine(line+1); //设置汉字下半部分显示页
column=column_temp;
SetColumn(column);
for(i=0;i<8;i++)
{
if(column>=64)
{
SlectScreen(2);
column=column%64;
SetColumn(column);
SetLine(line+1);
}
WriteData(*pt);
*pt++;
column++;
}
}
/*-------------------------------------------------------*/
//显示一个16X32的字符
void Show16X32(unsigned char line,unsigned char column,const unsigned char *pt)
{
unsigned char i,column_temp1,column_temp2,column_temp3;
column_temp1=column;
column_temp2=column;
column_temp3=column;
SlectScreen(1); //选通左屏
SetColumn(column); //设置起始列
SetLine(line); //设置行
for(i=0;i<16;i++)
{
if(column>=64) //如果列>=64,则选通右屏
{
SlectScreen(2);
column=column%64; //设置右屏起始列
SetColumn(column);
SetLine(line);
}
WriteData(*pt); //写数据
*pt++;
column++;
}
SlectScreen(1);
SetLine(line+1);
column=column_temp1;
SetColumn(column);
for(i=0;i<16;i++)
{
if(column>=64)
{
SlectScreen(2);
column=column%64;
SetColumn(column);
SetLine(line+1);
}
WriteData(*pt);
*pt++;
column++;
}
SlectScreen(1);
SetLine(line+2);
column=column_temp2;
SetColumn(column);
for(i=0;i<16;i++)
{
if(column>=64)
{
SlectScreen(2);
column=column%64;
SetColumn(column);
SetLine(line+2);
}
WriteData(*pt);
*pt++;
column++;
}
SlectScreen(1);
SetLine(line+3);
column=column_temp3;
SetColumn(column);
for(i=0;i<16;i++)
{
if(column>=64)
{
SlectScreen(2);
column=column%64;
SetColumn(column);
SetLine(line+3);
}
WriteData(*pt);
*pt++;
column++;
}
}
/*-------------------------------------------------------*/
//写入变化的参数子程序
void ChangeTemp1(int val)
{
int D_ge,D_shi,D_bai;
if(val>0)
{
Show8X16(2,72,zhenghao1); //如果大于0,显示正号
}
if(val<0)
{
val=0-val;//去除负号
Show8X16(2,72,fuhao1); //如果小于0,显示负号
}
if(val==0)
{
Show8X16(2,72,nono); //等于0不显示符号
}
D_ge=val%10; //取个位
D_shi=val%100/10; //取十位
D_bai=val/100; //取百位
switch(D_ge)
{
case 0:{Show8X16(2,96,s0);break;}
case 1:{Show8X16(2,96,s1);break;}
case 2:{Show8X16(2,96,s2);break;}
case 3:{Show8X16(2,96,s3);break;}
case 4:{Show8X16(2,96,s4);break;}
case 5:{Show8X16(2,96,s5);break;}
case 6:{Show8X16(2,96,s6);break;}
case 7:{Show8X16(2,96,s7);break;}
case 8:{Show8X16(2,96,s8);break;}
case 9:{Show8X16(2,96,s9);break;}
}
switch(D_shi)
{
case 0:{Show8X16(2,88,s0);break;}
case 1:{Show8X16(2,88,s1);break;}
case 2:{Show8X16(2,88,s2);break;}
case 3:{Show8X16(2,88,s3);break;}
case 4:{Show8X16(2,88,s4);break;}
case 5:{Show8X16(2,88,s5);break;}
case 6:{Show8X16(2,88,s6);break;}
case 7:{Show8X16(2,88,s7);break;}
case 8:{Show8X16(2,88,s8);break;}
case 9:{Show8X16(2,88,s9);break;}
}
switch(D_bai)
{
case 0:{Show8X16(2,80,s0);break;}
case 1:{Show8X16(2,80,s1);break;}
case 2:{Show8X16(2,80,s2);break;}
case 3:{Show8X16(2,80,s3);break;}
case 4:{Show8X16(2,80,s4);break;}
case 5:{Show8X16(2,80,s5);break;}
case 6:{Show8X16(2,80,s6);break;}
case 7:{Show8X16(2,80,s7);break;}
case 8:{Show8X16(2,80,s8);break;}
case 9:{Show8X16(2,80,s9);break;}
}
}
/*-------------------------------------------------------*/
void ChangeTemp2(int val)
{
int D_ge,D_shi,D_bai;
if(val>0)
{
Show8X16(4,72,zhenghao1);
}
if(val<0)
{
val=0-val;
Show8X16(4,72,fuhao1);
}
if(val==0)
{
Show8X16(4,72,nono);
}
D_ge=val%10;
D_shi=val%100/10;
D_bai=val/100;
switch(D_ge)
{
case 0:{Show8X16(4,96,s0);break;}
case 1:{Show8X16(4,96,s1);break;}
case 2:{Show8X16(4,96,s2);break;}
case 3:{Show8X16(4,96,s3);break;}
case 4:{Show8X16(4,96,s4);break;}
case 5:{Show8X16(4,96,s5);break;}
case 6:{Show8X16(4,96,s6);break;}
case 7:{Show8X16(4,96,s7);break;}
case 8:{Show8X16(4,96,s8);break;}
case 9:{Show8X16(4,96,s9);break;}
}
switch(D_shi)
{
case 0:{Show8X16(4,88,s0);break;}
case 1:{Show8X16(4,88,s1);break;}
case 2:{Show8X16(4,88,s2);break;}
case 3:{Show8X16(4,88,s3);break;}
case 4:{Show8X16(4,88,s4);break;}
case 5:{Show8X16(4,88,s5);break;}
case 6:{Show8X16(4,88,s6);break;}
case 7:{Show8X16(4,88,s7);break;}
case 8:{Show8X16(4,88,s8);break;}
case 9:{Show8X16(4,88,s9);break;}
}
switch(D_bai)
{
case 0:{Show8X16(4,80,s0);break;}
case 1:{Show8X16(4,80,s1);break;}
case 2:{Show8X16(4,80,s2);break;}
case 3:{Show8X16(4,80,s3);break;}
case 4:{Show8X16(4,80,s4);break;}
case 5:{Show8X16(4,80,s5);break;}
case 6:{Show8X16(4,80,s6);break;}
case 7:{Show8X16(4,80,s7);break;}
case 8:{Show8X16(4,80,s8);break;}
case 9:{Show8X16(4,80,s9);break;}
}
}
/*-------------------------------------------------------*/
void ChangeTemp3(int val)
{
int D_ge,D_shi,D_bai,sign;
sign=val&0x80;
val=val&0x7f;
if(sign==0)
{
Show16X32(2,20,zhenghao);
}
if(sign)
{
val=(~val+1)&0x7f;
Show16X32(2,20,fuhao);
}
if(val==0)
{
Show16X32(2,20,nonono);
}
D_ge=val%10;
D_shi=val%100/10;
D_bai=val/100;
switch(D_ge)
{
case 0:{Show16X32(2,68,ss0);break;}
case 1:{Show16X32(2,68,ss1);break;}
case 2:{Show16X32(2,68,ss2);break;}
case 3:{Show16X32(2,68,ss3);break;}
case 4:{Show16X32(2,68,ss4);break;}
case 5:{Show16X32(2,68,ss5);break;}
case 6:{Show16X32(2,68,ss6);break;}
case 7:{Show16X32(2,68,ss7);break;}
case 8:{Show16X32(2,68,ss8);break;}
case 9:{Show16X32(2,68,ss9);break;}
}
switch(D_shi)
{
case 0:{Show16X32(2,52,ss0);break;}
case 1:{Show16X32(2,52,ss1);break;}
case 2:{Show16X32(2,52,ss2);break;}
case 3:{Show16X32(2,52,ss3);break;}
case 4:{Show16X32(2,52,ss4);break;}
case 5:{Show16X32(2,52,ss5);break;}
case 6:{Show16X32(2,52,ss6);break;}
case 7:{Show16X32(2,52,ss7);break;}
case 8:{Show16X32(2,52,ss8);break;}
case 9:{Show16X32(2,52,ss9);break;}
}
switch(D_bai)
{
case 0:{Show16X32(2,36,ss0);break;}
case 1:{Show16X32(2,36,ss1);break;}
case 2:{Show16X32(2,36,ss2);break;}
case 3:{Show16X32(2,36,ss3);break;}
case 4:{Show16X32(2,36,ss4);break;}
case 5:{Show16X32(2,36,ss5);break;}
case 6:{Show16X32(2,36,ss6);break;}
case 7:{Show16X32(2,36,ss7);break;}
case 8:{Show16X32(2,36,ss8);break;}
case 9:{Show16X32(2,36,ss9);break;}
}
if(val>=val1)
{
Show16X16(6,32,zhu);
Show16X16(6,48,yi);
Show16X16(6,64,fang);
Show16X16(6,80,shu);
}
if(val<=val2)
{
Show16X16(6,32,zhu);
Show16X16(6,48,yi);
Show16X16(6,64,bao);
Show16X16(6,80,nuan);
}
if((val>val2)&&(val
Show16X16(6,32,nononono);
Show16X16(6,48,nononono);
Show16X16(6,64,nononono);
Show16X16(6,80,nononono);
}
}
/*-------------------------------------------------------*/
//显示第一页的内容
void LCDShow1()
{
Show16X16(0,16,wen);//温
Show16X16(0,32,du);//度
Show16X16(0,48,ce);//测
Show16X16(0,64,liang);//量
Show16X16(0,80,xi);//系
Show16X16(0,96,tong);//统
Show16X16(2,1,zuo);//作
Show16X16(2,17,zhe);//者
Show8X16(2,33,maohao);//’:‘
Show8X16(2,41,dw);//'w'
Show8X16(2,49,uu);//'u'
Show8X16(2,57,jj);//'j'
Show8X16(2,65,ii);//'i'
Show8X16(2,73,ee);//'e'
Show8X16(2,81,ff);//'f'
Show8X16(2,89,ll);//'l'
Show8X16(2,97,aa);//'a'
Show8X16(2,105,ss);//'s'
Show8X16(2,113,hh);//'h'
Show16X16(4,1,ban);//版
Show16X16(4,17,ben);//本
Show8X16(4,33,maohao);//':’
Show8X16(4,41,vv);//‘v'
Show8X16(4,49,s1);//'1'
Show8X16(4,57,juhao);//'.'
Show8X16(4,65,s0);//'0'
Show16X16(6,20,zheng);//正
Show16X16(6,36,zai);//在
Show16X16(6,52,chu);//初
Show16X16(6,68,shi);//使
Show16X16(6,84,hua);//化
Show8X16(6,100,dian);//’。‘
Show8X16(6,108,dian);//’。‘
Show8X16(6,116,dian);//’。‘
}
/*----------------------------------------------------------*/
//显示第二页子程序
void LCDShow2()
{
Show16X16(0,32,dang);//当
Show16X16(0,48,qian);//前
Show16X16(0,64,wen);//温
Show16X16(0,80,du);//度
//Show16X32(2,20,zhenghao);
//Show16X32(2,52,ss0);
//Show16X32(2,68,ss0);
Show16X32(2,84,danwei_c1);//单位
Show16X32(2,100,danwei_c2);
}
/*----------------------------------------------------------*/
//显示第三页子程序
void LCDShow3()
{
Show16X16(0,48,she);//设
Show16X16(0,64,zhi);//置
Show16X16(2,0,shang);//上
Show16X16(2,16,xian);//限
Show16X16(2,32,wen);//温
Show16X16(2,48,du);//度
Show8X16(2,64,maohao);//“:”
ChangeTemp1(val1);//参数
Show16X16(2,104,danwei_c);//单位
Show16X16(4,0,xia);//下
Show16X16(4,16,xian);//限
Show16X16(4,32,wen);//温
Show16X16(4,48,du);//度
Show8X16(4,64,maohao);//“:”
ChangeTemp2(val2);//参数
Show16X16(4,104,danwei_c);//单位
}
/*---------------------------------------------------------*/
//LCD初始化子程序
void LCDinit()
{
int i;
PORTD=0x00;
TRISB=0x00;
PORTB=0x00;
TRISC=0x80;
PORTC=0x00;
TRISC5=1;
TRISC6=1;
SetOnOff(1);//开显示
LCDShow1();//初始画面
for(i=30;i>0;i--)//延时
{
delay(201,132);
}
}
/*----------------------------------------------------------*/
//主函数
void main()
{
init(); //调用系统初始化函数
LCDinit();
ClearScreen(1);//清屏
ClearScreen(2);
LCDShow2();//显示第二页
while(1)
{
get_temp(); //调用温度转换函数
PORTD=TZ;
ChangeTemp3(TZ);
if(RC5==0)
{
while(1)
{
ClearScreen(1);
ClearScreen(2);
LCDShow3();
while(1)//等待RC5键松开
{
if(RC5==1)break;
}
Show8X16(4,120,nono);
Show8X16(2,120,xinghao);//显示星号,作指示
while(1)
{
if(RC6==0)
{
val1++;
if(val1>=128)
{
val1=128;
}
ChangeTemp1(val1);
}
if(RC7==0)
{
val1--;
if(val1<=-55)
{
val1=-55;
}
ChangeTemp1(val1);
}
while(1)
{
if((RC6==1)&&(RC7==1))break;//等待键松开
}
if(RC5==0)break;
}
while(1)
{
if(RC5==1)break;
}
Show8X16(2,120,nono);
Show8X16(4,120,xinghao);
while(1)
{
if(RC6==0)
{
val2++;
if(val2>=128)
{
val2=128;
}
ChangeTemp2(val2);
}
if(RC7==0)
{
val2--;
if(val2<-55)
{
val2=-55;
}
ChangeTemp2(val2);
}
while(1)
{
if((RC6==1)&&(RC7==1))break;
}
if(RC5==0)break;
}
ClearScreen(1);
ClearScreen(2);
LCDShow2();
break;
}
}
}
}
四. 总结
曾经因为一线总线检测存在脉冲的子程序的延时不对,低电平持续不到480uS,所以程序一直没有进展,且找不到问题原因,耽搁了好多天,昨天偶然中发现了这个原因,一下就成功了,看来还是不够仔细.
通过这个例子,我掌握了KS0108系列液晶的编程及DS18B20温度传感器的使用.但还是有很多不够完善的地方,如1.程序按键不是很灵敏,2.还可以多加些功能,3.有些子程序写得很烦.但不管怎么样,总算写出来了,有兴趣的朋友可以看一下.