解释代码void delay(int nTime) { int i, j = 0; for(i = 0; i < nTime; i++) { for(j = 0; j < 10; j++) { } } }
时间: 2024-05-18 20:12:52 浏览: 4
这段代码定义了一个名为 delay 的函数,它的参数是一个整数 nTime,表示延迟的时间。函数内部通过两个 for 循环来实现延迟。第一个 for 循环执行 nTime 次,用来控制延迟的总时间。第二个 for 循环执行 10 次,用来占用 CPU 时间,从而实现真正的延迟效果。具体来说,第二个 for 循环不做任何实质性的操作,只是进行了一些无用的计算,从而浪费了一定的 CPU 时间。这样,当第一个 for 循环执行完毕后,延迟的时间就达到了 nTime 个“空转”的时间。
相关问题
/* NAME : C1.C */ void delay(int nTime); main() { int i = 5; for( ; ; ) { delay(i); } } void delay(int nTime) { int i, j = 0; for(i = 0; i < nTime; i++) { for(j = 0; j < 10; j++); } }代码的结果及现象分析
这段代码是一个无限循环程序,每次执行 delay 函数会让程序暂停一段时间,时间长度为传入函数的参数 nTime 乘以 10。具体来说,delay 函数中的两个 for 循环会让程序卡在那里,第一个 for 循环控制了循环次数,第二个 for 循环只是简单地进行了一些无用的计算,以占用 CPU 时间。
由于主函数中的循环条件永远为真,因此程序会一直执行下去,不会停止。在程序运行期间,由于 delay 函数的存在,程序会不断地占用 CPU 时间,导致 CPU 利用率非常高,可能造成其他程序性能下降的问题。
因此,这段代码并不是一个好的实现方式,应该避免在实际的应用中使用。
为下面每一行代码添加注释:#include "stm32f10x.h" void RCC_Configuration(void) { /* Enable GPIOA, GPIOC and AFIO clocks / RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); / Enable SYSCFG clock / RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; / Configure PA0 pin as input floating / GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); / Configure PC13 pin as output push-pull / GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure / Configure the NVIC Preemption Priority Bits / NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); / Enable the EXTI0 Interrupt / NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void EXTI_Configuration(void) { EXTI_InitTypeDef EXTI_InitStructure; / Configure EXTI Line0 to generate an interrupt on falling edge / EXTI_InitStructure.EXTI_Line = EXTI_Line0; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); / Connect EXTI Line0 to PA0 pin / GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); } void SysTick_Configuration(void) { / Configure SysTick to generate an interrupt every 1ms / if (SysTick_Config(SystemCoreClock / 1000)) { / Capture error / while (1); } } void Delay(__IO uint32_t nTime) { / Wait for nTime millisecond / TimingDelay = nTime; while (TimingDelay != 0); } void TimingDelay_Decrement(void) { if (TimingDelay != 0x00) { TimingDelay--; } } int main(void) { RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); EXTI_Configuration(); SysTick_Configuration(); / Infinite loop / while (1) { / Toggle PC13 LED every 500ms / GPIOC->ODR ^= GPIO_Pin_13; Delay(500); } } void EXTI0_IRQHandler(void) { / Check if PA0 button is pressed / if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == RESET) { / Reset MCU / NVIC_SystemReset(); } / Clear EXTI Line0 pending bit */ EXTI_ClearITPendingBit(EXTI_Line0); }
// 引入STM32F10x库
#include "stm32f10x.h"
// 配置RCC
void RCC_Configuration(void) {
// 使能GPIOA, GPIOC和AFIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
// 使能SYSCFG时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
}
// 配置GPIO
void GPIO_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure;
// 配置PA0引脚为浮空输入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置PC13引脚为推挽输出模式,速率50MHz
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
// 配置NVIC
void NVIC_Configuration(void) {
NVIC_InitTypeDef NVIC_InitStructure;
// 配置NVIC抢占优先级位数
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
// 使能EXTI0中断
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
// 配置EXTI
void EXTI_Configuration(void) {
EXTI_InitTypeDef EXTI_InitStructure;
// 配置EXTI Line0在下降沿触发中断
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// 将EXTI Line0连接至PA0引脚
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
}
// 配置SysTick
void SysTick_Configuration(void) {
// 配置SysTick使其每1ms产生一次中断
if (SysTick_Config(SystemCoreClock / 1000)) {
// 发生错误
while (1);
}
}
// 延时函数
void Delay(__IO uint32_t nTime) {
// 等待nTime毫秒
TimingDelay = nTime;
while (TimingDelay != 0);
}
// 延时计数器递减函数
void TimingDelay_Decrement(void) {
if (TimingDelay != 0x00) {
TimingDelay--;
}
}
// 主函数
int main(void) {
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
EXTI_Configuration();
SysTick_Configuration();
// 无限循环
while (1) {
// 每500ms交替改变PC13引脚的电平
GPIOC->ODR ^= GPIO_Pin_13;
Delay(500);
}
}
// EXTI0中断处理函数
void EXTI0_IRQHandler(void) {
// 检查PA0引脚的按键是否被按下
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == RESET) {
// 复位MCU
NVIC_SystemReset();
}
// 清除EXTI Line0的中断挂起位
EXTI_ClearITPendingBit(EXTI_Line0);
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)