本帖最后由 ddllxxrr 于 2015-9-19 08:12 编辑
有的盆友不想用U口即EDBG口进行通讯,那么换一个行不行呢?答案是一定的。
打开saml21_xplained_pro.h
里边有:
#define EXT3_UART_MODULE SERCOM1
#define EXT3_UART_SERCOM_MUX_SETTING USART_RX_3_TX_2_XCK_3
#define EXT3_UART_SERCOM_PINMUX_PAD0 PINMUX_UNUSED
#define EXT3_UART_SERCOM_PINMUX_PAD1 PINMUX_UNUSED
#define EXT3_UART_SERCOM_PINMUX_PAD2 PINMUX_PA18C_SERCOM1_PAD2
#define EXT3_UART_SERCOM_PINMUX_PAD3 PINMUX_PA19C_SERCOM1_PAD3
#define EXT3_UART_SERCOM_DMAC_ID_TX SERCOM1_DMAC_ID_TX
#define EXT3_UART_SERCOM_DMAC_ID_RX SERCOM1_DMAC_ID_RX
那么,我们就用SERCOM1了。
由于在.h中各个管脚的重映射已经搞好,我只要改个速率及SERCOM1就行了。其余代码不变。
我用另一个U转串线加到PA18,PA19则输出结果如下:
程序如下:
#include <asf.h>
struct usart_module usart_instance;
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);
}
void configure_usart2(void)
{
struct usart_config config_usart;
usart_get_config_defaults(&config_usart);
config_usart.baudrate = 115200;
config_usart.mux_setting = EXT3_UART_SERCOM_MUX_SETTING;
config_usart.pinmux_pad0 = EXT3_UART_SERCOM_PINMUX_PAD0;
config_usart.pinmux_pad1 = EXT3_UART_SERCOM_PINMUX_PAD1;
config_usart.pinmux_pad2 = EXT3_UART_SERCOM_PINMUX_PAD2;
config_usart.pinmux_pad3 = EXT3_UART_SERCOM_PINMUX_PAD3;
while (usart_init(&usart_instance,
EXT3_UART_MODULE, &config_usart) != STATUS_OK) {
}
usart_enable(&usart_instance);
}
int main (void)
{
system_init();
configure_usart();
uint8_t string[] = "Hello World!\r\n";
usart_write_buffer_wait(&usart_instance, string, sizeof(string));
usart_disable(&usart_instance);
configure_usart2();
usart_write_buffer_wait(&usart_instance, string, sizeof(string));
uint16_t temp;
while (true) {
if (usart_read_wait(&usart_instance, &temp) == STATUS_OK) {
while (usart_write_wait(&usart_instance, temp) != STATUS_OK) {
}
}
}
}
注意,本程序的截图是在115200波特率的截图,证明管脚已经换成功,另外,注意的是,两个初始化之间一定要先停一下USART。
usart_disable(&usart_instance);
否则不好用。
|