[Atmel] 用SAM-BA或JLINK跑ATSAM4E16的程序(9)最简printf例程

[复制链接]
 楼主| ddllxxrr 发表于 2015-11-12 06:46 | 显示全部楼层 |阅读模式
大家在使用UART都想急于试下好不好用,而这个最好的方法是HELLO WORLD

我说HELLO WORLD 大家都应该明白,就是打印一段文字。但ATMEL SAM4E确很特别。

实现的模块叫Serial Standard I/O

大家别看不起我找到的模块,应为一般情况下只找UART或USART例程。

运行的效果如下:




程序如下:

  1. #include <board.h>
  2. #include <sysclk.h>
  3. #include <stdio_serial.h>

  4. #if SAM
  5. #include <conf_uart_serial.h>
  6. #else
  7. #include <conf_usart_serial.h>
  8. #endif

  9. /*! \brief Main function.
  10. */
  11. int main(void)
  12. {
  13.         uint32_t ch;

  14.         const usart_serial_options_t usart_serial_options = {
  15.                 .baudrate     = USART_SERIAL_EXAMPLE_BAUDRATE,
  16.                 .charlength   = USART_SERIAL_CHAR_LENGTH,
  17.                 .paritytype   = USART_SERIAL_PARITY,
  18.                 .stopbits     = USART_SERIAL_STOP_BIT
  19.         };

  20.         sysclk_init();

  21.         /* Initialize the board.  The board-specific conf_board.h file contains
  22.          * the configuration of the board initialization.
  23.          */
  24.         board_init();

  25.         // Initialize Serial Interface using Stdio Library
  26.         stdio_serial_init(USART_SERIAL_EXAMPLE, &usart_serial_options);

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

  29.         // Get and echo a character forever.
  30.         while (true) {
  31.                 scanf("%c",(char*)&ch);

  32.                 if (ch) {
  33.                         printf("%c",(char)ch);
  34.                 }
  35.         }
  36. }


解析一下:
首先声明了相应的宏。
#define CONF_BOARD_UART_CONSOLE
在conf_uart_serial.h里声明UART相关参数。

  1. #ifndef CONF_USART_SERIAL_H
  2. #define CONF_USART_SERIAL_H

  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif

  6. // USART serial configuration options
  7. #define USART_SERIAL_EXAMPLE            ((Usart*)CONSOLE_UART)

  8. #define USART_SERIAL_EXAMPLE_BAUDRATE   (115200)
  9. #define USART_SERIAL_CHAR_LENGTH        (US_MR_CHRL_8_BIT)
  10. #define USART_SERIAL_PARITY             (US_MR_PAR_NO)
  11. #define USART_SERIAL_STOP_BIT           (US_MR_NBSTOP_1_BIT)

  12. #ifdef __cplusplus
  13. }
  14. #endif

  15. #endif/* CONF_USART_SERIAL_H_INCLUDED */


用stdio_serial_init初始化串行标准I/O





本帖子中包含更多资源

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

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

本版积分规则

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

2403

主题

6994

帖子

68

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

2403

主题

6994

帖子

68

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