找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 849|回复: 0
打印 上一主题 下一主题
收起左侧

求改程序,液晶中文显示温度与时间,要求如图显示仿真结果

[复制链接]
跳转到指定楼层
楼主


单片机源程序如下:
  1. //---------------main.c----------------
  2. //内含DS1302驱动程序
  3. //------------------------------------------
  4. #include<intrins.h>
  5. #include<string.h>
  6. #include<LCD_160128.h>
  7. #include<DS18B20.h>
  8. #define ucharunsigned char
  9. #define uintunsigned int
  10. extern void cls();
  11. extern ucharLCD_Initialise();
  12. extern ucharDisplay_Str_at_xy(uchar x,uchar y,char *fmt ) reentrant;
  13. extern uchar codeASC_MSK[96*12];
  14. extern ucharCurrent_Temp_Display_Buffer[];
  15. sbit SDA = P1^0;
  16. sbit CLK = P1^1;
  17. sbit RST = P1^2;
  18. sbit hi=P1^6;
  19. sbit lo=P1^7;
  20. uchar tCount=0;
  21. //星期的中文对照表
  22. charcode*WeeksTable[]={"日","一","二","三","四","五","六"};
  23. uchar DateTime[7];
  24. char dat_str[13];
  25. bitDS18B20_IS_OK=1;   //传感器正常标志
  26. //向DS1302写入一个字节
  27. voidWrite_A_Byte_TO_DS1302(uchar x)
  28. {
  29.    uchar i;
  30.    for(i=0;i<8;i++)
  31.      {SDA=x&1;CLK=1;CLK=0;x>>=1;
  32.       }
  33.     }
  34. //从DS1302读取一个字节
  35. ucharGet_A_Byte_FROM_DS1302()
  36. {
  37.     uchar i,b,t;
  38.     for(i=0;i<8;i++)
  39. {
  40.      b>>=1;t=SDA;
  41.      b|=t<<7;CLK=1;CLK=0;
  42.      }
  43.     return b/16*10+b%16;
  44.    }
  45. //从DS1302指定位置读数据
  46. ucharRead_Data(uchar addr)
  47. {
  48.    uchar dat;
  49.    RST=0;CLK=0;RST=1;
  50.    Write_A_Byte_TO_DS1302(addr);
  51.    dat=Get_A_Byte_FROM_DS1302();
  52.    CLK=1;RST=0;
  53.    return dat;
  54.   }
  55. //读取当前时间
  56. void GetTime()
  57. {
  58.   uchar i;
  59.   for(i=0;i<7;i++)
  60.   {
  61.     DateTime[ i]=Read_Data(0x81+2*i);
  62.    }
  63.   }
  64. //定时器0每秒刷新LCD显示
  65. void T0_INT()interrupt 1
  66. {
  67.   TH0=-50000/256;
  68.   TL0=-50000%256;
  69.   if(++tCount!=20)
  70.   return;
  71.   GetTime();
  72.   tCount=0;
  73.   //显示年月日
  74.   sprintf(dat_str,"20%c%c年%c%c月%c%c日",
  75.   DateTime[6]/10+'0',DateTime[6]%10+'0',
  76.   DateTime[4]/10+'0',DateTime[4]%10+'0',
  77.   DateTime[3]/10+'0',DateTime[3]%10+'0');
  78.   Display_Str_at_xy(16,24,dat_str);
  79.   //显示星期
  80.   sprintf(dat_str,"星期%s",WeeksTable[DateTime[5]-1]);
  81.   Display_Str_at_xy(16,44,dat_str);
  82.   //星期时分秒
  83.   sprintf(dat_str,"%c%c时%c%c分%c%c秒",
  84.   DateTime[2]/10+'0',DateTime[2]%10+'0',
  85.   DateTime[1]/10+'0',DateTime[1]%10+'0',
  86.   DateTime[0]/10+'0',DateTime[0]%10+'0');
  87.   Display_Str_at_xy(16,64,dat_str);  
  88.    //读取并显示当前温度  
  89.   Read_Temperature();
  90.   if(flag==1)
  91. {
  92.   sprintf(dat_str,"-%c%c.%c",t/10+'0',t%10+'0',(a%16)*10/16+'0');
  93.   Display_Str_at_xy(16,104,dat_str);
  94.   if(t>(20-1))
  95.   {lo=1;}else{lo=0;}
  96.   }
  97.    else
  98.    {
  99.     sprintf(dat_str,"%c%c%c.%c",t/100+'0',t/10%10+'0',t%10+'0',(a%16)*10/16+'0');
  100.    Display_Str_at_xy(16,104,dat_str);
  101.    if(t>(100-1))
  102.    {hi=1;} else{hi=0;}
  103.    }
  104. }
  105. //主程序         
  106. void main()
  107. {

  108.   LCD_Initialise();
  109.   Read_Temperature();
  110.   Delay(50000);
  111.   Delay(50000);
  112.   IE=0x82;
  113.   hi=0;lo=0;
  114.   TH0=-50000/256;
  115.   TL0=-50000%256;
  116.   TR0=1;   
  117.   Display_Str_at_xy(16,88,"当前温度:");
  118.   Display_Str_at_xy(60,104,"℃");
  119.    while(1);
  120. }
  121. //---------------LCD_160128.c----------------
  122. //LCD_160128的C文件,内含控制函数
  123. //------------------------------------------
  124. #include<stdarg.h>
  125. #include<stdio.h>
  126. #include<math.h>
  127. #include<intrins.h>
  128. #include<absacc.h>
  129. #include<string.h>
  130. #include<lcd_160128.h>
  131. #defineASC_CHR_WIDTH  8
  132. #defineASC_CHR_HEIGHT 12
  133. #defineHZ_CHR_HEIGHT 12
  134. #defineHZ_CHR_WIDTH 12
  135. sbit RESET=P3^3;
  136. uchar codeLCD_WIDTH  = 20;
  137. uchar codeLCD_HEIGHT  = 128;
  138. uchar codeASC_MSK[96*12]={
  139. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,
  140. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' '
  141. 0x00,0x30,0x78,0x78,0x78,0x30,0x30,0x00,0x30,0x30,0x00,0x00,//'!'
  142. 0x00,0x66,0x66,0x66,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'"'
  143. 0x00,0x6c,0x6c,0xfe,0x6c,0x6c,0x6c,0xfe,0x6c,0x6c,0x00,0x00,//'#'
  144. 0x30,0x30,0x7c,0xc0,0xc0,0x78,0x0c,0x0c,0xf8,0x30,0x30,0x00,//'


  145. 0x00,0x00,0x00,0xc4,0xcc,0x18,0x30,0x60,0xcc,0x8c,0x00,0x00,//'%'
  146. 0x00,0x70,0xd8,0xd8,0x70,0xfa,0xde,0xcc,0xdc,0x76,0x00,0x00,//'&'
  147. 0x00,0x30,0x30,0x30,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'''
  148. 0x00,0x0c,0x18,0x30,0x60,0x60,0x60,0x30,0x18,0x0c,0x00,0x00,//'('
  149. 0x00,0x60,0x30,0x18,0x0c,0x0c,0x0c,0x18,0x30,0x60,0x00,0x00,//')'
  150. 0x00,0x00,0x00,0x66,0x3c,0xff,0x3c,0x66,0x00,0x00,0x00,0x00,//'*'
  151. 0x00,0x00,0x00,0x18,0x18,0x7e,0x18,0x18,0x00,0x00,0x00,0x00,//'+'
  152. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x38,0x60,0x00,//','
  153. 0x00,0x00,0x00,0x00,0x00,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,//'-'
  154. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x38,0x00,0x00,//'.'
  155. 0x00,0x00,0x02,0x06,0x0c,0x18,0x30,0x60,0xc0,0x80,0x00,0x00,//'/'
  156. 0x00,0x7c,0xc6,0xce,0xde,0xd6,0xf6,0xe6,0xc6,0x7c,0x00,0x00,//'0'
  157. 0x00,0x10,0x30,0xf0,0x30,0x30,0x30,0x30,0x30,0xfc,0x00,0x00,//'1'
  158. 0x00,0x78,0xcc,0xcc,0x0c,0x18,0x30,0x60,0xcc,0xfc,0x00,0x00,//'2'
  159. 0x00,0x78,0xcc,0x0c,0x0c,0x38,0x0c,0x0c,0xcc,0x78,0x00,0x00,//'3'
  160. 0x00,0x0c,0x1c,0x3c,0x6c,0xcc,0xfe,0x0c,0x0c,0x1e,0x00,0x00,//'4'
  161. 0x00,0xfc,0xc0,0xc0,0xc0,0xf8,0x0c,0x0c,0xcc,0x78,0x00,0x00,//'5'
  162. 0x00,0x38,0x60,0xc0,0xc0,0xf8,0xcc,0xcc,0xcc,0x78,0x00,0x00,//'6'
  163. 0x00,0xfe,0xc6,0xc6,0x06,0x0c,0x18,0x30,0x30,0x30,0x00,0x00,//'7'
  164. 0x00,0x78,0xcc,0xcc,0xec,0x78,0xdc,0xcc,0xcc,0x78,0x00,0x00,//'8'
  165. 0x00,0x78,0xcc,0xcc,0xcc,0x7c,0x18,0x18,0x30,0x70,0x00,0x00,//'9'
  166. 0x00,0x00,0x00,0x38,0x38,0x00,0x00,0x38,0x38,0x00,0x00,0x00,//':'
  167. 0x00,0x00,0x00,0x38,0x38,0x00,0x00,0x38,0x38,0x18,0x30,0x00,//';'
  168. 0x00,0x0c,0x18,0x30,0x60,0xc0,0x60,0x30,0x18,0x0c,0x00,0x00,//'<'
  169. 0x00,0x00,0x00,0x00,0x7e,0x00,0x7e,0x00,0x00,0x00,0x00,0x00,//'='
  170. 0x00,0x60,0x30,0x18,0x0c,0x06,0x0c,0x18,0x30,0x60,0x00,0x00,//'>'
  171. 0x00,0x78,0xcc,0x0c,0x18,0x30,0x30,0x00,0x30,0x30,0x00,0x00,//'?'
  172. 0x00,0x7c,0xc6,0xc6,0xde,0xde,0xde,0xc0,0xc0,0x7c,0x00,0x00,//'@'
  173. 0x00,0x30,0x78,0xcc,0xcc,0xcc,0xfc,0xcc,0xcc,0xcc,0x00,0x00,//'A'
  174. 0x00,0xfc,0x66,0x66,0x66,0x7c,0x66,0x66,0x66,0xfc,0x00,0x00,//'B'
  175. 0x00,0x3c,0x66,0xc6,0xc0,0xc0,0xc0,0xc6,0x66,0x3c,0x00,0x00,//'C'
  176. 0x00,0xf8,0x6c,0x66,0x66,0x66,0x66,0x66,0x6c,0xf8,0x00,0x00,//'D'
  177. 0x00,0xfe,0x62,0x60,0x64,0x7c,0x64,0x60,0x62,0xfe,0x00,0x00,//'E'
  178. 0x00,0xfe,0x66,0x62,0x64,0x7c,0x64,0x60,0x60,0xf0,0x00,0x00,//'F'
  179. 0x00,0x3c,0x66,0xc6,0xc0,0xc0,0xce,0xc6,0x66,0x3e,0x00,0x00,//'G'
  180. 0x00,0xcc,0xcc,0xcc,0xcc,0xfc,0xcc,0xcc,0xcc,0xcc,0x00,0x00,//'H'
  181. 0x00,0x78,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x78,0x00,0x00,//'I'
  182. 0x00,0x1e,0x0c,0x0c,0x0c,0x0c,0xcc,0xcc,0xcc,0x78,0x00,0x00,//'J'
  183. 0x00,0xe6,0x66,0x6c,0x6c,0x78,0x6c,0x6c,0x66,0xe6,0x00,0x00,//'K'
  184. 0x00,0xf0,0x60,0x60,0x60,0x60,0x62,0x66,0x66,0xfe,0x00,0x00,//'L'
  185. 0x00,0xc6,0xee,0xfe,0xfe,0xd6,0xc6,0xc6,0xc6,0xc6,0x00,0x00,//'M'
  186. 0x00,0xc6,0xc6,0xe6,0xf6,0xfe,0xde,0xce,0xc6,0xc6,0x00,0x00,//'N'
  187. 0x00,0x38,0x6c,0xc6,0xc6,0xc6,0xc6,0xc6,0x6c,0x38,0x00,0x00,//'O'
  188. 0x00,0xfc,0x66,0x66,0x66,0x7c,0x60,0x60,0x60,0xf0,0x00,0x00,//'P'
  189. 0x00,0x38,0x6c,0xc6,0xc6,0xc6,0xce,0xde,0x7c,0x0c,0x1e,0x00,//'Q'
  190. 0x00,0xfc,0x66,0x66,0x66,0x7c,0x6c,0x66,0x66,0xe6,0x00,0x00,//'R'
  191. 0x00,0x78,0xcc,0xcc,0xc0,0x70,0x18,0xcc,0xcc,0x78,0x00,0x00,//'S'
  192. 0x00,0xfc,0xb4,0x30,0x30,0x30,0x30,0x30,0x30,0x78,0x00,0x00,//'T'
  193. 0x00,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0x78,0x00,0x00,//'U'
  194. 0x00,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0x78,0x30,0x00,0x00,//'V'
  195. 0x00,0xc6,0xc6,0xc6,0xc6,0xd6,0xd6,0x6c,0x6c,0x6c,0x00,0x00,//'W'
  196. 0x00,0xcc,0xcc,0xcc,0x78,0x30,0x78,0xcc,0xcc,0xcc,0x00,0x00,//'X'
  197. 0x00,0xcc,0xcc,0xcc,0xcc,0x78,0x30,0x30,0x30,0x78,0x00,0x00,//'Y'
  198. 0x00,0xfe,0xce,0x98,0x18,0x30,0x60,0x62,0xc6,0xfe,0x00,0x00,//'Z'
  199. 0x00,0x3c,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x3c,0x00,0x00,//'['
  200. 0x00,0x00,0x80,0xc0,0x60,0x30,0x18,0x0c,0x06,0x02,0x00,0x00,//'\'
  201. 0x00,0x3c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x3c,0x00,0x00,//']'
  202. 0x10,0x38,0x6c,0xc6,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'^'
  203. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x00,//'_'
  204. 0x30,0x30,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'`'
  205. 0x00,0x00,0x00,0x00,0x78,0x0c,0x7c,0xcc,0xcc,0x76,0x00,0x00,//'a'
  206. 0x00,0xe0,0x60,0x60,0x7c,0x66,0x66,0x66,0x66,0xdc,0x00,0x00,//'b'
  207. 0x00,0x00,0x00,0x00,0x78,0xcc,0xc0,0xc0,0xcc,0x78,0x00,0x00,//'c'
  208. 0x00,0x1c,0x0c,0x0c,0x7c,0xcc,0xcc,0xcc,0xcc,0x76,0x00,0x00,//'d'
  209. 0x00,0x00,0x00,0x00,0x78,0xcc,0xfc,0xc0,0xcc,0x78,0x00,0x00,//'e'
  210. 0x00,0x38,0x6c,0x60,0x60,0xf8,0x60,0x60,0x60,0xf0,0x00,0x00,//'f'
  211. 0x00,0x00,0x00,0x00,0x76,0xcc,0xcc,0xcc,0x7c,0x0c,0xcc,0x78,//'g'
  212. 0x00,0xe0,0x60,0x60,0x6c,0x76,0x66,0x66,0x66,0xe6,0x00,0x00,//'h'
  213. 0x00,0x18,0x18,0x00,0x78,0x18,0x18,0x18,0x18,0x7e,0x00,0x00,//'i'
  214. 0x00,0x0c,0x0c,0x00,0x3c,0x0c,0x0c,0x0c,0x0c,0xcc,0xcc,0x78,//'j'
  215. 0x00,0xe0,0x60,0x60,0x66,0x6c,0x78,0x6c,0x66,0xe6,0x00,0x00,//'k'
  216. 0x00,0x78,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x7e,0x00,0x00,//'l'
  217. 0x00,0x00,0x00,0x00,0xfc,0xd6,0xd6,0xd6,0xd6,0xc6,0x00,0x00,//'m'
  218. 0x00,0x00,0x00,0x00,0xf8,0xcc,0xcc,0xcc,0xcc,0xcc,0x00,0x00,//'n'
  219. 0x00,0x00,0x00,0x00,0x78,0xcc,0xcc,0xcc,0xcc,0x78,0x00,0x00,//'o'
  220. 0x00,0x00,0x00,0x00,0xdc,0x66,0x66,0x66,0x66,0x7c,0x60,0xf0,//'p'
  221. 0x00,0x00,0x00,0x00,0x76,0xcc,0xcc,0xcc,0xcc,0x7c,0x0c,0x1e,//'q'
  222. 0x00,0x00,0x00,0x00,0xec,0x6e,0x76,0x60,0x60,0xf0,0x00,0x00,//'r'
  223. 0x00,0x00,0x00,0x00,0x78,0xcc,0x60,0x18,0xcc,0x78,0x00,0x00,//'s'
  224. 0x00,0x00,0x20,0x60,0xfc,0x60,0x60,0x60,0x6c,0x38,0x00,0x00,//'t'
  225. 0x00,0x00,0x00,0x00,0xcc,0xcc,0xcc,0xcc,0xcc,0x76,0x00,0x00,//'u'
  226. 0x00,0x00,0x00,0x00,0xcc,0xcc,0xcc,0xcc,0x78,0x30,0x00,0x00,//'v'
  227. 0x00,0x00,0x00,0x00,0xc6,0xc6,0xd6,0xd6,0x6c,0x6c,0x00,0x00,//'w'
  228. 0x00,0x00,0x00,0x00,0xc6,0x6c,0x38,0x38,0x6c,0xc6,0x00,0x00,//'x'
  229. 0x00,0x00,0x00,0x00,0x66,0x66,0x66,0x66,0x3c,0x0c,0x18,0xf0,//'y'
  230. 0x00,0x00,0x00,0x00,0xfc,0x8c,0x18,0x60,0xc4,0xfc,0x00,0x00,//'z'
  231. 0x00,0x1c,0x30,0x30,0x60,0xc0,0x60,0x30,0x30,0x1c,0x00,0x00,//'{'
  232. 0x00,0x18,0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x18,0x00,0x00,//'|'
  233. 0x00,0xe0,0x30,0x30,0x18,0x0c,0x18,0x30,0x30,0xe0,0x00,0x00,//'}'
  234. 0x00,0x73,0xda,0xce,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'~'
  235. };

  236. uchar gCurRow,gCurCol;
  237. uchar tCurRow,tCurCol;
  238. uchar ShowModeSW;
  239. uinttxthome,grhome;
  240. ucharStatus_BIT_01(); // 状态位STA1,STA0判断(读写指令和读写数据)
  241. ucharStatus_BIT_3(); // 状态位ST3判断(数据自动写状态)
  242. ucharLCD_Write_Command_P2(uchar cmd,uchar para1,uchar para2); // 写双参数的指令
  243. ucharLCD_Write_Command_P1(uchar cmd,uchar Para1); // 写单参数的指令
  244. ucharLCD_Write_Command(uchar cmd); // 写无参数的指令
  245. ucharLCD_Write_Data(uchar dat); // 写数据
  246. ucharLCD_Read_Data(); // 读数据
  247. voidSet_LCD_POS(uchar row, uchar col) reentrant;   //设置当前地址
  248. void cls();  // 清屏
  249. ucharLCD_Initialise(); // LCM 初始化
  250. uchar Display_Str_at_xy(ucharx,uchar y,char *fmt) reentrant;// ASCII(8*16) 及 汉字(16*16) 显示函数
  251. void OutToLCD(ucharDat,uchar x,uchar y);//显示辅助函数     
  252. void Line( unsignedchar x1, unsigned char y1, unsigned char x2, unsigned char y2, uchar Mode)reentrant;
  253. void Pixel(unsignedchar PointX,unsigned char PointY, uchar Mode);

  254. typedef structtypFNT_GB16 // 汉字字模显示数据结构
  255. {
  256. char Index[2];
  257. char Msk[24];
  258. };
  259.    
  260. struct typFNT_GB16code GB_16[]={
  261. "年",0x20,0x00,0x3F,0xE0,0x42,0x00,0x82,0x00,0x3F,0xC0,0x22,0x00,
  262.      0x22,0x00,0xFF,0xE0,0x02,0x00,0x02,0x00,0x02,0x00,0x00,0x00,
  263. "月",0x1F,0x80,0x10,0x80,0x10,0x80,0x1F,0x80,0x10,0x80,0x10,0x80,
  264.     0x1F,0x80,0x10,0x80,0x10,0x80,0x20,0x80,0x43,0x80,0x00,0x00,
  265. "日",0x3F,0xC0,0x20,0x40,0x20,0x40,0x20,0x40,0x3F,0xC0,0x20,0x40,
  266.      0x20,0x40,0x20,0x40,0x20,0x40,0x3F,0xC0,0x20,0x40,0x00,0x00,
  267. "时",0x00,0x80,0xF0,0x80,0x9F,0xE0,0x90,0x80,0x94,0x80,0xF2,0x80,
  268.     0x92,0x80,0x90,0x80,0xF0,0x80,0x90,0x80,0x03,0x80,0x00,0x00,
  269. "分",0x11,0x00,0x11,0x00,0x20,0x80,0x20,0x80,0x40,0x40,0xBF,0xA0,
  270.      0x08,0x80,0x08,0x80,0x10,0x80,0x20,0x80,0xC7,0x00,0x00,0x00,
  271. "秒",0x31,0x00,0xE1,0x00,0x25,0x40,0xFD,0x20,0x25,0x20,0x75,0x00,
  272.     0x69,0x40,0xA0,0x40,0xA0,0x80,0x23,0x00,0x3C,0x00,0x00,0x00,
  273. "星",0x3F,0xC0,0x20,0x40,0x3F,0xC0,0x20,0x40,0x3F,0xC0,0x24,0x00,
  274.      0x3F,0xC0,0x44,0x00,0xBF,0xC0,0x04,0x00,0xFF,0xE0,0x00,0x00,
  275. "期",0x49,0xE0,0xFD,0x20,0x49,0x20,0x79,0xE0,0x49,0x20,0x79,0x20,
  276.     0x49,0xE0,0xFD,0x20,0x29,0x20,0x45,0x20,0x82,0x60,0x00,0x00,
  277. "一",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0xFF,0xE0,
  278.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  279. "二",0x00,0x00,0x00,0x80,0x7F,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,
  280.     0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xE0,0x00,0x00,0x00,0x00,
  281. "三",0x00,0x80,0x7F,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x80,
  282.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0xFF,0xE0,0x00,0x00,
  283. "四",0x7F,0xE0,0x49,0x20,0x49,0x20,0x49,0x20,0x49,0x20,0x49,0x20,
  284.     0x51,0x20,0x61,0xE0,0x40,0x20,0x7F,0xE0,0x40,0x20,0x00,0x00,
  285. "五",0x7F,0xC0,0x08,0x00,0x08,0x00,0x08,0x00,0x7F,0x80,0x08,0x80,
  286.     0x08,0x80,0x10,0x80,0x10,0x80,0x10,0x80,0xFF,0xE0,0x00,0x00,
  287. "六",0x08,0x00,0x04,0x00,0x04,0x00,0xFF,0xE0,0x00,0x00,0x12,0x00,
  288.     0x11,0x00,0x20,0x80,0x20,0x80,0x40,0x40,0x80,0x40,0x00,0x00,
  289. "日",0x3F,0xC0,0x20,0x40,0x20,0x40,0x20,0x40,0x3F,0xC0,0x20,0x40,
  290.     0x20,0x40,0x20,0x40,0x20,0x40,0x3F,0xC0,0x20,0x40,0x00,0x00,
  291. "当",0x04,0x00,0x44,0x40,0x24,0x80,0x05,0x00,0xFF,0xC0,0x00,0x40,
  292.     0x00,0x40,0x7F,0xC0,0x00,0x40,0x00,0x40,0xFF,0xC0,0x00,0x00,
  293. "前",0x11,0x00,0x0A,0x40,0xFF,0xE0,0x00,0x00,0x79,0x40,0x49,0x40,
  294.     0x79,0x40,0x49,0x40,0x79,0x40,0x48,0x40,0x59,0xC0,0x00,0x00,
  295. "温",0x8F,0xC0,0x48,0x40,0x0F,0xC0,0x88,0x40,0x4F,0xC0,0x40,0x00,
  296.     0x5F,0xC0,0x95,0x40,0x95,0x40,0x95,0x40,0xBF,0xE0,0x00,0x00,
  297. "度",0x02,0x00,0x7F,0xE0,0x48,0x80,0x7F,0xE0,0x48,0x80,0x4F,0x80,
  298.     0x40,0x00,0x5F,0x80,0x45,0x00,0x87,0x00,0xB8,0xE0,0x00,0x00,
  299. ":",0x00,0x00,0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,
  300.     0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  301. "℃",0x00,0x00,0xE7,0x40,0xA8,0xC0,0xF0,0x40,0x10,0x00,0x10,0x00,
  302.     0x10,0x00,0x10,0x40,0x08,0x40,0x07,0x80,0x00,0x00,0x00,0x00,
  303. };
  304. ucharStatus_BIT_01(void) // 状态位STA1,STA0判断(读写指令和读写数据)
  305. {
  306.     uchar i;
  307.    
  308.     for(i=5;i>0;i--)
  309.     {
  310.         if((LCMCW & 0x03) == 0x03)
  311.         break;
  312.     }
  313.     return i; // 若返回零,说明错误
  314. }


  315. ucharStatus_BIT_3(void) // 状态位ST3判断(数据自动写状态)
  316. {
  317.     uchar i;
  318.    
  319.     for(i=5;i>0;i--)
  320.     {
  321.         if((LCMCW & 0x08) == 0x08)
  322.         break;
  323.     }
  324.     return i; // 若返回零,说明错误
  325. }

  326. ucharLCD_Write_Command_P2(uchar cmd,uchar para1,uchar para2) // 写双参数的指令
  327. {
  328.     if(Status_BIT_01() == 0) return 1;
  329.     LCMDW = para1;
  330.     if(Status_BIT_01() == 0) return 2;
  331.     LCMDW = para2;
  332.     if(Status_BIT_01() == 0) return 3;
  333.     LCMCW = cmd;
  334.     return 0; // 返回0成功
  335. }

  336. ucharLCD_Write_Command_P1(uchar cmd,uchar para1) // 写单参数的指令
  337. {
  338.     if(Status_BIT_01() == 0) return 1;
  339.     LCMDW = para1;
  340.     if(Status_BIT_01() == 0) return 2;
  341.     LCMCW = cmd;
  342.     return 0; // 返回0成功
  343. }

  344. ucharLCD_Write_Command(uchar cmd) // 写无参数的指令
  345. {
  346.     if(Status_BIT_01() == 0)
  347.     return 1;
  348.     LCMCW = cmd;
  349.     return 0; // 返回0成功
  350. }

  351. ucharLCD_Write_Data(uchar dat) // 写数据
  352. {
  353.     if(Status_BIT_3() == 0)
  354.     return 1;
  355.     LCMDW = dat;
  356.     return 0; // 返回0成功
  357. }

  358. ucharLCD_Read_Data() // 读数据
  359. {
  360.     if(Status_BIT_01() == 0)
  361.     return 1;
  362.     return LCMDW;
  363. }

  364. voidSet_LCD_Pos(uchar row, uchar col)  reentrant//设置当前地址
  365. {
  366.     uint Pos;
  367.    
  368.     Pos = row * LCD_WIDTH+ col;
  369.     LCD_Write_Command_P2(LC_ADD_POS,Pos %256,Pos / 256); //修改
  370.     gCurRow = row;
  371.     gCurCol = col;
  372. }

  373. // 清屏
  374. void cls(void)
  375. {
  376.     uint i;
  377.    
  378.     LCD_Write_Command_P2(LC_ADD_POS,0x00,0x00);// 置地址指针
  379.     LCD_Write_Command(LC_AUT_WR); // 自动写
  380.     for(i=0;i<0x2000;i++)
  381.     {
  382.         Status_BIT_3();
  383.         LCD_Write_Data(0x00); // 写数据
  384.     }
  385.     LCD_Write_Command(LC_AUT_OVR); // 自动写结束
  386.     LCD_Write_Command_P2(LC_ADD_POS,0x00,0x00);// 重置地址指针
  387.     gCurRow = 0; // 置地址指针存储变量
  388.     gCurCol = 0;
  389. }

  390. // LCM 初始化
  391. ucharLCD_Initialise()
  392. {
  393.     RESET=0;
  394.     RESET=1;
  395.     LCD_Write_Command_P2(LC_TXT_STP,0x00,0x00);// 文本显示区首地址
  396.     LCD_Write_Command_P2(LC_TXT_WID,LCD_WIDTH,0x00);// 文本显示区宽度
  397.     LCD_Write_Command_P2(LC_GRH_STP,0x00,0x00);// 图形显示区首地址
  398.     LCD_Write_Command_P2(LC_GRH_WID,LCD_WIDTH,0x00);// 图形显示区宽度
  399.     LCD_Write_Command_P1(LC_CGR_POS,CGRAMSTART>> 11);
  400.     LCD_Write_Command(LC_CUR_SHP | 0x01); // 光标形状
  401.     LCD_Write_Command(LC_MOD_OR); // 显示方式设置
  402.     LCD_Write_Command(LC_DIS_SW |0x08);
  403.     grhome = GRSTART;
  404.     txthome = TXTSTART;

  405.     return 0;
  406. }

  407. // ASCII及汉字显示函数

  408. ucharDisplay_Str_at_xy(uchar x,uchar y,char *fmt) reentrant
  409. {
  410.     char c1,c2,cData;
  411.     uchar i=0,j,uLen;
  412.     uchar k;
  413.     uLen=strlen(fmt);

  414.     while(i<uLen)
  415.     {
  416.         c1 = fmt;
  417.         c2 = fmt[i+1];
  418.         
  419.         Set_LCD_Pos(y,x/8);
  420.         if(c1 >= 0)
  421.         {
  422.          // ASCII
  423.            if(c1 < 0x20)
  424.            {
  425.                switch(c1)
  426.                {
  427.                     case CR:
  428.                     case LF: // 回车或换行
  429.                         i++;
  430.                        x=0;
  431.                         if(y < 112)y+=HZ_CHR_HEIGHT;
  432.                         continue;
  433.                     case BS: // 退格
  434.                        i++;
  435.                        if(y>ASC_CHR_WIDTH)y-=ASC_CHR_WIDTH;
  436.                         cData = 0x00;
  437.                         break;
  438.                }
  439.            }
  440.            for(j=0;j<ASC_CHR_HEIGHT;j++)
  441.            {
  442.                
  443.                if(c1 >= 0x1f)
  444.                {
  445.                     cData=ASC_MSK[(c1-0x1f)*ASC_CHR_HEIGHT+j];
  446.                    Set_LCD_Pos(y+j,x/8);
  447.                    if((x%8)==0)
  448.                    {
  449.                    LCD_Write_Command(LC_AUT_WR);// 写数据
  450.                         LCD_Write_Data(cData);  
  451.                         LCD_Write_Command(LC_AUT_OVR);
  452.                    }
  453.                    else
  454.                        OutToLCD(cData,x,y+j);
  455.                }
  456.                Set_LCD_Pos(y+j,x/8);
  457.            }
  458.            if(c1 != BS) // 非退格
  459.            x+=ASC_CHR_WIDTH;
  460.         }
  461.     else
  462.         { // 中文
  463.             
  464.            for(j=0;j<sizeof(GB_16)/sizeof(GB_16[0]);j++)
  465.            {
  466.                if(c1 == GB_16[j].Index[0] && c2 == GB_16[j].Index[1])
  467.                break;
  468.            }
  469.            for(k=0;k<HZ_CHR_HEIGHT;k++)
  470.            {
  471.                
  472.                Set_LCD_Pos(y+k,x/8);
  473.                if(j < sizeof(GB_16)/sizeof(GB_16[0]))
  474.                {
  475.                    c1=GB_16[j].Msk[k*2];
  476.                    c2=GB_16[j].Msk[k*2+1];
  477.                 }
  478.                 else      
  479.                    c1=c2=0;
  480.                 if((x%8)==0)
  481.                 {
  482.                    LCD_Write_Command(LC_AUT_WR);
  483.                    LCD_Write_Data(c1);
  484.                     LCD_Write_Command(LC_AUT_OVR);
  485.                }
  486.                 else
  487.                    OutToLCD(c1,x,y+k);
  488.                
  489.                 if(((x+2+HZ_CHR_WIDTH/2)%8)==0)
  490.                 {
  491.                    LCD_Write_Command(LC_AUT_WR);
  492.                    LCD_Write_Data(c2);
  493.                    LCD_Write_Command(LC_AUT_OVR);
  494.                }
  495.                 else
  496.                    OutToLCD(c2,x+2+HZ_CHR_WIDTH/2,y+k);
  497.            }
  498.            x+=HZ_CHR_WIDTH;
  499.            i++;
  500.         }
  501.         i++;
  502.     }
  503.     return uLen;
  504. }
  505. //输出起点X不是8的倍数时,原字节分成两部分输出到LCD
  506. void OutToLCD(ucharDat,uchar x,uchar y)
  507. {
  508.     uchar dat1,dat2,a,b;

  509.     b=x%8;a=8-b;
  510.     Set_LCD_Pos(y,x/8);
  511.     LCD_Write_Command(LC_AUT_RD); // 读数据
  512.     dat1=LCD_Read_Data();
  513.     dat2=LCD_Read_Data();
  514.     dat1=(dat1&(0xFF<<a))|(Dat>>b);
  515.     dat2=(dat2&(0xFF>>b))|(Dat<<a);
  516.     LCD_Write_Command(LC_AUT_OVR);
  517.     Set_LCD_POS(y,x/8);
  518.     LCD_Write_Command(LC_AUT_WR);
  519.     LCD_Write_Data(dat1);
  520.     LCD_Write_Data(dat2);
  521.     LCD_Write_Command(LC_AUT_OVR);
  522. }
  523. /**********************************
  524. //= 函数原型:Pixel(unsigned char PointX,unsigned char PointY, bit Mode)
  525. //= 功    能: 在指定坐标位置显示一个点
  526. //= 参    数: 坐标,显示点或清除点
  527. //= 返 回 值:
  528. //= 函数性质:私有函数
  529. //= 如果显示屏超过了256*256,请修改这个函数 PointX,PointY的类型
  530. //= Mode 1:显示 0:清除该点
  531. **********************************/
  532. void Pixel(unsignedchar x,unsigned char y, uchar Mode)
  533. {
  534.     unsigned char Start_Addr,dat;
  535.     Start_Addr=7-(x%8);
  536.     dat=LC_BIT_OP|Start_Addr; //生产位操作命令画点的数据
  537.     if(Mode) dat|=0x08;
  538.     Set_LCD_Pos(y,x/8);
  539.     LCD_Write_Command(LC_BIT_OP|dat); // 写数据
  540. }

  541. void Exchange(uchar*a,uchar *b)
  542. {
  543.     uchar t;
  544.     t = *a;*a = *b;*b =t;
  545. }  
  546. /**********************************
  547. //= 函数原型: voidline( unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2,bit Mode)
  548. //= 功    能: 划线函数
  549. //= 参    数: 坐标1,坐标2,显示点或清除点
  550. //= 返 回 值:
  551. //= 函数性质:私有函数
  552. //= 其它:显示点阵不超过255*255
  553. /**********************************/


  554. void Line( unsignedchar x1,unsigned char y1, unsigned char x2,unsigned char y2,  uchar Mode) reentrant
  555. {
  556.     unsigned char x,y;
  557.     float k,b;
  558.     if( abs(y1-y2) <= abs(x1-x2) ) //|k|<=1
  559.     {
  560.         k=(float)(y2-y1) / (float)(x2-x1) ;
  561.         b=y1-k*x1;
  562.         if( x1 > x2 )
  563.         Exchange(&x1,&x2);
  564.         for(x=x1;x<=x2;x++)
  565.         {
  566.             y=(uchar)(k*x+b);
  567.             Pixel(x, y, Mode);
  568.         }
  569.     }
  570.      else
  571.     {
  572.         k = (float)(x2-x1) / (float)(y2-y1);
  573.         b = x1 - k*y1;
  574.         if(y1 > y2)
  575.         Exchange(&y1,&y2);
  576.         for(y=y1;y<=y2;y++)
  577.         {
  578.             x=(uchar)(k*y+b);
  579.             Pixel(x, y, Mode);
  580.         }
  581.     }
  582. }  

  583. voidDraw_Image(uchar *G_Buffer,uchar Start_Row,uchar Start_Col) reentrant
  584. {
  585.     uchar i,j;
  586.     for(i=0;i < G_Buffer[1];i++)
  587.     {
  588.         Set_LCD_POS(Start_Row + i,Start_Col);
  589.         LCD_Write_Command(LC_AUT_WR);
  590.         for( j = 0;j< G_Buffer[0] /8;j++)
  591.             LCD_Write_Data(G_Buffer [ i *(G_Buffer[0] / 8) +j +2]);
  592.             LCD_Write_Command(LC_AUT_OVR);
  593.     }
  594. }
  595. //---------------LCD_160128.h----------------
  596. //LCD_160128的头文件
  597. //------------------------------------------
  598. #include<at89x52.h>
  599. #include<intrins.h>
  600. #include<stdarg.h>
  601. #include<stdio.h>
  602. #include<math.h>
  603. #include<absacc.h>
  604. #include<string.h>
  605. #define uintunsigned int
  606. #define ucharunsigned char
  607. #define STX 0x02
  608. #define ETX 0x03
  609. #define EOT 0x04
  610. #define ENQ 0x05
  611. #define BS  0x08
  612. #define CR  0x0d
  613. #define LF  0x0a
  614. #define DLE 0x10
  615. #define ETB 0x17
  616. #define SPACE 0x20
  617. #define COMMA 0x2c
  618. #define TRUE 1
  619. #define FALSE 0
  620. #define HIGH 1
  621. #define LOW 0
  622. #define LCMDWXBYTE[0x8000]
  623. #define LCMCWXBYTE[0x8100]
  624. #define DISRAM_SIZE0x7fff
  625. #define TXTSTART0x0000
  626. #define GRSTART0x6800
  627. #define CGRAMSTART0x7800
  628. #define LC_CUR_POS0x21
  629. #define LC_CGR_POS0x22
  630. #define LC_ADD_POS0x24
  631. #define LC_TXT_STP0x40
  632. #define LC_TXT_WID0x41
  633. #define LC_GRH_STP0x42
  634. #define LC_GRH_WID0x43
  635. #defineLC_MOD_OR  0x80
  636. #define LC_MOD_XOR0x81
  637. #define LC_MOD_AND0x82
  638. #define LC_MOD_TCH0x83
  639. #defineLC_DIS_SW  0x90
  640. #define LC_CUR_SHP0xa0
  641. #defineLC_AUT_WR  0xb0
  642. #defineLC_AUT_RD  0xb1
  643. #define LC_AUT_OVR0xb2
  644. #defineLC_INC_WR  0xc0
  645. #defineLC_INC_RD  0xc1
  646. #defineLC_DEC_WR  0xc2
  647. #define LC_DEC_RD  0xc3
  648. #defineLC_NOC_WR  0xc4
  649. #defineLC_NOC_RD  0xc5
  650. #defineLC_SCN_RD  0xe0
  651. #defineLC_INC_CP  0xe8
  652. #defineLC_BIT_OP  0xf0
  653. //---------------DS18B20.h----------------
  654. //温度转换
  655. //------------------------------------------
  656. #include<at89x52.h>
  657. #include <stdio.h>
  658. #define  DQ P3_5     //定义DS18B20总线I/O   
  659. unsigned char t;      //从DS18B20读取的温度值
  660. unsigned int flag;
  661. unsigned char a=0;
  662. unsigned char b=0;
  663. /*****延时子程序*****/
  664. void Delay(int num)
  665. {
  666.   while(num--) ;
  667. }
  668. /*****初始化DS18B20*****/
  669. voidInit_DS18B20(void)
  670. {
  671.   unsigned char x=0;
  672.   DQ = 1;         //DQ复位
  673.   Delay(8);   //稍做延时
  674.   DQ = 0;         //单片机将DQ拉低
  675.   Delay(80);  //精确延时,大于480us
  676.   DQ = 1;         //拉高总线
  677.   Delay(14);
  678.   x = DQ;           //稍做延时后,如果x=0则初始化成功,x=1则初始化失败
  679.   Delay(20);
  680. }
  681. /*****读一个字节*****/
  682. unsigned char ReadOneChar(void)
  683. {
  684.   unsigned char i=0;
  685.   unsigned char dat = 0;
  686.   for (i=8;i>0;i--)
  687.   {
  688.     DQ = 0;    // 给脉冲信号
  689.     dat>>=1;
  690.     DQ = 1;    // 给脉冲信号
  691.     if(DQ)
  692.     dat|=0x80;
  693.     Delay(4);
  694.   }
  695.   return(dat);
  696. }
  697. /*****写一个字节*****/
  698. voidWriteOneChar(unsigned char dat)
  699. {
  700.   unsigned char i=0;
  701.   for (i=8; i>0; i--)
  702.   {
  703.     DQ = 0;
  704.     DQ = dat&0x01;
  705.     Delay(5);
  706.     DQ = 1;
  707.     dat>>=1;
  708.   }
  709. }
  710. /*****读取温度*****/
  711. unsigned intRead_Temperature(void)
  712. {
  713.   Init_DS18B20();
  714.   WriteOneChar(0xCC);  //跳过读序号列号的操作
  715.   WriteOneChar(0x44);  //启动温度转换
  716.   Init_DS18B20();
  717.   WriteOneChar(0xCC);  //跳过读序号列号的操作
  718.   WriteOneChar(0xBE);  //读取温度寄存器
  719.   a=ReadOneChar();     //读低8位
  720.   b=ReadOneChar();    //读高8位

  721.   if(b>127)// 当高8位的第8位为1时,就是负值         
  722.      {                 // 下面是进行转换                  
  723.       a=(~a)+1; // 当为负值的时候,低8位的操作
  724.         if((~a) >= 0xff)// <-127度 ,则高8位进位     
  725.         {
  726.             b=(~b)+1;
  727.         }
  728.        else
  729.        {//不小于-127度时,高8位取反
  730.             b=~b;
  731.                     }
  732.       flag=1;//这个表示为负值,你只要在液晶上显示一个“-”就行。
  733.    }
  734.     else
  735.     { flag=0;}
  736.    t=((b*256)+a)*0.0625;      
  737.   return(t);
  738. }
  739. /*****END*****/
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表