完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
新手求助。
STM32F103C8T6最小系统板,使用stmubeide编写程序,系统时钟配置72M,配置了一个GPIO用来控制LED灯。用了三种方式进行延时(1.for 循环 2.hal 延时函数 3.定时器 延时)控制led亮灭,无论哪种方式,从示波器上看,0.425秒以内程序还能正常运行,一到0.425秒,程序就从头开始开始执行了。 就配置了时钟,GPIO和定时器1,其余没有配置。 不知是哪里出了问题,请教各位大神! 以下是main.c 内容: / USER CODE BEGIN Header //**
include "main.h" / Private includes ----------------------------------------------------------// USER CODE BEGIN Includes / / 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 ---------------------------------------------------------/TIM_HandleTypeDef htim1; / USER CODE BEGIN PV / / USER CODE END PV / / Private function prototypes -----------------------------------------------/void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_TIM1_Init(void);/ USER CODE BEGIN PFP / / USER CODE END PFP / / Private user code ---------------------------------------------------------// USER CODE BEGIN 0 / / USER CODE END 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();MX_TIM1_Init();/ USER CODE BEGIN 2 / void HAL_Delay_us(uint16_t us){uint16_t counter = 0;HAL_TIM_SetAutoreload( htim1, us); //设置定时器自动加载值HAL_TIM_SetCounter( htim1, counter); //设置定时器初始值HAL_TIM_Base_Start( htim1); //启动定时器 while(counter != us) //直到定时器计数从0计数到us结束循环,刚好是所需要的时间{counter = __HAL_TIM_GetCounter( htim1); // 获取定时器当前计数}HAL_TIM_Base_Stop( htim1); // 停止定时器} void HAL_Delay_ms(uint16_t ms){for (int i = 0; i < ms; i++)HAL_Delay_us(1000);} void delay(int ms){for(int i = 0; i < ms; i++)for(int j = 0; j < 1000; j++);} / USER CODE END 2 / / Infinite loop // USER CODE BEGIN WHILE /int a = 0;while (1){// HAL_Delay(100); // delay(1000);//// a %= 3;//// switch(a++)// {// case 0 : HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin); break;// case 1 : HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin); break;// case 2 : HAL_GPIO_TogglePin(LED3_GPIO_Port,LED3_Pin); break;// default: break;// }HAL_Delay_ms(300);// a += 1;// a++;// if (a == 3)// {// a = 0;// }HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);HAL_GPIO_TogglePin(LED3_GPIO_Port,LED3_Pin);// HAL_Delay(300);// HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin , GPIO_PIN_SET);// HAL_GPIO_WritePin(LED2_GPIO_Port,LED2_Pin , GPIO_PIN_SET);// HAL_GPIO_WritePin(LED3_GPIO_Port,LED3_Pin , GPIO_PIN_SET);// HAL_Delay(300);// HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);// HAL_GPIO_WritePin(LED2_GPIO_Port,LED2_Pin , GPIO_PIN_SET);// HAL_GPIO_WritePin(LED3_GPIO_Port,LED3_Pin , GPIO_PIN_SET); / USER CODE END WHILE / / USER CODE BEGIN 3 /}/ USER CODE END 3 /} /**
if (HAL_RCC_ClockConfig( RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK){Error_Handler();}} /**
/ USER CODE END TIM1_Init 0 / TIM_ClockConfigTypeDef sClockSourceConfig = {0};TIM_MasterConfigTypeDef sMasterConfig = {0}; / USER CODE BEGIN TIM1_Init 1 / / USER CODE END TIM1_Init 1 /htim1.Instance = TIM1;htim1.Init.Prescaler = 71;htim1.Init.CounterMode = TIM_COUNTERMODE_UP;htim1.Init.Period = 0;htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;htim1.Init.RepetitionCounter = 0;htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;if (HAL_TIM_Base_Init( htim1) != HAL_OK){Error_Handler();}sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;if (HAL_TIM_ConfigClockSource( htim1, sClockSourceConfig) != HAL_OK){Error_Handler();}sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;if (HAL_TIMEx_MasterConfigSynchronization( htim1, sMasterConfig) != HAL_OK){Error_Handler();}/ USER CODE BEGIN TIM1_Init 2 / / USER CODE END TIM1_Init 2 / } /**
/Configure GPIO pin Output Level /HAL_GPIO_WritePin(GPIOA, LED2_Pin|LED3_Pin, GPIO_PIN_RESET); /Configure GPIO pin Output Level /HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET); /Configure GPIO pins : LED2_Pin LED3_Pin /GPIO_InitStruct.Pin = LED2_Pin|LED3_Pin;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(GPIOA, GPIO_InitStruct); /Configure GPIO pin : LED1_Pin /GPIO_InitStruct.Pin = LED1_Pin;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(LED1_GPIO_Port, GPIO_InitStruct); / USER CODE BEGIN MX_GPIO_Init_2 // USER CODE END MX_GPIO_Init_2 /} / USER CODE BEGIN 4 / / USER CODE END 4 / /**
/**
|
|
相关推荐
1个回答
|
|
看不到main()主函数和主函数内的while();这样运行一次当然就跑飞了。
|
|
|
|
只有小组成员才能发言,加入小组>>
请教:在使用UDE STK时,单片机使用SPC560D30L1,在配置文件怎么设置或选择?里面只有SPC560D40的选项
2760 浏览 1 评论
3247 浏览 1 评论
请问是否有通过UART连接的两个微处理器之间实现双向值交换的方法?
1820 浏览 1 评论
3663 浏览 6 评论
6051 浏览 21 评论
1346浏览 4评论
207浏览 3评论
对H747I-DISCO写程序时将CN2的st-link复用为usart1,再次烧录时无法检测到stlink怎么解决?
363浏览 2评论
STM32G474RE芯片只是串口发个数据就发烫严重是怎么回事?
458浏览 2评论
STM32处理增量式编码器Z信号如何判断中断是正转的还是反向转的?
279浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-1-2 02:20 , Processed in 1.087214 second(s), Total 81, Slave 64 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号