找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3766|回复: 1
收起左侧

max7219 8位点阵数码管STM32程序

[复制链接]
ID:577130 发表于 2019-7-3 14:21 | 显示全部楼层 |阅读模式
学习的测试项目,keil5 stm32F0 驱动max7219 8位数码管

单片机源程序如下:
  1. /* USER CODE END Header */

  2. #include "main.h"
  3. #include "MAX7219.h"

  4. SPI_HandleTypeDef hspi1;

  5. void SystemClock_Config(void);
  6. static void MX_GPIO_Init(void);
  7. static void MX_SPI1_Init(void);

  8. /**
  9.   * @brief  The application entry point.
  10.   * @retval int
  11.   */
  12. int main(void)
  13. {
  14.   HAL_Init();

  15.   SystemClock_Config();

  16.   MX_GPIO_Init();
  17.   MX_SPI1_Init();
  18.   
  19.         HAL_Delay(50);
  20.         Init_MAX7219(); //初始化

  21.         /* 点阵屏输出测试 BEGIN */
  22.         MAX7219_Write(0x01, 0x01);
  23.         MAX7219_Write(0x02, 0x04);
  24.         MAX7219_Write(0x02, 0xD7);//加入小数点
  25.         MAX7219_Write(0x03, 0x02);
  26.         MAX7219_Write(0x04, 0x07);
  27.        
  28.         MAX7219_Write(0x05, 0x09);
  29.         MAX7219_Write(0x06, 0x07);
  30.         MAX7219_Write(0x07, 0x03);       
  31.         MAX7219_Write(0x08, 0xD4);//加入小数点
  32.         //MAX7219_Write(0x08, 0x09);
  33.        
  34.         /* 点阵屏输出测试 end */
  35.        
  36.   while (1)
  37.   {
  38.     HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);
  39.                 HAL_Delay(300);
  40.                
  41.                
  42.                
  43.                 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
  44.                 HAL_Delay(300);
  45.   }
  46. }

  47. /**
  48.   * @brief System Clock Configuration
  49.   * @retval None
  50.   */
  51. void SystemClock_Config(void)
  52. {
  53.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  54.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  55.   /**Initializes the CPU, AHB and APB busses clocks
  56.   */
  57.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  58.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  59.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  60.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  61.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  62.   {
  63.     Error_Handler();
  64.   }
  65.   /**Initializes the CPU, AHB and APB busses clocks
  66.   */
  67.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  68.                               |RCC_CLOCKTYPE_PCLK1;
  69.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  70.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  71.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  72.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  73.   {
  74.     Error_Handler();
  75.   }
  76. }

  77. /**
  78.   * @brief SPI1 Initialization Function
  79.   * @param None
  80.   * @retval None
  81.   */
  82. static void MX_SPI1_Init(void)
  83. {       
  84.   hspi1.Instance = SPI1;
  85.   hspi1.Init.Mode = SPI_MODE_MASTER;
  86.   hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  87.   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  88.   hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  89.   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  90.   hspi1.Init.NSS = SPI_NSS_SOFT;
  91.   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  92.   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  93.   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  94.   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  95.   hspi1.Init.CRCPolynomial = 7;
  96.   hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  97.   hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  98.   if (HAL_SPI_Init(&hspi1) != HAL_OK)
  99.   {
  100.     Error_Handler();
  101.   }
  102. }

  103. /**
  104.   * @brief GPIO Initialization Function
  105.   * @param None
  106.   * @retval None
  107.   */
  108. static void MX_GPIO_Init(void)
  109. {
  110.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  111.   /* GPIO Ports Clock Enable */
  112.   __HAL_RCC_GPIOA_CLK_ENABLE();
  113.         __HAL_RCC_GPIOC_CLK_ENABLE();
  114.        
  115.                 /*Configure GPIO pin : PC13 */
  116.   GPIO_InitStruct.Pin = GPIO_PIN_13;
  117.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  118.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  119.         GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  120.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  121.   /*Configure GPIO pin Output Level */
  122.   HAL_GPIO_WritePin(MAX7219_CS_GPIO_Port, MAX7219_CS_Pin, GPIO_PIN_RESET);

  123.   /*Configure GPIO pin : PC6 用于SPI1-CS*/
  124.   GPIO_InitStruct.Pin = MAX7219_CS_Pin;
  125.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  126.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  127.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  128.   HAL_GPIO_Init(MAX7219_CS_GPIO_Port, &GPIO_InitStruct);
  129.        
  130.        
  131. //        HAL_GPIO_WritePin(MAX7219_SCK_GPIO_Port, MAX7219_SCK_Pin, GPIO_PIN_RESET);
  132. //        GPIO_InitStruct.Pin = MAX7219_SCK_Pin|MAX7219_MOSI_Pin;
  133. //  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  134. //  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  135. //  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  136. }

  137. /**
  138.   * @brief  This function is executed in case of error occurrence.
  139.   * @retval None
  140.   */
  141. void Error_Handler(void)
  142. {
  143.   /* USER CODE BEGIN Error_Handler_Debug */
  144.   /* User can add his own implementation to report the HAL error return state */

  145.   /* USER CODE END Error_Handler_Debug */
  146. }

  147. #ifdef  USE_FULL_ASSERT
  148. /**
  149.   * @brief  Reports the name of the source file and the source line number
  150.   *         where the assert_param error has occurred.
  151.   * @param  file: pointer to the source file name
  152.   * @param  line: assert_param error line source number
  153.   * @retval None
  154.   */
  155. void assert_failed(char *file, uint32_t line)
  156. {
  157.   /* USER CODE BEGIN 6 */
  158.   /* User can add his own implementation to report the file name and line number,
  159.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  160.   /* USER CODE END 6 */
  161. }
  162. #endif /* USE_FULL_ASSERT */

  163. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
SPI_MAX7219_Test.7z (2.69 MB, 下载次数: 67)


回复

使用道具 举报

ID:1 发表于 2019-7-4 23:42 | 显示全部楼层
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

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

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表