12下一页
返回列表 发新帖我要提问本帖赏金: 100.00元(功能说明)

[MM32生态] 【MM32+模块】系列:07、数码管显示(二)

[复制链接]
 楼主| xld0932 发表于 2022-4-13 10:29 | 显示全部楼层 |阅读模式
<
#申请原创#   @21小跑堂   

随着集成电路的快速发展,LED驱动芯片的功能也随之丰富,很多在驱动LED的同时增加了按键检测的功能,支持矩阵式的按键扫描;使用这类的LED驱动在驱动LED的同时,又提供了按键功能,尽最大程度上节省了MCU所占用的软件、硬件等资源。
本篇将主要实现带有按键功能的LED驱动模块的显示及按键检测的功能:
1、数码管显示驱动之AiP650
2、数码管显示驱动之TM1638

1、数码管显示驱动之AiP650
AiP650是无锡中微爱芯推出的一款带有键盘扫描电路接口的LED驱动控制专用芯片。最多可驱动48段的共阴极数码管,最大支持7*4个按键;芯片内置了上电复位电路和时钟振荡电路,通过高速两线串行通讯接口进行控制;芯片工作在3V~5.5V之间,提供不小于25mA的段驱动电流和不小于150mA的字驱动电路,满足了市场上决大多数的数码管驱动能力。由于在淘宝上没有买到带按键检测的AiP650模组,所以就自己画了一个块来调试验证。
1.1、数码管显示驱动之AiP650原理图设计
参考AiP650数据手册上的应用线路图进行绘制,使用立创EDA进行设计,原理图如下所示:
AiP650原理图.png

1.2、数码管显示驱动之AiP650 PCB设计2D
AiP650_PCB.png

1.3、数码管显示驱动之AiP650焊接调试
根据AiP650数据手册上应用线路图的设计,在R1~R4起初都是选用的2K的电路,但在实际调试程序的时候,矩阵按键的扫描结果会出现异常,同一个按键会出现不同按键值的问题,在咨询了原厂以及把板子寄给他们调试都没法确认问题出在哪里,因为完全是参考数据手册来设计的;在后来的调试、尝试下不断的去修改R1~R4的阻值,加大阻值会有明显的改善效果,最后将R1~R4的阻值定在了4.7K
1.jpg

