[STM32U5] 【NUCLEO-U575ZI-Q测评】HT1621

[复制链接]
823|1
 楼主| 比神乐 发表于 2023-2-20 19:38 | 显示全部楼层 |阅读模式
今天搞了一下HT1621模块。
DA接PC10,WR接PD9,CS接PF7。
代码:
  1. #include "main.h"

  2. /* Private includes ----------------------------------------------------------*/
  3. /* USER CODE BEGIN Includes */
  4. #define DA_PIN                                GPIO_PIN_10
  5. #define DA_GPIO_PORT                          GPIOC
  6. #define DA_GPIO_CLK_ENABLE()                  __HAL_RCC_GPIOC_CLK_ENABLE()
  7. #define DA_GPIO_CLK_DISABLE()                 __HAL_RCC_GPIOC_CLK_DISABLE()

  8. #define WR_PIN                                GPIO_PIN_9
  9. #define WR_GPIO_PORT                          GPIOD
  10. #define WR_GPIO_CLK_ENABLE()                  __HAL_RCC_GPIOD_CLK_ENABLE()
  11. #define WR_GPIO_CLK_DISABLE()                 __HAL_RCC_GPIOD_CLK_DISABLE()

  12. #define CS_PIN                                GPIO_PIN_7
  13. #define CS_GPIO_PORT                          GPIOF
  14. #define CS_GPIO_CLK_ENABLE()                  __HAL_RCC_GPIOF_CLK_ENABLE()
  15. #define CS_GPIO_CLK_DISABLE()                 __HAL_RCC_GPIOF_CLK_DISABLE()


  16. #define CS_0           HAL_GPIO_WritePin(CS_GPIO_PORT,CS_PIN,GPIO_PIN_RESET)
  17. #define CS_1           HAL_GPIO_WritePin(CS_GPIO_PORT,CS_PIN,GPIO_PIN_SET)
  18. #define WR_0           HAL_GPIO_WritePin(WR_GPIO_PORT,WR_PIN,GPIO_PIN_RESET)
  19. #define WR_1           HAL_GPIO_WritePin(WR_GPIO_PORT,WR_PIN,GPIO_PIN_SET)
  20. #define DA_0           HAL_GPIO_WritePin(DA_GPIO_PORT,DA_PIN,GPIO_PIN_RESET)
  21. #define DA_1           HAL_GPIO_WritePin(DA_GPIO_PORT,DA_PIN,GPIO_PIN_SET)


  22. #define BIAS  0x52   //0b1000 0101 0010  1/3duty 4com

  23. #define SYSDIS  0X00    //0b1000 0000 0000  ???????LCD?????

  24. #define SYSEN 0X02 //0b1000 0000 0010 ???????

  25. #define LCDOFF  0X04 //0b1000 0000 0100  ?LCD??

  26. #define LCDON  0X06 //0b1000 0000 0110  ??LCD??

  27. #define XTAL 0x28  //0b1000 0010 1000 ?????

  28. #define RC256    0X30  //0b1000 0011 0000  ????

  29. #define WDTDIS1   0X0A    //0b1000 0000 1010  ?????

  30. /* USER CODE END Includes */
  31. #define uchar unsigned char

  32. #define uint unsigned int


  33. /* USER CODE BEGIN PV */
  34. static GPIO_InitTypeDef  GPIO_InitStruct;

  35. /* USER CODE END PV */

  36. /* Private function prototypes -----------------------------------------------*/
  37. void SystemClock_Config(void);
  38. static void SystemPower_Config(void);
  39. static void MX_ICACHE_Init(void);
  40. /* USER CODE BEGIN PFP */



  41. unsigned int tmp;

  42. unsigned int n1, n2, n3, n4;

  43. unsigned char Ht1621Tab[]={0x00,0x00,0x00,0x00};

  44. //unsigned char DispTab[]={0x7B,0x12,0x67,0x57,0x1E,0x5D,0x7D,0x13,0x7F,0x5F,0x69,0x45,0x3f, 0x7a, 0x3e};

  45. //          0    1    2    3    4    5    6    7    8    9    C    ?   A     V     H

  46. unsigned char DispTab[]={0x7B,0x12,0x67,0x57,0x1E,0x5D,0x7D,0x13,0x7F,0x5F,0x3F,0x7C,0x69,0x76,0x6D, 0x2D};

  47. //          0    1    2    3    4    5    6    7    8    9    A    b   C     d     E      F






  48. void Ht1621_CS_0() { CS_0;}   // define P2.2 ---> CS

  49. void Ht1621_CS_1() { CS_1;}



  50. void Ht1621_WR_0() { WR_0;} // define P2.1 ---> WR

  51. void Ht1621_WR_1() { WR_1;}



  52. void Ht1621_DO_0() { DA_0;} // define P2.0 ---> DAT

  53. void Ht1621_DO_1() { DA_1;}







  54. unsigned int counter;



  55. //-----------------------------------------------------------------------------------------
  56. //????:Delay()
  57. //?    ?:?????
  58. //-----------------------------------------------------------------------------------------
  59. void Delay(uint  us)  //5,7,9
  60. {
  61.   while(--us);
  62. }

  63. //-----------------------------------------------------------------------------------------
  64. //????:Delayms()
  65. //-----------------------------------------------------------------------------------------
  66. void Delayms(unsigned int ims)
  67. {
  68.   unsigned int i,j;
  69.     for(i=0;i<ims;i++)
  70.       for(j=0;j<65;j++) { Delay(1); }
  71. }

  72. //-----------------------------------------------------------------------------------------
  73. //Ht1621Wr_Data()
  74. //-----------------------------------------------------------------------------------------
  75. void Ht1621Wr_Data(unsigned char Data,unsigned char cnt)
  76. {
  77.   unsigned char i;
  78.   for (i=0;i<cnt;i++)
  79.    {
  80.      Ht1621_WR_0();
  81.      HAL_Delay(1);
  82.      if((Data & 0x80)==0x80) {Ht1621_DO_1();HAL_Delay(10);}
  83.      else {Ht1621_DO_0();HAL_Delay(10);}
  84.      Ht1621_WR_1();
  85.      HAL_Delay(1);
  86.      Data<<=1;
  87.    }
  88. }
  89. //-----------------------------------------------------------------------------------------
  90. //void Ht1621WrCmd(uchar Cmd)
  91. //-----------------------------------------------------------------------------------------
  92. void Ht1621WrCmd(unsigned char Cmd)
  93. {
  94.    Ht1621_CS_0();
  95.    HAL_Delay(1);
  96.    Ht1621Wr_Data(0x80,4);          //??????100
  97.    Ht1621Wr_Data(Cmd,8);           //??????
  98.    Ht1621_CS_1();
  99.    HAL_Delay(1);
  100. }
  101. //-----------------------------------------------------------------------------------------
  102. //void Ht1621WrOneData(uchar Addr,uchar Data)
  103. //-----------------------------------------------------------------------------------------
  104. void Ht1621WrOneData(unsigned char Addr,unsigned char Data)
  105. {
  106.   Ht1621_CS_0();
  107.   Ht1621Wr_Data(0xa0,3);  //??????101
  108.   Ht1621Wr_Data(Addr<<2,6); //??????
  109.   Ht1621Wr_Data(Data,4); //???????? 7  6  5  4
  110.   Ht1621_CS_1();
  111. }
  112. //-----------------------------------------------------------------------------------------
  113. //void Ht1621WrAllData()
  114. //-----------------------------------------------------------------------------------------
  115. void Ht1621WrAllData(unsigned char Addr,unsigned char *p,unsigned char cnt)
  116. {
  117.   unsigned char i;
  118.   Ht1621_CS_0();
  119.   Ht1621Wr_Data(0xa0,3); //??????101
  120.   Ht1621Wr_Data(Addr<<2,6); //??????
  121.   for (i=0;i<cnt;i++)
  122.    {
  123.     Ht1621Wr_Data(*p,8); //????
  124.     p++;
  125.    }
  126.   Ht1621_CS_1();
  127. }

  128. //-----------------------------------------------------------------------------------------
  129. //void Ht1621_Init(void)
  130. //-----------------------------------------------------------------------------------------
  131. void Ht1621_Init(void)
  132. {
  133.    Ht1621WrCmd(BIAS);
  134.    Ht1621WrCmd(RC256);             //???????
  135.    //Ht1621WrCmd(XTAL);             //???????
  136.    Ht1621WrCmd(SYSDIS);
  137.    Ht1621WrCmd(WDTDIS1);
  138.    Ht1621WrCmd(SYSEN);
  139.    Ht1621WrCmd(LCDON);
  140. }

  141. //-----------------------------------------------------------------------------------------
  142. //DISPLAY
  143. //-----------------------------------------------------------------------------------------
  144. void Display(void)
  145. {
  146.    unsigned char com;
  147.    com = 0;
  148.    if((DispTab[n1]&0x01) == 0x01){ com = com + 0x10;}
  149.    if((DispTab[n2]&0x01) == 0x01){ com = com + 0x20;}
  150.    if((DispTab[n3]&0x01) == 0x01){ com = com + 0x40;}
  151.    if((DispTab[n4]&0x01) == 0x01){ com = com + 0x80;}
  152.    Ht1621WrOneData(0 , com);
  153.    com = 0;
  154.    if((DispTab[n1]&0x02) == 0x02){ com = com + 0x10;}
  155.    if((DispTab[n2]&0x02) == 0x02){ com = com + 0x20;}
  156.    if((DispTab[n3]&0x02) == 0x02){ com = com + 0x40;}
  157.    if((DispTab[n4]&0x02) == 0x02){ com = com + 0x80;}
  158.    Ht1621WrOneData(1 , com);
  159.    com = 0;
  160.    if((DispTab[n1]&0x10) == 0x10){ com = com + 0x10;}
  161.    if((DispTab[n2]&0x10) == 0x10){ com = com + 0x20;}
  162.    if((DispTab[n3]&0x10) == 0x10){ com = com + 0x40;}
  163.    if((DispTab[n4]&0x10) == 0x10){ com = com + 0x80;}
  164.    Ht1621WrOneData(2 , com);
  165.    com = 0;
  166.    if((DispTab[n1]&0x40) == 0x40){ com = com + 0x10;}
  167.    if((DispTab[n2]&0x40) == 0x40){ com = com + 0x20;}
  168.    if((DispTab[n3]&0x40) == 0x40){ com = com + 0x40;}
  169.    if((DispTab[n4]&0x40) == 0x40){ com = com + 0x80;}
  170.    Ht1621WrOneData(3 , com);
  171.    com = 0;
  172.    if((DispTab[n1]&0x20) == 0x20){ com = com + 0x10;}
  173.    if((DispTab[n2]&0x20) == 0x20){ com = com + 0x20;}
  174.    if((DispTab[n3]&0x20) == 0x20){ com = com + 0x40;}
  175.    if((DispTab[n4]&0x20) == 0x20){ com = com + 0x80;}
  176.    Ht1621WrOneData(4 , com);
  177.    com = 0;
  178.    if((DispTab[n1]&0x08) == 0x08){ com = com + 0x10;}
  179.    if((DispTab[n2]&0x08) == 0x08){ com = com + 0x20;}
  180.    if((DispTab[n3]&0x08) == 0x08){ com = com + 0x40;}
  181.    if((DispTab[n4]&0x08) == 0x08){ com = com + 0x80;}
  182.    Ht1621WrOneData(5 , com);
  183.    com = 0;
  184.    if((DispTab[n1]&0x04) == 0x04){ com = com + 0x10;}
  185.    if((DispTab[n2]&0x04) == 0x04){ com = com + 0x20;}
  186.    if((DispTab[n3]&0x04) == 0x04){ com = com + 0x40;}
  187.    if((DispTab[n4]&0x04) == 0x04){ com = com + 0x80;}
  188.    Ht1621WrOneData(6 , com);
  189. }

  190. //-----------------------------------------------------------------------------------------
  191. //Display_lcd_dot
  192. //-----------------------------------------------------------------------------------------
  193. void Display_lcd_dot(void)
  194. {
  195.    Ht1621WrOneData(7 , 0x50);

  196. }

  197. //-----------------------------------------------------------------------------------------
  198. //convertor()
  199. //-----------------------------------------------------------------------------------------

  200. void data_convertor(unsigned long adc_value)
  201. {
  202.     tmp=adc_value;         //adc

  203.     n4=tmp/1000;

  204.     n3=tmp%1000/100;

  205.     n2=tmp%100/10;
  206.                  // 4
  207.     n1=tmp%10;       //display "C"

  208. }



  209. /* Private typedef -----------------------------------------------------------*/
  210. /* USER CODE BEGIN PTD */

  211. /* USER CODE END PTD */

  212. /* Private define ------------------------------------------------------------*/
  213. /* USER CODE BEGIN PD */

  214. /* USER CODE END PD */

  215. /* Private macro -------------------------------------------------------------*/
  216. /* USER CODE BEGIN PM */

  217. /* USER CODE END PM */

  218. /* Private variables ---------------------------------------------------------*/




  219. /* USER CODE END PFP */

  220. /* Private user code ---------------------------------------------------------*/
  221. /* USER CODE BEGIN 0 */

  222. /* USER CODE END 0 */

  223. /**
  224.   * [url=home.php?mod=space&uid=247401]@brief[/url]  The application entry point.
  225.   * @retval int
  226.   */
  227. int main(void)
  228. {
  229.   /* USER CODE BEGIN 1 */
  230.   /* STM32U5xx HAL library initialization:
  231.        - Configure the Flash prefetch
  232.        - Configure the Systick to generate an interrupt each 1 msec
  233.        - Set NVIC Group Priority to 3
  234.        - Low Level Initialization
  235.      */
  236.   /* USER CODE END 1 */

  237.   /* MCU Configuration--------------------------------------------------------*/

  238.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  239.   HAL_Init();

  240.   /* USER CODE BEGIN Init */

  241.   /* USER CODE END Init */

  242.   /* Configure the system clock */
  243.   SystemClock_Config();

  244.   /* Configure the System Power */
  245.   SystemPower_Config();

  246.   /* USER CODE BEGIN SysInit */

  247.   /* USER CODE END SysInit */

  248.   /* Initialize all configured peripherals */
  249.   MX_ICACHE_Init();
  250.   /* USER CODE BEGIN 2 */

  251.    /* -1- Enable GPIO Clock (to be able to program the configuration registers) */
  252.   LED1_GPIO_CLK_ENABLE();
  253.   LED2_GPIO_CLK_ENABLE();
  254.         DA_GPIO_CLK_ENABLE();
  255.         WR_GPIO_CLK_ENABLE();
  256.         CS_GPIO_CLK_ENABLE();
  257.   /* -2- Configure IO in output push-pull mode to drive external LEDs */
  258.   GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
  259.   GPIO_InitStruct.Pull  = GPIO_NOPULL;
  260.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  261.        
  262.         GPIO_InitStruct.Pin = LED1_PIN;
  263.   HAL_GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStruct);
  264.   GPIO_InitStruct.Pin = LED2_PIN;
  265.   HAL_GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStruct);

  266.   /* USER CODE END 2 */
  267.         GPIO_InitStruct.Pin = CS_PIN;
  268.   HAL_GPIO_Init(CS_GPIO_PORT, &GPIO_InitStruct);
  269.   GPIO_InitStruct.Pin = WR_PIN;
  270.   HAL_GPIO_Init(WR_GPIO_PORT, &GPIO_InitStruct);
  271.         GPIO_InitStruct.Pin = DA_PIN;
  272.   HAL_GPIO_Init(DA_GPIO_PORT, &GPIO_InitStruct);
  273.        
  274.        
  275.   /* Infinite loop */
  276.         counter = 235;
  277.         Ht1621_Init();        //?????LCD
  278.         HAL_Delay(1000);           //??????
  279.         Ht1621WrAllData(0,Ht1621Tab,16);// Clear LCD display
  280.         Ht1621_Init();   
  281.         data_convertor(counter);
  282.         Display();
  283.   /* USER CODE BEGIN WHILE */
  284.   while (1)
  285.   {
  286.     /* USER CODE END WHILE */
  287. //                counter = 235;
  288. //                Ht1621_Init();        //?????LCD
  289. //                Delay(1000);           //??????
  290. //                Ht1621WrAllData(0,Ht1621Tab,16);// Clear LCD display
  291. //                Ht1621_Init();   
  292. //                data_convertor(counter);
  293. //                Display();
  294.     /* USER CODE BEGIN 3 */
  295. //    HAL_GPIO_TogglePin(DA_GPIO_PORT,GPIO_PIN_10);
  296. //                Delay(1000);
  297.     /* Insert delay 100 ms */
  298. //    HAL_GPIO_TogglePin(WR_GPIO_PORT,WR_PIN);
  299. //                HAL_Delay(1000);
  300.                
  301.   }
  302.   /* USER CODE END 3 */
  303. }

  304. /**
  305.   * @brief System Clock Configuration
  306.   * @retval None
  307.   */
  308. void SystemClock_Config(void)
  309. {
  310.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  311.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  312.   /** Configure the main internal regulator output voltage
  313.   */
  314.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  315.   {
  316.     Error_Handler();
  317.   }

  318.   /** Initializes the CPU, AHB and APB busses clocks
  319.   */
  320.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  321.   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  322.   RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  323.   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
  324.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  325.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  326.   RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
  327.   RCC_OscInitStruct.PLL.PLLM = 1;
  328.   RCC_OscInitStruct.PLL.PLLN = 80;
  329.   RCC_OscInitStruct.PLL.PLLP = 2;
  330.   RCC_OscInitStruct.PLL.PLLQ = 2;
  331.   RCC_OscInitStruct.PLL.PLLR = 2;
  332.   RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_0;
  333.   RCC_OscInitStruct.PLL.PLLFRACN = 0;
  334.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  335.   {
  336.     Error_Handler();
  337.   }

  338.   /** Initializes the CPU, AHB and APB busses clocks
  339.   */
  340.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  341.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  342.                               |RCC_CLOCKTYPE_PCLK3;
  343.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  344.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  345.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  346.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  347.   RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

  348.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  349.   {
  350.     Error_Handler();
  351.   }
  352. }

  353. /**
  354.   * @brief Power Configuration
  355.   * @retval None
  356.   */
  357. static void SystemPower_Config(void)
  358. {

  359.   /*
  360.    * Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral
  361.    */
  362.   HAL_PWREx_DisableUCPDDeadBattery();

  363.   /*
  364.    * Switch to SMPS regulator instead of LDO
  365.    */
  366.   if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK)
  367.   {
  368.     Error_Handler();
  369.   }
  370. }

  371. /**
  372.   * @brief ICACHE Initialization Function
  373.   * @param None
  374.   * @retval None
  375.   */
  376. static void MX_ICACHE_Init(void)
  377. {

  378.   /* USER CODE BEGIN ICACHE_Init 0 */

  379.   /* USER CODE END ICACHE_Init 0 */

  380.   /* USER CODE BEGIN ICACHE_Init 1 */

  381.   /* USER CODE END ICACHE_Init 1 */

  382.   /** Enable instruction cache in 1-way (direct mapped cache)
  383.   */
  384.   if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK)
  385.   {
  386.     Error_Handler();
  387.   }
  388.   if (HAL_ICACHE_Enable() != HAL_OK)
  389.   {
  390.     Error_Handler();
  391.   }
  392.   /* USER CODE BEGIN ICACHE_Init 2 */

  393.   /* USER CODE END ICACHE_Init 2 */

  394. }

  395. /* USER CODE BEGIN 4 */
  396. /* USER CODE END 4 */

  397. /**
  398.   * @brief  This function is executed in case of error occurrence.
  399.   * @retval None
  400.   */
  401. void Error_Handler(void)
  402. {
  403.   /* USER CODE BEGIN Error_Handler_Debug */
  404.   /* User can add his own implementation to report the HAL error return state */
  405.   while(1)
  406.   {
  407.   }
  408.   /* USER CODE END Error_Handler_Debug */
  409. }

  410. #ifdef  USE_FULL_ASSERT
  411. /**
  412.   * @brief  Reports the name of the source file and the source line number
  413.   *         where the assert_param error has occurred.
  414.   * @param  file: pointer to the source file name
  415.   * @param  line: assert_param error line source number
  416.   * @retval None
  417.   */
  418. void assert_failed(uint8_t *file, uint32_t line)
  419. {
  420.   /* USER CODE BEGIN 6 */
  421.   /* User can add his own implementation to report the file name and line number,
  422.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  423.   /* Infinite loop */
  424.   while (1)
  425.   {
  426.   }

  427.   /* USER CODE END 6 */
  428. }
  429. #endif /* USE_FULL_ASSERT */
效果图:
5.jpg
Henryko 发表于 2024-1-12 16:17 | 显示全部楼层
这个屏幕走的什么协议啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

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