{"id":754,"date":"2022-02-04T04:42:09","date_gmt":"2022-02-04T04:42:09","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=754"},"modified":"2022-02-04T04:42:12","modified_gmt":"2022-02-04T04:42:12","slug":"working-with-stm32-and-displays-tm1637-4-digit-7-segment-led-display","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=754","title":{"rendered":"Working with STM32 and Displays: TM1637 4-digit 7-segment LED display"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/solectroshop.com\/1234-large_default\/modulo-display-digital-tm1637-4-digitos.jpg\" alt=\"\" width=\"400\" height=\"400\" \/><\/figure><\/div>\n\n\n\n<p>In this guide, we shall see how to work with TM1637 4-digit 7-segment LED display and use STM32F411 to display the encoder values on the display.<\/p>\n\n\n\n<p>In this guide, we will cover the following :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>TM1637 4-digit 7-segment LED display<\/li><li>Connection with STM32<\/li><li>Code<\/li><li>Demo<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. TM1637 4-digit 7-segment LED display:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Bare 4-digit 7-segment displays\u00a0usually require 12 connection pins. That\u2019s quite a lot and doesn\u2019t leave much room for other sensors or modules. Thanks to the TM1637 IC mounted on the back of the display module, this number can be reduced to just four. Two pins are required for the power connections and the other two pins are used to control the segments.<\/p>\n\n\n\n<p>7-segment displays contain 7 (or 8) individually addressable LEDs. The segments are labeled A to G and some displays also have a dot (the 8th LED). Use this image as a reference when setting the individual segments in the code later.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/pinterest.com\/pin\/create\/bookmarklet\/?media=https%3A%2F%2Fwww.makerguides.com%2Fwp-content%2Fuploads%2F2019%2F08%2F7-segment-display-annotated.jpg&amp;url=https%3A%2F%2Fwww.makerguides.com%2Ftm1637-arduino-tutorial%2F&amp;is_video=false&amp;description=7-segment-display-annotated\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/www.makerguides.com\/wp-content\/uploads\/2019\/08\/7-segment-display-annotated.jpg\" alt=\"7-segment-display-annotated\" \/><\/a><\/figure><\/div>\n\n\n\n<p>You can buy many different display modules that use a TM1637 IC. The color, size, dots, and connection points can all be different. I don\u2019t have experience with all the different versions of this display but as long as they use the TM1637, the code examples provided below should work.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Here you can find the basic specifications of the display module that I used in this tutorial.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">TM1637 4-Digit 7-Segment Display Specifications<\/h3>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>Operating voltage<\/td><td>3.3 \u2013 5 V<\/td><\/tr><tr><td>Current draw<\/td><td>80 mA<\/td><\/tr><tr><td>Luminance levels<\/td><td>8<\/td><\/tr><tr><td>Display dimensions<\/td><td>30 x 14 mm (0.36\u2033 digits)<\/td><\/tr><tr><td>Overall dimensions<\/td><td>42 x 24 x 12 mm<\/td><\/tr><tr><td>Hole dimensions<\/td><td>38 x 20, \u2300 2.2 mm<\/td><\/tr><tr><td>Operating temperature<\/td><td>-10 \u2013 80 \u00b0C<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Connection with STM32<\/h2>\n\n\n\n<p>The connection of M1637 4-digit 7-segment LED display as following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter is-style-regular\"><table><tbody><tr><td>TM1637<\/td><td>STM32F411 Nucleo-64<\/td><\/tr><tr><td>Vcc<\/td><td>5V<\/td><\/tr><tr><td>GNG<\/td><td>GND<\/td><\/tr><tr><td>DIO<\/td><td>PC1 (A4)<\/td><\/tr><tr><td>CLK<\/td><td>PC0(A5)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The encoder connection as following: <\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter is-style-regular\"><table><tbody><tr><td>Encoder Pin<\/td><td>STM32F411-Nucleo64 Pin<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>+<\/td><td>3V3<\/td><\/tr><tr><td>DT<\/td><td>PA0<\/td><\/tr><tr><td>CLK<\/td><td>PA1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>For encoder hookup guide and code, please refer to <a href=\"https:\/\/blog.embeddedexpert.io\/?p=430\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=430\" target=\"_blank\" rel=\"noreferrer noopener\">this<\/a> topic<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"755\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-04-at-7.16.13-AM-1024x755.png\" alt=\"\" class=\"wp-image-755\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-04-at-7.16.13-AM-1024x755.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-04-at-7.16.13-AM-300x221.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-04-at-7.16.13-AM-768x566.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-04-at-7.16.13-AM-1536x1133.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-04-at-7.16.13-AM-2048x1510.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-04-at-7.16.13-AM-1150x848.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-04-at-7.16.13-AM-750x553.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-04-at-7.16.13-AM-400x295.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-04-at-7.16.13-AM-250x184.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p>in tm1637.c file, we start of by defining some macros that will allow us access some functionality in the code later and segments definitions as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define PORTC_CLK_ENABLE RCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOCEN \n\n\n#define CLK_PIN_ENABLE GPIOC-&gt;MODER|=(1&lt;&lt;0)|(0&lt;&lt;1)\n#define DIO_PIN_ENABLE GPIOC-&gt;MODER|=(1&lt;&lt;2)|(0&lt;&lt;3)\n\n\nconst char segmentMap[] = {\n    0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, \/\/ 0-7\n    0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, \/\/ 8-9, A-F\n    0x00\n};\n\n<\/pre><\/div>\n\n\n\n<p>For the initializing, we enable clock access to the port, set pins as output with internal pull-up and set brightness to 8<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\nvoid tm1637Init(void)\n{\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOCEN;\n  GPIOC-&gt;MODER|=(1&lt;&lt;0)|(0&lt;&lt;1);\n\tGPIOC-&gt;MODER|=(1&lt;&lt;2)|(0&lt;&lt;3);\n\tGPIOC-&gt;OSPEEDR|=0xF;\n\tGPIOC-&gt;OTYPER|=(1&lt;&lt;0)|(1&lt;&lt;0);\n\tGPIOC-&gt;PUPDR|=(1&lt;&lt;0)|(1&lt;&lt;2);\n\ttm1637SetBrightness(8);\n}<\/pre><\/div>\n\n\n\n<p>For displaying a decimal value, the following function is used:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void tm1637DisplayDecimal(int v, int displaySeparator)\n{\n    unsigned char digitArr[4];\n    for (int i = 0; i &lt; 4; ++i) {\n        digitArr[i] = segmentMap[v % 10];\n        if (i == 2 &amp;&amp; displaySeparator) {\n            digitArr[i] |= 1 &lt;&lt; 7;\n        }\n        v \/= 10;\n    }\n\n    _tm1637Start();\n    _tm1637WriteByte(0x40);\n    _tm1637ReadResult();\n    _tm1637Stop();\n\n    _tm1637Start();\n    _tm1637WriteByte(0xc0);\n    _tm1637ReadResult();\n\n    for (int i = 0; i &lt; 4; ++i) {\n        _tm1637WriteByte(digitArr[3 - i]);\n        _tm1637ReadResult();\n    }\n\n    _tm1637Stop();\n}<\/pre><\/div>\n\n\n\n<p>To start the brightness, we can use the following function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\/\/ Valid brightness values: 0 - 8.\n\/\/ 0 = display off.\nvoid tm1637SetBrightness(char brightness)\n{\n    \/\/ Brightness command:\n    \/\/ 1000 0XXX = display off\n    \/\/ 1000 1BBB = display on, brightness 0-7\n    \/\/ X = don't care\n    \/\/ B = brightness\n    _tm1637Start();\n    _tm1637WriteByte(0x87 + brightness);\n    _tm1637ReadResult();\n    _tm1637Stop();\n}\n<\/pre><\/div>\n\n\n\n<p>For start, read results, write byte and stop, the functions shall be as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\nvoid _tm1637Start(void)\n{\n    _tm1637ClkHigh();\n    _tm1637DioHigh();\n    _tm1637DelayUsec(20);\n    _tm1637DioLow();\n}\n\nvoid _tm1637Stop(void)\n{\n    _tm1637ClkLow();\n    _tm1637DelayUsec(20);\n    _tm1637DioLow();\n    _tm1637DelayUsec(20);\n    _tm1637ClkHigh();\n    _tm1637DelayUsec(20);\n    _tm1637DioHigh();\n}\n\nvoid _tm1637ReadResult(void)\n{\n    _tm1637ClkLow();\n    _tm1637DelayUsec(50);\n    \/\/ while (dio); \/\/ We're cheating here and not actually reading back the response.\n    _tm1637ClkHigh();\n    _tm1637DelayUsec(20);\n    _tm1637ClkLow();\n}\n\nvoid _tm1637WriteByte(unsigned char b)\n{\n    for (int i = 0; i &lt; 8; ++i) {\n        _tm1637ClkLow();\n        if (b &amp; 0x01) {\n            _tm1637DioHigh();\n        }\n        else {\n            _tm1637DioLow();\n        }\n        _tm1637DelayUsec(30);\n        b &gt;&gt;= 1;\n        _tm1637ClkHigh();\n        _tm1637DelayUsec(30);\n    }\n}<\/pre><\/div>\n\n\n\n<p>and finally delay as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void _tm1637DelayUsec(unsigned int i)\n{\ndelayuS(i);\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>You may download the code from this github repository <\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/github.com\/hussamaldean\/Simple_TM1637_Interface_with_STM32\" data-type=\"URL\" data-id=\"https:\/\/github.com\/hussamaldean\/Simple_TM1637_Interface_with_STM32\" target=\"_blank\" rel=\"noreferrer noopener\">Simple TM1637 Interface with STM32<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Demo:<\/h2>\n\n\n\n<figure class=\"wp-block-video\"><video controls preload=\"none\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/02\/IMG_7829.mp4\"><\/video><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall see how to work with TM1637 4-digit 7-segment LED display and use STM32F411 to display the encoder values on the display. In this guide, we will cover the following : TM1637 4-digit 7-segment LED display Connection with STM32 Code Demo 1. TM1637 4-digit 7-segment LED display: Bare 4-digit 7-segment displays\u00a0usually [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-754","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/754"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=754"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/754\/revisions"}],"predecessor-version":[{"id":757,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/754\/revisions\/757"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}