由于上次做串口切换,还是因为别的,我在搞中断时,几次都没成功,
归结原因是,上次管脚切换是一方面,还有一方面是中断除了定义以外必须使能。
我没有使能所心不好用。现在才搞定。
首先,从ASF EXPLORER中找到指导说明。然后形成程序。
程序如下:
#include <asf.h>
#include <stdio.h>
void usart_read_callback(struct usart_module *const usart_module);
void usart_write_callback(struct usart_module *const usart_module);
void configure_usart(void);
void configure_usart_callbacks(void);
//! [module_inst]
struct usart_module usart_instance;
//! [module_inst]
//! [rx_buffer_var]
#define MAX_RX_BUFFER_LENGTH 5
volatile uint8_t rx_buffer[MAX_RX_BUFFER_LENGTH];
//! [rx_buffer_var]
//! [callback_funcs]
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);
}
//! [callback_funcs]
//! [setup]
void configure_usart(void)
{
//! [setup_config]
struct usart_config config_usart;
//! [setup_config]
//! [setup_config_defaults]
usart_get_config_defaults(&config_usart);
//! [setup_config_defaults]
//! [setup_change_config]
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;
//! [setup_change_config]
//! [setup_set_config]
while (usart_init(&usart_instance,
EDBG_CDC_MODULE, &config_usart) != STATUS_OK) {
}
//! [setup_set_config]
//! [setup_enable]
usart_enable(&usart_instance);
//! [setup_enable]
}
void configure_usart_callbacks(void)
{
//! [setup_register_callbacks]
usart_register_callback(&usart_instance,
usart_write_callback, USART_CALLBACK_BUFFER_TRANSMITTED);
usart_register_callback(&usart_instance,
usart_read_callback, USART_CALLBACK_BUFFER_RECEIVED);
//! [setup_register_callbacks]
//! [setup_enable_callbacks]
usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_TRANSMITTED);
usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_RECEIVED);
//! [setup_enable_callbacks]
}
//! [setup]
int main(void)
{
system_init();
//! [setup_init]
configure_usart();
configure_usart_callbacks();
//! [setup_init]
//! [main]
//! [enable_global_interrupts]
system_interrupt_enable_global();
//! [enable_global_interrupts]
//! [main_send_string]
uint8_t string[] = "Hello World!\r\n";
usart_write_buffer_wait(&usart_instance, string, sizeof(string));
//! [main_send_string]
//! [main_loop]
while (true) {
//! [main_loop]
//! [main_read]
usart_read_buffer_job(&usart_instance,
(uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);
//! [main_read]
}
//! [main]
}
运行结果如下:
先打印出一行信息,接着用户只要输入五个字符或数字,就马上在串口打印出来。
|