Introduction: USART integrates SPI and UART, so only the SPI demo is not enough. Therefore, the simplest UART running routine is available. Send in a loop. Interrupt reception is used for receiving.
Hardware preparation: tg stk. Connect the 4-pin and 6-pin ports of the 20 PIN expansion port. 4th feet are PD0, Tx, 6th feet are PD1, Rx.
Software Process: After the UART configuration is complete, the main function continuously sends 0 to 9, while the receiving interruption determines whether the received 5, if yes, the LED lamp status is flipped. The result shows that the LED lights are constantly on and off.
The routine code is as follows:
# Include <stdint. h>
# Include <stdbool. h>
# Include "efm32.h"
# Include "efm32_chip.h"
# Include "efm32_cmu.h"
# Include "efm32_gpio.h"
# Include "efm32_usart.h"
Void usart+rx_irqhandler (void)
{
If (USART_Rx (USART1) = 0x5)
{
GPIO_PinOutToggle (gpioPortD, 7 );
}
}
Void UART_Initial (void)
{
Cmu_clockenable (cmuclock_hfper, true );
Cmu_clockenable (cmuclock_gpio, true );
Cmu_clockenable (cmuclock_usart1, true );
Usart_initasync_typedef uart_init = usart_initasync_default;
Uart_init.baudrate = 9600;
Usart_initasync (usart1, & uart_init );
Usart1-> IFC = _ usart_ifc_mask;
Nvic_clearpendingirq (usart1_rx_irqn );
Nvic_enableirq (usart1_rx_irqn );
Usart1-> ien = usart_ien_rxdatav;
Gpio_pinmodeset (gpioportd, 0, gpiomodepushpull, 1); // TX
Gpio_pinmodeset (gpioportd, 1, gpiomodeinput, 1); // RX
Usart1-> route | = usart_route_txpen | usart_route_rxpen | usart_route_location_loc1;
}
/*************************************** ***********************************//**
* @ Brief Main Function
**************************************** *************************************/
/*************************************** ***********************************//**
* @ Brief Main Function
**************************************** *************************************/
Int main (void)
{
/* Chip errata */
Chip_init ();
Systemcoreclockupdate ();
UART_Initial ();
CMU_ClockEnable (cmuClock_GPIO, true );
GPIO_PinModeSet (gpioPortD, 7, gpioModePushPull, 0 );
Unsigned char ucData = 0;
/* Infinite blink loop */
While (1)
{
Unsigned long ulDelay = 100000;
While (ulDelay --);
If (ucData <10) ucData ++;
Else ucData = 0;
USART_Tx (USART1, ucData );
}
}