找回密码
 立即注册

QQ登录

只需一步,快速开始

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

ST7565 128x64 LCD驱动程序

[复制链接]
跳转到指定楼层
楼主
ID:374824 发表于 2023-9-2 22:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
普通128x64点阵LCD点阵显示,采用ST7565
Init_LCD_ST7565();
LCD_CLS_ST7565(0);
LCD_disp_printR_ST7565(Test",0,0,4);
LCD_disp_printR_ST7565(“2023/07/03",0,2,10);

LCD_Driver_7565.c
  1. // LCD_128X64
  2. // Driver: st7565/st7567
  3. // Mode: 4 line SPI
  4. // WR,RD : GND
  5. // leo_20160120

  6. #define LCD_DRIVER_7565_C

  7. #include "LCD_DRIVER_7565.h"
  8. #include "L_ASCII_8x8.h"
  9. #include "L_HZ_16x16.h"
  10. // #include "L_BMP.h"


  11. xdata u8 Dis_u16_Buf[5];
  12. xdata u8 Dis_u8_Buf[3];
  13. xdata u8 Dis_Sensor_Buf[7];


  14. //当前字符显示的位置
  15. //unsigned char  Lcd_Charcter_CurrentX,Lcd_Charcter_CurrentY;
  16. //当前像素显示位置
  17. unsigned char  Lcd_CurrentX,Lcd_CurrentY;
  18. //图像反色显示 0 否 1是
  19. unsigned char LCD_DisplayReserve_Driver;

  20. //--------------------------------------------------------------------------
  21. //串口移位输出
  22. //--------------------------------------------------------------------------
  23. void SPI_Write_ST7565(char datain)
  24. {
  25. unsigned char i;
  26. unsigned char Series,Temp;
  27. Series = datain;

  28. for(i=8;i>0;i--)
  29.         {
  30.         LCD_SCK_L();

  31.         Temp=Series & 0x80;
  32.         if(Temp)
  33.                 {
  34.                 LCD_SDA_H();
  35.                 }
  36.         else
  37.                 {
  38.                 LCD_SDA_L();
  39.                 }
  40.         LCD_SCK_H();
  41.         Series = Series << 1;
  42.         }
  43. }

  44. /***********************************
  45. ** 函数名称: Write_Data
  46. ** 功能描述: 传送数据
  47. ** 输 入: dat
  48. ** 输 出 : 无
  49. ** 全局变量:无
  50. ** 调用模块: Busy,
  51. ******************************************/

  52. void Write_Data_ST7565(unsigned char dat)
  53. {
  54. LCD_CS_L();
  55. LCD_DC_H();                //A0=1,数据
  56. SPI_Write_ST7565(dat);
  57. LCD_CS_H();
  58. return;
  59. }

  60. /***********************************
  61. ** 函数名称: Write_Instruction
  62. ** 功能描述: 传送命令
  63. ** 输 入: dat
  64. ** 输 出 : 无
  65. ** 全局变量:无
  66. ** 调用模块: Busy,
  67. ******************************************/
  68. void Write_Instruction_ST7565(unsigned char cmd)
  69. {
  70. LCD_CS_L();
  71. LCD_DC_L();                //A0=0,命令
  72. SPI_Write_ST7565(cmd);
  73. LCD_CS_H();
  74. return;
  75. }

  76. //==============================================================================高一级函数
  77. //设置像素显示坐标(x:0-127,y:0-7)
  78. void LCD_setpos_ST7565(unsigned char Lx,unsigned char Ly)
  79. {
  80. Write_Instruction_ST7565(0xB0|Ly);// Page(Row)
  81. Write_Instruction_ST7565((0x10|(Lx>>4)));
  82. Write_Instruction_ST7565((0x0f&Lx));
  83. Lcd_CurrentX=Lx;
  84. Lcd_CurrentY=Ly;

  85. }

  86. // ************************************************************************************************************
  87. //设置字符位置(x:0-8,y:0-3)
  88. void LCD_setCharpos_ST7565(unsigned char Lx,unsigned char Ly)
  89. {
  90. //当前像素显示位置
  91. /*       
  92. // 8*8       
  93. Lcd_CurrentX=Lx*16;
  94. Lcd_CurrentY=Ly*2;
  95. */       
  96. // 4*4               
  97. Lcd_CurrentX=Lx*8;
  98. Lcd_CurrentY=Ly*1;
  99.        
  100. LCD_setpos_ST7565(Lcd_CurrentX,Lcd_CurrentY);
  101. }

  102. // ************************************************************************************************************
  103. //清屏
  104. void LCD_CLS_ST7565(char value)  
  105. {  
  106. unsigned char i,n;  

  107. for(i=0;i<9;i++)  
  108.         {  
  109.         LCD_setpos_ST7565(0,i);           
  110.         for(n=0;n<128;n++)  
  111.                 {  
  112.                 Write_Data_ST7565(value);  
  113.                 }
  114.         }
  115. }

  116. // ************************************************************************************************************
  117. //显示BMP图片
  118. void LCD_DisplayBMP_ST7565(const unsigned char *PicData)  //信息显示
  119. {
  120. unsigned char BMPwithLen,BMPheightLen;
  121. unsigned char BMPwith;
  122. unsigned char BMPheight;
  123. BMPwith=*PicData;
  124. PicData++;
  125. BMPheight=(*PicData)/8;
  126. PicData++;

  127. //BMPLen=BMPheight/8*BMPwith

  128. for(BMPheightLen=0;BMPheightLen<BMPheight;BMPheightLen++)
  129. {
  130. //   Lcd_CurrentY++;
  131.    LCD_setpos_ST7565(Lcd_CurrentX,Lcd_CurrentY);
  132.     for(BMPwithLen=0;BMPwithLen<BMPwith;BMPwithLen++)
  133.     {
  134.         //图像反色显示 0 否 1是
  135.         if (LCD_DisplayReserve_Driver==0)
  136.         {
  137.         Write_Data_ST7565(*PicData);
  138.         }
  139.         else
  140.         {
  141.         Write_Data_ST7565(~(*PicData));
  142.         }
  143.         PicData++;
  144.     }
  145.     Lcd_CurrentY++;
  146. }

  147. }

  148. // ************************************************************************************************************
  149. void LCD_disp_DisplayImage_ST7565(const unsigned char * PicData,unsigned char PicLen)  //信息显示
  150. {

  151. for(;PicLen>0;PicLen--)
  152. {

  153. //图像反色显示 0 否 1是
  154. if (LCD_DisplayReserve_Driver==0)
  155. {
  156. Write_Data_ST7565(*PicData);
  157. }
  158. else
  159. {
  160. Write_Data_ST7565(~(*PicData));
  161. }


  162. PicData++;
  163. };

  164. }



  165. // ************************************************************************************************************
  166. //显示一个Unicode
  167. void LCD_disp_Putchar_ST7565(unsigned int uChar)
  168. {  
  169. unsigned int i;
  170. const unsigned char *p;
  171.    if(uChar<128)
  172.    {
  173.       //for(i=0;i != ENGLISHCHARNUMBER;i++)
  174.       //{  
  175.       //if(uChar==EnglishCode[i][0])
  176.          //{
  177.             p=(uChar-0x20)*(ENGLISHCHARLegth)+&nAsciiDot[0];
  178.             LCD_disp_DisplayImage_ST7565(p, ENGLISHCHARLegth/2);
  179.             Lcd_CurrentY++;
  180.             //设置像素显示坐标(y:0-7)
  181.             LCD_setpos_ST7565(Lcd_CurrentX,Lcd_CurrentY);
  182.             LCD_disp_DisplayImage_ST7565(p+(ENGLISHCHARLegth/2),(ENGLISHCHARLegth/2));
  183.             Lcd_CurrentY--;
  184.             Lcd_CurrentX+=8;
  185.             //设置像素显示坐标(y:0-7)
  186.                 LCD_setpos_ST7565(Lcd_CurrentX,Lcd_CurrentY);
  187.             //break;
  188.          //}
  189.       //}
  190.     }
  191.         else
  192.         {
  193.       for(i=0;i!=GB_ZK_NUM;i++)
  194.       {  
  195.       if(uChar==(GB_16[i].Index[0]*256+GB_16[i].Index[1]))
  196.          {  
  197.             //分别在两页显示
  198.             LCD_disp_DisplayImage_ST7565(GB_16[i].Msk,(CHINESECHARlegth/2));
  199.             Lcd_CurrentY++;
  200.             //设置像素显示坐标(y:0-7)
  201.             LCD_setpos_ST7565(Lcd_CurrentX,Lcd_CurrentY);
  202.             LCD_disp_DisplayImage_ST7565(GB_16[i].Msk+(CHINESECHARlegth/2),(CHINESECHARlegth/2));
  203.             Lcd_CurrentY--;
  204.             Lcd_CurrentX+=16;
  205.             //设置像素显示坐标(y:0-7)
  206.             LCD_setpos_ST7565(Lcd_CurrentX,Lcd_CurrentY);
  207.             break;
  208.          }
  209.       }
  210.       }
  211. }

  212. // ************************************************************************************************************
  213. //图像反色显示 0 否 1是,执行此命令后的所有操作均是按照设置显示
  214. void LCD_disp_SetReverse_ST7565(unsigned char ReverseTrue)
  215. {
  216. //图像反色显示 0 否 1是
  217. if  (ReverseTrue==0)
  218. {
  219. LCD_DisplayReserve_Driver=0;
  220. }
  221. else
  222. {
  223. LCD_DisplayReserve_Driver=1;
  224. }
  225. }

  226. // ************************************************************************************************************
  227. //对比度设置
  228. void Set_Contrast_Control_ST7565(unsigned char Level)
  229. {
  230. unsigned char Num,Temp1,Temp2;
  231. Temp1 = (Level/16)<<4;
  232. switch(Level%16)
  233. {
  234. case 10:
  235. Temp2 = 0x0a;
  236. break;
  237. case 11:
  238. Temp2 = 0x0b;
  239. break;
  240. case 12:
  241. Temp2 = 0x0c;
  242. break;
  243. case 13:
  244. Temp2 = 0x0d;
  245. break;
  246. case 14:
  247. Temp2 = 0x0e;
  248. break;
  249. case 15:
  250. Temp2 = 0x0f;
  251. break;
  252. default:
  253. Temp2 = Level%16;
  254. break;
  255. }
  256. Num = Temp1|Temp2;
  257. Write_Instruction_ST7565(0x81);
  258. Write_Instruction_ST7565(Num);
  259. }



  260. // ************************************************************************************************************
  261. //初始化LCD屏
  262. void Init_LCD_ST7565(void)
  263. {

  264. // CLRBit(RST); // RST=0;
  265. // LCD_DelayMS(50);
  266. // SETBit(RST); // RST=1;
  267. // LCD_DelayMS(50);
  268. LCD_RST_L();
  269. Delay_nms(10);
  270. LCD_RST_H();
  271. Delay_nms(10);

  272. Write_Instruction_ST7565(0xa2);                 // lcd bias select 1/9 BIAS
  273. // Write_Instruction(0xa1);                 // ADC select,REVERSE 127-->0(a0,a1)
  274. Write_Instruction_ST7565(0xa0);                 // ADC=0
  275. // Write_Instruction(0xc0);                 // com select,NORMAL 0-->63(c8,c0)
  276. Write_Instruction_ST7565(0xc8);                 // Common output mode select= reverse
  277. Write_Instruction_ST7565(0x26);                 // RESISTOR RATIO
  278. Write_Instruction_ST7565(0x81);                 // ELECTRONIC VOLUME mode setting 100B 对比度命令
  279. // Write_Instruction_ST7565(0x00);                 // Set reference voltagel register 对比度数值   // leo_20170622       
  280. Write_Instruction_ST7565(0x0A);                 // Set reference voltagel register 对比度数值   // leo_20170622
  281. // Write_Instruction_ST7565(0x28);                 // Set reference voltagel register 对比度数值   // LM6060C
  282. // Write_Instruction_ST7565(0x12);                 // Set reference voltagel register 对比度数值   ,RYX207, leo_20170512
  283. Write_Instruction_ST7565(0x2f);                 // power control(VB,VR,VF=1,1,1)
  284. Delay_nms(10);
  285. Write_Instruction_ST7565(0x40);            // display start line=0
  286. Write_Instruction_ST7565(0xa6);            // normal display
  287. Write_Instruction_ST7565(0xa4);            // Duisplay all point = off
  288. Write_Instruction_ST7565(0xaf);                 // set display on
  289. Write_Instruction_ST7565(0xf8);                 // set booster ratio = 5x
  290. // Write_Instruction(0x00);
  291. Write_Instruction_ST7565(0x01);            // (two byte command)

  292. /*
  293. Write_Instruction(0xaf);            // display on
  294. Write_Instruction(0x40);            // display start line=0
  295. Write_Instruction(0xa0);            // ADC=0
  296. Write_Instruction(0xa6);            // normal display
  297. Write_Instruction(0xa4);            // Duisplay all point = off
  298. Write_Instruction(0xa2);            // LCD bias = 1/9
  299. Write_Instruction(0xc8);            // Common output mode select= reverse
  300. Write_Instruction(0x2f);            // Power control = all on
  301. Write_Instruction(0xf8);            // Booster Ratio = 5x
  302. Write_Instruction(0x01);            // (two byte command)

  303. Write_Instruction(0x81); //ELECTRONIC VOLUME mode setting 100B 对比度命令
  304. Write_Instruction(0x28);
  305. */

  306. //当前像素显示位置
  307. Lcd_CurrentX=0;
  308. Lcd_CurrentY=0;
  309. //图像反色显示 0 否 1是
  310. LCD_DisplayReserve_Driver=0;
  311. // LCD_DisplayReserve_Driver=1;                 // 20170622
  312. // LCD_setpos_Driver(Lcd_CurrentX,Lcd_CurrentY);
  313. }

  314. void LCD_Set_Place_ST7565(unsigned char x,unsigned char y)
  315. {
  316. if(y>LCD_MAX_Y) y=LCD_MIN_Y;
  317. if(x>LCD_MAX_X) x=LCD_MIN_X;
  318. LCD_setCharpos_ST7565(x,y);
  319. }

  320. // ************************************************************************************************************
  321. void LCD_disp_printR_ST7565(unsigned char *s,unsigned char x,unsigned char y,u8 length)                // 显示汉字或英文字符
  322. {
  323. u8 temp;  
  324. unsigned int i;
  325.        
  326. LCD_Set_Place_ST7565(x,y);
  327.    for (temp=0;temp<length;temp++)
  328.    {  
  329.       i=s[temp];
  330.       if(s[temp] > 127)
  331.       {  
  332.          temp++;
  333.          i=i*256+s[temp];
  334.       }
  335.       LCD_disp_Putchar_ST7565(i);
  336.    }
  337. }

  338. // ******
  339. void LCD_7565_Power_ON(void)
  340. {
  341. Write_Instruction_ST7565(0xAF);                // ST7565 Power ON
  342. }

  343. // ******
  344. void LCD_7565_Power_OFF(void)
  345. {
  346. Write_Instruction_ST7565(0xAE);                // ST7565 Power OFF
  347. }


  348. // ************************************************************************************************************
  349. /*
  350. Init_LCD_ST7565();
  351. LCD_CLS_ST7565(0);
  352. LCD_disp_printR_ST7565("leo 2016/01/20",0,0);
  353. Delay_nms(1000);
  354. LCD_CLS_ST7565(0);
  355. LCD_disp_printR_ST7565("科技",2,0);
  356. Delay_nms(1000);
  357. // LCD_disp_SetReverse_ST7565();
  358. LCD_DisplayBMP_ST7565(nBitmapDot_BMP1);

  359.         INT_Dec_disposal(Pressure,Dis_u16_Buf);
  360.         LCD_disp_printR_ST7565(Dis_u16_Buf,5,0,5);
  361.        
  362.         INT_Dec_disposal(Motor_Current,Dis_u16_Buf);
  363.         LCD_disp_printR_ST7565(Dis_u16_Buf,5,2,5);
  364.        
  365.         Dec_disposal(Temperature,Dis_u8_Buf);
  366.         LCD_disp_printR_ST7565(Dis_u8_Buf,5,4,3);       
  367.        
  368.         Dec_disposal(Humidity,Dis_u8_Buf);
  369.         LCD_disp_printR_ST7565(Dis_u8_Buf,5,6,3);       

  370. LCD_7565_Power_ON();
  371. LCD_7565_Power_OFF();       
  372. */
复制代码


LCD_DRIVER_7565.h
  1. // LCD_128X64
  2. // Driver: st7565/st7567
  3. // Mode: 4 line SPI
  4. // WR,RD : GND
  5. // leo_20160120

  6. #ifndef LCD_DRIVER_7565_H
  7. #define LCD_DRIVER_7565_H

  8. #include "includes.h"

  9. // ******
  10. #define LCD_PORT_1                P1
  11. #define LCD_PORT_2                P2

  12. // ******
  13. #define IO_LCD_Backlight                P14       
  14. #define LCD_Backlight_ON()        IO_LCD_Backlight=1
  15. #define LCD_Backlight_OFF()        IO_LCD_Backlight=0

  16. /*
  17. #define LCD_Backlight_pin                GPIO_PIN_2
  18. #define LCD_Backlight_ON()                LCD_PORT_1&=~LCD_Backlight_pin       
  19. #define LCD_Backlight_OFF()                LCD_PORT_1|=LCD_Backlight_pin
  20. #define LCD_Backlight_Reverse()        LCD_PORT_1^= LCD_Backlight_pin
  21. */
  22. #define LCD_CS_Pin                GPIO_PIN_3
  23. #define LCD_RST_Pin                GPIO_PIN_0
  24. #define LCD_DC_Pin                GPIO_PIN_3
  25. #define LCD_SCK_Pin                GPIO_PIN_4
  26. #define LCD_SDA_Pin                GPIO_PIN_5

  27. #define LCD_CS_H()                LCD_PORT_1|= LCD_CS_Pin
  28. #define LCD_CS_L()                LCD_PORT_1&=~LCD_CS_Pin

  29. #define LCD_RST_H()                LCD_PORT_2|= LCD_RST_Pin
  30. #define LCD_RST_L()                LCD_PORT_2&=~ LCD_RST_Pin

  31. #define LCD_DC_H()                LCD_PORT_2|= LCD_DC_Pin
  32. #define LCD_DC_L()                LCD_PORT_2&=~ LCD_DC_Pin

  33. #define LCD_SCK_H()                LCD_PORT_2|=LCD_SCK_Pin
  34. #define LCD_SCK_L()                LCD_PORT_2&=~LCD_SCK_Pin

  35. #define LCD_SDA_H()                LCD_PORT_2|= LCD_SDA_Pin
  36. #define LCD_SDA_L()                LCD_PORT_2&=~ LCD_SDA_Pin

  37. #define ENGLISHCHARLegth ((8*16)/8/1)
  38. #define CHINESECHARlegth ((16*16)/8/1)

  39. /*
  40. //以8*8字符计算,显示屏横向、纵向可以显示的点阵坐标;左、上、右、下以及当前的位置坐标
  41. #define LCD_MAX_X 7
  42. #define LCD_MAX_Y 3
  43. */

  44. //以4*4字符计算,显示屏横向、纵向可以显示的点阵坐标;左、上、右、下以及当前的位置坐标
  45. #define LCD_MAX_X 15
  46. #define LCD_MAX_Y 7

  47. #define LCD_MIN_X 0
  48. #define LCD_MIN_Y 0

  49. extern xdata u8 Dis_u16_Buf[];
  50. extern xdata u8 Dis_u8_Buf[];
  51. extern xdata u8 Dis_Sensor_Buf[];

  52. //当前字符显示的位置
  53. //unsigned char  Lcd_Charcter_CurrentX,Lcd_Charcter_CurrentY;
  54. //当前像素显示位置
  55. extern unsigned char  Lcd_CurrentX,Lcd_CurrentY;
  56. //图像反色显示 0 否 1是
  57. extern unsigned char LCD_DisplayReserve_Driver;


  58. #if defined LCD_DRIVER_7565_C
  59. void Init_LCD_ST7565();                                                                                                // 初始化LCD屏
  60. void LCD_CLS_ST7565(char value);                                                                        // 清屏
  61. void Set_Contrast_Control_ST7565(unsigned char Level);                        // 设置对比度
  62. void LCD_disp_Putchar_ST7565(unsigned int uChar);                                        // 显示一个Unicode
  63. void LCD_disp_SetReverse_ST7565(unsigned char ReverseTrue);                // 图像反色显示 0 否 1是,执行此命令后的所有操作均是按照设置显示
  64. void LCD_DisplayBMP_ST7565(const unsigned char *PicData);                                // 显示BMP图片
  65. void LCD_setpos_ST7565(unsigned char Lx,unsigned char Ly);                        // 设置像素显示坐标(x:0-127,y:0-7)
  66. void LCD_Set_Place_ST7565(unsigned char x,unsigned char y);
  67. void LCD_setCharpos_ST7565(unsigned char Lx,unsigned char Ly);                // 设置字符位置(x:0-8,y:0-3)
  68. void LCD_disp_printR_ST7565(unsigned char *s,unsigned char x,unsigned char y,u8 length);        // display 中英文字符
  69. void Write_Data_ST7565(unsigned char dat);
  70. void LCD_7565_Power_ON(void);
  71. void LCD_7565_Power_OFF(void);
  72. #else
  73. extern void Init_LCD_ST7565();                                                                                                // 初始化LCD屏
  74. extern void LCD_CLS_ST7565(char value);                                                                        // 清屏
  75. extern void Set_Contrast_Control_ST7565(unsigned char Level);                        // 设置对比度
  76. extern void LCD_disp_Putchar_ST7565(unsigned int uChar);                                        // 显示一个Unicode
  77. extern void LCD_disp_SetReverse_ST7565(unsigned char ReverseTrue);                // 图像反色显示 0 否 1是,执行此命令后的所有操作均是按照设置显示
  78. extern void LCD_DisplayBMP_ST7565(const unsigned char *PicData);                                // 显示BMP图片
  79. extern void LCD_setpos_ST7565(unsigned char Lx,unsigned char Ly);                        // 设置像素显示坐标(x:0-127,y:0-7)
  80. extern void LCD_Set_Place_ST7565(unsigned char x,unsigned char y);
  81. extern void LCD_setCharpos_ST7565(unsigned char Lx,unsigned char Ly);                // 设置字符位置(x:0-8,y:0-3)
  82. extern void LCD_disp_printR_ST7565(unsigned char *s,unsigned char x,unsigned char y,u8 length);                // display 中英文字符
  83. extern void Write_Data_ST7565(unsigned char dat);
  84. extern void LCD_7565_Power_ON(void);
  85. extern void LCD_7565_Power_OFF(void);
  86. #endif
  87. #endif
复制代码


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

使用道具 举报

沙发
ID:43600 发表于 2023-12-27 09:40 | 只看该作者
謝謝分享。如果能附上 L_ASCII_8x8.h ,讓 LCD_disp_printR_ST7565(Test",0,0,4);
LCD_disp_printR_ST7565(“2023/07/03",0,2,10);
能順利運行的話,就更完美了。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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