中级会员
- 积分
- 329
- 金钱
- 329
- 注册时间
- 2017-3-17
- 在线时间
- 83 小时
|
本帖最后由 乘云十万八千里 于 2024-2-24 09:22 编辑
void uart_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
//USART3_TX PB10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//USART3_RX PB11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
//Usart3 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启中断
USART_Cmd(USART3, ENABLE); //使能串口
}
void USART3_IRQHandler(void) //串口3中断服务程序
{
u8 Res;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
Res =USART_ReceiveData(USART3);//(USART1->DR); //读取接收到的数据
if((USART_RX_STA3&0x80)==0)//接收未完成
{
if(USART_RX_STA3&0x40)//接收到了0x0d
{
if(Res!=0x0a)USART_RX_STA3=0;//接收错误,重新开始
else
{
USART_RX_BUF3[USART_RX_STA3&0x3f]='\0';
USART_RX_STA3|=0x80; //接收完成了
}
}
else //还没收到0X0D
{
if(Res==0x0d)USART_RX_STA3|=0x40;
else
{
USART_RX_BUF3[USART_RX_STA3&0X3F]=Res ;
USART_RX_STA3++;
if(USART_RX_STA3>63)USART_RX_STA3=0;//接收数据错误,重新开始接收
}
}
}
}
}
//==============================================================================================
void uart3_sendChar(u16 value)
{
// if(value=='\n') {//Enter
// /* 向相应串口发送数据*/
// USART_SendData(USART1, '\r');
// /* 等待数据发送完成 ,传输完成标志置位*/
// while (USART_GetFlagStatus(USART1, USART_FLAG_TC) != SET);
// /* 向相应串口发送数据*/
// USART_SendData(USART1, value);
// /* 等待数据发送完成 ,传输完成标志置位*/
// while (USART_GetFlagStatus(USART1, USART_FLAG_TC) != SET);
// }else{
// /* 向相应串口发送数据*/
// uart1_Printf("in usart3_sendChar:%X\r\n",value);
USART_SendData(USART3, value);
/* 等待数据发送完成 ,传输完成标志置位*/
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) != SET);
// }
}
//==============================================================================================
void uart3_sendString(char *ptr)
{
while(*ptr!='\0')
uart3_sendChar(*ptr++);
}
//==============================================================================================
void uart3_Printf(const char *fmt,...)
{
va_list ap; //This is support the format output
char string[200];
va_start(ap,fmt);
vsprintf(string,fmt,ap); //Store the string in the String buffer
uart3_sendString(string);
va_end(ap);
}
//==============================================================================================
主函数通过USART_RX_STA3的最高位来拍断是否完成一个字符串的接收,如果完成接收,则在处理完字符串后需要配置USART_RX_STA3=0。uart3_Printf函数是串口3的输出函数。
|
|