在Atmle Studio6.2的开发环境直接插入开发板。就可直接打开例程。
今天例程简单,通过串口打印一个字符串,然后接收用户的输入字符,每输入一个回显一个
我觉得高难的是初始化函数用了大量的指针:
*/
static inline void stdio_serial_init(volatile void *usart, const usart_serial_options_t *opt)
{
stdio_base = (void *)usart;
ptr_put = (int (*)(void volatile*,char))&usart_serial_putchar;
ptr_get = (void (*)(void volatile*,char*))&usart_serial_getchar;
# if (XMEGA || MEGA_RF)
usart_serial_init((USART_t *)usart,opt);
# elif UC3
usart_serial_init(usart,(usart_serial_options_t *)opt);
# elif SAM
usart_serial_init((Usart *)usart,(usart_serial_options_t *)opt);
# else
# error Unsupported chip type
# endif
# if defined(__GNUC__)
# if (XMEGA || MEGA_RF)
// For AVR GCC libc print redirection uses fdevopen.
fdevopen((int (*)(char, FILE*))(_write),(int (*)(FILE*))(_read));
# endif
# if UC3 || SAM
// For AVR32 and SAM GCC
// Specify that stdout and stdin should not be buffered.
setbuf(stdout, NULL);
setbuf(stdin, NULL);
// Note: Already the case in IAR's Normal DLIB default configuration
// and AVR GCC library:
// - printf() emits one character at a time.
// - getchar() requests only 1 byte to exit.
# endif
# endif
}
平时用,我想不用管这些函数只是那来用就行。
接下来是打印一串字符然后就等待用户输入
// 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);
}
}
这个例和调用的大量的库函数:
read.c : System implementation function used by standard library
* - write.c : System implementation function used by standard library
本例运行如下:
|