极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11092|回复: 7

01 两个闪灯实验的比较

[复制链接]
发表于 2013-10-17 21:53:55 | 显示全部楼层 |阅读模式
本帖最后由 lyy-cy 于 2013-10-17 21:55 编辑

    Arduino的好处自然不用再说,但是很多人说它效率低什么的。刚好在学单片机,也对arduino感兴趣,于是就做了个比较。先看第一段代码:
  1. /*
  2. 作者:极客工坊
  3. 时间:2012年5月18日
  4. 发表地址:[url]www.geek-workshop.com[/url]
  5. 程序说明:
  6. 使一个Led亮一秒,灭一秒,如此往复。
  7. */

  8. void setup() {               
  9.   // 初始化数字引脚,使其为输出状态。
  10.   // 大部分Arduino控制板上,数字13号引脚都有一颗Led。
  11.   pinMode(13, OUTPUT);     
  12. }

  13. void loop() {
  14.   digitalWrite(13, HIGH);   // 使Led亮
  15.   delay(1000);              // 持续1秒钟
  16.   digitalWrite(13, LOW);    // 使Led灭
  17.   delay(1000);              // 持续1秒钟。
  18. }

复制代码



然后看下面的这一段:


[pre lang="c" line="1"]#include <avr/io.h>
#include <util/delay.h>   //GCC内置的延时函数


int main(void)
{
  PORTB &= ~(1<<5) ; //PB5 清零
  DDRB |= (1<<5)  ;
  
  while(1)
  {
    PORTB |= (1<<5) ;      //PB5 高电平
    _delay_ms(50);   //调用延时函数
    PORTB &= ~(1<<5) ;     //PB5 清零
    _delay_ms(50);   //调用延时函数     
   }
     
}
[/code]

功能相似,但是生成的二进制文件大小却很大的差别。























以后用GCC来写一些我们力所能及的代码,这样可以保持效率。
然后又可以借用arduino丰富的库。岂不……


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2013-10-17 22:19:08 | 显示全部楼层
光一个 setup() 就要做很多事情
回复 支持 反对

使用道具 举报

发表于 2013-10-17 22:19:35 | 显示全部楼层
谢谢分享,
不过感觉实际上对于一般的玩家,两者差距不大,制约我们的一般不是硬件的空间和速度,而是好的创意。
嘿嘿

回复 支持 反对

使用道具 举报

发表于 2013-10-18 05:59:08 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2013-10-18 13:50:09 | 显示全部楼层
arduino有些库调试出错很麻烦啊啊啊
回复 支持 反对

使用道具 举报

发表于 2013-10-19 02:53:04 | 显示全部楼层
这个有点意义,可以深究下运行的速度。我觉得这个值得好好研究,甚至会改变一些人的编程习惯
回复 支持 反对

使用道具 举报

发表于 2013-10-20 11:17:46 | 显示全部楼层
digitalwrite的运行速度是超慢的,尤其是通过多个74HC595控制LED,你可能会看到闪烁。此时必须使用Direct Port Access(寄存器直接访问)

例:使用寄存器直接访问来控制74HC595
  1. const byte BIT_COUNT = 8;
  2. // latchPin=Digital 8(74HC595 pin 12)
  3. // clockPin=Digital 12(74HC595 pin 11)
  4. // dataPin=Digital 11(74HC595 pin 14)

  5. void setup() {
  6. //PORTB maps to Arduino digital pins 8 to 13
  7. //set bits 0,3,4 high, this will set digital pin 8,11,12 as output & all other pins as input
  8. DDRB=B00011001;
  9. }

  10. void loop() {
  11.   for (int col = 0; col < BIT_COUNT; col++) {
  12.     PORTB &= ~(1<<0); //set latch pin (bit 0) low, all other pin unchange
  13.     int data = 1 << col; //bitshift left to generate required serial data
  14.     ShiftOutLSBFIRST(data);
  15.     PORTB |= 1<<0; //set latch pin (bit 0) high, all other pin unchange
  16.     delay(200);
  17.   }
  18. }


  19. void ShiftOutLSBFIRST(int data) {
  20.   for(int i = 0; i < BIT_COUNT; i++) {
  21.     if (data & (1<<i)) { //LSB first
  22.       PORTB |= 1<<3; //set data pin (bit 3) high, all other pin unchange
  23.     } else {
  24.       PORTB &= ~(1<<3); //set data pin (bit 3) low, all other pin unchange

  25.   }

  26.     // generate a pulse for SERIAL CLOCK
  27.     PORTB &= ~(1 << 4); // Set the clockPin low
  28.     PORTB |= (1 << 4); // Set the clockPin high
  29.     // it can also use the code below to generate SERIAL CLOCK
  30.     // ^= toggle bit, doing this twice to generate a high low pulse
  31.     // PORTB ^= 1 << 4; //toogle bit 3, all other pin unchange
  32.     // PORTB ^= 1 << 4;
  33.   }
  34. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2013-11-23 16:08:28 | 显示全部楼层
《Arduino技术内幕》这本书对于代码的优化讲的挺细致的,楼主可以看一下
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-23 15:08 , Processed in 0.046469 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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