STM32-TM1637-MAIN

by Marwen Maghrebi
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "TM1637.h"
/* USER CODE END Includes */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

// Module connection pins (GPIO Pins)
#define CLK_PORT GPIOB
#define CLK_PIN GPIO_PIN_10
#define DIO_PORT GPIOB
#define DIO_PIN GPIO_PIN_11
 // The amount of time (in milliseconds) between tests
#define TEST_DELAY   250

const uint8_t SEG_DONE[] = {
        SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,           // d
        SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,   // O
        SEG_C | SEG_E | SEG_G,                           // n
        SEG_A | SEG_D | SEG_E | SEG_F | SEG_G            //e
};TM1637Display display;


int main(void)
{

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* Configure the system clock */
  SystemClock_Config();
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */
  TM1637Display_init(&display, CLK_PORT, CLK_PIN, DIO_PORT, DIO_PIN, DEFAULT_BIT_DELAY);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
      int k;
      uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
      uint8_t blank[] = { 0x00, 0x00, 0x00, 0x00 };

      TM1637Display_setBrightness(&display, 0x0f, true);

      // All segments onr t
      TM1637Display_setSegments(&display, data, 4, 0);
      HAL_Delay(TEST_DELAY);
       // Selectively set different digits
      data[0] = TM1637Display_encodeDigit(0);
      data[1] = TM1637Display_encodeDigit(1);
      data[2] = TM1637Display_encodeDigit(2);
      data[3] = TM1637Display_encodeDigit(3);
      TM1637Display_setSegments(&display, data, 4, 0);
      HAL_Delay(TEST_DELAY);

      TM1637Display_clear(&display);
      TM1637Display_setSegments(&display, data+2, 2, 2);
      HAL_Delay(TEST_DELAY);

      TM1637Display_clear(&display);
      TM1637Display_setSegments(&display, data+2, 2, 1);
      HAL_Delay(TEST_DELAY);

      TM1637Display_clear(&display);
      TM1637Display_setSegments(&display, data+1, 3, 1);
      HAL_Delay(TEST_DELAY);

      // Show decimal numbers with/without leading zeros
      TM1637Display_showNumberDec(&display, 0, false, 4, 0); // Expect: ___0
      HAL_Delay(TEST_DELAY);
      TM1637Display_showNumberDec(&display, 0, true, 4, 0);  // Expect: 0000
      HAL_Delay(TEST_DELAY);
      TM1637Display_showNumberDec(&display, 1, false, 4, 0); // Expect: ___1
      HAL_Delay(TEST_DELAY);
      TM1637Display_showNumberDec(&display, 1, true, 4, 0);  // Expect: 0001
      HAL_Delay(TEST_DELAY);
      TM1637Display_showNumberDec(&display, 301, false, 4, 0); // Expect: _301
      HAL_Delay(TEST_DELAY);
      TM1637Display_showNumberDec(&display, 301, true, 4, 0); // Expect: 0301
      HAL_Delay(TEST_DELAY);

      TM1637Display_clear(&display);
      TM1637Display_showNumberDec(&display, 14, false, 2, 1); // Expect: *14*
      HAL_Delay(TEST_DELAY);

      TM1637Display_clear(&display);
      TM1637Display_showNumberDec(&display, 4, true, 2, 2);  // Expect: __04
      HAL_Delay(TEST_DELAY);

      TM1637Display_showNumberDec(&display, -1, false, 4, 0);  // Expect: __-1
      HAL_Delay(TEST_DELAY);
      TM1637Display_showNumberDec(&display, -12, false, 4, 0);        // Expect: _-12
      HAL_Delay(TEST_DELAY);
      TM1637Display_showNumberDec(&display, -999, false, 4, 0);       // Expect: -999
      HAL_Delay(TEST_DELAY);

      TM1637Display_clear(&display);
      TM1637Display_showNumberDec(&display, -5, false, 3, 0); // Expect: *-5*
      HAL_Delay(TEST_DELAY);

      TM1637Display_showNumberHexEx(&display, 0xf1af, 0, false, 4, 0);        // Expect: f1Af
      HAL_Delay(TEST_DELAY);
      TM1637Display_showNumberHexEx(&display, 0x2c, 0, false, 4, 0);          // Expect: __2C
      HAL_Delay(TEST_DELAY);
      TM1637Display_showNumberHexEx(&display, 0xd1, 0, true, 4, 0); // Expect: 00d1
      HAL_Delay(TEST_DELAY);

      TM1637Display_clear(&display);
      TM1637Display_showNumberHexEx(&display, 0xd1, 0, true, 2, 0); // Expect: d1__
      HAL_Delay(TEST_DELAY);

      // Run through all the dots
      for(k=0; k <= 4; k++) {
          TM1637Display_showNumberDecEx(&display, 0, (0x80 >> k), true, 4, 0);
          HAL_Delay(TEST_DELAY);
      }

      // Brightness Test
      for(k = 0; k < 4; k++)
          data[k] = 0xff;
      for(k = 0; k < 7; k++) {
          TM1637Display_setBrightness(&display, k, true);
          TM1637Display_setSegments(&display, data, 4, 0);
          HAL_Delay(TEST_DELAY);
      }

      // On/Off test
      for(k = 0; k < 4; k++) {
          TM1637Display_setBrightness(&display, 7, false);  // Turn off
          TM1637Display_setSegments(&display, data, 4, 0);
          HAL_Delay(TEST_DELAY);
          TM1637Display_setBrightness(&display, 7, true); // Turn on
          TM1637Display_setSegments(&display, data, 4, 0);
          HAL_Delay(TEST_DELAY);
      }

      // Done!
      TM1637Display_setSegments(&display, SEG_DONE, 4, 0);
      while(1);
  }
  /* USER CODE END 3 */
}
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00