[Atmel] 跑一下SAM4N例程(二十二):USART1

[复制链接]
906|0
 楼主| ddllxxrr 发表于 2015-4-9 21:59 | 显示全部楼层 |阅读模式
在Atmle Studio6.2的开发环境直接插入开发板。就可直接打开例程。

今天例程简单,通过串口打印一个字符串,然后接收用户的输入字符,每输入一个回显一个

我觉得高难的是初始化函数用了大量的指针:

  1. */
  2. static inline void stdio_serial_init(volatile void *usart, const usart_serial_options_t *opt)
  3. {
  4.         stdio_base = (void *)usart;
  5.         ptr_put = (int (*)(void volatile*,char))&usart_serial_putchar;
  6.         ptr_get = (void (*)(void volatile*,char*))&usart_serial_getchar;
  7. # if (XMEGA || MEGA_RF)
  8.         usart_serial_init((USART_t *)usart,opt);
  9. # elif UC3
  10.         usart_serial_init(usart,(usart_serial_options_t *)opt);
  11. # elif SAM
  12.         usart_serial_init((Usart *)usart,(usart_serial_options_t *)opt);
  13. # else
  14. #  error Unsupported chip type
  15. # endif

  16. # if defined(__GNUC__)
  17. #  if (XMEGA || MEGA_RF)
  18.         // For AVR GCC libc print redirection uses fdevopen.
  19.         fdevopen((int (*)(char, FILE*))(_write),(int (*)(FILE*))(_read));
  20. #  endif
  21. #  if UC3 || SAM
  22.         // For AVR32 and SAM GCC
  23.         // Specify that stdout and stdin should not be buffered.
  24.         setbuf(stdout, NULL);
  25.         setbuf(stdin, NULL);
  26.         // Note: Already the case in IAR's Normal DLIB default configuration
  27.         // and AVR GCC library:
  28.         // - printf() emits one character at a time.
  29.         // - getchar() requests only 1 byte to exit.
  30. #  endif
  31. # endif
  32. }
平时用,我想不用管这些函数只是那来用就行。

接下来是打印一串字符然后就等待用户输入

  1. // Print welcome message
  2.         printf("\n\rHello ATMEL World!\n\r");

  3.         // Get and echo a character forever.
  4.         while (true) {
  5.                 scanf("%c",(char*)&ch);

  6.                 if (ch) {
  7.                         printf("%c",(char)ch);
  8.                 }
  9.         }


这个例和调用的大量的库函数:

  1. read.c : System implementation function used by standard library
  2. * - write.c : System implementation function used by standard library


本例运行如下:


本帖子中包含更多资源

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

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

本版积分规则

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

2404

主题

7001

帖子

68

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