1.概述
欲先善其事,必先利其器。开发应用程序的基础就是基于底层硬件驱动的实现,欲先修炼如来神掌,必先打通任督二脉^_^,本文将说明基于uFun开发板板载资源的硬件调试测试和运行,为后期的实现应用层打好基础。
2.硬件准备
2.1.uFun开发板及其它
说明:
uFun开发板、调试烧录工具使用J-LINK V9.6,使用SWD接口、示波器等。
2.2.uFun开发板板载资源分配
说明:
根据uFun开发板的原理图,再结合MCU的引脚资源,对uFun开发板板载资源进行设计规划;在后期的实际调试中PWM_R和PWM_G的硬件对调了,所以如上图中的更正了一下。
3.STM32CubeMX设置
3.1.RCC设置如下图,根据原理图得。
3.2.SYS设置如下图,将Debug选择Serial Wire,即为SWD接口调试。
3.3.ADC设置如下图,配置AD_SIG1和AD_SIG2,分别为ADC1的IN12和IN13,ADC的数据采集使用DMA方式,启动ADC转换后,MCU自动完成。
3.4.TIM1设置如下图,使用TIM1的Channel1产生1kHz的PWM波形驱动无源蜂鸣器发出声音。
3.5.TIM4设置如下图,使用TIM4的Channel3和Channel4这两个通道产生频率为1kHz,点空比为50%的PWM波形输出,通过示波器测量PWMCH1和PWMCH2的输出波形。
3.6.TIM5设置如下图,使用TIM5的Channel1、Channel2和Channel3这3个通道生产PWM波形输出,通过控制输出PWM波形的点空比来控制RGB灯的显示颜色。
3.7.TIM6设置如下图,使用TIM6的计数中断功能,给系统运行的时间根本提供时间基准,每间隔1ms产生一次中断。
3.8.USART1设置如下图,使用USART1通过USB转串口芯片CH340与PC实现数据通讯。
3.9.I2C设置如下图,使用I2C1串行总线与传感器进行数据通讯。
3.10.GPIO设置如下图,将LED的控制引脚配置为输出,将按键KEY的控制引脚配置为输入。
4.主要程序
- /**
- ******************************************************************************
- * @file uFun.c
- * @version V1.00
- * @author xld0932
- * @brief
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2019. All rights reserved.
- *
- ******************************************************************************
- */
- /* Define to prevent recursive inclusion -------------------------------------*/
- #define __UFUN_C__
- /* Includes ------------------------------------------------------------------*/
- #include "uFun.h"
- /* Private constants ---------------------------------------------------------*/
- /* Private defines -----------------------------------------------------------*/
- /* Private typedef -----------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- #define uFUN_RGB(r,g,b) {TIM5->CCR1 = (g); TIM5->CCR2 = (r); TIM5->CCR3 = (b);}
- /* Private variables ---------------------------------------------------------*/
- ADC_HandleTypeDef hadc1;
- DMA_HandleTypeDef hdma_adc1;
- I2C_HandleTypeDef hi2c1;
- TIM_HandleTypeDef htim1;
- TIM_HandleTypeDef htim4;
- TIM_HandleTypeDef htim5;
- TIM_HandleTypeDef htim6;
- UART_HandleTypeDef huart1;
- /* Private variables ---------------------------------------------------------*/
- static volatile uint32_t ADC_Value[100];
- /* Private function prototypes -----------------------------------------------*/
- /* Exported variables --------------------------------------------------------*/
- /* Exported function prototypes ----------------------------------------------*/
- extern void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
- /**
- * @brief : ADC1 Initialization Function
- * @param :
- * @returns:
- * @details:
- */
- static void MX_ADC1_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig = {0};
- /**
- * Common config
- */
- hadc1.Instance = ADC1;
- hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
- hadc1.Init.ContinuousConvMode = ENABLE;
- hadc1.Init.DiscontinuousConvMode = DISABLE;
- hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc1.Init.NbrOfConversion = 2;
- if (HAL_ADC_Init(&hadc1) != HAL_OK)
- {
- }
- /**
- * Configure Regular Channel
- */
- sConfig.Channel = ADC_CHANNEL_12;
- sConfig.Rank = ADC_REGULAR_RANK_1;
- sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- }
- /**
- * Configure Regular Channel
- */
- sConfig.Channel = ADC_CHANNEL_13;
- sConfig.Rank = ADC_REGULAR_RANK_2;
- sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- }
- HAL_ADC_Start_DMA(&hadc1, (uint32_t *)&ADC_Value, 100);
- }
- /**
- * @brief : Enable DMA controller clock
- * @param :
- * @returns:
- * @details:
- */
- static void MX_DMA_Init(void)
- {
- /* DMA controller clock enable */
- __HAL_RCC_DMA1_CLK_ENABLE();
- /* DMA interrupt init */
- /* DMA1_Channel1_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
- }
- /**
- * @brief : I2C1 Initialization Function
- * @param :
- * @returns:
- * @details:
- */
- static void MX_I2C1_Init(void)
- {
- hi2c1.Instance = I2C1;
- hi2c1.Init.ClockSpeed = 100000;
- hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
- hi2c1.Init.OwnAddress1 = 0;
- hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
- hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
- hi2c1.Init.OwnAddress2 = 0;
- hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
- hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
- if (HAL_I2C_Init(&hi2c1) != HAL_OK)
- {
- }
- }
- /**
- * @brief : TIM1 Initialization Function
- * @param :
- * @returns:
- * @details: PWM 1kHz For BEEP
- */
- static void MX_TIM1_Init(void)
- {
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- TIM_OC_InitTypeDef sConfigOC = {0};
- TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
- htim1.Instance = TIM1;
- htim1.Init.Prescaler = 72-1;
- htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim1.Init.Period = 1000-1;
- htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim1.Init.RepetitionCounter = 0;
- htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
- if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
- {
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
- {
- }
- sConfigOC.OCMode = TIM_OCMODE_PWM1;
- sConfigOC.Pulse = 500;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
- sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
- sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
- if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
- {
- }
- sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
- sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
- sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
- sBreakDeadTimeConfig.DeadTime = 0;
- sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
- sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
- sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
- if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
- {
- }
- HAL_TIM_MspPostInit(&htim1);
- }
- /**
- * @brief : TIM4 Initialization Function
- * @param :
- * @returns:
- * @details: PWM 1kHz for PWMCH1 & PWMCH2
- */
- static void MX_TIM4_Init(void)
- {
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- TIM_OC_InitTypeDef sConfigOC = {0};
- htim4.Instance = TIM4;
- htim4.Init.Prescaler = 72-1;
- htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim4.Init.Period = 1000-1;
- htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
- if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
- {
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
- {
- }
- sConfigOC.OCMode = TIM_OCMODE_PWM1;
- sConfigOC.Pulse = 500;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
- {
- }
- if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
- {
- }
- HAL_TIM_MspPostInit(&htim4);
- }
- /**
- * @brief : TIM5 Initialization Function
- * @param :
- * @returns:
- * @details: PWM 1kHz for RGB
- */
- static void MX_TIM5_Init(void)
- {
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- TIM_OC_InitTypeDef sConfigOC = {0};
- htim5.Instance = TIM5;
- htim5.Init.Prescaler = 72-1;
- htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim5.Init.Period = 256-1;
- htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
- if (HAL_TIM_PWM_Init(&htim5) != HAL_OK)
- {
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)
- {
- }
- sConfigOC.OCMode = TIM_OCMODE_PWM1;
- sConfigOC.Pulse = 8;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
- {
- }
- if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
- {
- }
- if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
- {
- }
- HAL_TIM_MspPostInit(&htim5);
- HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_1);
- HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_2);
- HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_3);
- }
- /**
- * @brief : TIM6 Initialization Function : 1ms
- * @param :
- * @returns:
- * @details: For System Run Tick
- */
- static void MX_TIM6_Init(void)
- {
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- htim6.Instance = TIM6;
- htim6.Init.Prescaler = 72-1;
- htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim6.Init.Period = 1000-1;
- htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
- if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
- {
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
- {
- }
- if (HAL_TIM_Base_Start_IT(&htim6) != HAL_OK)
- {
- }
- }
- /**
- * @brief : USART1 Initialization Function
- * @param :
- * @returns:
- * @details:
- */
- static void MX_USART1_UART_Init(void)
- {
- huart1.Instance = USART1;
- huart1.Init.BaudRate = 115200;
- huart1.Init.WordLength = UART_WORDLENGTH_8B;
- huart1.Init.StopBits = UART_STOPBITS_1;
- huart1.Init.Parity = UART_PARITY_NONE;
- huart1.Init.Mode = UART_MODE_TX_RX;
- huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart1.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_UART_Init(&huart1) != HAL_OK)
- {
- }
- SET_BIT(huart1.Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
- }
- /**
- * @brief : GPIO Initialization Function
- * @param :
- * @returns:
- * @details:
- */
- 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();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pins : KEY1_Pin KEY0_Pin */
- GPIO_InitStruct.Pin = KEY1_Pin | KEY0_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- /*Configure GPIO pins : KEY2_Pin KEY3_Pin G_INT_Pin */
- GPIO_InitStruct.Pin = KEY2_Pin | KEY3_Pin | G_INT_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);
- /*Configure LED GPIO pin : PA3 */
- GPIO_InitStruct.Pin = LED_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
- }
- /**
- * @brief :
- * @param :
- * @returns:
- * @details:
- */
- void uFUN_Init(void)
- {
- uint8_t flag = 0;
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_ADC1_Init();
- if(flag) MX_I2C1_Init();
- MX_TIM1_Init();
- MX_TIM4_Init();
- MX_TIM5_Init();
- MX_TIM6_Init();
- MX_USART1_UART_Init();
- }
- /**
- * @brief :
- * @param :
- * @returns:
- * @details:
- */
- void uFUN_TestBEEP(void)
- {
- HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
- HAL_Delay(250);
- HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1);
- }
- /**
- * @brief :
- * @param :
- * @returns:
- * @details:
- */
- void uFUN_TestPWMn(void)
- {
- HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
- HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
- HAL_Delay(1000);
- HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3);
- HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4);
- }
- /**
- * @brief :
- * @param :
- * @returns:
- * @details:
- */
- void uFUN_TestRGB(void)
- {
- uFUN_RGB(255, 0, 0); HAL_Delay(500);
- uFUN_RGB(0, 255, 0); HAL_Delay(500);
- uFUN_RGB(0, 0, 255); HAL_Delay(500);
- uFUN_RGB(16, 16, 16);
- }
- /**
- * @brief :
- * @param :
- * @returns:
- * @details:
- */
- void uFUN_TestLED(void)
- {
- uint8_t i = 0;
- for(i = 0; i < 10; i++)
- {
- if((i % 2) == 0)
- {
- HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); //---ON
- }
- else
- {
- HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET); //---OFF
- }
- HAL_Delay(100);
- }
- }
- /**
- * @brief :
- * @param :
- * @returns:
- * @details:
- */
- void uFUN_TestADC(void)
- {
- uint32_t i = 0, AD_SIG1 = 0, AD_SIG2 = 0;
- for(i = 0; i < 100;)
- {
- AD_SIG1 += ADC_Value[i++];
- AD_SIG2 += ADC_Value[i++];
- }
- AD_SIG1 /= 50;
- AD_SIG2 /= 50;
- printf("%d, %d\r\n", AD_SIG1, AD_SIG2);
- }
- /**
- * @brief :
- * @param :
- * @returns:
- * @details:
- */
- void uFUN_TestI2C(void)
- {
- }
- /**
- * @brief :
- * @param :
- * @returns:
- * @details:
- */
- void uFUN_Test(void)
- {
- printf("\r\n----------uFun Hardware Test----------\r\n");
- printf("Test BEEP\r\n");
- uFUN_TestBEEP();
- printf("Test PWM\r\n");
- uFUN_TestPWMn();
- printf("Test LED\r\n");
- uFUN_TestLED();
- printf("Test RGB\r\n");
- uFUN_TestRGB();
- printf("Test ADC :: ");
- uFUN_TestADC();
- printf("Test I2C\r\n");
- uFUN_TestI2C();
- printf("--------------------------------------\r\n\r\n");
- }
- /************************ (C) COPYRIGHT ************************END OF FILE****/
5.运行结果
6.工程代码