Efm32-chip peripherals-uart rx interrupt of usart

Source: Internet
Author: User

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 );
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.