Arduino 背景可以参考官方网站www.arduino.cc

先看一个最简单的示例程序:

打开 Arduino IDE , 选择菜单:文件 -> 示例 -> 01.Basics -> Blink

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain.
*/ // Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13; // the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
} // the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

  

程序主体很简单,提供了 setup() 和 loop() 二个函数。

setup 函数做一些初始化的工作,在系统上电或复位后,此函数只会执行一次。

loop 函数会在 setup 之后一直循环运行。

在了解功能之后,我们可能对背后的机制很感兴趣,那么我们可以到安装目录下打开代码文件:C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino\main.cpp  代码如下:

#include <Arduino.h>

int main(void)
{
init(); #if defined(USBCON)
USBDevice.attach();
#endif setup(); for (;;) {
loop();
if (serialEventRun) serialEventRun();
} return 0;
}

相信您看到了这段代码,就该知道 setup 和 loop 函数的前世今生了吧。在 loop 函数运行之后,我们还会看到 serialEventRun 的函数,此函数的功能是当串口有数据过来的时候,它可以调用Arduino的另一个函数 serialEvent。

打开 Arduino IDE , 选择菜单:文件 -> 示例 -> 04.Communication -> SerialEvent 具体看下面的代码:

/*
Serial Event example When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it. A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences. Created 9 May 2011
by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/SerialEvent */ String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
} void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
} /*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}

此代码的功能是:系统上电后,接收串口的输入数据并发送回去,类似 echo。系统的实现是通过在主循环判断全局变量 stringComplete 的状态来决定是否发送接收到的数据。而 stringComplete 的状态是在 serialEvent 这个函数里赋值的。根据 serialEvent 函数注释看,此函数的调用是在每次 loop 函数运行之后才执行的。再回到我们之前的代码:

for (;;) {
loop();
if (serialEventRun) serialEventRun();
}

通过上面的代码,可以很明确的看出 serialEventRun 函数是在 loop 函数之后执行的。如果我们有多个串口,比如 2560 的板子提供了4个串口,那么 serialEventRun 函数又是如何处理的呢,我们打开代码文件:C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino\HardwareSerial.cpp 找到 serialEventRun 函数的实现,代码如下:

void serialEventRun(void)
{
#ifdef serialEvent_implemented
if (Serial.available()) serialEvent();
#endif
#ifdef serialEvent1_implemented
if (Serial1.available()) serialEvent1();
#endif
#ifdef serialEvent2_implemented
if (Serial2.available()) serialEvent2();
#endif
#ifdef serialEvent3_implemented
if (Serial3.available()) serialEvent3();
#endif
}

serialEventRun 的函数体中是依次的调用 serialEvent() serialEvent1() serialEvent2() serialEvent3() 函数的。如果您的应用需要通过多个串口读写数据,那么在使用 serialEvent 函数的过程中,就要考虑接受的数据长度以及函数的处理时间,否则极有可能会导致其它串口的接收缓冲区满而影响应用。

**** 看门狗 (测试卡死了 bootloader) 请谨慎操作 ****

最新的 Arduino 1.5.6-r2 烧写的bootloader 已经支持看门狗了。

重新烧写mega2560的bootloader 烧写完之后 用usb直接编译下载 已经可以支持avr的这个wdt.h了,不用再像之前那样非得用ISP下载程序了。

使用代码如下:

/*------ avr看门狗测试 -----*/

#include <avr/wdt.h>
void setup()
{
pinMode(13,OUTPUT);
wdt_enable(WDTO_4S); //开启看门狗,并设置溢出时间为4秒
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
delay(100);
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
delay(100);
} void loop()
{
digitalWrite(13,HIGH);
delay(600);
digitalWrite(13,LOW);
delay(600);
//wdt_reset(); //喂狗操作,使看门狗定时器复位
}

溢出时间如下:

序号
常量
定义
1
WDTO_15MS
看门狗定时器15ms超时
2
WDTO_30MS
看门狗定时器30ms超时
3
WDTO_60MS
看门狗定时器60ms超时
4
WDTO_120MS
看门狗定时器120ms超时
5
WDTO_250MS
看门狗定时器250ms超时
6
WDTO_500MS
看门狗定时器500ms超时
7
WDTO_1S
看门狗定时器1S超时
8
WDTO_2S
看门狗定时器2S超时
9
WDTO_4S
看门狗定时器4S超时
10
WDTO_8S
看门狗定时器8S超时

  

Arduino Ethernet Shield MEGA hack

http://mcukits.com/2009/04/06/arduino-ethernet-shield-mega-hack/

or

pinMode(11,INPUT);
pinMode(12,INPUT);
pinMode(13,INPUT);

 

