- #include "main.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #define uchar unsigned char
- #define uint unsigned int
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- #define Max7219_pinCLK0 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_RESET)
- #define Max7219_pinCLK1 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET)
- #define Max7219_pinCS0 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_RESET)
- #define Max7219_pinCS1 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_SET)
- #define Max7219_pinDIN0 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET)
- #define Max7219_pinDIN1 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET)
- #define cs0 HAL_GPIO_WritePin(GPIOD,GPIO_PIN_4,GPIO_PIN_RESET)
- #define cs1 HAL_GPIO_WritePin(GPIOD,GPIO_PIN_4,GPIO_PIN_SET)
- #define clk0 HAL_GPIO_WritePin(GPIOD,GPIO_PIN_5,GPIO_PIN_RESET)
- #define clk1 HAL_GPIO_WritePin(GPIOD,GPIO_PIN_5,GPIO_PIN_SET)
- #define din0 HAL_GPIO_WritePin(GPIOD,GPIO_PIN_6,GPIO_PIN_RESET)
- #define din1 HAL_GPIO_WritePin(GPIOD,GPIO_PIN_6,GPIO_PIN_SET)
- /* USER CODE END Includes */
- uchar const disp1[38][8]={
- {0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//0
- {0x10,0x18,0x14,0x10,0x10,0x10,0x10,0x10},//1
- {0x7E,0x2,0x2,0x7E,0x40,0x40,0x40,0x7E},//2
- {0x3E,0x2,0x2,0x3E,0x2,0x2,0x3E,0x0},//3
- {0x8,0x18,0x28,0x48,0xFE,0x8,0x8,0x8},//4
- {0x3C,0x20,0x20,0x3C,0x4,0x4,0x3C,0x0},//5
- {0x3C,0x20,0x20,0x3C,0x24,0x24,0x3C,0x0},//6
- {0x3E,0x22,0x4,0x8,0x8,0x8,0x8,0x8},//7
- {0x0,0x3E,0x22,0x22,0x3E,0x22,0x22,0x3E},//8
- {0x3E,0x22,0x22,0x3E,0x2,0x2,0x2,0x3E},//9
- {0x8,0x14,0x22,0x3E,0x22,0x22,0x22,0x22},//A
- {0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C,0x0},//B
- {0x3C,0x40,0x40,0x40,0x40,0x40,0x3C,0x0},//C
- {0x7C,0x42,0x42,0x42,0x42,0x42,0x7C,0x0},//D
- {0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x7C},//E
- {0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x40},//F
- {0x3C,0x40,0x40,0x40,0x40,0x44,0x44,0x3C},//G
- {0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0x44},//H
- {0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x7C},//I
- {0x3C,0x8,0x8,0x8,0x8,0x8,0x48,0x30},//J
- {0x0,0x24,0x28,0x30,0x20,0x30,0x28,0x24},//K
- {0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x7C},//L
- {0x81,0xC3,0xA5,0x99,0x81,0x81,0x81,0x81},//M
- {0x0,0x42,0x62,0x52,0x4A,0x46,0x42,0x0},//N
- {0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//O
- {0x3C,0x22,0x22,0x22,0x3C,0x20,0x20,0x20},//P
- {0x1C,0x22,0x22,0x22,0x22,0x26,0x22,0x1D},//Q
- {0x3C,0x22,0x22,0x22,0x3C,0x24,0x22,0x21},//R
- {0x0,0x1E,0x20,0x20,0x3E,0x2,0x2,0x3C},//S
- {0x0,0x3E,0x8,0x8,0x8,0x8,0x8,0x8},//T
- {0x42,0x42,0x42,0x42,0x42,0x42,0x22,0x1C},//U
- {0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18},//V
- {0x0,0x49,0x49,0x49,0x49,0x2A,0x1C,0x0},//W
- {0x0,0x41,0x22,0x14,0x8,0x14,0x22,0x41},//X
- {0x41,0x22,0x14,0x8,0x8,0x8,0x8,0x8},//Y
- {0x0,0x7F,0x2,0x4,0x8,0x10,0x20,0x7F},//Z
- {0x8,0x7F,0x49,0x49,0x7F,0x8,0x8,0x8},//?D
- {0xFE,0xBA,0x92,0xBA,0x92,0x9A,0xBA,0xFE},//1ú
- };
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- void Delay_xms(uint x)
- {
- uint i,j;
- for(i=0;i<x;i++)
- for(j=0;j<112;j++);
- }
- //--------------------------------------------
- //1|?ü£o?òMAX7219(U3)D′è?×??ú
- //è??ú2?êy£oDATA
- //3??ú2?êy£o?T
- //?μ?÷£o
- void Write_Max7219_byte(uchar DATA)
- {
- uchar i;
- Max7219_pinCS0;
- for(i=8;i>=1;i--)
- {
- Max7219_pinCLK0;
- if((DATA&0x80)==0x80)
- //Max7219_pinDIN=DATA&0x80;
- Max7219_pinDIN1;else Max7219_pinDIN0;
- DATA=DATA<<1;
- Max7219_pinCLK1;
- }
- }
- //-------------------------------------------
- //1|?ü£o?òMAX7219D′è?êy?Y
- //è??ú2?êy£oaddress?¢dat
- //3??ú2?êy£o?T
- //?μ?÷£o
- void Write_Max7219(uchar address,uchar dat)
- {
- Max7219_pinCS0;
- Write_Max7219_byte(address); //D′è?μ??·£??′êy??1ü±ào?
- Write_Max7219_byte(dat); //D′è?êy?Y£??′êy??1ü??ê?êy×?
- Max7219_pinCS1;
- }
- void Init_MAX7219(void)
- {
- Write_Max7219(0x09, 0x00); //ò???·?ê?£oBCD??
- Write_Max7219(0x0a, 0x03); //áá?è
- Write_Max7219(0x0b, 0x07); //é¨?è???T£?8??êy??1ü??ê?
- Write_Max7219(0x0c, 0x01); //μ?μ??£ê?£o0£???í¨?£ê?£o1
- Write_Max7219(0x0f, 0x00); //??ê?2aê?£o1£?2aê??áê?£??y3£??ê?£o0
- }
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- void delay()
- {
- int i = 300;
- while(i--);
- }
- void DA_Conver(unsigned int DAValue)
- {
- unsigned char i;
- DAValue <<= 6;
- cs0; // 片选DA芯片
- clk0; // 在以下12个时钟周期内,每当在上升沿的
- // 数据被锁存,形成DA输出。在前10个时钟
- for(i = 0; i < 12; i++) // 内输入的是10位DA数据,后两个时钟周期
- {
- if(DAValue & 0x8000)// 为填充字节
- din1;
- else
- din0;
- //din = (bit)(DAValue & 0x8000); //
- clk1; //
- DAValue <<= 1; //
- clk0;
- }
- cs1; // CS的上升沿和下降沿只有在clk为低的时候
- clk0; // 才有效
- }
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * [url=/u/brief]@brief[/url] The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- uchar i,j=0;
- /* USER CODE END 1 */
- long DAValue = 0;
- delay();
- DAValue = 0;
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- /* USER CODE BEGIN 2 */
- /* USER CODE END 2 */
- Delay_xms(500);
- Init_MAX7219();
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- DA_Conver(DAValue); //加上滤波后,就可以形成模拟输出
- delay();
- DAValue++;
- if(DAValue==512)
- DAValue = 0;
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /** Configure the main internal regulator output voltage
- */
- if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
- RCC_OscInitStruct.MSIState = RCC_MSI_ON;
- RCC_OscInitStruct.MSICalibrationValue = 0;
- RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOD_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
- /*Configure GPIO pins : PC13 PC14 PC15 */
- GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
效果图: