[STM32F3] STM32F303ZET6驱动TLC5615

[复制链接]
12078|0
 楼主| 比神乐 发表于 2023-4-25 18:49 | 显示全部楼层 |阅读模式
用获奖的板子搞了一下TLC5615,CS接PC8,SCLK接PC9,DIN接PC10。
代码:
  1. #include "main.h"

  2. /* Private includes ----------------------------------------------------------*/
  3. /* USER CODE BEGIN Includes */
  4. #define uchar unsigned char
  5. #define uint  unsigned int
  6. /* USER CODE END Includes */
  7. #define CS_0       HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET)
  8. #define CS_1       HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET)
  9. #define SCLK_0     HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_RESET)
  10. #define SCLK_1     HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_SET)
  11. #define DIN_0      HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_RESET)
  12. #define DIN_1      HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_SET)
  13. /* Private typedef -----------------------------------------------------------*/
  14. /* USER CODE BEGIN PTD */

  15. /* USER CODE END PTD */

  16. /* Private define ------------------------------------------------------------*/
  17. /* USER CODE BEGIN PD */

  18. /* USER CODE END PD */

  19. /* Private macro -------------------------------------------------------------*/
  20. /* USER CODE BEGIN PM */
  21. void write_5615(uint da)
  22. {
  23.    uchar i;
  24.    CS_1;
  25.    SCLK_0;
  26.    CS_0;
  27.    //da<<=6;//有效位位10位去掉没用的6位
  28.    da=da&0x03ff;
  29.    for(i=0;i<12;i++)
  30.    {
  31.      if((da&0x0200)==0X0200)//送时是10位两个0是后来加的在此处加
  32.       DIN_1;
  33.      else
  34.       DIN_0;
  35.      SCLK_1;
  36.      da<<=1;
  37.      SCLK_0;
  38.      
  39.     }
  40.     SCLK_0;
  41.     CS_1;
  42.     HAL_Delay(1);
  43. }
  44. /* USER CODE END PM */

  45. /* Private variables ---------------------------------------------------------*/

  46. /* USER CODE BEGIN PV */

  47. /* USER CODE END PV */

  48. /* Private function prototypes -----------------------------------------------*/
  49. void SystemClock_Config(void);
  50. static void MX_GPIO_Init(void);
  51. /* USER CODE BEGIN PFP */

  52. /* USER CODE END PFP */

  53. /* Private user code ---------------------------------------------------------*/
  54. /* USER CODE BEGIN 0 */

  55. /* USER CODE END 0 */

  56. /**
  57.   * [url=home.php?mod=space&uid=247401]@brief[/url]  The application entry point.
  58.   * @retval int
  59.   */
  60. int main(void)
  61. {
  62.   /* USER CODE BEGIN 1 */

  63.   /* USER CODE END 1 */
  64.         uint i=30;
  65.   /* MCU Configuration--------------------------------------------------------*/

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

  68.   /* USER CODE BEGIN Init */

  69.   /* USER CODE END Init */

  70.   /* Configure the system clock */
  71.   SystemClock_Config();

  72.   /* USER CODE BEGIN SysInit */

  73.   /* USER CODE END SysInit */

  74.   /* Initialize all configured peripherals */
  75.   MX_GPIO_Init();
  76.   /* USER CODE BEGIN 2 */

  77.   /* USER CODE END 2 */

  78.   /* Infinite loop */
  79.   /* USER CODE BEGIN WHILE */
  80.   while (1)
  81.   {
  82.     /* USER CODE END WHILE */
  83.                 i+=10;
  84.                 if(i==1020)
  85.                 i=0;
  86.        
  87.     write_5615(i);
  88.                 HAL_Delay(1);
  89.     /* USER CODE BEGIN 3 */
  90.   }
  91.   /* USER CODE END 3 */
  92. }

  93. /**
  94.   * @brief System Clock Configuration
  95.   * @retval None
  96.   */
  97. void SystemClock_Config(void)
  98. {
  99.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  100.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  101.   /** Initializes the RCC Oscillators according to the specified parameters
  102.   * in the RCC_OscInitTypeDef structure.
  103.   */
  104.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  105.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  106.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  107.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  108.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  109.   {
  110.     Error_Handler();
  111.   }

  112.   /** Initializes the CPU, AHB and APB buses clocks
  113.   */
  114.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  115.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  116.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  117.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  118.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  119.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  120.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  121.   {
  122.     Error_Handler();
  123.   }
  124. }

  125. /**
  126.   * @brief GPIO Initialization Function
  127.   * @param None
  128.   * @retval None
  129.   */
  130. static void MX_GPIO_Init(void)
  131. {
  132.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  133. /* USER CODE BEGIN MX_GPIO_Init_1 */
  134. /* USER CODE END MX_GPIO_Init_1 */

  135.   /* GPIO Ports Clock Enable */
  136.   __HAL_RCC_GPIOC_CLK_ENABLE();

  137.   /*Configure GPIO pin Output Level */
  138.   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10, GPIO_PIN_RESET);

  139.   /*Configure GPIO pins : PC8 PC9 PC10 */
  140.   GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
  141.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  142.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  143.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  144.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  145. /* USER CODE BEGIN MX_GPIO_Init_2 */
  146. /* USER CODE END MX_GPIO_Init_2 */
  147. }

  148. /* USER CODE BEGIN 4 */

  149. /* USER CODE END 4 */

  150. /**
  151.   * @brief  This function is executed in case of error occurrence.
  152.   * @retval None
  153.   */
  154. void Error_Handler(void)
  155. {
  156.   /* USER CODE BEGIN Error_Handler_Debug */
  157.   /* User can add his own implementation to report the HAL error return state */
  158.   __disable_irq();
  159.   while (1)
  160.   {
  161.   }
  162.   /* USER CODE END Error_Handler_Debug */
  163. }

  164. #ifdef  USE_FULL_ASSERT
  165. /**
  166.   * @brief  Reports the name of the source file and the source line number
  167.   *         where the assert_param error has occurred.
  168.   * @param  file: pointer to the source file name
  169.   * @param  line: assert_param error line source number
  170.   * @retval None
  171.   */
  172. void assert_failed(uint8_t *file, uint32_t line)
  173. {
  174.   /* USER CODE BEGIN 6 */
  175.   /* User can add his own implementation to report the file name and line number,
  176.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  177.   /* USER CODE END 6 */
  178. }
  179. #endif /* USE_FULL_ASSERT */
效果图:
6.jpg


8.jpg


您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

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