快乐的小陈 发表于 2015-5-13 14:49:41

软件延时:for循环中使用不同的参数,延时有很大的差异

while(1)
    {
   GPIO_SetBits(GPIOG, GPIO_Pin_1);

      //延时1ms的程序
         for(count =0 ; count < 6; count++)
    {
            for(i = 0; i < 1000; i++);
    }
      
    //SET TO LOW
    GPIO_ResetBits(GPIOG, GPIO_Pin_1);
   
    //延时1ms的程序
         for(count =0 ; count < 6; count++)
    {
      for(i = 0; i < 1000; i++);
    }
}






while(1)
    {
      GPIO_SetBits(GPIOG, GPIO_Pin_1);
      //延时1ms的程序
         for(count =0 ; count < 1; count++)
    {
            for(i = 0; i < 6000; i++);
    }
       GPIO_ResetBits(GPIOG, GPIO_Pin_1);
    //延时1ms的程序
         for(count =0 ; count <1; count++)
    {
      for(i = 0; i < 6000; i++);
    }

}
只是修改了一下参数,G1脚高电平持续的时间,变化很大,这是什么原因呢?

mark0668 发表于 2015-5-13 14:54:50

这是肯定的,因为指令周期不同

快乐的小陈 发表于 2015-5-13 14:57:43

mark0668 发表于 2015-5-13 14:54
这是肯定的,因为指令周期不同

for(count =0 ; count < ms; count++)
    {
      for(i = 0; i < 1000; i++);
    }
外面的for和里面的for周期各是多少呢?

发表于 2015-5-13 14:59:16

一个for循环执行的时间差不多是4个机器周期,如果循环次数大可以忽略for的次数,小的话就要考虑了。如果想精确用定时器。

mark0668 发表于 2015-5-13 15:01:24

快乐的小陈 发表于 2015-5-13 14:57
for(count =0 ; count < ms; count++)
    {
      for(i = 0; i < 1000; i++);


这个你去查编程手册了,我也不清楚这个

916862920 发表于 2015-5-13 15:34:35

看一下反汇编就知道总共延时多少了,

wambob 发表于 2015-5-13 16:05:54

快乐的小陈 发表于 2015-5-13 14:57
for(count =0 ; count < ms; count++)
    {
      for(i = 0; i < 1000; i++);


软件延时是粗略延迟,不准的,建议是用定时器或系统滴答

bldc小菜鸟 发表于 2015-5-14 08:16:04

建议使用systemtick,你这种软件延时可以用在对延时时间不是很精确的场合,而且会受程序运行的影响,也就是说相同的延时程序在不同的运行情况下可能会有不同的延时时间。

sfee2002 发表于 2015-5-14 09:07:51

定时器之类的硬件时钟更准确些

stary666 发表于 2015-5-14 09:26:09

看看主频。。。。
页: [1] 2
查看完整版本: 软件延时:for循环中使用不同的参数,延时有很大的差异