大家在使用UART都想急于试下好不好用,而这个最好的方法是HELLO WORLD
我说HELLO WORLD 大家都应该明白,就是打印一段文字。但ATMEL SAM4E确很特别。
实现的模块叫Serial Standard I/O
大家别看不起我找到的模块,应为一般情况下只找UART或USART例程。
运行的效果如下:
程序如下:
#include <board.h>
#include <sysclk.h>
#include <stdio_serial.h>
#if SAM
#include <conf_uart_serial.h>
#else
#include <conf_usart_serial.h>
#endif
/*! \brief Main function.
*/
int main(void)
{
uint32_t ch;
const usart_serial_options_t usart_serial_options = {
.baudrate = USART_SERIAL_EXAMPLE_BAUDRATE,
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};
sysclk_init();
/* Initialize the board. The board-specific conf_board.h file contains
* the configuration of the board initialization.
*/
board_init();
// Initialize Serial Interface using Stdio Library
stdio_serial_init(USART_SERIAL_EXAMPLE, &usart_serial_options);
// Print welcome message
printf("\n\rHello ATMEL World!\n\r");
// Get and echo a character forever.
while (true) {
scanf("%c",(char*)&ch);
if (ch) {
printf("%c",(char)ch);
}
}
}
解析一下:
首先声明了相应的宏。
#define CONF_BOARD_UART_CONSOLE
在conf_uart_serial.h里声明UART相关参数。
#ifndef CONF_USART_SERIAL_H
#define CONF_USART_SERIAL_H
#ifdef __cplusplus
extern "C" {
#endif
// USART serial configuration options
#define USART_SERIAL_EXAMPLE ((Usart*)CONSOLE_UART)
#define USART_SERIAL_EXAMPLE_BAUDRATE (115200)
#define USART_SERIAL_CHAR_LENGTH (US_MR_CHRL_8_BIT)
#define USART_SERIAL_PARITY (US_MR_PAR_NO)
#define USART_SERIAL_STOP_BIT (US_MR_NBSTOP_1_BIT)
#ifdef __cplusplus
}
#endif
#endif/* CONF_USART_SERIAL_H_INCLUDED */
用stdio_serial_init初始化串行标准I/O
|