打印

【我的DIY设计】问题求助:STM32F3 DISCOVERY开发板延时函数错误

[复制链接]
4565|31
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
在调试STM32F3 DISCOVERY开发板的时候,测试了一个LED闪灯程序,这个应该是很基本的,刚开始用软件延时,后来用了SYTSTICK 定时器延时,编写的delay_ms()这个函数,设置延时 350ms 以下时,程序正常,
设置delay_ms(400),程序就会死在这里,400以上也同样,用了定时器和软件延时都是一样的问题,感觉比较奇怪,求大侠指导!谢谢
沙发
uet_cache| | 2013-1-6 18:48 | 只看该作者
先帖上关键代码给大家看看撒。。。

使用特权

评论回复
板凳
jomosiron| | 2013-1-6 21:40 | 只看该作者
定义的参数类型对吧?不看代码真不清楚

使用特权

评论回复
地板
airwill| | 2013-1-6 22:21 | 只看该作者
我也感觉很可能延时计数的变量类型是不是小了, 比如用 unsigned char , 而定时长度却超过了 255, 导致永远都到不了设定的定时值, 就死在那里了.

使用特权

评论回复
5
fengye5340|  楼主 | 2013-1-7 09:21 | 只看该作者
airwill 发表于 2013-1-6 22:21
我也感觉很可能延时计数的变量类型是不是小了, 比如用 unsigned char , 而定时长度却超过了 255, 导致永远 ...

我用的是long 整型,变量定义最够了,可是一旦超过350,就不行了,后来在官网里面的例程上改了,还是出现同样的问题,现在把修改官网的例程附上,延时参数改成350以下是正常的

/**
  ******************************************************************************
  * @file    GPIO_IOToggle/main.c
  * @author  MCD Application Team
  * @version V1.1.0
  * @date    20-September-2012
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/** @addtogroup STM32F3_Discovery_Peripheral_Examples
  * @{
  */
/** @addtogroup GPIO_IOToggle
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define BSRR_VAL 0xC000
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef        GPIO_InitStructure;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void Delay(__IO uint32_t nCount)
{
  
  while (nCount != 0)
  {
  nCount--;
  }
}
void Delay_Us(__IO uint32_t nCount)
{
  while (nCount != 0)
  {
   nCount--;  
    Delay(150);
  }
}
void Delay_Ms(__IO uint32_t nCount)
{
  
  while (nCount != 0)
  {
nCount--;  
Delay(15000);
  }
}



/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
  this is done through SystemInit() function which is called from startup
  file (startup_stm32f30x.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32f30x.c file
  */
  
  /* GPIOE Periph clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
  
  /* Configure PE14 and PE15 in output pushpull mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
  
  /* To achieve GPIO toggling maximum frequency, the following  sequence 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( GPIOE,GPIO_Pin_14 | GPIO_Pin_15);
     
     Delay_Ms(500);
   
     GPIO_ResetBits( GPIOE,GPIO_Pin_14 | GPIO_Pin_15);
  
     Delay_Ms(500);
  
  }
}
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  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 can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
/**
  * @}
  */
/**
  * @}
  */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

使用特权

评论回复
6
jlass| | 2013-1-7 09:26 | 只看该作者
八成是定时器的配置值不对。

使用特权

评论回复
7
fengye5340|  楼主 | 2013-1-7 09:38 | 只看该作者
jlass 发表于 2013-1-7 09:26
八成是定时器的配置值不对。

可是,这里面只是最简单的软件延时,没有用到定时器呢

使用特权

评论回复
8
雨落沉轩| | 2013-1-7 10:19 | 只看该作者
我用了一下你写的延时函数在我的程序跑了一下,没问题,我延时了1000都OK。但实在不知道你的问题出在哪。

使用特权

评论回复
9
uet_cache| | 2013-1-7 10:25 | 只看该作者
呵呵,程序上看起来没什么问题。要么你检查你的软件是不是支持F3,或者软件是不是注册了,。。或者其它,,

