logo
首页 产品中心 支付方式 联系我们
AVR32应用笔记 AVR32单片机笑傲江湖 AVR32 USART(UART)程序

AVR32 USART(UART)程序

以下资料由微雪电子整理并发布,未经许可不得转载,否则追究相应责任!

系统功能

  使用AVR32的USART功能,通过RS232接口接入PC机,在PC端显示相关字符!
  目标MCU为32UC3B0256,目标系统采用EVK3B开发板。


硬件设计

   关于AVR32的USART结构及相关介绍详见Datasheet,这里仅给出原理图。


按键检查电路原理图(点击图片放大,不需要放大镜!

软件设计

//建立好工程,导入相应的Software Framework
/* This source file is part of the ATMEL AVR32-SoftwareFramework-1.3.0-AT32UC3B Release */

/*This file is prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
*
* \brief USART example application for AVR32 USART driver.
*
* - Compiler: IAR EWAVR32 and GNU GCC for AVR32
* - Supported devices: All AVR32 devices with a USART module can be used.
* - AppNote:
*
* \author Atmel Corporation: http://www.atmel.com \n
* Support and FAQ: http://support.atmel.no/
*
******************************************************************************/

/*! \page License
* Copyright (C) 2006-2008, Atmel Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of ATMEL may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
* SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*! \mainpage
* \section intro Introduction
* This is the documentation for the data structures, functions, variables,
* defines, enums, and typedefs for the USART software driver.\n It also comes
* bundled with an example. This example is a basic Hello-World example.\n
* <b>Example's operating mode: </b>
* -# A message is displayed on the PC terminal ("Hello, this is AT32UC3 saying hello! (press enter)")
* -# You may then type any character other than CR(Carriage Return) and it will
* be echoed back to the PC terminal.
* -# If you type a CR, "'CRLF'Goodbye." is echoed back to the PC terminal and
* the application ends.
*
* \section files Main Files
* - usart.c: USART driver;
* - usart.h: USART driver header file;
* - usart_example.c: USART example application.
*
* \section compilinfo Compilation Information
* This software is written for GNU GCC for AVR32 and for IAR Embedded Workbench
* for Atmel AVR32. Other compilers may or may not work.
*
* \section deviceinfo Device Information
* All AVR32 devices with a USART module can be used.
*
* \section configinfo Configuration Information
* This example has been tested with the following configuration:
* - EVK1100 evaluation kit or EVK1101 evalutation kit;
* - CPU clock: 12 MHz;
* - USART1 connected to a PC serial port via a standard RS232 DB9 cable;
* - PC terminal settings:
* - 57600 bps,
* - 8 data bits,
* - no parity bit,
* - 1 stop bit,
* - no flow control.
*
* \section contactinfo Contact Information
* For further information, visit
* <A href="http://www.atmel.com/products/AVR32/">Atmel AVR32</A>.\n
* Support and FAQ: http://support.atmel.no/
*/

#include <avr32/io.h>
#include "compiler.h"
#include "board.h"
#include "pm.h"
#include "gpio.h"
#include "usart.h"

/*! \name USART Settings
*/
//! @{
#if BOARD == EVK1100
# define EXAMPLE_USART (&AVR32_USART1)
# define EXAMPLE_USART_RX_PIN AVR32_USART1_RXD_0_0_PIN
# define EXAMPLE_USART_RX_FUNCTION AVR32_USART1_RXD_0_0_FUNCTION
# define EXAMPLE_USART_TX_PIN AVR32_USART1_TXD_0_0_PIN
# define EXAMPLE_USART_TX_FUNCTION AVR32_USART1_TXD_0_0_FUNCTION
#elif BOARD == EVK1101
# define EXAMPLE_USART (&AVR32_USART1)
# define EXAMPLE_USART_RX_PIN AVR32_USART1_RXD_0_0_PIN
# define EXAMPLE_USART_RX_FUNCTION AVR32_USART1_RXD_0_0_FUNCTION
# define EXAMPLE_USART_TX_PIN AVR32_USART1_TXD_0_0_PIN
# define EXAMPLE_USART_TX_FUNCTION AVR32_USART1_TXD_0_0_FUNCTION
#endif
//! @}

/*! \brief This is an example demonstrating the USART RS232 TX and RX
* functionalities using the USART driver.
*/
int main(void)
{
static const gpio_map_t USART_GPIO_MAP =
{
{EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION},
{EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION}
};

// USART options.
static const usart_options_t USART_OPTIONS =
{
.baudrate = 57600, //baud可在这里修改
.charlength = 8,
.paritytype = USART_NO_PARITY,
.stopbits = USART_1_STOPBIT,
.channelmode = USART_NORMAL_CHMODE
};

// Switch main clock to external oscillator 0 (crystal).
pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);

// Assign GPIO to USART.
gpio_enable_module(USART_GPIO_MAP,
sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));

// Initialize USART in RS232 mode.
usart_init_rs232(EXAMPLE_USART, &USART_OPTIONS, FOSC0);

// Hello world!
usart_write_line(EXAMPLE_USART, "Hello, this is AT32UC3 saying hello! (press enter)\n");

// Press enter to continue.
while (usart_get_echo_line(EXAMPLE_USART) == USART_FAILURE); // Get and echo characters until end of line.

usart_write_line(EXAMPLE_USART, "Goodbye.\n");

while (TRUE);
}


系统调试

  打开相应串口,注意波特率为57600,字符长度为8位,无奇偶校验,无停止位。
  如果程序正常执行,PC端的串口将接收到相应的字符。

AVR32 USART(UART)程序

——