【复旦微车规MCU FM32FT0A测评】串口通讯与LED控制
[复制链接]
FM33FT0A可向用户提供多达6个UART通道,为进行测试,这里只选取UART4来实现。
串口UART4所占用的引脚为:
TX--PB3
RX--PB2
图1 线路连接
为了对UART4进行初始化,需在mf_config.c里添加下面的函数:
void MF_UART4_Init(void)
{
FL_UART_InitTypeDef uart = {0};
FL_GPIO_InitTypeDef gpio = {0};
FL_GPIO_StructInit(&gpio);
gpio.pin = FL_GPIO_PIN_2 | FL_GPIO_PIN_3;
gpio.mode = FL_GPIO_MODE_DIGITAL;
gpio.pull = FL_GPIO_BOTH_DISABLE;
gpio.outputType = FL_GPIO_OUTPUT_PUSHPULL;
gpio.remapPin = FL_GPIO_PINREMAP_FUNCTON1;
FL_GPIO_Init(GPIOB, &gpio);
uart.baudRate = 115200;
uart.dataWidth = FL_UART_DATA_WIDTH_8B;
uart.parity = FL_UART_PARITY_NONE;
uart.stopBits = FL_UART_STOP_BIT_WIDTH_1B;
uart.transferDirection = FL_UART_DIRECTION_TX_RX;
FL_UART_Init(UART4, &uart);
FL_UART_ClearFlag_TXShiftBuffEmpty(UART4);
FL_UART_EnableIT_TXShiftBuffEmpty(UART4);
}
此外,为了能实现printf打印功能,还需添加下面的函数:
int fputc(int ch, FILE *f)
{
FL_UART_WriteTXBuff(UART4,(uint8_t)ch); /* 将发送数据写入发送寄存器 */
while(FL_UART_IsActiveFlag_TXBuffEmpty(UART4) != 0x01UL){}; /* 等待发送完成 */
return ch;
}
以指令方式控制LED灯的主程序为:
int main(void)
{
uint8_t zl;
/* 使能IWDT */
IWDT_Init(FL_IWDT_PERIOD_4000MS);
/* Initialize FL Driver Library */
/* SHOULD BE KEPT!!! */
FL_Init();
/* 使能SVD, 阈值4.157V(falling)~4.257V(rising) */
SVD_Init(SVD_MONTIOR_VDD,FL_SVD_WARNING_THRESHOLD_GROUP11, FL_SVD_REFERENCE_1P0V);
/* 确认SVD监测结果是否高于阈值,如否则持续等待 */
while(false == SVD_Result_Confirmed(SVD_HIGHER_THRESHOLD, 2000U/*us*/));
/* 使能BOR */
RMU_BOR_Init(FL_RMU_BOR_THRESHOLD_2P00V);
MF_Config_Init();
printf("UART4 & RGB_LED test\r\n");
LED_OFF(RED);
LED_OFF(GREEN);
LED_OFF(BULE);
while(1)
{
/* 清狗 */
FL_IWDT_ReloadCounter(IWDT);
/* 电源掉电监测处理 */
PowerDownMonitoring();
/* 功能执行 */
if (0x01UL == FL_UART_IsActiveFlag_RXBuffFull(UART4)) /* 等待接收到一个字节 */
{
zl = (uint8_t)FL_UART_ReadRXBuff(UART4);
/* uart接收中断标志可通过读取rxreg清除 */
}
if(zl=='R')
{
LED_ON(RED);
LED_OFF(GREEN);
LED_OFF(BULE);
printf("RED\r\n");
}
if(zl=='G')
{
LED_OFF(RED);
LED_ON(GREEN);
LED_OFF(BULE);
printf("GREEN\r\n");
}
if(zl=='B')
{
LED_OFF(RED);
LED_OFF(GREEN);
LED_ON(BULE);
printf("BULE\r\n");
}
if(zl=='F')
{
LED_OFF(RED);
LED_OFF(GREEN);
LED_OFF(BULE);
printf("OFF\r\n");
}
}
}
经程序的编译与下载,其测试效果如图2和图3所示。
图2 串口测试
图3 测试效果
|