中级会员
- 积分
- 269
- 金钱
- 269
- 注册时间
- 2022-11-1
- 在线时间
- 53 小时
|
楼主 |
发表于 2023-4-21 14:20:48
|
显示全部楼层
大佬你看这是我修改完的源文件,但是它就是打印不出来东西
- #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((USART2->SR&0X40)==0);//循环发送,直到发送完毕
- USART2->DR = (u8) ch;
- return ch;
- }
- #endif
- /*使用microLib的方法*/
- /*
- int fputc(int ch, FILE *f)
- {
- USART_SendData(USART1, (uint8_t) ch);
- while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) {}
-
- return ch;
- }
- int GetKey (void) {
- while (!(USART2->SR & USART_FLAG_RXNE));
- return ((int)(USART2->DR & 0x1FF));
- }
- */
-
- #if EN_USART2_RX
- 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_GPIOA, ENABLE); //
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //串口2是挂载在APB1下面的外设
- //USART1_TX连接在PA9上
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //USART1_RX在PA10上
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- NVIC_InitStructure.NVIC_IRQChannel = USART2_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(USART2, &USART_InitStructure);
-
-
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
- USART_Cmd(USART2, ENABLE);
- }
- void USART2_IRQHandler(void) //串口1中断响应函数
- {
- u8 Res;
- #if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
- OSIntEnter();
- #endif
- if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //判断是否接收中断,如果接受中断,
- //则读取串口接受到的数据(接收到的数据必须是0x0d 0x0a结尾)
-
- {
- Res =USART_ReceiveData(USART2); //读取接收到的数据
-
- 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;//接收数据错误,重新开始接收
- }
- }
- }
- }
- #if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
- OSIntExit();
- #endif
- }
- #endif
复制代码 |
|