TA的每日心情 | 擦汗 2024-9-30 02:33 |
---|
签到天数: 444 天 连续签到: 1 天 [LV.9]以坛为家II
|
本帖最后由 木子鱼 于 2013-5-8 21:01 编辑
今晚抽空做了下 Atmel XMEGA-A3BU Xplained评估板评测(二)
https://www.cirmall.com/evaluation/xmega-a3bu-xplained-%e8%af%84%e6%b5%8b%ef%bc%88%e4%ba%8c%ef%bc%89/ 里面的例子
熟悉了下 Atmel Studio 6.0 编程的方式,发现真的跟官方所说是一样子的, ASF 用于单片机编程很爽。很多底层的驱动软件不用自己去编写,可以直接调用。
Atmel Software Framework框架,进一步简化了开发流程,缩短了开发周期。
视频上传出我拍的视频
························································································································································································································································································································
今晚抽空打开了 Atmel Studio 想看看ASF是如何定义一些操作的,吃了一惊:
首先看让LED灯闪的代码
#include <asf.h>
int main (void)
{
board_init();
sysclk_init();//调用ASF框架函数初始化时钟
delay_init(sysclk_get_cpu_hz());//调用ASF框架函数初始化delay服务
while(1)
{
delay_ms(500); //调用ASF框架函数延时500Ms
gpio_set_pin_low(LED0);
gpio_set_pin_low(LED1);
delay_ms(500);//调用ASF框架函数延时500Ms
gpio_set_pin_high(LED0);
gpio_set_pin_high(LED1);
}
前面的初始化函数 init()很好理解,然后就是 gpio_set_pin_low(LED0);gpio_set_pin_high(LED0); 这两句让LED0的I/O端口置1、置0 实现闪亮。
很显然,点开asf.h
#ifndef ASF_H
#define ASF_H
#include <ccp.h>
#include <xmega_reset_cause.h>
#include <delay.h>
#include <gpio.h>
#include <board.h>
#include <ioport.h>
#include <interrupt.h>
#include <nvm.h>
#include <parts.h>
#include <sysclk.h>
#include <compiler.h>
#include <status_codes.h>
#endif
又是include 许多的头文件!
打开xmega_reset_cause.h 找到
#define LED0_GPIO IOPORT_CREATE_PIN(PORTR, 0)
#define LED1_GPIO IOPORT_CREATE_PIN(PORTR, 1)
#define LED0 LED0_GPIO
#define LED1 LED1_GPIO
好吧,到这里应该是看懂了,IOPORT_CREATE_PIN(PORTR, 0) 这句函数是定义了 端口PR0,然后这句gpio_set_pin_low(LED0);
是将PR0端口置0
我以前学AVR单片机得知 AVR单片机的I/O口 是要定义它为输入模式还是输出模式的
好吧,我打开了
然后吃了一惊,ASF 果然是神器 神马预定义的函数都写好了 ,都可以直接拿来调用,节省开发时间。
···················································································································································································································································································································································································································································································· 最近实在是太忙了,各种考试,琐事缠身,但是 但是 还是阻挡不了我对 ASF 的狂热,晚上抽空就研究了下 ATXMEGA256的串口通讯的代码测试:
首先是 1 初始化 2设置波特率 3设置数据位数、停止位、校验位 4打开 5发送或者接收数据
代码:
#include <asf.h>
//&USARTD0发送 &USARTE0接收
#define USART_SERIAL_BAUDRATE 9600
#define USART_SERIAL_CHAR_LENGTH USART_CHSIZE_8BIT_gc
#define USART_SERIAL_PARITY USART_PMODE_DISABLED_gc
#define USART_SERIAL_STOP_BIT false
int main (void)
{
board_init();
sysclk_init();//调用ASF框架函数初始化时钟
delay_init(sysclk_get_cpu_hz());//调用ASF框架函数初始化delay服务
//Create USART options struct
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);//Enable the clock for the USART module
usart_init_rs232(&USARTD0, &USART_SERIAL_OPTIONS);//Initialize in RS232 mode:
sysclk_enable_module(SYSCLK_PORT_E, PR_USART0_bm);//Enable the clock for the USART module
usart_init_rs232(&USARTE0, &USART_SERIAL_OPTIONS);//Initialize in RS232 mode:
uint8_t received_byte;//A variable for the received byte must be added:
while(1)
{
usart_putchar(&USARTD0, 'a');//Send an 'a' character via USART
received_byte = usart_getchar(&USARTE0);//Wait for reception of a character:
usart_putchar(&USARTE0, received_byte);//Echo the character back:
if ( received_byte=='a' )
{
gpio_set_pin_low(LED0);
delay_ms(2000); //调用ASF框架函数延时500Ms
gpio_set_pin_high(LED0);
received_byte=0; //经过测试 必须加上这一条置0语句,否则会见到LED0一直亮着
delay_ms(2000); //经过测试 也要加上这个延迟,否则也看不到效果 预计是因为CPU的指令运算速度太快了,上一句 received_byte=0 没来得及反应 就又有 received_byte=='a' 然后也是LED会一直亮着
}
}
}
传上视频哈
我用的是串口USARTD0 发生一个字符a,用USARTE0去接收字符,如果接收到字符,就让LED0灯闪亮。
之前打算用 USARTD0 发生 USARTD1 去接收的,可是在
看到 USARTD0 用的的RXD0 、TXD0 为PD2、PD3 , USARTD1 用的的RXD1 、TXD1 为PD6、PD7 而 A3BU Xplained板子的J1 J2 J3 J4 木有引出 PD6、PD7 在板子的PCB图上见到这个 PD6、PD7 用于USB转串口去了 。
就随手改了板子引出的资源 USARTE0 对应的是 PE2 PE3
有一点很奇怪 当我打开 ASF 资源中的 usart.c 文件 找到
#ifdef USARTC0
if ((uint16_t)usart == (uint16_t)&USARTC0) {
sck_pin = IOPORT_CREATE_PIN(PORTC, 1);
}
#endif
#ifdef USARTC1
if ((uint16_t)usart == (uint16_t)&USARTC1) {
sck_pin = IOPORT_CREATE_PIN(PORTC, 5);
}
#endif
#ifdef USARTD0
if ((uint16_t)usart == (uint16_t)&USARTD0) {
sck_pin = IOPORT_CREATE_PIN(PORTD, 1);
}
#endif
#ifdef USARTD1
if ((uint16_t)usart == (uint16_t)&USARTD1) {
sck_pin = IOPORT_CREATE_PIN(PORTD, 5);
}······················
开始以为这些语句的意思是 把PC1作为USARTC0 的输入端 PC5作为 USARTC1的输入端 很显然不对的。与上面芯片的引脚图注解不对应
看来还得好好研究下usart.c 里面的语句。
|
|