注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

W_中原的博客笔记

通知:新博客日志将更新到个人站点:http://www.zhgyuan.cn/

 
 
 

日志

 
 

Arduino之与单片机  

2013-08-09 11:30:05|  分类: 嵌入式 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
这两天因为需要熟悉了下Arduino。主要是看重了它可以输出多路PWM和UART功能。14路PWM跟4路独立UART真的很强大。

在正式使用它之前我也查了些关于Arduino的资料,对这个突然之间冒出来的好像很高技术的名次产生了好奇。原来它用的就是AVR的控制器,只不过做成了系统模块。

用的是2560的开发板,把资料拿到手以后按步骤搭好了环境,很轻易的就建好了。接着看了下数据手册,再看了例程代码,对Arduino了解的有个很清晰的概念了。

才知道它的控制是多么的方便简单,至少跟单片机相比是这样,比如UART输出,Serial.begin(9600);一条语句就打开了串口并设置好了波特率,而单片机还要配置定时器,比较寄存器,还要算波特率,Serial.println("Arduino");就可以从串口输出“Arduino”,PWM输出只要analogWrite(pin,value),就可以设定哪个引脚输出多大的脉宽了。其他设置也都很简单,不到一天就上手了。

这让我想起了PLC,Arduino的控制很像PLC,编程简单明了,一个工程不需要很多代码,如此开发周期大大缩小了,如果是单片机的话有时一个简单的串口通信可能都要调试很久。

但个人觉得虽然Arduino编程简单也有它的劣势,虽然它很容易操作但感觉还是没有单片机灵活,本质上来讲arduino也算是单片机,它应该是给自动控制或是非专业的来使用的。我只是用它来控制电机和串口通信,因为方便的多。

算是了解了下arduino吧,我还是偏向于单片机。大家有兴趣也可以学习下。感觉就是入门很简单,而且控制很方便,对于非专业的童鞋来说是件好事,因为没必要为了个简单的控制去学很深的编程,但建议若是做开发学习的话最好还是选择普通的单片机(虽然Arduino就是),如PIC、MSP430、STM32之类的,我把自己之前调试过的一个小程序分享给大家,是用串口控制小车的方向和速度的小程序:

int ledPin=13;
int p1=8;
int p2=9;
int p3=10;
int p4=11;
int value=0;
int la=10;
int val;
void setup()
{
    pinMode(ledPin,OUTPUT);
    pinMode(p1,OUTPUT);
   pinMode(p2,OUTPUT);
    pinMode(p3,OUTPUT);
    pinMode(p4,OUTPUT);
    Serial.begin(9600); // 打开串口,设置波特率为9600 bps
}

void loop()
{

 if (Serial.available() > 0) //判断串口缓冲器是否有数据装入
{
  digitalWrite(ledPin,HIGH);
   val=Serial.read();//读取串口
    if(val=='w')
       {
        Serial.println("up");
        digitalWrite(p1,LOW);
        digitalWrite(p3,LOW);
        analogWrite(p2,value);
        analogWrite(p4,value);
       }
         if(val=='s')
       {
        Serial.println("down");
        analogWrite(p1,value);
        analogWrite(p3,value);
        digitalWrite(p2,LOW);
        digitalWrite(p4,LOW);
       }
        if(val=='a')
       {
        Serial.println("left");
        digitalWrite(p1,LOW);
        analogWrite(p3,value);
        analogWrite(p2,value);
        digitalWrite(p4,LOW);       
      }
       if(val=='d')
       {
        Serial.println("right");
        analogWrite(p1,value);
        digitalWrite(p3,LOW);
        digitalWrite(p2,LOW);
        analogWrite(p4,value);
       }
       if(val=='o')
       {
        Serial.println("+1");
        value=value+la;
        if(value==250);
        {
          value==100;
        }
       }
       if(val=='p')
       {
        Serial.println("-1");
        value=value-la;
        if(value==0);
        {
          value==100;
        }
       }
  }
}

上面的代码作用是用串口调试助手发送相应的字符(我用的是无线串口,不然老拖根线车没法跑。。),w:前进,s:后退,a:左转,d:右转,o:加速(每次加10 PWM),p:减速

最后把Arduino的常用函数分享给大家(从官网找的)

write()


Description

Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.

Syntax

Serial.write(val) 

Serial.write(str) 

Serial.write(buf, len)

Arduino Mega also supports: Serial1, Serial2, Serial3 (in place of Serial)

