接着昨天的工程来哈,这样我就不用再建工程了。一般懒人都这样。
今天是用CALLBACK方式来进行通讯。打开ASF WIZARD把POLL换成CALLBACK。
然后打开ASF EXPLOEROR点Quick Start Guide
里边都有步聚指导,程序如下:
#include <asf.h>
struct usart_module usart_instance;
#define MAX_RX_BUFFER_LENGTH 15
volatile uint8_t rx_buffer[MAX_RX_BUFFER_LENGTH];
void configure_usart(void);
void usart_read_callback(struct usart_module *const usart_module);
void usart_write_callback(struct usart_module *const usart_module);
void configure_usart_callbacks(void);
void configure_usart(void)
{
struct usart_config config_usart;
usart_get_config_defaults(&config_usart);
config_usart.baudrate = 9600;
config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
while (usart_init(&usart_instance,
EDBG_CDC_MODULE, &config_usart) != STATUS_OK) {
}
usart_enable(&usart_instance);
}
int main (void)
{
system_init();
configure_usart();
configure_usart_callbacks();
system_interrupt_enable_global();
uint8_t string[] = "Hello World!\r\n";
usart_write_buffer_wait(&usart_instance, string, sizeof(string));
while (true) {
usart_read_buffer_job(&usart_instance,
(uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);
}
}
void usart_read_callback(struct usart_module *const usart_module)
{
usart_write_buffer_job(&usart_instance,
(uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);
}
void usart_write_callback(struct usart_module *const usart_module)
{
port_pin_toggle_output_level(LED_0_PIN);
}
void configure_usart_callbacks(void)
{
usart_register_callback(&usart_instance,
usart_write_callback, USART_CALLBACK_BUFFER_TRANSMITTED);
usart_register_callback(&usart_instance,
usart_read_callback, USART_CALLBACK_BUFFER_RECEIVED);
usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_TRANSMITTED);
usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_RECEIVED);
}
最后的结果是,先打出世界你好,然后我输入到满缓冲后,就又显示在串口上。以下是截图:
ASF 确实是很方便,点点画画,我的程序就运行了。
|