找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机+LCD12864屏画点程序出现这种乱码是啥情况?

[复制链接]
跳转到指定楼层
楼主
  1. #include "lcd12864.h"
  2. /**
  3. * @name:
  4. * @description:
  5. * @author: 何影萌
  6. * @version:
  7. * @return {*}
  8. * @param {int} ms
  9. */
  10. int delay(int ms)
  11. {
  12.   int lanzixuan;
  13.     while(ms--)
  14.   {
  15.       uint8_t i;
  16.     for(i=0;i<250;i++)  
  17.      {
  18.       _nop_();         
  19.     _nop_();
  20.     _nop_();
  21.     _nop_();
  22.      }
  23.   }
  24.   return lanzixuan;
  25. }   
  26. /*******************************************************************/
  27. /*                                                                 */
  28. /*检查LCD忙状态                                                    */
  29. /*lcd_busy为1时,忙,等待。lcd-busy为0时,闲,可写指令与数据。      */
  30. /*                                                                 */
  31. /*******************************************************************/
  32. bit lcd_busy()
  33. {                          
  34.     bit result;
  35.     LCD_RS = 0;
  36.     LCD_RW = 1;
  37.     LCD_EN = 1;
  38.     delayNOP();
  39.     result = (bit)(P0&0x80);
  40.     LCD_EN = 0;
  41.     return(result);
  42. }
  43. /*******************************************************************/
  44. /*                                                                 */
  45. /*写指令数据到LCD                                                  */
  46. /*RS=L,RW=L,E=高脉冲,D0-D7=指令码。                             */
  47. /*                                                                 */
  48. /*******************************************************************/
  49. void lcd_wcmd(uint8_t cmd)
  50. {                          
  51.    while(lcd_busy());
  52.     LCD_RS = 0;
  53.     LCD_RW = 0;
  54.     LCD_EN = 0;
  55.     _nop_();
  56.     _nop_();
  57.     P0 = cmd;
  58.     delayNOP();
  59.     LCD_EN = 1;
  60.     delayNOP();
  61.     LCD_EN = 0;  
  62. }
  63. /*******************************************************************/
  64. /*                                                                 */
  65. /*写显示数据到LCD                                                  */
  66. /*RS=H,RW=L,E=高脉冲,D0-D7=数据。                               */
  67. /*                                                                 */
  68. /*******************************************************************/
  69. void lcd_wdat(uint8_t dat)
  70. {                          
  71.    while(lcd_busy());
  72.     LCD_RS = 1;
  73.     LCD_RW = 0;
  74.     LCD_EN = 0;
  75.     P0 = dat;
  76.     delayNOP();
  77.     LCD_EN = 1;
  78.     delayNOP();
  79.     LCD_EN = 0;
  80. }
  81. /*******************************************************************/
  82. /*                                                                 */
  83. /*写字符串到LCD                                                  */
  84. /*RS=H,RW=L,E=高脉冲,D0-D7=数据。                               */
  85. /*                                                                 */
  86. /*******************************************************************/
  87. void lcd_wstr(uint8_t *Str)
  88. {
  89.   while(*Str)
  90.   {
  91.     lcd_wdat(*Str++);
  92.   }
  93. }

  94. /*******************************************
  95. 函数名称:Draw_PM
  96. 功    能:在整个液晶屏幕上画图
  97. 参    数:无
  98. 返回值  :无
  99. ********************************************/
  100. void Draw_PM(const uint8_t *ptr)
  101. {
  102.   uint8_t i,j,k;
  103.   lcd_wcmd(0x34);        //打开扩展指令集
  104.   i = 0x80;            
  105.   for(j = 0;j < 32;j++)
  106.   {
  107.         lcd_wcmd(i++);
  108.         lcd_wcmd(0x80);
  109.       for(k = 0;k < 16;k++)
  110.       {
  111.           lcd_wdat(*ptr++);
  112.       }
  113.   }
  114.   i = 0x80;
  115.   for(j = 0;j < 32;j++)
  116.   {
  117.       lcd_wcmd(i++);
  118.         lcd_wcmd(0x88);   
  119.       for(k = 0;k < 16;k++)
  120.       {
  121.           lcd_wdat(*ptr++);
  122.       }
  123.   }  
  124.     lcd_wcmd(0x36);        //打开绘图显示
  125.     lcd_wcmd(0x30);        //回到基本指令集
  126. }

  127. /*******************************************************************/
  128. /*                                                                 */
  129. /*  LCD初始化设定                                                  */
  130. /*                                                                 */
  131. /*******************************************************************/
  132. void lcd_init()
  133. {
  134.     delay(50);
  135.     wela=0;
  136.     dula=0;
  137.     LCD_PSB = 1;         //并口方式

  138.     lcd_wcmd(0x34);      //扩充指令操作
  139.     delay(5);
  140.     lcd_wcmd(0x30);      //基本指令操作
  141.     delay(5);
  142.     lcd_wcmd(0x0C);      //显示开,关光标
  143.     delay(5);
  144.     lcd_wcmd(0x01);      //清除LCD的显示内容
  145.     delay(5);
  146. }

  147. /*********************************************************/
  148. /*                                                       */
  149. /* 设定显示位置                                          */
  150. /*                                                       */
  151. /*********************************************************/

  152. void lcd_pos(uint8_t X,uint8_t Y)
  153. {                          
  154.    uint8_t  pos;
  155.    if (X==0)
  156.      {X=0x80;}
  157.    else if (X==1)
  158.      {X=0x90;}
  159.    else if (X==2)
  160.      {X=0x88;}
  161.    else if (X==3)
  162.      {X=0x98;}
  163.    pos = X+Y ;  
  164.    lcd_wcmd(pos);     //显示地址
  165. }
  166. /**
  167. * @name:
  168. * @description: 以下为蓝灵电子新增加的功能
  169. * @author: 何影萌
  170. * @version:
  171. * @return {*}
  172. */
  173. /**
  174. * @brief 读取数据
  175. * @return uint8_t
  176. */

  177. uint8_t Lanling_Read_Dat()
  178. {
  179.   uint8_t datas;
  180.   while(lcd_busy());
  181.   LCD_RS=1;
  182.   LCD_RW=1;
  183.   LCD_EN=1;
  184.   delayNOP();
  185.   datas=P0;
  186.   delayNOP();
  187.   LCD_EN=0;
  188.   return datas;
  189. }
  190. /**
  191. * @brief 画点
  192. * @param x
  193. * @param y
  194. */
  195. void Lanling_Draw_Point(uint8_t x,uint8_t y)
  196. {
  197.   uint8_t temph,templ;
  198.   uint8_t x_byte,x_bit;
  199.   uint8_t y_byte,y_bit;
  200.   lcd_wcmd(0x34);
  201.   lcd_wcmd(0x36);
  202.   x_byte=x>>4;
  203.   x_bit=x&0x0f;
  204.   y_byte=y>>5;
  205.   y_bit=y%32;
  206.   lcd_wcmd(0x80+y_bit);
  207.   lcd_wcmd(0x80+x_byte+y_byte*8);
  208.   temph=Lanling_Read_Dat();
  209.   temph=Lanling_Read_Dat();
  210.   templ=Lanling_Read_Dat();
  211.   lcd_wcmd(0x80+y_bit);
  212.   lcd_wcmd(0x80+x_byte+y_byte*8);
  213.   if(x_bit<8)
  214.   {
  215.     lcd_wdat(temph|(0x01<<(7-x_bit)));
  216.     lcd_wdat(templ);
  217.   }
  218.   else
  219.   {
  220.     lcd_wdat(temph);
  221.     lcd_wdat(templ|(0x01<<(15-x_bit)));   
  222.   }  

  223. }
  224. /**
  225. * @brief 清除点
  226. * @param x
  227. * @param y
  228. */
  229. void Lanling_Clear_Point(uint8_t x,uint8_t y)
  230. {
  231.   uint8_t temph,templ;
  232.   uint8_t x_byte,x_bit;
  233.   uint8_t y_byte,y_bit;
  234.   lcd_wcmd(0x34);
  235.   lcd_wcmd(0x36);
  236.   x_byte=x>>4;
  237.   x_bit=x&0x0f;
  238.   y_byte=y/32;
  239.   y_bit=y%32;
  240.   lcd_wcmd(0x80+y_bit);
  241.   lcd_wcmd(0x80+x_byte+y_byte*8);
  242.   temph=Lanling_Read_Dat();
  243.   temph=Lanling_Read_Dat();
  244.   templ=Lanling_Read_Dat();
  245.   lcd_wcmd(0x80+y_bit);
  246.   lcd_wcmd(0x80+x_byte+y_byte*8);
  247.   if(x_bit<8)
  248.   {
  249.     lcd_wdat(temph&(~(0x01<<(7-x_bit))));
  250.     lcd_wdat(templ);
  251.   }
  252.   else
  253.   {
  254.     lcd_wdat(temph);
  255.     lcd_wdat(templ&(~(0x01<<(15-x_bit))));   
  256.   }  
  257. }
  258. /**
  259. * @brief 蓝灵电子专属清屏函数
  260. */
  261. void Lanling_Clr()
  262. {
  263.   uint8_t i,j,k;
  264.   lcd_wcmd(0x34);
  265.   lcd_wcmd(0x36);
  266.   for(i=0;i<2;i++)
  267.   {
  268.     for(j=0;j<32;j++)
  269.     {
  270.       lcd_wcmd(0x80+j);
  271.       lcd_wcmd(0x80+i*8);
  272.       for(k=0;k<16;k++)
  273.       {
  274.         lcd_wdat(0x00);
  275.       }
  276.     }
  277.   }
  278.   lcd_wcmd(0x30);      //基本指令操作
  279. }


  280. /*Main.c*/
  281. void main()
  282. {
  283.     unsigned char i;
  284.     lcd_init();  
  285.     Lanling_Clr();
  286.     for(i=0;i<64;i++)
  287.     {
  288.         Lanling_Draw_Point(i,i);
  289.     }
  290.     for(i=64;i<128;i++)
  291.     {
  292.         Lanling_Draw_Point(i,127-i);
  293.     }
  294.     while(1);
  295. }
复制代码


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

使用道具 举报

沙发
ID:584814 发表于 2021-4-21 10:29 | 只看该作者
1、程序有问题
2、液晶屏坏了
没有第3
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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