STM32--STM32CubeMX的Timer3定时1ms功能HAL库操作

一. STM32CubeMX的设置

① 时钟源的选择【Crystal/Ceramic Resonator】

② 调试方法选择【Serial Wire】

③ 时钟输入为40MHz

④ Timer3的参数设置

⑤ 使能Timer3的中断

点击【Generate Code】生成Keil5的代码工程。

二. 编写Keil5 代码

【tim.c】

/* USER CODE BEGIN 0 */
#include "gpio.h"
/* USER CODE END 0 */


/* USER CODE BEGIN 1 */

/*!
@Brief
	Timer3 callback function
@Param
	htim 					[Timer object]
@Return
	null
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
		static uint16_t TimerCount = 0;

		TimerCount++;
	
		//static unsigned char ledState = 0;
    if (htim == (&htim3))
    {
			//User processing function
			
			if(TimerCount == 1000)
			{
				HAL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
				TimerCount = 0;
			}			
    }
}

/* USER CODE END 1 */

【gpio.c】

/* USER CODE BEGIN 2 */

/*!
@Brief
	Config the GPIO.
@Param
	PIN_PORT		 	[The port of the PIN]
	PIN_Number		[The PIN number]
@Return
	null
*/
void GPIO_Config(GPIO_TypeDef *PIN_PORT, uint16_t PIN_Number)
{
	GPIO_InitTypeDef GPIO_InitStruct;			   //Define the GPIO_InitTypeDef structure 
	GPIO_InitStruct.Pin = PIN_Number;			   //Define the pin
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;		//Push-Pull output
	GPIO_InitStruct.Pull = GPIO_PULLUP;				//PULL-up mode
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;	//The pin rate is high speed
	HAL_GPIO_Init(PIN_PORT, &GPIO_InitStruct);
}

/*!
@Brief
	Config the user GPIO define.
@Param
	null
@Return
	null
*/
void GPIOUserInit(void)
{
	GPIO_Config(LED2_GPIO_PORT, LED2_PIN);
}

/* USER CODE END 2 */

【gpio.h】

/* USER CODE BEGIN Private defines */
#define LED2_PIN                           GPIO_PIN_5
#define LED2_GPIO_PORT                     GPIOA

#define GPIO_PIN_HIGH					    0x01
#define GPIO_PIN_LOW					    0x00
/* USER CODE END Private defines */

【main.c】

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
	uint8_t  Data[5] = {1, 2, 3, 4, 5};  //test code
	uint16_t CanID = 0x001;
	uint16_t usPWD_Duty = 0;
  /* USER CODE END 1 */

  /* 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();
  MX_DMA_Init();
  //MX_CAN1_Init();
  MX_ADC1_Init();
  MX_TIM2_Init();
  MX_USART2_UART_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
	
	/**Enable timer3**/
	HAL_TIM_Base_Start_IT(&htim3);
	/**Initialize CAN and filter and start the CAN**/
	//CAN_User_Init(&hcan1);
	
	/**GPIO user config LED**/
	GPIOUserInit();

	printf("Hello World\r\n");
	
	//HAL_UART_Receive_IT(&huart2, (uint8_t *)g_ucUSART_RecvBuffer, 1);
	
	printf("Hello World2\r\n");
	
	/**Start PWM Timer2Channel1**/
	//verify the PWM function
	//USER_TIM_PWM_OpenTim2Ch1();

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {	

  }
  /* USER CODE END 3 */
}

主要添加如下代码

HAL_TIM_Base_Start_IT() : 使能Timer

GPIOUserInit() : 初始化用户的GPIO

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值