Parameters

val: a value to send as a single byte

str: a string to send as a series of bytes

buf: an array to send as a series of bytes

len: the length of the buffer

Returns

byte

write() will return the number of bytes written, though reading that number is optional

Example

void setup(){

  Serial.begin(9600);

}


void loop(){

  Serial.write(45); // send a byte with the value 45


   int bytesSent = Serial.write(“hello”); //send the string “hello” and return the length of the string.

}


read()


Description

Reads incoming serial data. read() inherits from the Stream utility class.

Syntax

Serial.read()

Arduino Mega only: 

Serial1.read() 

Serial2.read() 

Serial3.read()

Parameters

None

Returns

the first byte of incoming serial data available (or -1 if no data is available) - int

Example

int incomingByte = 0;   // for incoming serial data


void setup() {

        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps

}


void loop() {


        // send data only when you receive data:

        if (Serial.available() > 0) {

                // read the incoming byte:

                incomingByte = Serial.read();


                // say what you got:

                Serial.print("I received: ");

                Serial.println(incomingByte, DEC);

        }

}

print()


Description

Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is. For example:

Serial.print(78) gives "78"

Serial.print(1.23456) gives "1.23"

Serial.print('N') gives "N"

Serial.print("Hello world.") gives "Hello world."

An optional second parameter specifies the base (format) to use; permitted values are BIN (binary, or base 2), OCT (octal, or base 8), DEC (decimal, or base 10), HEX (hexadecimal, or base 16). For floating point numbers, this parameter specifies the number of decimal places to use. For example:

Serial.print(78, BIN) gives "1001110"

Serial.print(78, OCT) gives "116"

Serial.print(78, DEC) gives "78"

Serial.print(78, HEX) gives "4E"

Serial.println(1.23456, 0) gives "1"

Serial.println(1.23456, 2) gives "1.23"

Serial.println(1.23456, 4) gives "1.2346"

You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :

Serial.print(F(“Hello World”))

To send a single byte, use Serial.write().

Syntax

Serial.print(val) 

Serial.print(val, format)

Parameters

val: the value to print - any data type

format: specifies the number base (for integral data types) or number of decimal places (for floating point types)

Returns

size_t (long): print() returns the number of bytes written, though reading that number is optional

Example:

/*

Uses a FOR loop for data and prints a number in various formats.

*/

int x = 0;    // variable


void setup() {

  Serial.begin(9600);      // open the serial port at 9600 bps:    

}


void loop() {  

  // print labels 

  Serial.print("NO FORMAT");       // prints a label

  Serial.print("\t");              // prints a tab


  Serial.print("DEC");  

  Serial.print("\t");      


  Serial.print("HEX"); 

  Serial.print("\t");   


  Serial.print("OCT");

  Serial.print("\t");


  Serial.print("BIN");

  Serial.print("\t"); 


  for(x=0; x< 64; x++){    // only part of the ASCII chart, change to suit


    // print it out in many formats:

    Serial.print(x);       // print as an ASCII-encoded decimal - same as "DEC"

    Serial.print("\t");    // prints a tab


    Serial.print(x, DEC);  // print as an ASCII-encoded decimal

    Serial.print("\t");    // prints a tab


    Serial.print(x, HEX);  // print as an ASCII-encoded hexadecimal

    Serial.print("\t");    // prints a tab


    Serial.print(x, OCT);  // print as an ASCII-encoded octal

    Serial.print("\t");    // prints a tab


    Serial.println(x, BIN);  // print as an ASCII-encoded binary

    //                             then adds the carriage return with "println"

    delay(200);            // delay 200 milliseconds

  }

  Serial.println("");      // prints another carriage return

}

available()


Description

Get the number of bytes (characters) available for reading from the serial port. This is data that's already arrived and stored in the serial receive buffer (which holds 64 bytes). available() inherits from the Stream utility class.

Syntax

Serial.available()

Arduino Mega only: 

Serial1.available() 

Serial2.available() 

Serial3.available()

Parameters

none

Returns

the number of bytes available to read

Example

int incomingByte = 0;   // for incoming serial data


void setup() {

        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps

}


void loop() {


        // send data only when you receive data:

        if (Serial.available() > 0) {

                // read the incoming byte:

                incomingByte = Serial.read();


                // say what you got:

                Serial.print("I received: ");

                Serial.println(incomingByte, DEC);

        }

}



  评论这张
 
阅读(2772)| 评论(1)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017