使用特权

评论回复
10
fengye5340|  楼主 | 2013-1-7 11:43 | 只看该作者
雨落沉轩 发表于 2013-1-7 10:19
我用了一下你写的延时函数在我的程序跑了一下,没问题,我延时了1000都OK。但实在不知道你的问题出在哪。 ...

楼主用的那个版本的KEIL软件呢?

使用特权

评论回复
11
fengye5340|  楼主 | 2013-1-7 11:44 | 只看该作者
uet_cache 发表于 2013-1-7 10:25
呵呵,程序上看起来没什么问题。要么你检查你的软件是不是支持F3,或者软件是不是注册了,。。或者其它,, ...

现在还有更奇怪的问题,在main()主函数内写入了一段代码,没有放在循环中执行,结果,这些代码不是正常的仅运行一次,而是循环执行,不知道这个问题有什么解决方法吗?谢谢

使用特权

评论回复
12
uet_cache| | 2013-1-7 11:50 | 只看该作者
你可以先看下你的MDK中有没有F3芯片的选择,FLASH选 项对不对。还有,工程你是自己建的还是用现成的?

看你的BOOT设置对不对,,  如果你的程序不能正确顺序执行,就先不要看程序,而是最基本的工程配置和建立了。。

使用特权

评论回复
13
fengye5340|  楼主 | 2013-1-7 13:02 | 只看该作者
雨落沉轩 发表于 2013-1-7 10:19
我用了一下你写的延时函数在我的程序跑了一下,没问题,我延时了1000都OK。但实在不知道你的问题出在哪。 ...

我现在也查不出问题出在什么地方,楼主能把你的工程文件发我一份,我测试一下行吗?谢谢

使用特权

评论回复
14
acgean| | 2013-1-7 13:18 | 只看该作者
无法弄清楚问题时, 我喜欢看汇编代码, 可分上编译后的 LST(或 TXT) 文件

使用特权

评论回复
15
雨落沉轩| | 2013-1-7 13:53 | 只看该作者
fengye5340 发表于 2013-1-7 13:02
我现在也查不出问题出在什么地方,楼主能把你的工程文件发我一份,我测试一下行吗?谢谢 ...

好的

m_GPIO_IOToggle.zip

2 MB

使用特权

评论回复
16
fengye5340|  楼主 | 2013-1-7 14:41 | 只看该作者
acgean 发表于 2013-1-7 13:18
无法弄清楚问题时, 我喜欢看汇编代码, 可分上编译后的 LST(或 TXT) 文件

谢谢指导

使用特权

评论回复
17
fengye5340|  楼主 | 2013-1-7 14:42 | 只看该作者
雨落沉轩 发表于 2013-1-7 13:53
好的

非常感谢!

使用特权

评论回复
18
fengye5340|  楼主 | 2013-1-7 15:37 | 只看该作者
雨落沉轩 发表于 2013-1-7 13:53
好的

楼主,我用你的程序测试了一下,还是不行,改成100时就没问题,500以上问题照旧,看来不是软件的问题,好像是芯片本身的问题

使用特权

评论回复
19
云仔| | 2013-1-7 19:19 | 只看该作者
fengye5340 发表于 2013-1-7 11:44
现在还有更奇怪的问题,在main()主函数内写入了一段代码,没有放在循环中执行,结果,这些代码不是正常的 ...

我昨天也遇到过这样的问题,就是在主函数里的一句初始化的程序理应执行一次就够了却一直循环执行,后来发现是我的IAR的配置有问题,不知道你用的是不是IAR

使用特权

评论回复
20
fengye5340|  楼主 | 2013-1-7 19:29 | 只看该作者
云仔 发表于 2013-1-7 19:19
我昨天也遇到过这样的问题,就是在主函数里的一句初始化的程序理应执行一次就够了却一直循环执行,后来发 ...

我用的是KEIL MDK4.6,楼主后来改了哪个地方就正常了呢?求指导,谢谢

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

99

主题

454

帖子

11

粉丝