[Atmel] mbed环境下SAMR21开发板3:串口通讯

[复制链接]
 楼主| ddllxxrr 发表于 2016-2-10 20:38 | 显示全部楼层 |阅读模式
mbed串口上手也很方便哈。只要包括mbed.h函数即可,一些初始化在mbed.h中已经定义好了。

以下是mbed.h:
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *     http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_H
  17. #define MBED_H

  18. #define MBED_LIBRARY_VERSION 113

  19. #include "platform.h"

  20. // Useful C libraries
  21. #include <math.h>
  22. #include <time.h>

  23. // mbed Debug libraries
  24. #include "mbed_error.h"
  25. #include "mbed_interface.h"

  26. // mbed Peripheral components
  27. #include "DigitalIn.h"
  28. #include "DigitalOut.h"
  29. #include "DigitalInOut.h"
  30. #include "BusIn.h"
  31. #include "BusOut.h"
  32. #include "BusInOut.h"
  33. #include "PortIn.h"
  34. #include "PortInOut.h"
  35. #include "PortOut.h"
  36. #include "AnalogIn.h"
  37. #include "Ana**ut.h"
  38. #include "PwmOut.h"
  39. #include "Serial.h"
  40. #include "SPI.h"
  41. #include "SPISlave.h"
  42. #include "I2C.h"
  43. #include "I2CSlave.h"
  44. #include "Ethernet.h"
  45. #include "CAN.h"
  46. #include "RawSerial.h"

  47. // mbed Internal components
  48. #include "Timer.h"
  49. #include "Ticker.h"
  50. #include "Timeout.h"
  51. #include "LowPowerTimeout.h"
  52. #include "LowPowerTicker.h"
  53. #include "LowPowerTimer.h"
  54. #include "LocalFileSystem.h"
  55. #include "InterruptIn.h"
  56. #include "wait_api.h"
  57. #include "sleep_api.h"
  58. #include "rtc_time.h"

  59. using namespace mbed;
  60. using namespace std;

  61. #endif
可见只要你把API打对了,就可以工作了

而Serial.h中又把接收继承父类。

  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *     http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_SERIAL_H
  17. #define MBED_SERIAL_H

  18. #include "platform.h"

  19. #if DEVICE_SERIAL

  20. #include "Stream.h"
  21. #include "SerialBase.h"
  22. #include "serial_api.h"

  23. namespace mbed {

  24. /** A serial port (UART) for communication with other serial devices
  25. *
  26. * Can be used for Full Duplex communication, or Simplex by specifying
  27. * one pin as NC (Not Connected)
  28. *
  29. * Example:
  30. * @code
  31. * // Print "Hello World" to the PC
  32. *
  33. * #include "mbed.h"
  34. *
  35. * Serial pc(USBTX, USBRX);
  36. *
  37. * int main() {
  38. *     pc.printf("Hello World\n");
  39. * }
  40. * @endcode
  41. */
  42. class Serial : public SerialBase, public Stream {

  43. public:
  44. #if DEVICE_SERIAL_ASYNCH
  45.     using SerialBase::read;
  46.     using SerialBase::write;
  47. #endif

  48.     /** Create a Serial port, connected to the specified transmit and receive pins
  49.      *
  50.      *  @param tx Transmit pin
  51.      *  @param rx Receive pin
  52.      *
  53.      *  @note
  54.      *    Either tx or rx may be specified as NC if unused
  55.      */
  56.     Serial(PinName tx, PinName rx, const char *name=NULL);

  57. protected:
  58.     virtual int _getc();
  59.     virtual int _putc(int c);
  60. };

  61. } // namespace mbed

  62. #endif

  63. #endif
而实际操作只要掌握基本函数即可:


类名
方法
用途
Serial
Serial(PinName tx, PinName rx, const char *name=NULL);
构造函数,把tx,rx设成Uart的输出输入管脚
void baud(int baudrate);
设置Uart的波特率,默认为9600
void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
设置Uart传输的格式,包括一个字长的位数、奇偶检验的方法、停止位的位数,默认为一个字长为8位,无奇偶检验,1位停止位
int readable();
返回Uart是否有数据到达
int writeable();
返回Uart是否还有空间进行数据发送
void attach(void (*fptr)(void), IrqType type=RxIrq);
设置Uart中断是需要执行的用户自定义函数
void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
设置Uart的流控方法,其目标是提高数据发送的可靠性,流控方法有无流控,RTS流控,CTS流控,RTSCTS流控,后面两个常数为流控的管脚设置
int getc()
从Uart读取一个字符
int putc(int c);
向Uart发送一个字符
int printf(const char* format, ...)
格式化Uart的输出,参数等同标准C的printf
int scanf(const char* format, ...);
格式化Uart的输入,参数等同标准C的scanf




以下是我的程序:
  1. #include "mbed.h"



  2. Serial pc(USBTX, USBRX);

  3. int main() {
  4.      while(1)
  5.      {
  6.      pc.printf("Hello World\n");
  7.      wait(0.2);
  8.      }
  9. }
以下是编译通过的截图:



以下是输出结果。总体感觉mbed上手极速地快。


本帖子中包含更多资源

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

×
shaoziyang 发表于 2016-2-10 21:02 | 显示全部楼层
R21的ADC采样波动比较大,可能是什么原因?
 楼主| ddllxxrr 发表于 2016-2-11 11:24 | 显示全部楼层
shaoziyang 发表于 2016-2-10 21:02
R21的ADC采样波动比较大,可能是什么原因?

可能是外围电路不稳,或ADC采得太快,再有分辨率设低了
枫吹落 发表于 2018-8-8 18:06 | 显示全部楼层
您好,请问宏定义中DEVICE_SERIAL和DEVICE_SERIAL_ASYNCH有什么功能上的区别吗?串口本来就是异步的呀?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2403

主题

6994

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2403

主题

6994

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部