单片机源程序如下:
- //---------------main.c----------------
- //内含DS1302驱动程序
- //------------------------------------------
- #include<intrins.h>
- #include<string.h>
- #include<LCD_160128.h>
- #include<DS18B20.h>
- #define ucharunsigned char
- #define uintunsigned int
- extern void cls();
- extern ucharLCD_Initialise();
- extern ucharDisplay_Str_at_xy(uchar x,uchar y,char *fmt ) reentrant;
- extern uchar codeASC_MSK[96*12];
- extern ucharCurrent_Temp_Display_Buffer[];
- sbit SDA = P1^0;
- sbit CLK = P1^1;
- sbit RST = P1^2;
- sbit hi=P1^6;
- sbit lo=P1^7;
- uchar tCount=0;
- //星期的中文对照表
- charcode*WeeksTable[]={"日","一","二","三","四","五","六"};
- uchar DateTime[7];
- char dat_str[13];
- bitDS18B20_IS_OK=1; //传感器正常标志
- //向DS1302写入一个字节
- voidWrite_A_Byte_TO_DS1302(uchar x)
- {
- uchar i;
- for(i=0;i<8;i++)
- {SDA=x&1;CLK=1;CLK=0;x>>=1;
- }
- }
- //从DS1302读取一个字节
- ucharGet_A_Byte_FROM_DS1302()
- {
- uchar i,b,t;
- for(i=0;i<8;i++)
- {
- b>>=1;t=SDA;
- b|=t<<7;CLK=1;CLK=0;
- }
- return b/16*10+b%16;
- }
- //从DS1302指定位置读数据
- ucharRead_Data(uchar addr)
- {
- uchar dat;
- RST=0;CLK=0;RST=1;
- Write_A_Byte_TO_DS1302(addr);
- dat=Get_A_Byte_FROM_DS1302();
- CLK=1;RST=0;
- return dat;
- }
- //读取当前时间
- void GetTime()
- {
- uchar i;
- for(i=0;i<7;i++)
- {
- DateTime[ i]=Read_Data(0x81+2*i);
- }
- }
- //定时器0每秒刷新LCD显示
- void T0_INT()interrupt 1
- {
- TH0=-50000/256;
- TL0=-50000%256;
- if(++tCount!=20)
- return;
- GetTime();
- tCount=0;
- //显示年月日
- sprintf(dat_str,"20%c%c年%c%c月%c%c日",
- DateTime[6]/10+'0',DateTime[6]%10+'0',
- DateTime[4]/10+'0',DateTime[4]%10+'0',
- DateTime[3]/10+'0',DateTime[3]%10+'0');
- Display_Str_at_xy(16,24,dat_str);
- //显示星期
- sprintf(dat_str,"星期%s",WeeksTable[DateTime[5]-1]);
- Display_Str_at_xy(16,44,dat_str);
- //星期时分秒
- sprintf(dat_str,"%c%c时%c%c分%c%c秒",
- DateTime[2]/10+'0',DateTime[2]%10+'0',
- DateTime[1]/10+'0',DateTime[1]%10+'0',
- DateTime[0]/10+'0',DateTime[0]%10+'0');
- Display_Str_at_xy(16,64,dat_str);
- //读取并显示当前温度
- Read_Temperature();
- if(flag==1)
- {
- sprintf(dat_str,"-%c%c.%c",t/10+'0',t%10+'0',(a%16)*10/16+'0');
- Display_Str_at_xy(16,104,dat_str);
- if(t>(20-1))
- {lo=1;}else{lo=0;}
- }
- else
- {
- sprintf(dat_str,"%c%c%c.%c",t/100+'0',t/10%10+'0',t%10+'0',(a%16)*10/16+'0');
- Display_Str_at_xy(16,104,dat_str);
- if(t>(100-1))
- {hi=1;} else{hi=0;}
- }
- }
- //主程序
- void main()
- {
- LCD_Initialise();
- Read_Temperature();
- Delay(50000);
- Delay(50000);
- IE=0x82;
- hi=0;lo=0;
- TH0=-50000/256;
- TL0=-50000%256;
- TR0=1;
- Display_Str_at_xy(16,88,"当前温度:");
- Display_Str_at_xy(60,104,"℃");
- while(1);
- }
- //---------------LCD_160128.c----------------
- //LCD_160128的C文件,内含控制函数
- //------------------------------------------
- #include<stdarg.h>
- #include<stdio.h>
- #include<math.h>
- #include<intrins.h>
- #include<absacc.h>
- #include<string.h>
- #include<lcd_160128.h>
- #defineASC_CHR_WIDTH 8
- #defineASC_CHR_HEIGHT 12
- #defineHZ_CHR_HEIGHT 12
- #defineHZ_CHR_WIDTH 12
- sbit RESET=P3^3;
- uchar codeLCD_WIDTH = 20;
- uchar codeLCD_HEIGHT = 128;
- uchar codeASC_MSK[96*12]={
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' '
- 0x00,0x30,0x78,0x78,0x78,0x30,0x30,0x00,0x30,0x30,0x00,0x00,//'!'
- 0x00,0x66,0x66,0x66,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'"'
- 0x00,0x6c,0x6c,0xfe,0x6c,0x6c,0x6c,0xfe,0x6c,0x6c,0x00,0x00,//'#'
- 0x30,0x30,0x7c,0xc0,0xc0,0x78,0x0c,0x0c,0xf8,0x30,0x30,0x00,//'
- 0x00,0x00,0x00,0xc4,0xcc,0x18,0x30,0x60,0xcc,0x8c,0x00,0x00,//'%'
- 0x00,0x70,0xd8,0xd8,0x70,0xfa,0xde,0xcc,0xdc,0x76,0x00,0x00,//'&'
- 0x00,0x30,0x30,0x30,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'''
- 0x00,0x0c,0x18,0x30,0x60,0x60,0x60,0x30,0x18,0x0c,0x00,0x00,//'('
- 0x00,0x60,0x30,0x18,0x0c,0x0c,0x0c,0x18,0x30,0x60,0x00,0x00,//')'
- 0x00,0x00,0x00,0x66,0x3c,0xff,0x3c,0x66,0x00,0x00,0x00,0x00,//'*'
- 0x00,0x00,0x00,0x18,0x18,0x7e,0x18,0x18,0x00,0x00,0x00,0x00,//'+'
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x38,0x60,0x00,//','
- 0x00,0x00,0x00,0x00,0x00,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,//'-'
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x38,0x00,0x00,//'.'
- 0x00,0x00,0x02,0x06,0x0c,0x18,0x30,0x60,0xc0,0x80,0x00,0x00,//'/'
- 0x00,0x7c,0xc6,0xce,0xde,0xd6,0xf6,0xe6,0xc6,0x7c,0x00,0x00,//'0'
- 0x00,0x10,0x30,0xf0,0x30,0x30,0x30,0x30,0x30,0xfc,0x00,0x00,//'1'
- 0x00,0x78,0xcc,0xcc,0x0c,0x18,0x30,0x60,0xcc,0xfc,0x00,0x00,//'2'
- 0x00,0x78,0xcc,0x0c,0x0c,0x38,0x0c,0x0c,0xcc,0x78,0x00,0x00,//'3'
- 0x00,0x0c,0x1c,0x3c,0x6c,0xcc,0xfe,0x0c,0x0c,0x1e,0x00,0x00,//'4'
- 0x00,0xfc,0xc0,0xc0,0xc0,0xf8,0x0c,0x0c,0xcc,0x78,0x00,0x00,//'5'
- 0x00,0x38,0x60,0xc0,0xc0,0xf8,0xcc,0xcc,0xcc,0x78,0x00,0x00,//'6'
- 0x00,0xfe,0xc6,0xc6,0x06,0x0c,0x18,0x30,0x30,0x30,0x00,0x00,//'7'
- 0x00,0x78,0xcc,0xcc,0xec,0x78,0xdc,0xcc,0xcc,0x78,0x00,0x00,//'8'
- 0x00,0x78,0xcc,0xcc,0xcc,0x7c,0x18,0x18,0x30,0x70,0x00,0x00,//'9'
- 0x00,0x00,0x00,0x38,0x38,0x00,0x00,0x38,0x38,0x00,0x00,0x00,//':'
- 0x00,0x00,0x00,0x38,0x38,0x00,0x00,0x38,0x38,0x18,0x30,0x00,//';'
- 0x00,0x0c,0x18,0x30,0x60,0xc0,0x60,0x30,0x18,0x0c,0x00,0x00,//'<'
- 0x00,0x00,0x00,0x00,0x7e,0x00,0x7e,0x00,0x00,0x00,0x00,0x00,//'='
- 0x00,0x60,0x30,0x18,0x0c,0x06,0x0c,0x18,0x30,0x60,0x00,0x00,//'>'
- 0x00,0x78,0xcc,0x0c,0x18,0x30,0x30,0x00,0x30,0x30,0x00,0x00,//'?'
- 0x00,0x7c,0xc6,0xc6,0xde,0xde,0xde,0xc0,0xc0,0x7c,0x00,0x00,//'@'
- 0x00,0x30,0x78,0xcc,0xcc,0xcc,0xfc,0xcc,0xcc,0xcc,0x00,0x00,//'A'
- 0x00,0xfc,0x66,0x66,0x66,0x7c,0x66,0x66,0x66,0xfc,0x00,0x00,//'B'
- 0x00,0x3c,0x66,0xc6,0xc0,0xc0,0xc0,0xc6,0x66,0x3c,0x00,0x00,//'C'
- 0x00,0xf8,0x6c,0x66,0x66,0x66,0x66,0x66,0x6c,0xf8,0x00,0x00,//'D'
- 0x00,0xfe,0x62,0x60,0x64,0x7c,0x64,0x60,0x62,0xfe,0x00,0x00,//'E'
- 0x00,0xfe,0x66,0x62,0x64,0x7c,0x64,0x60,0x60,0xf0,0x00,0x00,//'F'
- 0x00,0x3c,0x66,0xc6,0xc0,0xc0,0xce,0xc6,0x66,0x3e,0x00,0x00,//'G'
- 0x00,0xcc,0xcc,0xcc,0xcc,0xfc,0xcc,0xcc,0xcc,0xcc,0x00,0x00,//'H'
- 0x00,0x78,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x78,0x00,0x00,//'I'
- 0x00,0x1e,0x0c,0x0c,0x0c,0x0c,0xcc,0xcc,0xcc,0x78,0x00,0x00,//'J'
- 0x00,0xe6,0x66,0x6c,0x6c,0x78,0x6c,0x6c,0x66,0xe6,0x00,0x00,//'K'
- 0x00,0xf0,0x60,0x60,0x60,0x60,0x62,0x66,0x66,0xfe,0x00,0x00,//'L'
- 0x00,0xc6,0xee,0xfe,0xfe,0xd6,0xc6,0xc6,0xc6,0xc6,0x00,0x00,//'M'
- 0x00,0xc6,0xc6,0xe6,0xf6,0xfe,0xde,0xce,0xc6,0xc6,0x00,0x00,//'N'
- 0x00,0x38,0x6c,0xc6,0xc6,0xc6,0xc6,0xc6,0x6c,0x38,0x00,0x00,//'O'
- 0x00,0xfc,0x66,0x66,0x66,0x7c,0x60,0x60,0x60,0xf0,0x00,0x00,//'P'
- 0x00,0x38,0x6c,0xc6,0xc6,0xc6,0xce,0xde,0x7c,0x0c,0x1e,0x00,//'Q'
- 0x00,0xfc,0x66,0x66,0x66,0x7c,0x6c,0x66,0x66,0xe6,0x00,0x00,//'R'
- 0x00,0x78,0xcc,0xcc,0xc0,0x70,0x18,0xcc,0xcc,0x78,0x00,0x00,//'S'
- 0x00,0xfc,0xb4,0x30,0x30,0x30,0x30,0x30,0x30,0x78,0x00,0x00,//'T'
- 0x00,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0x78,0x00,0x00,//'U'
- 0x00,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0x78,0x30,0x00,0x00,//'V'
- 0x00,0xc6,0xc6,0xc6,0xc6,0xd6,0xd6,0x6c,0x6c,0x6c,0x00,0x00,//'W'
- 0x00,0xcc,0xcc,0xcc,0x78,0x30,0x78,0xcc,0xcc,0xcc,0x00,0x00,//'X'
- 0x00,0xcc,0xcc,0xcc,0xcc,0x78,0x30,0x30,0x30,0x78,0x00,0x00,//'Y'
- 0x00,0xfe,0xce,0x98,0x18,0x30,0x60,0x62,0xc6,0xfe,0x00,0x00,//'Z'
- 0x00,0x3c,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x3c,0x00,0x00,//'['
- 0x00,0x00,0x80,0xc0,0x60,0x30,0x18,0x0c,0x06,0x02,0x00,0x00,//'\'
- 0x00,0x3c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x3c,0x00,0x00,//']'
- 0x10,0x38,0x6c,0xc6,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'^'
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x00,//'_'
- 0x30,0x30,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'`'
- 0x00,0x00,0x00,0x00,0x78,0x0c,0x7c,0xcc,0xcc,0x76,0x00,0x00,//'a'
- 0x00,0xe0,0x60,0x60,0x7c,0x66,0x66,0x66,0x66,0xdc,0x00,0x00,//'b'
- 0x00,0x00,0x00,0x00,0x78,0xcc,0xc0,0xc0,0xcc,0x78,0x00,0x00,//'c'
- 0x00,0x1c,0x0c,0x0c,0x7c,0xcc,0xcc,0xcc,0xcc,0x76,0x00,0x00,//'d'
- 0x00,0x00,0x00,0x00,0x78,0xcc,0xfc,0xc0,0xcc,0x78,0x00,0x00,//'e'
- 0x00,0x38,0x6c,0x60,0x60,0xf8,0x60,0x60,0x60,0xf0,0x00,0x00,//'f'
- 0x00,0x00,0x00,0x00,0x76,0xcc,0xcc,0xcc,0x7c,0x0c,0xcc,0x78,//'g'
- 0x00,0xe0,0x60,0x60,0x6c,0x76,0x66,0x66,0x66,0xe6,0x00,0x00,//'h'
- 0x00,0x18,0x18,0x00,0x78,0x18,0x18,0x18,0x18,0x7e,0x00,0x00,//'i'
- 0x00,0x0c,0x0c,0x00,0x3c,0x0c,0x0c,0x0c,0x0c,0xcc,0xcc,0x78,//'j'
- 0x00,0xe0,0x60,0x60,0x66,0x6c,0x78,0x6c,0x66,0xe6,0x00,0x00,//'k'
- 0x00,0x78,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x7e,0x00,0x00,//'l'
- 0x00,0x00,0x00,0x00,0xfc,0xd6,0xd6,0xd6,0xd6,0xc6,0x00,0x00,//'m'
- 0x00,0x00,0x00,0x00,0xf8,0xcc,0xcc,0xcc,0xcc,0xcc,0x00,0x00,//'n'
- 0x00,0x00,0x00,0x00,0x78,0xcc,0xcc,0xcc,0xcc,0x78,0x00,0x00,//'o'
- 0x00,0x00,0x00,0x00,0xdc,0x66,0x66,0x66,0x66,0x7c,0x60,0xf0,//'p'
- 0x00,0x00,0x00,0x00,0x76,0xcc,0xcc,0xcc,0xcc,0x7c,0x0c,0x1e,//'q'
- 0x00,0x00,0x00,0x00,0xec,0x6e,0x76,0x60,0x60,0xf0,0x00,0x00,//'r'
- 0x00,0x00,0x00,0x00,0x78,0xcc,0x60,0x18,0xcc,0x78,0x00,0x00,//'s'
- 0x00,0x00,0x20,0x60,0xfc,0x60,0x60,0x60,0x6c,0x38,0x00,0x00,//'t'
- 0x00,0x00,0x00,0x00,0xcc,0xcc,0xcc,0xcc,0xcc,0x76,0x00,0x00,//'u'
- 0x00,0x00,0x00,0x00,0xcc,0xcc,0xcc,0xcc,0x78,0x30,0x00,0x00,//'v'
- 0x00,0x00,0x00,0x00,0xc6,0xc6,0xd6,0xd6,0x6c,0x6c,0x00,0x00,//'w'
- 0x00,0x00,0x00,0x00,0xc6,0x6c,0x38,0x38,0x6c,0xc6,0x00,0x00,//'x'
- 0x00,0x00,0x00,0x00,0x66,0x66,0x66,0x66,0x3c,0x0c,0x18,0xf0,//'y'
- 0x00,0x00,0x00,0x00,0xfc,0x8c,0x18,0x60,0xc4,0xfc,0x00,0x00,//'z'
- 0x00,0x1c,0x30,0x30,0x60,0xc0,0x60,0x30,0x30,0x1c,0x00,0x00,//'{'
- 0x00,0x18,0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x18,0x00,0x00,//'|'
- 0x00,0xe0,0x30,0x30,0x18,0x0c,0x18,0x30,0x30,0xe0,0x00,0x00,//'}'
- 0x00,0x73,0xda,0xce,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'~'
- };
- uchar gCurRow,gCurCol;
- uchar tCurRow,tCurCol;
- uchar ShowModeSW;
- uinttxthome,grhome;
- ucharStatus_BIT_01(); // 状态位STA1,STA0判断(读写指令和读写数据)
- ucharStatus_BIT_3(); // 状态位ST3判断(数据自动写状态)
- ucharLCD_Write_Command_P2(uchar cmd,uchar para1,uchar para2); // 写双参数的指令
- ucharLCD_Write_Command_P1(uchar cmd,uchar Para1); // 写单参数的指令
- ucharLCD_Write_Command(uchar cmd); // 写无参数的指令
- ucharLCD_Write_Data(uchar dat); // 写数据
- ucharLCD_Read_Data(); // 读数据
- voidSet_LCD_POS(uchar row, uchar col) reentrant; //设置当前地址
- void cls(); // 清屏
- ucharLCD_Initialise(); // LCM 初始化
- uchar Display_Str_at_xy(ucharx,uchar y,char *fmt) reentrant;// ASCII(8*16) 及 汉字(16*16) 显示函数
- void OutToLCD(ucharDat,uchar x,uchar y);//显示辅助函数
- void Line( unsignedchar x1, unsigned char y1, unsigned char x2, unsigned char y2, uchar Mode)reentrant;
- void Pixel(unsignedchar PointX,unsigned char PointY, uchar Mode);
- typedef structtypFNT_GB16 // 汉字字模显示数据结构
- {
- char Index[2];
- char Msk[24];
- };
-
- struct typFNT_GB16code GB_16[]={
- "年",0x20,0x00,0x3F,0xE0,0x42,0x00,0x82,0x00,0x3F,0xC0,0x22,0x00,
- 0x22,0x00,0xFF,0xE0,0x02,0x00,0x02,0x00,0x02,0x00,0x00,0x00,
- "月",0x1F,0x80,0x10,0x80,0x10,0x80,0x1F,0x80,0x10,0x80,0x10,0x80,
- 0x1F,0x80,0x10,0x80,0x10,0x80,0x20,0x80,0x43,0x80,0x00,0x00,
- "日",0x3F,0xC0,0x20,0x40,0x20,0x40,0x20,0x40,0x3F,0xC0,0x20,0x40,
- 0x20,0x40,0x20,0x40,0x20,0x40,0x3F,0xC0,0x20,0x40,0x00,0x00,
- "时",0x00,0x80,0xF0,0x80,0x9F,0xE0,0x90,0x80,0x94,0x80,0xF2,0x80,
- 0x92,0x80,0x90,0x80,0xF0,0x80,0x90,0x80,0x03,0x80,0x00,0x00,
- "分",0x11,0x00,0x11,0x00,0x20,0x80,0x20,0x80,0x40,0x40,0xBF,0xA0,
- 0x08,0x80,0x08,0x80,0x10,0x80,0x20,0x80,0xC7,0x00,0x00,0x00,
- "秒",0x31,0x00,0xE1,0x00,0x25,0x40,0xFD,0x20,0x25,0x20,0x75,0x00,
- 0x69,0x40,0xA0,0x40,0xA0,0x80,0x23,0x00,0x3C,0x00,0x00,0x00,
- "星",0x3F,0xC0,0x20,0x40,0x3F,0xC0,0x20,0x40,0x3F,0xC0,0x24,0x00,
- 0x3F,0xC0,0x44,0x00,0xBF,0xC0,0x04,0x00,0xFF,0xE0,0x00,0x00,
- "期",0x49,0xE0,0xFD,0x20,0x49,0x20,0x79,0xE0,0x49,0x20,0x79,0x20,
- 0x49,0xE0,0xFD,0x20,0x29,0x20,0x45,0x20,0x82,0x60,0x00,0x00,
- "一",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0xFF,0xE0,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- "二",0x00,0x00,0x00,0x80,0x7F,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xE0,0x00,0x00,0x00,0x00,
- "三",0x00,0x80,0x7F,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x80,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0xFF,0xE0,0x00,0x00,
- "四",0x7F,0xE0,0x49,0x20,0x49,0x20,0x49,0x20,0x49,0x20,0x49,0x20,
- 0x51,0x20,0x61,0xE0,0x40,0x20,0x7F,0xE0,0x40,0x20,0x00,0x00,
- "五",0x7F,0xC0,0x08,0x00,0x08,0x00,0x08,0x00,0x7F,0x80,0x08,0x80,
- 0x08,0x80,0x10,0x80,0x10,0x80,0x10,0x80,0xFF,0xE0,0x00,0x00,
- "六",0x08,0x00,0x04,0x00,0x04,0x00,0xFF,0xE0,0x00,0x00,0x12,0x00,
- 0x11,0x00,0x20,0x80,0x20,0x80,0x40,0x40,0x80,0x40,0x00,0x00,
- "日",0x3F,0xC0,0x20,0x40,0x20,0x40,0x20,0x40,0x3F,0xC0,0x20,0x40,
- 0x20,0x40,0x20,0x40,0x20,0x40,0x3F,0xC0,0x20,0x40,0x00,0x00,
- "当",0x04,0x00,0x44,0x40,0x24,0x80,0x05,0x00,0xFF,0xC0,0x00,0x40,
- 0x00,0x40,0x7F,0xC0,0x00,0x40,0x00,0x40,0xFF,0xC0,0x00,0x00,
- "前",0x11,0x00,0x0A,0x40,0xFF,0xE0,0x00,0x00,0x79,0x40,0x49,0x40,
- 0x79,0x40,0x49,0x40,0x79,0x40,0x48,0x40,0x59,0xC0,0x00,0x00,
- "温",0x8F,0xC0,0x48,0x40,0x0F,0xC0,0x88,0x40,0x4F,0xC0,0x40,0x00,
- 0x5F,0xC0,0x95,0x40,0x95,0x40,0x95,0x40,0xBF,0xE0,0x00,0x00,
- "度",0x02,0x00,0x7F,0xE0,0x48,0x80,0x7F,0xE0,0x48,0x80,0x4F,0x80,
- 0x40,0x00,0x5F,0x80,0x45,0x00,0x87,0x00,0xB8,0xE0,0x00,0x00,
- ":",0x00,0x00,0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- "℃",0x00,0x00,0xE7,0x40,0xA8,0xC0,0xF0,0x40,0x10,0x00,0x10,0x00,
- 0x10,0x00,0x10,0x40,0x08,0x40,0x07,0x80,0x00,0x00,0x00,0x00,
- };
- ucharStatus_BIT_01(void) // 状态位STA1,STA0判断(读写指令和读写数据)
- {
- uchar i;
-
- for(i=5;i>0;i--)
- {
- if((LCMCW & 0x03) == 0x03)
- break;
- }
- return i; // 若返回零,说明错误
- }
- ucharStatus_BIT_3(void) // 状态位ST3判断(数据自动写状态)
- {
- uchar i;
-
- for(i=5;i>0;i--)
- {
- if((LCMCW & 0x08) == 0x08)
- break;
- }
- return i; // 若返回零,说明错误
- }
- ucharLCD_Write_Command_P2(uchar cmd,uchar para1,uchar para2) // 写双参数的指令
- {
- if(Status_BIT_01() == 0) return 1;
- LCMDW = para1;
- if(Status_BIT_01() == 0) return 2;
- LCMDW = para2;
- if(Status_BIT_01() == 0) return 3;
- LCMCW = cmd;
- return 0; // 返回0成功
- }
- ucharLCD_Write_Command_P1(uchar cmd,uchar para1) // 写单参数的指令
- {
- if(Status_BIT_01() == 0) return 1;
- LCMDW = para1;
- if(Status_BIT_01() == 0) return 2;
- LCMCW = cmd;
- return 0; // 返回0成功
- }
- ucharLCD_Write_Command(uchar cmd) // 写无参数的指令
- {
- if(Status_BIT_01() == 0)
- return 1;
- LCMCW = cmd;
- return 0; // 返回0成功
- }
- ucharLCD_Write_Data(uchar dat) // 写数据
- {
- if(Status_BIT_3() == 0)
- return 1;
- LCMDW = dat;
- return 0; // 返回0成功
- }
- ucharLCD_Read_Data() // 读数据
- {
- if(Status_BIT_01() == 0)
- return 1;
- return LCMDW;
- }
- voidSet_LCD_Pos(uchar row, uchar col) reentrant//设置当前地址
- {
- uint Pos;
-
- Pos = row * LCD_WIDTH+ col;
- LCD_Write_Command_P2(LC_ADD_POS,Pos %256,Pos / 256); //修改
- gCurRow = row;
- gCurCol = col;
- }
- // 清屏
- void cls(void)
- {
- uint i;
-
- LCD_Write_Command_P2(LC_ADD_POS,0x00,0x00);// 置地址指针
- LCD_Write_Command(LC_AUT_WR); // 自动写
- for(i=0;i<0x2000;i++)
- {
- Status_BIT_3();
- LCD_Write_Data(0x00); // 写数据
- }
- LCD_Write_Command(LC_AUT_OVR); // 自动写结束
- LCD_Write_Command_P2(LC_ADD_POS,0x00,0x00);// 重置地址指针
- gCurRow = 0; // 置地址指针存储变量
- gCurCol = 0;
- }
- // LCM 初始化
- ucharLCD_Initialise()
- {
- RESET=0;
- RESET=1;
- LCD_Write_Command_P2(LC_TXT_STP,0x00,0x00);// 文本显示区首地址
- LCD_Write_Command_P2(LC_TXT_WID,LCD_WIDTH,0x00);// 文本显示区宽度
- LCD_Write_Command_P2(LC_GRH_STP,0x00,0x00);// 图形显示区首地址
- LCD_Write_Command_P2(LC_GRH_WID,LCD_WIDTH,0x00);// 图形显示区宽度
- LCD_Write_Command_P1(LC_CGR_POS,CGRAMSTART>> 11);
- LCD_Write_Command(LC_CUR_SHP | 0x01); // 光标形状
- LCD_Write_Command(LC_MOD_OR); // 显示方式设置
- LCD_Write_Command(LC_DIS_SW |0x08);
- grhome = GRSTART;
- txthome = TXTSTART;
- return 0;
- }
- // ASCII及汉字显示函数
- ucharDisplay_Str_at_xy(uchar x,uchar y,char *fmt) reentrant
- {
- char c1,c2,cData;
- uchar i=0,j,uLen;
- uchar k;
- uLen=strlen(fmt);
- while(i<uLen)
- {
- c1 = fmt;
- c2 = fmt[i+1];
-
- Set_LCD_Pos(y,x/8);
- if(c1 >= 0)
- {
- // ASCII
- if(c1 < 0x20)
- {
- switch(c1)
- {
- case CR:
- case LF: // 回车或换行
- i++;
- x=0;
- if(y < 112)y+=HZ_CHR_HEIGHT;
- continue;
- case BS: // 退格
- i++;
- if(y>ASC_CHR_WIDTH)y-=ASC_CHR_WIDTH;
- cData = 0x00;
- break;
- }
- }
- for(j=0;j<ASC_CHR_HEIGHT;j++)
- {
-
- if(c1 >= 0x1f)
- {
- cData=ASC_MSK[(c1-0x1f)*ASC_CHR_HEIGHT+j];
- Set_LCD_Pos(y+j,x/8);
- if((x%8)==0)
- {
- LCD_Write_Command(LC_AUT_WR);// 写数据
- LCD_Write_Data(cData);
- LCD_Write_Command(LC_AUT_OVR);
- }
- else
- OutToLCD(cData,x,y+j);
- }
- Set_LCD_Pos(y+j,x/8);
- }
- if(c1 != BS) // 非退格
- x+=ASC_CHR_WIDTH;
- }
- else
- { // 中文
-
- for(j=0;j<sizeof(GB_16)/sizeof(GB_16[0]);j++)
- {
- if(c1 == GB_16[j].Index[0] && c2 == GB_16[j].Index[1])
- break;
- }
- for(k=0;k<HZ_CHR_HEIGHT;k++)
- {
-
- Set_LCD_Pos(y+k,x/8);
- if(j < sizeof(GB_16)/sizeof(GB_16[0]))
- {
- c1=GB_16[j].Msk[k*2];
- c2=GB_16[j].Msk[k*2+1];
- }
- else
- c1=c2=0;
- if((x%8)==0)
- {
- LCD_Write_Command(LC_AUT_WR);
- LCD_Write_Data(c1);
- LCD_Write_Command(LC_AUT_OVR);
- }
- else
- OutToLCD(c1,x,y+k);
-
- if(((x+2+HZ_CHR_WIDTH/2)%8)==0)
- {
- LCD_Write_Command(LC_AUT_WR);
- LCD_Write_Data(c2);
- LCD_Write_Command(LC_AUT_OVR);
- }
- else
- OutToLCD(c2,x+2+HZ_CHR_WIDTH/2,y+k);
- }
- x+=HZ_CHR_WIDTH;
- i++;
- }
- i++;
- }
- return uLen;
- }
- //输出起点X不是8的倍数时,原字节分成两部分输出到LCD
- void OutToLCD(ucharDat,uchar x,uchar y)
- {
- uchar dat1,dat2,a,b;
- b=x%8;a=8-b;
- Set_LCD_Pos(y,x/8);
- LCD_Write_Command(LC_AUT_RD); // 读数据
- dat1=LCD_Read_Data();
- dat2=LCD_Read_Data();
- dat1=(dat1&(0xFF<<a))|(Dat>>b);
- dat2=(dat2&(0xFF>>b))|(Dat<<a);
- LCD_Write_Command(LC_AUT_OVR);
- Set_LCD_POS(y,x/8);
- LCD_Write_Command(LC_AUT_WR);
- LCD_Write_Data(dat1);
- LCD_Write_Data(dat2);
- LCD_Write_Command(LC_AUT_OVR);
- }
- /**********************************
- //= 函数原型:Pixel(unsigned char PointX,unsigned char PointY, bit Mode)
- //= 功 能: 在指定坐标位置显示一个点
- //= 参 数: 坐标,显示点或清除点
- //= 返 回 值:
- //= 函数性质:私有函数
- //= 如果显示屏超过了256*256,请修改这个函数 PointX,PointY的类型
- //= Mode 1:显示 0:清除该点
- **********************************/
- void Pixel(unsignedchar x,unsigned char y, uchar Mode)
- {
- unsigned char Start_Addr,dat;
- Start_Addr=7-(x%8);
- dat=LC_BIT_OP|Start_Addr; //生产位操作命令画点的数据
- if(Mode) dat|=0x08;
- Set_LCD_Pos(y,x/8);
- LCD_Write_Command(LC_BIT_OP|dat); // 写数据
- }
- void Exchange(uchar*a,uchar *b)
- {
- uchar t;
- t = *a;*a = *b;*b =t;
- }
- /**********************************
- //= 函数原型: voidline( unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2,bit Mode)
- //= 功 能: 划线函数
- //= 参 数: 坐标1,坐标2,显示点或清除点
- //= 返 回 值:
- //= 函数性质:私有函数
- //= 其它:显示点阵不超过255*255
- /**********************************/
- void Line( unsignedchar x1,unsigned char y1, unsigned char x2,unsigned char y2, uchar Mode) reentrant
- {
- unsigned char x,y;
- float k,b;
- if( abs(y1-y2) <= abs(x1-x2) ) //|k|<=1
- {
- k=(float)(y2-y1) / (float)(x2-x1) ;
- b=y1-k*x1;
- if( x1 > x2 )
- Exchange(&x1,&x2);
- for(x=x1;x<=x2;x++)
- {
- y=(uchar)(k*x+b);
- Pixel(x, y, Mode);
- }
- }
- else
- {
- k = (float)(x2-x1) / (float)(y2-y1);
- b = x1 - k*y1;
- if(y1 > y2)
- Exchange(&y1,&y2);
- for(y=y1;y<=y2;y++)
- {
- x=(uchar)(k*y+b);
- Pixel(x, y, Mode);
- }
- }
- }
- voidDraw_Image(uchar *G_Buffer,uchar Start_Row,uchar Start_Col) reentrant
- {
- uchar i,j;
- for(i=0;i < G_Buffer[1];i++)
- {
- Set_LCD_POS(Start_Row + i,Start_Col);
- LCD_Write_Command(LC_AUT_WR);
- for( j = 0;j< G_Buffer[0] /8;j++)
- LCD_Write_Data(G_Buffer [ i *(G_Buffer[0] / 8) +j +2]);
- LCD_Write_Command(LC_AUT_OVR);
- }
- }
- //---------------LCD_160128.h----------------
- //LCD_160128的头文件
- //------------------------------------------
- #include<at89x52.h>
- #include<intrins.h>
- #include<stdarg.h>
- #include<stdio.h>
- #include<math.h>
- #include<absacc.h>
- #include<string.h>
- #define uintunsigned int
- #define ucharunsigned char
- #define STX 0x02
- #define ETX 0x03
- #define EOT 0x04
- #define ENQ 0x05
- #define BS 0x08
- #define CR 0x0d
- #define LF 0x0a
- #define DLE 0x10
- #define ETB 0x17
- #define SPACE 0x20
- #define COMMA 0x2c
- #define TRUE 1
- #define FALSE 0
- #define HIGH 1
- #define LOW 0
- #define LCMDWXBYTE[0x8000]
- #define LCMCWXBYTE[0x8100]
- #define DISRAM_SIZE0x7fff
- #define TXTSTART0x0000
- #define GRSTART0x6800
- #define CGRAMSTART0x7800
- #define LC_CUR_POS0x21
- #define LC_CGR_POS0x22
- #define LC_ADD_POS0x24
- #define LC_TXT_STP0x40
- #define LC_TXT_WID0x41
- #define LC_GRH_STP0x42
- #define LC_GRH_WID0x43
- #defineLC_MOD_OR 0x80
- #define LC_MOD_XOR0x81
- #define LC_MOD_AND0x82
- #define LC_MOD_TCH0x83
- #defineLC_DIS_SW 0x90
- #define LC_CUR_SHP0xa0
- #defineLC_AUT_WR 0xb0
- #defineLC_AUT_RD 0xb1
- #define LC_AUT_OVR0xb2
- #defineLC_INC_WR 0xc0
- #defineLC_INC_RD 0xc1
- #defineLC_DEC_WR 0xc2
- #define LC_DEC_RD 0xc3
- #defineLC_NOC_WR 0xc4
- #defineLC_NOC_RD 0xc5
- #defineLC_SCN_RD 0xe0
- #defineLC_INC_CP 0xe8
- #defineLC_BIT_OP 0xf0
- //---------------DS18B20.h----------------
- //温度转换
- //------------------------------------------
- #include<at89x52.h>
- #include <stdio.h>
- #define DQ P3_5 //定义DS18B20总线I/O
- unsigned char t; //从DS18B20读取的温度值
- unsigned int flag;
- unsigned char a=0;
- unsigned char b=0;
- /*****延时子程序*****/
- void Delay(int num)
- {
- while(num--) ;
- }
- /*****初始化DS18B20*****/
- voidInit_DS18B20(void)
- {
- unsigned char x=0;
- DQ = 1; //DQ复位
- Delay(8); //稍做延时
- DQ = 0; //单片机将DQ拉低
- Delay(80); //精确延时,大于480us
- DQ = 1; //拉高总线
- Delay(14);
- x = DQ; //稍做延时后,如果x=0则初始化成功,x=1则初始化失败
- Delay(20);
- }
- /*****读一个字节*****/
- unsigned char ReadOneChar(void)
- {
- unsigned char i=0;
- unsigned char dat = 0;
- for (i=8;i>0;i--)
- {
- DQ = 0; // 给脉冲信号
- dat>>=1;
- DQ = 1; // 给脉冲信号
- if(DQ)
- dat|=0x80;
- Delay(4);
- }
- return(dat);
- }
- /*****写一个字节*****/
- voidWriteOneChar(unsigned char dat)
- {
- unsigned char i=0;
- for (i=8; i>0; i--)
- {
- DQ = 0;
- DQ = dat&0x01;
- Delay(5);
- DQ = 1;
- dat>>=1;
- }
- }
- /*****读取温度*****/
- unsigned intRead_Temperature(void)
- {
- Init_DS18B20();
- WriteOneChar(0xCC); //跳过读序号列号的操作
- WriteOneChar(0x44); //启动温度转换
- Init_DS18B20();
- WriteOneChar(0xCC); //跳过读序号列号的操作
- WriteOneChar(0xBE); //读取温度寄存器
- a=ReadOneChar(); //读低8位
- b=ReadOneChar(); //读高8位
- if(b>127)// 当高8位的第8位为1时,就是负值
- { // 下面是进行转换
- a=(~a)+1; // 当为负值的时候,低8位的操作
- if((~a) >= 0xff)// <-127度 ,则高8位进位
- {
- b=(~b)+1;
- }
- else
- {//不小于-127度时,高8位取反
- b=~b;
- }
- flag=1;//这个表示为负值,你只要在液晶上显示一个“-”就行。
- }
- else
- { flag=0;}
- t=((b*256)+a)*0.0625;
- return(t);
- }
- /*****END*****/
复制代码
|