金牌会员
 
- 积分
- 2099
- 金钱
- 2099
- 注册时间
- 2017-2-11
- 在线时间
- 306 小时
|
本帖最后由 jiangyy 于 2021-3-25 13:29 编辑
1.滴答定时器中断配置,默认在HAL_Init(void)初始化了
- HAL_StatusTypeDef HAL_Init(void)
- {
- /* Configure Flash prefetch, Instruction cache, Data cache */
- #if (INSTRUCTION_CACHE_ENABLE != 0U)
- __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
- #endif /* INSTRUCTION_CACHE_ENABLE */
- #if (DATA_CACHE_ENABLE != 0U)
- __HAL_FLASH_DATA_CACHE_ENABLE();
- #endif /* DATA_CACHE_ENABLE */
- #if (PREFETCH_ENABLE != 0U)
- __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
- #endif /* PREFETCH_ENABLE */
- /* Set Interrupt Group Priority */
- HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
- /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
- HAL_InitTick(TICK_INT_PRIORITY);//中断配置,最高
- /* Init the low level hardware */
- HAL_MspInit();
- /* Return function status */
- return HAL_OK;
- }
复制代码 2.初始化系统时钟:
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
- /** Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 4;
- RCC_OscInitStruct.PLL.PLLN = 168;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 7;
- 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_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
- PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV8;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- }
复制代码 3.自定义一个延时函数:
3.1 bsp_delay.c
- /*
- * bsp_delay.c
- *
- * Created on: Aug 19, 2020
- * Author: jiangyuanyuan
- */
- #include "bsp_delay.h"
- //初始化延迟函数
- //当使用ucos的时候,此函数会初始化ucos的时钟节拍
- //SYSTICK的时钟固定为AHB时钟
- //SYSCLK:系统时钟频率
- void delay_init(void)
- {
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000000); // 配置并启动系统滴答定时器
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- }
- //延时nus
- void delay_us(uint32_t nus)
- {
- HAL_Delay(nus);
- }
- //延时nms
- //nms:要延时的ms数
- void delay_ms(uint16_t nms)
- {
- uint32_t i;
- for(i=0;i<nms;i++) HAL_Delay(1000);
- }
- /****************************************************************************
- * 名 称: void Delay_us(u32 nus)
- * 功 能:延时nus
- * 入口参数:药延时的微秒数
- * 返回参数:无
- * 说 明:nus得值,不要大于798915us
- ****************************************************************************/
- void Delay_us(uint32_t nus)
- {
- uint32_t i,j;
- for(i = 0; i < nus; i++)
- {
- j=10; //change it for timing adjustment
- while(j--)
- {
- __NOP(); // nop's may be added or removed for timing
- }
- }
- }
- void Delay_ms(uint16_t nms)
- {
- uint16_t i,j,k;
- for(i = 0; i < nms; i++)
- {
- for(j = 0; j < 1000; j++)
- {
- k=10; //change it for timing adjustment
- while(k--)
- {
- __NOP(); // nop's may be added or removed for timing
- }
- }
- }
- }
- void delay_Xms(uint16_t nms,uint8_t Cnt)
- {
- uint8_t ucCnt = 0;
- for(;ucCnt<Cnt;ucCnt++)
- delay_ms(nms);
- }
复制代码 3.2 bsp_delay.h
- /*
- * bsp_delay.h
- *
- * Created on: Aug 19, 2020
- * Author: jiangyuanyuan
- */
- #ifndef INC_BSP_DELAY_H_
- #define INC_BSP_DELAY_H_
- #include "gpio.h"
- #include "stm32f4xx_hal.h"
- #include "bsp_delay.h"
- void delay_init(void);
- void delay_ms(uint16_t nms);
- void delay_us(uint32_t nus);
- void Delay_us(uint32_t nus);
- void Delay_ms(uint16_t nms);
- #endif /* INC_BSP_delay_H_ */
复制代码 4.主函数main初始化
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * [url=home.php?mod=space&uid=175224]@file[/url] : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2020 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "adc.h"
- #include "dac.h"
- #include "dma.h"
- #include "fatfs.h"
- #include "rng.h"
- #include "rtc.h"
- #include "sdio.h"
- #include "spi.h"
- #include "tim.h"
- #include "usart.h"
- #include "usb_device.h"
- #include "gpio.h"
- #include "fsmc.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "stdio.h"
- #include "bsp_sys.h"
- #include "bsp_led.h"
- #include "bsp_key.h"
- #include "bsp_delay.h"
- #include "bsp_tim3.h"
- #include "bsp_errom.h"
- #include "bsp_adc.h"
- #include "bsp_dac.h"
- #include "bsp_pwm.h"
- #include "bsp_rtc.h"
- #include "bsp_stmflash.h"
- #include "bsp_w25q.h"
- #include "bsp_fsmc.h"
- #include "bsp_malloc.h"
- #include "bsp_fatfs.h"
- #include "rng.h"
- #include "parameter.h"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* 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);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- int cnt = 0;
- u16 dacval=0;
- u8 key;
- /* 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 */
- delay_init();
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_TIM3_Init();
- MX_USART1_UART_Init();
- MX_RNG_Init();
- MX_TIM4_Init();
- MX_ADC1_Init();
- MX_RTC_Init();
- MX_DAC_Init();
- MX_SPI1_Init();
- MX_FSMC_Init();
- MX_SDIO_SD_Init();
- MX_FATFS_Init();
- MX_USB_DEVICE_Init();
- /* USER CODE BEGIN 2 */
- W25QXX_Init();
- HAL_TIM_Base_Start_IT(&htim3);
- HAL_UART_Receive_DMA(&huart1, UsartType1.usartDMA_rxBuf, RECEIVELEN);
- __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
- HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);
- HAL_DAC_Start(&hdac,DAC_CHANNEL_1);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- handle_Param();
- HAL_DAC_SetValue(&hdac,DAC_CHANNEL_1,DAC_ALIGN_12B_R,0);//初始值为0
- W25QXX_Test();
- fsmc_sram_test();
- // stmflash_test();
- my_mem_init(SRAMEX); //初始化外部内存池
- Malloc_Test();
- SD_Auto_NewFolder();
- while (1)
- {
- cnt = RNG_Get_RandomRange(1,10);
- // MRTC_Get();
- Pwm_Test();
- key=KEY_Scan(0);
- if(key==KEY0_PRES)
- {
- if(dacval<3300)dacval+=300;
- DAC1_Set_Vol(dacval);
- }else if(key==KEY2_PRES)
- {
- if(dacval>=300)dacval-=300;
- else dacval=0;
- DAC1_Set_Vol(dacval);
- }
- Adc_printf();
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- delay_ms(1000);
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
- /** Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 4;
- RCC_OscInitStruct.PLL.PLLN = 168;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 7;
- 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_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
- PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV8;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* 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 */
- printf("HAL error!\r\n");
- /* 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.
- * [url=home.php?mod=space&uid=271674]@param[/url] 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,
- tex: 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****/
复制代码 4.调试结果
- Memory SRAMEX ADDR:0X680EF800
- mount sucess!!!
- open file sucess!!!
- write file sucess!!!
- write Data : This is a HDSC Card!!!
- close sucess!!!
- open file sucess!!!
- read sucess!!!
- read Data : This is a HDSC Card!!!
- close sucess!!!
- FatFs is working well!!!
- temp = 0.04
- temp = 0.05
复制代码
|
|