AndyYau-268557 发表于 2015-1-24 15:45:18

一个很奇怪的问题:LED只能亮或只能灭,不能闪烁

写个简单的LED程序试下,发现LED不能闪烁。程序如下:
u16 i = 10000;
//SysTick_Config(SystemCoreClock / 1000);
/* GPIOE Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

/* Configure PA5 Pin output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* To achieve GPIO toggling maximum frequency, the followingsequence is mandatory.
You can monitor PE14 and PE15 on the scope to measure the output signal.
If you need to fine tune this frequency, you can add more GPIO set/reset
cycles to minimize more the infinite loop timing.
This code needs to be compiled with high speed optimization option.*/
while (1)
{
      
   GPIO_SetBits(GPIOA, GPIO_Pin_5);
   while(i--);
   GPIO_ResetBits(GPIOA, GPIO_Pin_5);
   while(i--);
}

请高手指点下。

小丁 发表于 2015-1-24 15:57:32

延时时间短了吧

wambob 发表于 2015-1-24 15:58:38

本帖最后由 wambob 于 2015-1-24 16:01 编辑

延迟时间短吧,人眼能分辨的时间大于等于20MS,即50HZ以上的频率分辨不出来,延迟的短了,LED闪烁频率超过50HZ,人眼分辨不出来,看上去一直亮

wambob 发表于 2015-1-24 16:05:17

你那10000 ,你输出频率是10MHZ,相当于0.1US,延迟10000*0.1=1000US=1MS

zfz0122 发表于 2015-1-24 16:18:16

设置一个阈值吧

数码小叶 发表于 2015-1-24 16:41:50

楼上回答正确

lkl0305 发表于 2015-1-24 17:37:48

用32位整数uint32_t,把i值加大试试

fjjjnk1234 发表于 2015-1-24 18:35:24

i是全局变量,GPIO_SetBits(GPIOA, GPIO_Pin_5);下一句while(i--);已经把i减到0了,之后i延时就不起作用了

renegade 发表于 2015-1-24 19:08:01

延时时间太短:lol:lol

damiaa 发表于 2015-1-24 22:12:36

while (1)
{
    i = 10000;   
   GPIO_SetBits(GPIOA, GPIO_Pin_5);
   while(i--);
   i = 10000;
   GPIO_ResetBits(GPIOA, GPIO_Pin_5);
   while(i--);
}
页: [1] 2
查看完整版本: 一个很奇怪的问题:LED只能亮或只能灭,不能闪烁