码迷,mamicode.com
首页 > 其他好文 > 详细

基于ATxmega128的ASF串口应用

时间:2019-06-05 18:10:06      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:头文件   user   get   usart   cloc   insert   port   zed   发送   

1、编辑串口的配置参数,一般将这些参数放在conf_usart.h配置头文件中,本程序将这些参数放在user_board.h头文件中

#define USART_SERIAL &USARTD0                 //使用USARTD0作为串口
#define USART_SERIAL_BAUDRATE 9600                 //波特率设置为9600
#define USART_SERIAL_CHAR_LENGTH USART_CHSIZE_8BIT_gc   //字符长度8位
#define USART_SERIAL_PARITY USART_PMODE_DISABLED_gc    //无校验位
#define USART_SERIAL_STOP_BIT false                  //1位停止位

 

2、发送一个字符a;

int main (void)
{
sysclk_init(); //系统时钟初始化,默认主时钟;
board_init(); //板卡初始化,打开IO时钟,初始化引脚等

static usart_rs232_options_t USART_SERIAL_OPTIONS ={
.baudrate = USART_SERIAL_BAUDRATE,
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};

sysclk_enable_module(SYSCLK_PORT_D,PR_USART0_bm);

usart_init_rs232(USART_SERIAL, &USART_SERIAL_OPTIONS); //串口初始化

usart_putchar(USART_SERIAL,‘a‘);//通过串口发送一个字符

}

3、接收一个字符,并回传

int main (void)
{
/* Insert system clock initialization code here (sysclk_init()). */
sysclk_init(); //系统时钟初始化,默认主时钟;
board_init(); //板卡初始化,打开IO时钟,初始化引脚等

static usart_rs232_options_t USART_SERIAL_OPTIONS ={
.baudrate = USART_SERIAL_BAUDRATE,
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};

sysclk_enable_module(SYSCLK_PORT_D,PR_USART0_bm);
usart_init_rs232(USART_SERIAL, &USART_SERIAL_OPTIONS); //串口初始化
received_byte = usart_getchar(USART_SERIAL);//接收一个字符
usart_putchar(USART_SERIAL,received_byte);//将接收的字符发出

/* Insert application code here, after the board has been initialized. */
}

 

基于ATxmega128的ASF串口应用

标签:头文件   user   get   usart   cloc   insert   port   zed   发送   

原文地址:https://www.cnblogs.com/FZLGYZ/p/10980904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!