1.4、数码管显示驱动之AiP650驱动程序
  1. sI2C_TypeDef sI2C_AiP650 =
  2. {
  3.     RCC_AHBENR_GPIOB, GPIOB, GPIO_Pin_10,
  4.     RCC_AHBENR_GPIOB, GPIOB, GPIO_Pin_11,
  5.     500
  6. };

  7. uint8_t AiP650_GetKey(sI2C_TypeDef *sI2C)
  8. {
  9.     uint8_t KeyValue = 0;

  10.     sI2C_GenerateStart(sI2C);

  11.     sI2C_WriteByte(sI2C, 0x4F);

  12.     if(sI2C_GetACK(sI2C))
  13.     {
  14.         sI2C_GenerateStop(sI2C);    return 0x00;
  15.     }

  16.     KeyValue = sI2C_ReadByte(sI2C);

  17.     sI2C_PutACK(sI2C, 1);

  18.     sI2C_GenerateStop(sI2C);

  19.     return KeyValue;
  20. }

  21. void AiP650_WriteCMD(sI2C_TypeDef *sI2C, uint16_t Command)
  22. {
  23.     sI2C_GenerateStart(sI2C);

  24.     sI2C_WriteByte(sI2C, (Command >> 8) & 0xFF);

  25.     if(sI2C_GetACK(sI2C))
  26.     {
  27.         sI2C_GenerateStop(sI2C);    return;
  28.     }

  29.     sI2C_WriteByte(sI2C, (Command >> 0) & 0xFF);

  30.     if(sI2C_GetACK(sI2C))
  31.     {
  32.         sI2C_GenerateStop(sI2C);    return;
  33.     }

  34.     sI2C_GenerateStop(sI2C);
  35. }

  36. void AiP650_Display(char *str)
  37. {
  38.     uint8_t i = 0, j = 0, Data[4];

  39.     memset(Data, 0, sizeof(Data));

  40.     for(i = 0; i < 4; i++)
  41.     {
  42.         for(j = 0; j < 38; j++)
  43.         {
  44.             if(DIGITRON_TABLE[j].ch == str[i])
  45.             {
  46.                 Data[i] = DIGITRON_TABLE[j].Data;
  47.             }
  48.         }
  49.     }

  50.     AiP650_WriteCMD(&sI2C_AiP650, AiP650_DIG0 | Data[0]);
  51.     AiP650_WriteCMD(&sI2C_AiP650, AiP650_DIG1 | Data[1]);
  52.     AiP650_WriteCMD(&sI2C_AiP650, AiP650_DIG2 | Data[2]);
  53.     AiP650_WriteCMD(&sI2C_AiP650, AiP650_DIG3 | Data[3]);
  54. }

  55. void AiP650_DisplayNumber(uint16_t Value)
  56. {
  57.     char Number[10];

  58.     memset(Number, 0, sizeof(Number));
  59.     sprintf(Number,  "%04d",  Value);

  60.     AiP650_Display(Number);
  61. }

  62. void AiP650_Init(void)
  63. {
  64.     /* 初始化模拟I2C引脚 */
  65.     sI2C_Init(&sI2C_AiP650);

  66.     /* AiP650芯片配置 */
  67.     AiP650_WriteCMD(&sI2C_AiP650, AiP650_SYSON_7_8SEG_ON);

  68.     /* 在LED数码管上显示内容 */
  69.     AiP650_Display("----");
  70. }

  71. void AiP650_Handler(void)
  72. {
  73.     static uint8_t OldKeyValue = 0;

  74.     uint8_t KeyTable[][4] =
  75.     {
  76.         {0x74, 0x75, 0x76, 0x77},
  77.         {0x6C, 0x6D, 0x6E, 0x6F},
  78.         {0x64, 0x65, 0x66, 0x67},
  79.         {0x5C, 0x5D, 0x5E, 0x5F},
  80.         {0x54, 0x55, 0x56, 0x57},
  81.         {0x4C, 0x4D, 0x4E, 0x4F},
  82.         {0x44, 0x45, 0x46, 0x47},
  83.     };

  84.     uint8_t KeyValue = AiP650_GetKey(&sI2C_AiP650);

  85.     if(KeyValue != OldKeyValue)
  86.     {
  87.         OldKeyValue = KeyValue;

  88.         for(uint8_t i = 0; i < 4; i++)
  89.         {
  90.             for(uint8_t j = 0; j < 7; j++)
  91.             {
  92.                 if(KeyValue == KeyTable[j][i])
  93.                 {
  94.                     AiP650_DisplayNumber(i * 7 + j + 1);

  95.                     printf("\r\nSW%02d : 0x%02x", i * 7 + j + 1, KeyValue);
  96.                 }
  97.             }
  98.         }
  99.     }
  100. }

  101. void HMI_Init(void)
  102. {
  103.     AiP650_Init();

  104.     TASK_Append(TASK_ID_HMI, HMI_Handler, 10);
  105. }

  106. void HMI_Handler(void)
  107. {
  108.     AiP650_Handler();
  109. }

1.5、数码管显示驱动之AiP650运行演示
2.png AiP650.GIF


2、数码管显示驱动之TM1638
10.png
TM1638是深圳天微电子推出的一款带按键扫描接口的LED(发光二极管显示器)驱动控制专用IC。最多可驱动810段的数码管(共阳极、共阴极都支持),最大支持8*3个按键;芯片内置了上电复位电路和RC振荡电路;通过3线串行通讯接口进行控制;在数据手册上提供典型的带矩阵按键扫描的共阳极和共阴极的应用硬件电路图。
4.jpg

