[STM32L4] STM32L4R5ZIT6驱动TLC5615

[复制链接]
459|5
Tom952331 发表于 2026-5-16 15:09 | 显示全部楼层 |阅读模式
今天用STM32L4R5ZIT6搞了一下TLC5615.

代码:
  1. #include "main.h"

  2. /* Private includes ----------------------------------------------------------*/
  3. /* USER CODE BEGIN Includes */
  4. #define uchar unsigned char
  5. #define uint  unsigned int
  6. /* Private typedef -----------------------------------------------------------*/
  7. /* USER CODE BEGIN PTD */
  8. #define Max7219_pinCLK0        HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_RESET)
  9. #define Max7219_pinCLK1        HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET)
  10. #define Max7219_pinCS0               HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_RESET)
  11. #define Max7219_pinCS1         HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_SET)
  12. #define Max7219_pinDIN0             HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET)
  13. #define Max7219_pinDIN1        HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET)
  14. #define cs0                                                                                HAL_GPIO_WritePin(GPIOD,GPIO_PIN_4,GPIO_PIN_RESET)
  15. #define cs1                                                                                HAL_GPIO_WritePin(GPIOD,GPIO_PIN_4,GPIO_PIN_SET)
  16. #define clk0                                                                                HAL_GPIO_WritePin(GPIOD,GPIO_PIN_5,GPIO_PIN_RESET)
  17. #define clk1                                                                                HAL_GPIO_WritePin(GPIOD,GPIO_PIN_5,GPIO_PIN_SET)
  18. #define din0                                                                                HAL_GPIO_WritePin(GPIOD,GPIO_PIN_6,GPIO_PIN_RESET)
  19. #define din1                                                                                HAL_GPIO_WritePin(GPIOD,GPIO_PIN_6,GPIO_PIN_SET)
  20. /* USER CODE END Includes */
  21. uchar const   disp1[38][8]={
  22. {0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//0
  23. {0x10,0x18,0x14,0x10,0x10,0x10,0x10,0x10},//1
  24. {0x7E,0x2,0x2,0x7E,0x40,0x40,0x40,0x7E},//2
  25. {0x3E,0x2,0x2,0x3E,0x2,0x2,0x3E,0x0},//3
  26. {0x8,0x18,0x28,0x48,0xFE,0x8,0x8,0x8},//4
  27. {0x3C,0x20,0x20,0x3C,0x4,0x4,0x3C,0x0},//5
  28. {0x3C,0x20,0x20,0x3C,0x24,0x24,0x3C,0x0},//6
  29. {0x3E,0x22,0x4,0x8,0x8,0x8,0x8,0x8},//7
  30. {0x0,0x3E,0x22,0x22,0x3E,0x22,0x22,0x3E},//8
  31. {0x3E,0x22,0x22,0x3E,0x2,0x2,0x2,0x3E},//9
  32. {0x8,0x14,0x22,0x3E,0x22,0x22,0x22,0x22},//A
  33. {0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C,0x0},//B
  34. {0x3C,0x40,0x40,0x40,0x40,0x40,0x3C,0x0},//C
  35. {0x7C,0x42,0x42,0x42,0x42,0x42,0x7C,0x0},//D
  36. {0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x7C},//E
  37. {0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x40},//F
  38. {0x3C,0x40,0x40,0x40,0x40,0x44,0x44,0x3C},//G
  39. {0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0x44},//H
  40. {0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x7C},//I
  41. {0x3C,0x8,0x8,0x8,0x8,0x8,0x48,0x30},//J
  42. {0x0,0x24,0x28,0x30,0x20,0x30,0x28,0x24},//K
  43. {0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x7C},//L
  44. {0x81,0xC3,0xA5,0x99,0x81,0x81,0x81,0x81},//M
  45. {0x0,0x42,0x62,0x52,0x4A,0x46,0x42,0x0},//N
  46. {0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//O
  47. {0x3C,0x22,0x22,0x22,0x3C,0x20,0x20,0x20},//P
  48. {0x1C,0x22,0x22,0x22,0x22,0x26,0x22,0x1D},//Q
  49. {0x3C,0x22,0x22,0x22,0x3C,0x24,0x22,0x21},//R
  50. {0x0,0x1E,0x20,0x20,0x3E,0x2,0x2,0x3C},//S
  51. {0x0,0x3E,0x8,0x8,0x8,0x8,0x8,0x8},//T
  52. {0x42,0x42,0x42,0x42,0x42,0x42,0x22,0x1C},//U
  53. {0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18},//V
  54. {0x0,0x49,0x49,0x49,0x49,0x2A,0x1C,0x0},//W
  55. {0x0,0x41,0x22,0x14,0x8,0x14,0x22,0x41},//X
  56. {0x41,0x22,0x14,0x8,0x8,0x8,0x8,0x8},//Y
  57. {0x0,0x7F,0x2,0x4,0x8,0x10,0x20,0x7F},//Z
  58. {0x8,0x7F,0x49,0x49,0x7F,0x8,0x8,0x8},//?D
  59. {0xFE,0xBA,0x92,0xBA,0x92,0x9A,0xBA,0xFE},//1ú
  60. };
  61. /* Private typedef -----------------------------------------------------------*/
  62. /* USER CODE BEGIN PTD */
  63. void Delay_xms(uint x)
  64. {
  65. uint i,j;
  66. for(i=0;i<x;i++)
  67.   for(j=0;j<112;j++);
  68. }
  69. //--------------------------------------------
  70. //1|?ü£o?òMAX7219(U3)D′è?×??ú
  71. //è??ú2?êy£oDATA
  72. //3??ú2?êy£o?T
  73. //?μ?÷£o
  74. void Write_Max7219_byte(uchar DATA)         
  75. {
  76.             uchar i;   
  77.                 Max7219_pinCS0;               
  78.             for(i=8;i>=1;i--)
  79.           {                  
  80.             Max7219_pinCLK0;
  81.                         if((DATA&0x80)==0x80)
  82.             //Max7219_pinDIN=DATA&0x80;
  83.                          Max7219_pinDIN1;else Max7219_pinDIN0;
  84.             DATA=DATA<<1;
  85.             Max7219_pinCLK1;
  86.            }                                 
  87. }
  88. //-------------------------------------------
  89. //1|?ü£o?òMAX7219D′è?êy?Y
  90. //è??ú2?êy£oaddress?¢dat
  91. //3??ú2?êy£o?T
  92. //?μ?÷£o
  93. void Write_Max7219(uchar address,uchar dat)
  94. {
  95.      Max7219_pinCS0;
  96.          Write_Max7219_byte(address);           //D′è?μ??·£??′êy??1ü±ào?
  97.      Write_Max7219_byte(dat);               //D′è?êy?Y£??′êy??1ü??ê?êy×?
  98.          Max7219_pinCS1;                        
  99. }

  100. void Init_MAX7219(void)
  101. {
  102. Write_Max7219(0x09, 0x00);       //ò???·?ê?£oBCD??
  103. Write_Max7219(0x0a, 0x03);       //áá?è
  104. Write_Max7219(0x0b, 0x07);       //é¨?è???T£?8??êy??1ü??ê?
  105. Write_Max7219(0x0c, 0x01);       //μ?μ??£ê?£o0£???í¨?£ê?£o1
  106. Write_Max7219(0x0f, 0x00);       //??ê?2aê?£o1£?2aê??áê?£??y3£??ê?£o0
  107. }
  108. /* USER CODE END PTD */

  109. /* Private define ------------------------------------------------------------*/
  110. /* USER CODE BEGIN PD */
  111. /* USER CODE END PD */

  112. /* Private macro -------------------------------------------------------------*/
  113. /* USER CODE BEGIN PM */

  114. /* USER CODE END PM */

  115. /* Private variables ---------------------------------------------------------*/

  116. /* USER CODE BEGIN PV */

  117. /* USER CODE END PV */

  118. /* Private function prototypes -----------------------------------------------*/
  119. void SystemClock_Config(void);
  120. static void MX_GPIO_Init(void);
  121. /* USER CODE BEGIN PFP */

  122. /* USER CODE END PFP */
  123. void delay()
  124. {
  125.         int i = 300;
  126.         while(i--);
  127. }

  128. void DA_Conver(unsigned int DAValue)
  129. {
  130.       unsigned char i;

  131.       DAValue <<= 6;

  132.       cs0;                             // 片选DA芯片
  133.       clk0;                             //   在以下12个时钟周期内,每当在上升沿的
  134.                                           //   数据被锁存,形成DA输出。在前10个时钟
  135.       for(i = 0; i < 12; i++)              //   内输入的是10位DA数据,后两个时钟周期
  136.       {   
  137.                                         if(DAValue & 0x8000)//   为填充字节
  138.                                                 din1;
  139.                                         else
  140.                                                 din0;
  141.           //din = (bit)(DAValue & 0x8000);   //
  142.           clk1;                         //
  143.           DAValue <<= 1;                   //
  144.           clk0;
  145.       }

  146.       cs1;                              // CS的上升沿和下降沿只有在clk为低的时候
  147.       clk0;                             // 才有效
  148. }
  149. /* Private user code ---------------------------------------------------------*/
  150. /* USER CODE BEGIN 0 */

  151. /* USER CODE END 0 */

  152. /**
  153.   * [url=/u/brief]@brief[/url]  The application entry point.
  154.   * @retval int
  155.   */
  156. int main(void)
  157. {
  158.   /* USER CODE BEGIN 1 */
  159.         uchar i,j=0;
  160.   /* USER CODE END 1 */
  161.         long DAValue = 0;
  162.         delay();
  163.         DAValue = 0;
  164.   /* MCU Configuration--------------------------------------------------------*/

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

  167.   /* USER CODE BEGIN Init */

  168.   /* USER CODE END Init */

  169.   /* Configure the system clock */
  170.   SystemClock_Config();

  171.   /* USER CODE BEGIN SysInit */

  172.   /* USER CODE END SysInit */

  173.   /* Initialize all configured peripherals */
  174.   MX_GPIO_Init();
  175.   /* USER CODE BEGIN 2 */

  176.   /* USER CODE END 2 */
  177.         Delay_xms(500);
  178.         Init_MAX7219();
  179.        
  180.   /* Infinite loop */
  181.   /* USER CODE BEGIN WHILE */
  182.   while (1)
  183.   {
  184.     /* USER CODE END WHILE */
  185.                 DA_Conver(DAValue); //加上滤波后,就可以形成模拟输出
  186.                 delay();
  187.                 DAValue++;
  188.                 if(DAValue==512)
  189.                         DAValue = 0;
  190.     /* USER CODE BEGIN 3 */
  191.   }
  192.   /* USER CODE END 3 */
  193. }

  194. /**
  195.   * @brief System Clock Configuration
  196.   * @retval None
  197.   */
  198. void SystemClock_Config(void)
  199. {
  200.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  201.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  202.   /** Configure the main internal regulator output voltage
  203.   */
  204.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  205.   {
  206.     Error_Handler();
  207.   }
  208.   /** Initializes the RCC Oscillators according to the specified parameters
  209.   * in the RCC_OscInitTypeDef structure.
  210.   */
  211.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  212.   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  213.   RCC_OscInitStruct.MSICalibrationValue = 0;
  214.   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  215.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  216.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  217.   {
  218.     Error_Handler();
  219.   }
  220.   /** Initializes the CPU, AHB and APB buses clocks
  221.   */
  222.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  223.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  224.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  225.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  226.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  227.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  228.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  229.   {
  230.     Error_Handler();
  231.   }
  232. }

  233. /**
  234.   * @brief GPIO Initialization Function
  235.   * @param None
  236.   * @retval None
  237.   */
  238. static void MX_GPIO_Init(void)
  239. {
  240.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  241.   /* GPIO Ports Clock Enable */
  242.   __HAL_RCC_GPIOC_CLK_ENABLE();
  243.         __HAL_RCC_GPIOD_CLK_ENABLE();
  244.   /*Configure GPIO pin Output Level */
  245.   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);

  246.   /*Configure GPIO pins : PC13 PC14 PC15 */
  247.   GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  248.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  249.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  250.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  251.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  252.        
  253.         GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;
  254.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  255.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  256.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  257.   HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  258. }

  259. /* USER CODE BEGIN 4 */

  260. /* USER CODE END 4 */

  261. /**
  262.   * @brief  This function is executed in case of error occurrence.
  263.   * @retval None
  264.   */
  265. void Error_Handler(void)
  266. {
  267.   /* USER CODE BEGIN Error_Handler_Debug */
  268.   /* User can add his own implementation to report the HAL error return state */
  269.   __disable_irq();
  270.   while (1)
  271.   {
  272.   }
  273.   /* USER CODE END Error_Handler_Debug */
  274. }

  275. #ifdef  USE_FULL_ASSERT
  276. /**
  277.   * @brief  Reports the name of the source file and the source line number
  278.   *         where the assert_param error has occurred.
  279.   * @param  file: pointer to the source file name
  280.   * @param  line: assert_param error line source number
  281.   * @retval None
  282.   */
  283. void assert_failed(uint8_t *file, uint32_t line)
  284. {
  285.   /* USER CODE BEGIN 6 */
  286.   /* User can add his own implementation to report the file name and line number,
  287.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  288.   /* USER CODE END 6 */
  289. }
  290. #endif /* USE_FULL_ASSERT */

  291. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
效果图:
4.jpg 5.jpg





pacer81 发表于 2026-5-16 20:57 | 显示全部楼层
效果看着不错,上面这个示波器和下面的板子是连在一起的吗
xch 发表于 2026-5-16 22:14 | 显示全部楼层
为啥外挂一个DAC? 内置的不够用?
 楼主| Tom952331 发表于 2026-5-17 06:20 | 显示全部楼层
pacer81 发表于 2026-5-16 20:57
效果看着不错,上面这个示波器和下面的板子是连在一起的吗

是的。
 楼主| Tom952331 发表于 2026-5-17 06:21 | 显示全部楼层
xch 发表于 2026-5-16 22:14
为啥外挂一个DAC? 内置的不够用?

学习TLC5615.再者,找不到STM32L4R5的例程。

评论

xch
摸着石头过河  发表于 2026-5-17 21:40
您需要登录后才可以回帖 登录 | 注册

本版积分规则

130

主题

585

帖子

0

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