You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
4.2 KiB
128 lines
4.2 KiB
#include "uart0.h"
|
|
void uart0_init(void) {
|
|
/**
|
|
* @brief 配置串口0的波特率为115200
|
|
* 时钟为系统时钟 Fpclk 为系统时钟频率
|
|
*
|
|
*/
|
|
GPIO_InitSettingType InitSet;
|
|
InitSet.Dir = GPIO_Direction_Output;
|
|
InitSet.DS = GPIO_DS_Output_Strong;
|
|
InitSet.Func = GPIO_Reuse_Func2;
|
|
InitSet.ODE = GPIO_ODE_Output_Disable;
|
|
InitSet.PDE = GPIO_PDE_Input_Disable;
|
|
InitSet.PUE = GPIO_PUE_Input_Enable;
|
|
InitSet.Signal = GPIO_Pin_Signal_Digital;
|
|
GPIO_Init(UART0_TXD0_PIN, &InitSet);
|
|
|
|
InitSet.Dir = GPIO_Direction_Input;
|
|
InitSet.DS = GPIO_DS_Output_Strong;
|
|
InitSet.Func = GPIO_Reuse_Func2;
|
|
InitSet.ODE = GPIO_ODE_Output_Disable;
|
|
InitSet.PDE = GPIO_PDE_Input_Disable;
|
|
InitSet.PUE = GPIO_PUE_Input_Disable;
|
|
InitSet.Signal = GPIO_Pin_Signal_Digital;
|
|
GPIO_Init(UART0_RXD0_PIN, &InitSet);
|
|
|
|
UART_InitStruType UART_InitStruct;
|
|
|
|
UART_InitStruct.UART_BaudRate = 115200; //波特率
|
|
UART_InitStruct.UART_ClockSet = UART_Clock_1; //时钟选择不分频
|
|
UART_InitStruct.UART_RxMode = UART_DataMode_8; // 8数据位无奇偶校验位
|
|
//标准极性端口数据和传输数据相同
|
|
UART_InitStruct.UART_RxPolar = UART_Polar_Normal;
|
|
UART_InitStruct.UART_StopBits = UART_StopBits_1; //一个停止位
|
|
UART_InitStruct.UART_TxMode = UART_DataMode_8; // 8数据位无奇偶校验位
|
|
UART_InitStruct.UART_TxPolar = UART_Polar_Normal; //标准usart极性
|
|
UART_Init(UART0, &UART_InitStruct);
|
|
UART_ITConfig(UART0, UART_IT_RB, Enable); /* UART0接收中断使能 */
|
|
/* UART0发送缓冲区空中断模式: 全空中断 */
|
|
UART_TBIMConfig(UART0, UART_TBIM_Byte);
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_TB);
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_RB);
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_FE);
|
|
NVIC_Init(NVIC_UART0_IRQn, NVIC_Priority_1, Enable); //使能串口0中断
|
|
UART0_TxEnable();
|
|
UART0_RxEnable();
|
|
}
|
|
|
|
void fun(void) {}
|
|
|
|
void UART0_IRQHandler(void) {
|
|
/**
|
|
* @brief 接收来自串口的数据并做回显
|
|
*
|
|
*/
|
|
// int i;
|
|
// for (i = 0; i < 100; i++) {
|
|
// }
|
|
// fun();
|
|
// i++;
|
|
// return;
|
|
uint8_t uart0_data;
|
|
uart0_data = UART_RecByte(UART0);
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_TB);
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_RB);
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_FE);
|
|
// UART0->IF.Word = (uint32_t)0xffffffff;
|
|
// UART_ClearITPendingBit(UART0, UART_FLAG_TB);
|
|
// UART_ClearITPendingBit(UART0, UART_FLAG_TC);
|
|
// UART_ClearITPendingBit(UART0, UART_FLAG_TBWE);
|
|
// UART_ClearITPendingBit(UART0, UART_FLAG_TBWO);
|
|
// UART_ClearITPendingBit(UART0, UART_FLAG_RB);
|
|
// UART_ClearITPendingBit(UART0, UART_FLAG_ID);
|
|
// UART_ClearITPendingBit(UART0, UART_FLAG_RO);
|
|
// UART_ClearITPendingBit(UART0, UART_FLAG_FE);
|
|
// UART_ClearITPendingBit(UART0, UART_FLAG_PE);
|
|
// UART_ClearITPendingBit(UART0, UART_FLAG_BDE);
|
|
// UART_ClearITPendingBit(UART0, UART_FLAG_RBRE);
|
|
// UART_ClearITPendingBit(UART0, UART_FLAG_RBRO);
|
|
|
|
// if (UART_GetFlagStatus(UART0, UART_FLAG_TB) == SET) {
|
|
// fun();
|
|
// }
|
|
// if (UART_GetFlagStatus(UART0, UART_FLAG_TC) == SET) {
|
|
// fun();
|
|
// }
|
|
// if (UART_GetFlagStatus(UART0, UART_FLAG_TBWE) == SET) {
|
|
// fun();
|
|
// }
|
|
// if (UART_GetFlagStatus(UART0, UART_FLAG_TBWO) == SET) {
|
|
// fun();
|
|
// }
|
|
// if (UART_GetFlagStatus(UART0, UART_FLAG_RB) == SET) {
|
|
// fun();
|
|
// }
|
|
// if (UART_GetFlagStatus(UART0, UART_FLAG_ID) == SET) {
|
|
// fun();
|
|
// }
|
|
// if (UART_GetFlagStatus(UART0, UART_FLAG_RO) == SET) {
|
|
// fun();
|
|
// }
|
|
// if (UART_GetFlagStatus(UART0, UART_FLAG_FE) == SET) {
|
|
// fun();
|
|
// }
|
|
// if (UART_GetFlagStatus(UART0, UART_FLAG_PE) == SET) {
|
|
// fun();
|
|
// }
|
|
// if (UART_GetFlagStatus(UART0, UART_FLAG_BDE) == SET) {
|
|
// fun();
|
|
// }
|
|
// if (UART_GetFlagStatus(UART0, UART_FLAG_RBRE) == SET) {
|
|
// fun();
|
|
// }
|
|
// if (UART_GetFlagStatus(UART0, UART_FLAG_RBRO) == SET) {
|
|
// fun();
|
|
// }
|
|
// UART_SendByte(UART0, uart0_data);
|
|
}
|
|
|
|
void Uart0SendBuff(uint8_t *buff) {
|
|
while (*buff != '\0') {
|
|
// UART_SendByte(UART0, *buff);
|
|
while (UART_GetFlagStatus(UART0, UART_FLAG_TC) == RESET)
|
|
;
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_TC);
|
|
buff++;
|
|
}
|
|
}
|