2.1、数码管显示驱动之TM1638驱动程序
  1. #define TM1638_STB_H()  GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_SET)
  2. #define TM1638_STB_L()  GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_RESET)

  3. #define TM1638_CLK_H()  GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_SET)
  4. #define TM1638_CLK_L()  GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_RESET)

  5. #define TM1638_DIO_H()  GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET)
  6. #define TM1638_DIO_L()  GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_RESET)

  7. #define TM1638_DIO_GET()   GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2)

  8. uint8_t TM1638_RAM[16];

  9. void TM1638_WriteCMD(uint8_t Command)
  10. {
  11.     TM1638_STB_L();

  12.     for(uint8_t i = 0; i < 8; i++)
  13.     {
  14.         TM1638_CLK_L();

  15.         if(Command & (0x01 << i)) TM1638_DIO_H();
  16.         else                      TM1638_DIO_L();

  17.         TM1638_CLK_H();
  18.     }

  19.     TM1638_STB_H();
  20. }

  21. void TM1638_WriteRAM(uint8_t Address, uint8_t Data)
  22. {
  23.     TM1638_STB_L();

  24.     for(uint8_t i = 0; i < 8; i++)
  25.     {
  26.         TM1638_CLK_L();

  27.         if(Address & (0x01 << i)) TM1638_DIO_H();
  28.         else                      TM1638_DIO_L();

  29.         TM1638_CLK_H();
  30.     }

  31.     for(uint8_t i = 0; i < 8; i++)
  32.     {
  33.         TM1638_CLK_L();

  34.         if(Data    & (0x01 << i)) TM1638_DIO_H();
  35.         else                      TM1638_DIO_L();

  36.         TM1638_CLK_H();
  37.     }

  38.     TM1638_STB_H();
  39. }

  40. void TM1638_Read(uint8_t Command, uint8_t *Buffer)
  41. {
  42.     GPIO_InitTypeDef GPIO_InitStructure;

  43.     uint8_t Data = 0;

  44.     TM1638_STB_L();

  45.     for(uint8_t i = 0; i < 8; i++)
  46.     {
  47.         TM1638_CLK_L();

  48.         if(Command & (0x01 << i)) TM1638_DIO_H();
  49.         else                      TM1638_DIO_L();

  50.         TM1638_CLK_H();
  51.     }

  52.     GPIO_StructInit(&GPIO_InitStructure);
  53.     GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_2;
  54.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_FLOATING;
  55.     GPIO_Init(GPIOB, &GPIO_InitStructure);

  56.     for(uint8_t j = 0; j < 4; j++)
  57.     {
  58.         Data = 0;

  59.         for(uint8_t i = 0; i < 8; i++)
  60.         {
  61.             TM1638_CLK_L();

  62.             Data >>= 1;

  63.             if(TM1638_DIO_GET() != Bit_RESET)
  64.             {
  65.                 Data |= 0x80;
  66.             }

  67.             TM1638_CLK_H();
  68.         }

  69.         Buffer[j] = Data;
  70.     }

  71.     TM1638_STB_H();

  72.     GPIO_StructInit(&GPIO_InitStructure);
  73.     GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_2;
  74.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  75.     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  76.     GPIO_Init(GPIOB, &GPIO_InitStructure);

  77.     TM1638_DIO_H();
  78. }

  79. void TM1638_RAM_Refresh(void)
  80. {
  81.     TM1638_WriteCMD(0x88);
  82.     TM1638_WriteCMD(0x40);

  83.     for(uint8_t i = 0; i < 16; i++)
  84.     {
  85.         TM1638_WriteRAM(0xC0 + i, TM1638_RAM[i]);
  86.     }
  87. }

  88. void TM1638_DisplayChar(uint8_t Index, char ch, uint8_t Update)
  89. {
  90.     uint8_t  Data = 0x00;

  91.     if(Index > 8) return;

  92.     for(uint8_t i = 0; i < 38; i++)
  93.     {
  94.         if(DIGITRON_TABLE[i].ch == ch)
  95.         {
  96.             Data = DIGITRON_TABLE[i].Data;
  97.         }
  98.     }

  99.     for(uint8_t i = 0; i < 8; i++)
  100.     {
  101.         if(Data & (0x01 << i))
  102.         {
  103.             TM1638_RAM[i*2] |=  (0x01 << Index);
  104.         }
  105.         else
  106.         {
  107.             TM1638_RAM[i*2] &= ~(0x01 << Index);
  108.         }
  109.     }

  110.     if(Update) TM1638_RAM_Refresh();
  111. }

  112. void TM1638_DisplayString(char *str)
  113. {
  114.     uint8_t Index = 8;

  115.     while(*str != '\0')
  116.     {
  117.         if(Index == 0) break;

  118.         TM1638_DisplayChar(--Index, *str++, 0);
  119.     }

  120.     TM1638_RAM_Refresh();
  121. }

  122. void TM1638_Init(void)
  123. {
  124.     GPIO_InitTypeDef GPIO_InitStructure;

  125.     memset(TM1638_RAM, 0, sizeof(TM1638_RAM));

  126.     RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOB, ENABLE);

  127.     GPIO_StructInit(&GPIO_InitStructure);
  128.     GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
  129.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  130.     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  131.     GPIO_Init(GPIOB, &GPIO_InitStructure);

  132.     GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_SET);
  133.     GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_SET);
  134.     GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET);

  135.     TM1638_RAM_Refresh();

  136.     TM1638_DisplayString("--------");
  137. }

  138. void TM1638_Handler(void)
  139. {
  140.     uint8_t Buffer[4] = {0, 0, 0, 0};
  141.     uint8_t String[10];

  142.     TM1638_Read(0x42, Buffer);

  143.     if((Buffer[0] != 0) || (Buffer[1] != 0) || (Buffer[2] != 0) || (Buffer[3] != 0))
  144.     {
  145.         printf("\r\n");

  146.         if(Buffer[0] & 0x02) printf("S09");
  147.         if(Buffer[0] & 0x20) printf("S10");
  148.         if(Buffer[0] & 0x04) printf("S01");
  149.         if(Buffer[0] & 0x40) printf("S02");

  150.         if(Buffer[1] & 0x02) printf("S11");
  151.         if(Buffer[1] & 0x20) printf("S12");
  152.         if(Buffer[1] & 0x04) printf("S03");
  153.         if(Buffer[1] & 0x40) printf("S04");

  154.         if(Buffer[2] & 0x02) printf("S13");
  155.         if(Buffer[2] & 0x20) printf("S14");
  156.         if(Buffer[2] & 0x04) printf("S05");
  157.         if(Buffer[2] & 0x40) printf("S06");

  158.         if(Buffer[3] & 0x02) printf("S15");
  159.         if(Buffer[3] & 0x20) printf("S16");
  160.         if(Buffer[3] & 0x04) printf("S07");
  161.         if(Buffer[3] & 0x40) printf("S08");

  162.         memset(String, 0,  sizeof(String));
  163.         sprintf((char *)String, "%02x%02x%02x%02x", Buffer[0], Buffer[1], Buffer[2], Buffer[3]);

  164.         TM1638_DisplayString((char *)String);
  165.     }
  166. }

  167. void HMI_Init(void)
  168. {
  169.     TM1638_Init();

  170.     TASK_Append(TASK_ID_HMI, HMI_Handler, 200);
  171. }

  172. void HMI_Handler(void)
  173. {
  174.     TM1638_Handler();
  175. }

