极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15786|回复: 9

for循环居然会死机

[复制链接]
发表于 2014-2-18 22:11:47 | 显示全部楼层 |阅读模式
本帖最后由 shihaipeng04 于 2014-2-19 02:26 编辑

玩了几天的arduino越来越发现这东西不靠谱,几天编个小程序居然会死机?
想测量一个传感器的分辨率,那就搞一个大数组,然后循环的读传感器,把数值都记录到大数组里,开始的时候读一次millis(),结束后再读一个millis(),这样就知道读5000次的时间了,然后把数据用Serial.println打出来,再用excel分析一下,就能看到传感器发生变化用了多长时间了。 开始没接传感器,写程序的时候1000次大约是 900毫秒,觉得有点短,怕来不及做动作,就改成了10000,结果说这个数组太大了。最后8000可以通过编译。可是接好光线传感器后死机了。检测了半天发现循环到220次的时候循环变量就不增加了。后来改小数组变为360左右就不增加了。太神奇了。。。请大侠帮忙分析分析

  1. void loop()
  2. {  
  3. int i;
  4. unsigned long t[5000],st,ed;

  5. st = millis();
  6. for(i=0; i < 5000; i++)
  7. {
  8.    t[i]=analogRead(A1); //i这220的时候就不再增加了
  9. }
  10. ed = millis();

  11. for(i=0; i < 5000; i++)
  12. {
  13.    Serial.println(t[i]);   
  14.    delayMicroseconds(1);
  15. }
复制代码



——————————————华丽分割结贴线————————————————
问题解决,多谢楼下几位,原来我都没注意到,这东西内存居然这么小,2k的sram  难怪会崩溃,测试的时候接了个pc喇叭,声音都变调了。哈哈,后来改成了int型的800,也算勉强够用了。

最后得了一个结论,uno r3的处理速度还不错,从模拟口读800次,并写入数组,这个工作只用了90毫秒,每次0.1125毫秒。
回复

使用道具 举报

发表于 2014-2-18 22:19:50 | 显示全部楼层
资源不够吧
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-18 22:21:18 | 显示全部楼层
本帖最后由 shihaipeng04 于 2014-2-18 22:27 编辑
林定祥 发表于 2014-2-18 22:19
资源不够吧


现在改成while也不行,还死。。。。太神奇了。看来这小东西搞大数据是有点捉襟见肘了。
回复 支持 反对

使用道具 举报

发表于 2014-2-19 00:30:12 | 显示全部楼层
这应该是运行空间的限制吧,你这个函数栈空间就需要 2+4*5000+4+4=20010字节,栈里还要压返回地址,也就是需要大约19.55K字节,有这么大的运行空间么?不知道你用什么板子,查查手册看。
回复 支持 反对

使用道具 举报

发表于 2014-2-19 00:34:15 | 显示全部楼层
栈溢出了,你先把unsigned long t[5000]移动到函数外部试试,局部变量是申明在栈上的,栈上空间很小。
回复 支持 反对

使用道具 举报

发表于 2014-2-19 01:12:45 | 显示全部楼层
内存溢出了

没记错的话328P只有1k的RAM,这才够几个long啊。
回复 支持 反对

使用道具 举报

发表于 2014-2-19 07:22:34 | 显示全部楼层
本帖最后由 friskit 于 2014-2-19 07:25 编辑

开的数组太大了内存不够。
换个类型

建议在最后一行加上 delete[] t。。。。否则就是在无限申请内存但是从来不释放。。。。
回复 支持 反对

使用道具 举报

发表于 2014-2-28 11:50:22 | 显示全部楼层
friskit 发表于 2014-2-19 07:22
开的数组太大了内存不够。
换个类型

不要瞎说。又没有new哪需要delete。
回复 支持 反对

使用道具 举报

发表于 2014-2-28 17:13:56 | 显示全部楼层
转角 发表于 2014-2-28 11:50
不要瞎说。又没有new哪需要delete。

不好意思。。没看清……
回复 支持 反对

使用道具 举报

发表于 2014-2-28 23:24:51 | 显示全部楼层
本帖最后由 Super169 于 2014-2-28 23:27 编辑

請看看這裡: http://arduino.cc/en/Tutorial/Memory#.UxCpO_mSwfg

The ATmega328 chip found on the Uno has the following amounts of memory:
Flash  32k bytes (of which .5k is used for the bootloader)
SRAM   2k bytes
EEPROM 1k byte

The ATmega2560 in the Mega2560 has larger memory space :
Flash  256k bytes (of which 8k is used for the bootloader)
SRAM   8k bytes
EEPROM 4k byte

如果你用的是 ATmega328 chip, SRAM 只有 2k bytes.

SRAM (static random access memory) is where the sketch creates and manipulates variables when it runs.

If you run out of SRAM, your program may fail in unexpected ways; it will appear to upload successfully, but not run, or run strangely. To check if this is happening, you can try commenting out or shortening the strings or other data structures in your sketch (without changing the code). If it then runs successfully, you're probably running out of SRAM.

如果 SRAM 不足, 會出現不正常結果.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-9 17:43 , Processed in 0.046896 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表