中级会员
- 积分
- 269
- 金钱
- 269
- 注册时间
- 2022-11-1
- 在线时间
- 53 小时
|
发表于 2023-4-26 09:57:09
|
显示全部楼层
时钟没问题,我把源文件里面的代码发你看看
- #include "sys.h"
- #include "usart.h"
- //如果使用ucos,则包括下面的头文件即可.
- #if SYSTEM_SUPPORT_OS
- #include "includes.h" //ucos 使用
- #endif
- //加入以下代码,支持printf函数,而不需要选择use MicroLIB
- #if 1
- #pragma import(__use_no_semihosting)
- //标准库需要的支持函数
- struct __FILE
- {
- int handle;
- };
- FILE __stdout;
- //定义_sys_exit()以避免使用半主机模式
- void _sys_exit(int x)
- {
- x = x;
- }
- //重定义fputc函数
- int fputc(int ch, FILE *f)
- {
- while((USART3->SR&0X40)==0);//循环发送,直到发送完毕
- USART3->DR = (u8) ch;
- return ch;
- }
- #endif
- /*使用microLib的方法*/
- /*
- int fputc(int ch, FILE *f)
- {
- USART_SendData(USART3, (uint8_t) ch);
- while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET) {}
-
- return ch;
- }
- int GetKey (void) {
- while (!(USART3->SR & USART_FLAG_RXNE));
- return ((int)(USART3->DR & 0x1FF));
- }
- */
-
- #if EN_USART3_RX //使能串口3中断接收
- u8 USART_RX_BUF[USART_REC_LEN]; //接收字节的长度
- u16 USART_RX_STA=0; //接收状态标志
-
- void uart_init(u32 bound)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //串口3是挂载在APB1下面的外设
-
-
- 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);
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- USART_InitStructure.USART_BaudRate = 115200;
- 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); //使能串口3
- }
- void USART3_IRQHandler(void) //串口1中断响应函数
- {
- u8 Res;
- if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //判断是否接收中断,如果接受中断,
- //则读取串口接受到的数据(接收到的数据必须是0x0d 0x0a结尾)
-
- {
- Res =USART_ReceiveData(USART3); //读取接收到的数据
-
- if((USART_RX_STA&0x8000)==0)//接收未完成
- {
- if(USART_RX_STA&0x4000)//接收到了0x0d
- {
- if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始
- else USART_RX_STA|=0x8000; //接收完成了
- }
- else //还没收到0X0D
- {
- if(Res==0x0d)USART_RX_STA|=0x4000;
- else
- {
- USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
- USART_RX_STA++;
- if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收
- }
- }
- }
- }
- }
- #endif
复制代码 |
|