2.2、数码管显示驱动之TM1638运行演示
5.png TM1638.GIF

附件:
AiP650数据手册: AiP650.PDF (641.27 KB, 下载次数: 16)
AiP650原理图设计: AiP650原理图.PDF (88.71 KB, 下载次数: 9)
TM1638数据手册: TM1638_V1.4.PDF (657.09 KB, 下载次数: 11)
软件工程源代码: NIXIETUBE2.zip (715.6 KB, 下载次数: 33)

打赏榜单

21小跑堂 打赏了 50.00 元 2022-04-15

21小跑堂 打赏了 50.00 元 2022-04-14
理由:恭喜通过原创文章审核!请多多加油哦!

 楼主| xld0932 发表于 2022-4-13 10:39 | 显示全部楼层
AiP650的矩阵按键扫描是单个按键的,无法检测2个按键被同时按下;而TM1638的矩阵按键每一个都有独立的检测码,支持任意个按键的组合!!!
www5911839 发表于 2022-4-14 12:01 | 显示全部楼层
太高产了
 楼主| xld0932 发表于 2022-4-14 13:34 | 显示全部楼层

多多捧场哈
simonwifi 发表于 2022-4-19 17:36 | 显示全部楼层
本帖最后由 simonwifi 于 2022-4-19 17:38 编辑