Arduino 学习的更多相关文章

  1. Arduino学习笔记① 初识Arduino

    1.前言     近段时间,博主陆续更新了ESP8266学习笔记,主要开发平台是Arduino.但是,对于很多无基础的初学者来说,甚至不了解Arduino是什么.因此,博主决定加入一个Arduino学 ...

  2. Arduino学习经验(一)之解决舵机库和pwm输出冲突

    一.前言 最近在公司学习Arduino uno ,用它实现小车超声波避障功能.实现的功能很简单,就是在小车前方挂一个超声波模块,当碰到障碍物时,会通过舵机进行摆头,判断两边的距离,进行左右转弯.但是碰 ...

  3. Arduino学习笔记⑤ 模拟IO实验

    1.前言     还记得前几个我们都是在讲解数字IO,而其实我们生活中大多数信号都是模拟信号,如声音以及温度变化.在Arduino中,常用0~5v的电压来表示模拟信号. 1.1 模拟输入功能      ...

  4. Arduino学习笔记② Arduino语言基础

    授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...

  5. Arduino—学习笔记—基础语法

    图解 函数具体讲解 pinMode(工作接脚,模式) 工作接脚 工作接脚编号(0--13与A0--A5) 模式 工作模式:INPUT或OUTPUT 例子 将8接口设置为输出模式 pinMode(8,O ...

  6. 基于Proteus仿真的Arduino学习(2)——LED点阵探究A(LED点阵基础)

    一.前言: 随着LED的普及,以LED点阵为基础的显示设置层出不穷.例如,公交车的线路提示牌.高速公路的信息提示牌,安装在大楼上的广告屏幕等.下面,我们将由简单到复杂地探索各种LED点阵的使用方法,同 ...

  7. 基于Proteus仿真的Arduino学习(1)——Arduino Uno最小系统及LED的简单使用

    一.前言:  A.Arduino简介 Arduino是由一个欧洲开发团队于2005年冬季开发.其成员包括Massimo Banzi.David Cuartielles.Tom Igoe.Gianluc ...

  8. Arduino学习笔记二:修改LED点灯程序

    看了开源社区的LED控制程序,开始上手代码编写,修改,下载以及调试,原文地址:http://www.arduino.cn/thread-1072-1-1.html,这个帖子写的比较通俗易懂. 自己移植 ...

  9. 四位数码管SH5461AS的问题,arduino学习实测.

    arduino入门教程到第16课遇到些问题.效果一直是混乱的状态. 琢磨了半天发现一些问题,和大家分享下 1)接线图,原图没有问题,只是比较含糊,线比较多不好看. 我用红色数字标示数码管的12个脚,并 ...

随机推荐

  1. 对象池与.net—从一个内存池实现说起

    本来想写篇关于System.Collections.Immutable中提供的ImmutableList里一些实现细节来着,结果一时想不起来源码在哪里--为什么会变成这样呢--第一次有了想写分析的源码 ...

  2. What's New in C# 6.0

    Static Types as using So, we are all quite familiar with this notion of accessing static class membe ...

  3. C#结合LumiSoft.Net.dll读取Outlook邮件(.eml格式邮件)

    如果直接从Outlook(或者微软的其它邮件客户端如:Outlook Express.Windows Live Mail)的邮件文件(.eml格式)中提取各种电子邮件内容,使用LumiSoft.Net ...

  4. 用sass画蜗牛

    一.sass的好处 用css画图也算是简单的实战吧,虽然用到的东西还比较少..用过之后,发现sass主要有以下优势: 可维护性.最重要的一点,可维护性的很大一部分来自变量 嗯,最简单的例子,画图总要有 ...

  5. NodeJS package.json

    #3 NodeJS package.json 工作目录 package.json 导航至工作目录后,执行下图中的命令

  6. js 合并数组

    <script type="text/javascript">               var a = '[{"name":"aaa& ...

  7. Book Review: PowerShell 3.0 Advanced Administration Handbook

    Recently I read a book, PowerShell 3.0 Advanced Administration Handbook, which I found really worthy ...

  8. HTML5中已经不支持元素汇总,持续更新

    HTML5中已经不支持以下的元素,不建议在进行开发时再使用以下的元素. 1.acronym(建议abbr) : 定义首字母缩写 2.applet(建议object):  定义 applet 3.bas ...

  9. Atitit.识别损坏的图像

    Atitit.识别损坏的图像 判断jpg图像损坏原理.读取gray line perc ent Png图片送货原理,直接回报EOFException /atiplat_cms/src/com/atti ...

  10. atitit。 hb Hibernate sql 查询使用

    atitit. hb  Hibernate sql 查询使用 #----------返回list<map>法..这个推荐使用.      q.setResultTransformer(Tr ...