树莓派驱动LCD12864(st7920控制器)

本文介绍了一种使用 C 语言在 Linux 环境下控制 12864 LCD 显示模块的方法。通过具体的代码示例展示了如何实现 LCD 的初始化、编码转换、总线写入及显示字符串等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. /*
  2. 运行:sudo ./12864 [字符]
  3. 编译:gcc 12864.c -o 12864 -L lib -l wiringPi (需已安装wiringPi)
  4. by:WuSiYu
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdint.h>
  9. #include <wiringPi.h>
  10. #include <iconv.h>
  11.  
  12. #define LCD_RS 4 //显示屏控制线
  13. #define LCD_RW 5
  14. #define LCD_EN 1
  15.  
  16. #define D1 30 //显示屏数据线
  17. #define D2 21
  18. #define D3 22
  19. #define D4 23
  20. #define D5 24
  21. #define D6 25
  22. #define D7 26
  23. #define D8 27
  24.  
  25. char u2g_out [ 255 ] ;
  26.  
  27. /*===================================================================
  28. 功能:编码转换
  29. 输入:UTF8
  30. 输出:GB2312
  31. ====================================================================*/
  32. int code_convert ( char *from_charset , char *to_charset , char *inbuf , int inlen , char *outbuf , int outlen ) {
  33. iconv_t cd ;
  34. int rc ;
  35. char **pin = &inbuf ;
  36. char **pout = &outbuf ;
  37.  
  38. cd = iconv_open (to_charset ,from_charset ) ;
  39. if (cd == 0 ) return - 1 ;
  40. memset (outbuf , 0 ,outlen ) ;
  41. if (iconv (cd ,pin ,&inlen ,pout ,&outlen ) ==- 1 ) return - 1 ;
  42. iconv_close (cd ) ;
  43. return 0 ;
  44. }
  45.  
  46. int u2g ( char *inbuf , int inlen , char *outbuf , int outlen ) {
  47. return code_convert ( "utf-8" , "gb2312" ,inbuf ,inlen ,outbuf ,outlen ) ;
  48. }
  49.  
  50. /*===================================================================
  51. 功能:总线写入
  52. 输入:十六进制数据
  53. 输出:无
  54. ====================================================================*/
  55. void bus_write ( unsigned char data ) {
  56. int t [ 10 ] ;
  57. int f = 0 ,i = 0 ,d =data ;
  58.  
  59. //进制转换
  60. for (i = 0 ;i < 8 ;i ++ ) {
  61. t [i ] =data % 2 ;
  62. data =data / 2 ;
  63. }
  64.  
  65. //输出
  66. digitalWrite (D1 ,t [ 0 ] ) ;
  67. digitalWrite (D2 ,t [ 1 ] ) ;
  68. digitalWrite (D3 ,t [ 2 ] ) ;
  69. digitalWrite (D4 ,t [ 3 ] ) ;
  70. digitalWrite (D5 ,t [ 4 ] ) ;
  71. digitalWrite (D6 ,t [ 5 ] ) ;
  72. digitalWrite (D7 ,t [ 6 ] ) ;
  73. digitalWrite (D8 ,t [ 7 ] ) ;
  74. }
  75. /*===================================================================
  76. 功能:检查LCD忙状态
  77. 输入:无
  78. 输出:lcd_busy为1时,忙,等待。lcd-busy为0时,闲,可写指令与数据。
  79. ====================================================================*/
  80. void chk_busy ( ) { //检查忙位
  81. digitalWrite (LCD_RS , 0 ) ;
  82. digitalWrite (LCD_RW , 1 ) ;
  83. digitalWrite (LCD_EN , 1 ) ;
  84. bus_write ( 0xff ) ;
  85. pinMode (D8 , INPUT ) ;
  86. while (digitalRead (D8 ) ) ;
  87. pinMode (D8 , OUTPUT ) ;
  88. digitalWrite (LCD_EN , 0 ) ;
  89. }
  90. /*====================================================================
  91. 功能:写命令
  92. 输入:8位数据
  93. 输出:无
  94. =====================================================================*/
  95. void WriteCmd_LCD12864 ( unsigned char cmdcode ) {
  96. chk_busy ( ) ;
  97. digitalWrite (LCD_RS , 0 ) ;
  98. digitalWrite (LCD_RW , 0 ) ;
  99. digitalWrite (LCD_EN , 1 ) ;
  100. delay ( 5 ) ;
  101. bus_write (cmdcode ) ;
  102. digitalWrite (LCD_EN , 0 ) ;
  103. delay ( 5 ) ;
  104. }
  105. /*====================================================================
  106. 功能:写数据
  107. 输入:8位数据
  108. 输出:无
  109. =====================================================================*/
  110. void WriteData_LCD12864 ( unsigned char Dispdata ) {
  111. chk_busy ( ) ;
  112. digitalWrite (LCD_RS , 1 ) ;
  113. digitalWrite (LCD_RW , 0 ) ;
  114. digitalWrite (LCD_EN , 1 ) ;
  115. delay ( 5 ) ;
  116. bus_write (Dispdata ) ;
  117. digitalWrite (LCD_EN , 0 ) ;
  118. delay ( 5 ) ;
  119. }
  120. /*==========================================================================
  121. 功能:发送字符串
  122. 输入:地址,字符串
  123. 输出:无
  124. ===========================================================================*/
  125. void WriteWord_LCD12864 ( unsigned char a , unsigned char *d ) { //向LCD指定位置发送一个字符串,长度64字符之内。
  126. unsigned char *s ;
  127. u2g (d , strlen (d ) ,u2g_out , 255 ) ;
  128. s =u2g_out ;
  129. WriteCmd_LCD12864 (a ) ;
  130. while ( *s > 0 ) {
  131. WriteData_LCD12864 ( *s ) ;
  132. s ++;
  133. }
  134. }
  135. /*==========================================================================
  136. 功能:发送字符串2
  137. 输入:字符串
  138. 输出:无
  139. ===========================================================================*/
  140. void WriteWord_LCD12864_2 ( unsigned char *d ) { //向LCD发送一屏字符串,长度64字符之内。
  141. int i = 0 ;
  142. unsigned char *s ;
  143. u2g (d , strlen (d ) ,u2g_out , 255 ) ;
  144. s =u2g_out ;
  145. WriteCmd_LCD12864 ( 0x80 ) ;
  146. while ( *s > 0 ) {
  147. WriteData_LCD12864 ( *s ) ;
  148. s ++;
  149. i ++;
  150. if (i == 16 ) {
  151. WriteCmd_LCD12864 ( 0x90 ) ;
  152. }
  153. if (i == 32 ) {
  154. WriteCmd_LCD12864 ( 0x88 ) ;
  155. }
  156. if (i == 48 ) {
  157. WriteCmd_LCD12864 ( 0x98 ) ;
  158. }
  159. }
  160. }
  161. /*==========================================================================
  162. 功能:初始化LCD
  163. 输入:无
  164. 输出:无
  165. ===========================================================================*/
  166. void Init_LCD12864 ( void ) { //初始化LCD屏
  167. pinMode (D1 , OUTPUT ) ; //设置GPIO
  168. pinMode (D2 , OUTPUT ) ;
  169. pinMode (D3 , OUTPUT ) ;
  170. pinMode (D4 , OUTPUT ) ;
  171. pinMode (D5 , OUTPUT ) ;
  172. pinMode (D6 , OUTPUT ) ;
  173. pinMode (D7 , OUTPUT ) ;
  174. pinMode (D8 , OUTPUT ) ;
  175.  
  176. pinMode (LCD_RS , OUTPUT ) ;
  177. pinMode (LCD_RW , OUTPUT ) ;
  178. pinMode (LCD_EN , OUTPUT ) ;
  179.  
  180. WriteCmd_LCD12864 ( 0x38 ) ; //选择8bit数据流
  181. delay ( 20 ) ;
  182. WriteCmd_LCD12864 ( 0x01 ) ; //清除显示,并且设定地址指针为00H
  183. delay ( 20 ) ;
  184. WriteCmd_LCD12864 ( 0x0c ) ; //开显示(无游标、不反白)
  185. delay ( 20 ) ;
  186. }
  187.  
  188. int main ( int args , char *argv [ ] ) {
  189. wiringPiSetup ( ) ;
  190. Init_LCD12864 ( ) ;
  191.  
  192. WriteCmd_LCD12864 ( 0x01 ) ;
  193. WriteWord_LCD12864 ( 0x80 , "Hello LCD12864" ) ;
  194. if (argv [ 1 ] ) {
  195. WriteCmd_LCD12864 ( 0x01 ) ;
  196. WriteCmd_LCD12864 ( 0x80 ) ;
  197. WriteWord_LCD12864_2 (argv [ 1 ] ) ;
  198. }
  199. }
  200.  
  201.  
  202.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值