太棒了 想要222
 楼主| xld0932 发表于 2022-4-19 17:40 | 显示全部楼层

MM32F0140的最小系统板在这个系列帖子的第一篇附件中有提供哦
下面这个是AiP650板子的Gerber文件,可以直接打板哦,供参考: Gerber_AiP650E0.zip (75.97 KB, 下载次数: 0)
lzm199516 发表于 2022-4-19 18:11 | 显示全部楼层
太强了,整成一个系列的
 楼主| xld0932 发表于 2022-4-20 08:45 | 显示全部楼层
lzm199516 发表于 2022-4-19 18:11
太强了,整成一个系列的

tpgf 发表于 2022-5-3 13:18 | 显示全部楼层
不同的驱动有什么具体的区别呀
renzheshengui 发表于 2022-5-3 13:25 | 显示全部楼层
楼主手上的资源太丰富了
wakayi 发表于 2022-5-3 13:35 | 显示全部楼层
这种小板子感觉不好飞线啊
wowu 发表于 2022-5-3 13:46 | 显示全部楼层
我就看这个小板子真是不错
xiaoqizi 发表于 2022-5-3 14:26 | 显示全部楼层
各种大板子都能用啊
木木guainv 发表于 2022-5-3 14:35 | 显示全部楼层
板子的成本大概是多少啊
 楼主| xld0932 发表于 2022-5-3 15:12 | 显示全部楼层
木木guainv 发表于 2022-5-3 14:35
板子的成本大概是多少啊

哪块板子的成本呀?
 楼主| xld0932 发表于 2022-5-4 21:57 | 显示全部楼层
tpgf 发表于 2022-5-3 13:18
不同的驱动有什么具体的区别呀

只是硬件方案不同而已,在使用的时候也需要根据你们需求来判断,选择的驱动芯片能否满足功能需求。
 楼主| xld0932 发表于 2022-5-4 21:57 | 显示全部楼层
renzheshengui 发表于 2022-5-3 13:25
楼主手上的资源太丰富了

 楼主| xld0932 发表于 2022-5-4 21:58 | 显示全部楼层
wowu 发表于 2022-5-3 13:46
我就看这个小板子真是不错

 楼主| xld0932 发表于 2022-5-4 21:59 | 显示全部楼层
wakayi 发表于 2022-5-3 13:35
这种小板子感觉不好飞线啊

核心板方便连线,连接外部各种模块;功能板只是特定功能画的,功能相对固定,就不需要太多飞线了;具体看想怎么用
keydongle2 发表于 2022-5-26 16:16 | 显示全部楼层
不错
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:King.Xu

77

主题

3023

帖子

38

粉丝
快速回复 在线客服 返回列表 返回顶部