EFM32例程——LEUART

硬件:EFM32GG230512
工具:keil5

LEUART实验
LEUART测试,leuart串口接收和发送

初始化

/*********************************************Copyright (c)***********************************************
** File name:             	leuart.c
** Created by:				Gui              
** Version:               	V1.0.0
** Descriptions:           	LEUART测试,leuart串口接收和发送
*********************************************************************************************************/
#include "leuart.h"

uint32_t GucLeuartIF;                                                     /* 用于存储LEUART中断标志位     */
uint8_t get_buff[32];
uint8_t leuart_data_len = 0;

void LEUART0_IRQHandler(void){
	
    GucLeuartIF = LEUART_IntGet(LEUART0);                               /* 获取中断标志                 */
    LEUART_IntClear(LEUART0, GucLeuartIF);                              /* 清除中断标志位               */            
	
	get_buff[leuart_data_len] = (uint8_t)LEUART0->RXDATA;
	LEUART_Tx(LEUART0,get_buff[leuart_data_len]);
	leuart_data_len++;
	if(leuart_data_len > 32)leuart_data_len = 0;
}

void leuart_init(void){
	    
//	CMU_ClockEnable(cmuClock_GPIO,true);    
    CMU_ClockEnable(cmuClock_CORELE,true);
    CMU_ClockEnable(cmuClock_LEUART0,true);
	CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFRCO);
	
    GPIO_PinModeSet(gpioPortE, 14, gpioModePushPull,  1);                /* 配置PD4为推挽模式            */
    GPIO_PinModeSet(gpioPortE, 15, gpioModeInputPull, 1);                /* 配置PD5为输入上拉模式        */
	
    LEUART_Init_TypeDef tLeuartInit = {                                 /* LEUART初始化结构体           */
        .enable   = leuartEnable,                                       /* 使能收发                     */
        .refFreq  = 0,                                                  /* 一般设为0                    */
        .baudrate = 9600,                                               /* 波特率                       */
        .databits = leuartDatabits8,                                    /* 数据位                       */
        .parity   = leuartNoParity,                                     /* 奇偶校验                     */
        .stopbits = leuartStopbits1                                     /* 停止位                       */
    };
	
    LEUART_Reset(LEUART0);                                              /* 复位LEUART                   */
    LEUART_Init(LEUART0, &tLeuartInit);
	
//    LEUART0->CTRL  = LEUART0->CTRL & (~(_LEUART_CTRL_AUTOTRI_MASK)) | LEUART_CTRL_AUTOTRI;
//	LEUART0->CTRL  = LEUART0->CTRL & (~(_LEUART_CTRL_RXDMAWU_MASK));
	
	//使能发送接收管脚
    LEUART0->ROUTE = LEUART_ROUTE_TXPEN | LEUART_ROUTE_RXPEN | LEUART_ROUTE_LOCATION_LOC2;
        
    LEUART_IntEnable(LEUART0, LEUART_IEN_RXDATAV);                      /* 使能接收中断 					*/
    NVIC_EnableIRQ(LEUART0_IRQn);                                       /* 使能LEUART0中断向量          	*/
}



main

/*********************************************Copyright (c)***********************************************
** File name:             	  	main.c
** Created by:					Gui              
** Version:               		V1.0.0        
** Descriptions:            	LEUART测试,leuart串口接收和发送
*********************************************************************************************************/
#include "system.h"
#include "em_device.h"
#include "em_chip.h"
#include "em_cmu.h"
#include "em_int.h"
#include "delay.h"
#include "leuart.h"
#include "bsp_led.h"

uint32_t g_chip_tick = 0;

int main(){
	
	CMU_ClockEnable(cmuClock_HFPER, true); //前置芯片设置
	CMU_ClockEnable(cmuClock_GPIO, true);
	if(SysTick_Config(SystemCoreClockGet()/1000))while(1);//1ms的systick中断
	led_init();//PD4 PD5
	leuart_init();
	
	{
		uint8_t test_buf[10] = "0123456789";
		uint8_t i = 0;
		for(i=0;i<10;i++){
			LEUART_Tx(LEUART0,test_buf[i]);
		}
	}
	while(1){
		EMU_EnterEM2(false); 
	}
}


你可能感兴趣的:(EFM32)