找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

实用任意三个单片机IO口连接LCD1602A液晶屏

查看数: 7020 | 评论数: 10 | 收藏 6
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2020-2-27 15:13

正文摘要:

之前看到一个牛人zhu123帖子,是用二线连接1602。只用2个IO口节省了资源,但效果不太好,显示速度慢。改成三线后效果就很好,可以实用化。这种接法的原理就是利用电容的充电放电效应进行存储数字位,类似动态存储器 ...

回复

ID:161164 发表于 2022-12-13 13:27
8051版本


  1. #include <reg52.h>

  2. typedef         unsigned char        u8;  //0 to 255
  3. typedef         unsigned int        u16;  //0 to 65535
  4. typedef         unsigned long        u32;  //0 to 4294967295
  5. sbit EN_RS = P2^5;
  6. sbit D4_D6 = P2^6;
  7. sbit D5_D7 = P2^7;
  8. #define SetRS 0x01
  9. #define RstRS 0x00
  10. void Delay2ms()                //@12.000MHz
  11. {
  12.     unsigned char i, j;

  13.     i = 4;
  14.     j = 225;
  15.     do
  16.     {
  17.         while (--j);
  18.     }
  19.     while (--i);
  20. }
  21. void Delay200us()                //@12.000MHz
  22. {
  23.     unsigned char i;

  24.     i = 97;
  25.     while (--i);
  26. }
  27. void ThreeWire(u8 dat)
  28. {
  29.     EN_RS = (bit)(dat & 0x01);
  30.     D4_D6 = (bit)(dat & 0x40);
  31.     D5_D7 = (bit)(dat & 0x80);
  32.     Delay200us();
  33.     D4_D6 = (bit)(dat & 0x10);
  34.     D5_D7 = (bit)(dat & 0x20);
  35.     EN_RS = 1;
  36.     EN_RS = 0;
  37.     D4_D6 = 0;
  38.     D5_D7 = 0;
  39.     Delay200us();
  40. }
  41. void LCD_Init_4bit();
  42. void LCD_WChar_4bit(u8 Row, u8 Address, char chr);
  43. void LCD_WStr_4bit(u8 Row, u8 Address, char *Str);

  44. void LCD_WCMD(u8 Cmd)
  45. {
  46.     ThreeWire(RstRS | (Cmd & 0xF0));
  47.     ThreeWire(RstRS | (Cmd << 4));
  48. }
  49. void LCD_WDAT(u8 Dat)
  50. {
  51.     ThreeWire(SetRS | (Dat & 0xF0));
  52.     ThreeWire(SetRS | (Dat << 4));
  53. }
  54. void LCD_Init_4bit()
  55. {
  56.     LCD_WCMD(0x02);
  57.     LCD_WCMD(0x28);
  58.     LCD_WCMD(0x0c);
  59.     LCD_WCMD(0x06);
  60.     LCD_WCMD(0x01);
  61.     Delay2ms();
  62. }
  63. void LCD_WChar_4bit(u8 Row, u8 Address, char chr)
  64. {
  65.     if(Row == 1)LCD_WCMD(0x80 + Address);
  66.     if(Row == 2)LCD_WCMD(0xc0 + Address);

  67.     LCD_WDAT(chr);
  68. }
  69. void LCD_WStr_4bit(u8 Row, u8 Address, char *Str)
  70. {
  71.     if(Row == 1)LCD_WCMD(0x80 + Address);
  72.     if(Row == 2)LCD_WCMD(0xc0 + Address);
  73.     while(*Str)
  74.     {
  75.         LCD_WDAT(*Str);
  76.         Str++;
  77.     }
  78. }
  79. void Delay1000ms()                //@12.000MHz
  80. {
  81.     unsigned char i, j, k;

  82.     i = 8;
  83.     j = 154;
  84.     k = 122;
  85.     do
  86.     {
  87.         do
  88.         {
  89.             while (--k);
  90.         }
  91.         while (--j);
  92.     }
  93.     while (--i);
  94. }

  95. u8 Count = 0;
  96. void main()
  97. {
  98.     EN_RS = 0;
  99.     D4_D6 = 0;
  100.     D5_D7 = 0;
  101.     LCD_Init_4bit();
  102.     LCD_WStr_4bit(1,0,"Hello World!");
  103.     LCD_WStr_4bit(2,0,"3 Wire LCD");
  104.     while (1)
  105.     {
  106.         LCD_WChar_4bit(2,13,Count/100%10 + '0');
  107.         LCD_WChar_4bit(2,14,Count/10%10 + '0');
  108.         LCD_WChar_4bit(2,15,Count%10 + '0');
  109.         Count++;
  110.         Delay1000ms();
  111.     }
  112. }
复制代码



ID:951974 发表于 2022-12-12 21:03
自个玩玩可以,别真把它做进产品
ID:71297 发表于 2021-5-20 21:01
可以用上少脚的单片机
ID:824490 发表于 2021-5-19 10:50
自个玩玩可以,别真把它做进产品。
ID:906120 发表于 2021-5-13 09:22
这个用stc89c52能实现吗?
ID:102702 发表于 2020-3-3 21:17
gdbin001 发表于 2020-3-3 16:22
M8我没有用过。属于Arduino系列应该可以用。

好的,我试一下,谢谢!
ID:688914 发表于 2020-3-3 16:22
raymondau 发表于 2020-3-3 00:27
楼主用的是什么单片机,M8可以吗?

M8我没有用过。属于Arduino系列应该可以用。
ID:688914 发表于 2020-3-3 16:11
Liangkan 发表于 2020-3-2 21:42
很好的东西,可惜不会Arduino,不能移植。

参考zhu123的帖子,应该就可以了。
ID:102702 发表于 2020-3-3 00:27
楼主用的是什么单片机,M8可以吗?
ID:163501 发表于 2020-3-2 21:42
很好的东西,可惜不会Arduino,不能移植。

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

Powered by 单片机教程网

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