初级会员

- 积分
- 83
- 金钱
- 83
- 注册时间
- 2020-11-17
- 在线时间
- 43 小时
|
40金钱
这两天一直在研究串口程序,准备用printf讲串口数据打印在串口调试助手上,自己也移植了相应程序到usart.c中,头文件也添加了,然后MicroLIB也勾选了,在主函数中就打印1111这个数字测试下,结果串口调试助手啥也不显示,自己也尝试用USART_SendData直接向串口发送数字1,还是没反应,弄了一两天也没弄出来,程序中的while中的LED等亮着,我用的F107的板子,请好心人帮我看看程序,十分感谢!!!
#include "sys.h"
#include "stdio.h"
#include "usart.h"
#include "uart4.h"
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
void _sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f)
{
/* e.g.给USART写一个字符 */
USART_SendData(USART1, (uint8_t) ch);
/* 循环直到发送完成 */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return ch;
}
void uart_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOB, ENABLE); //使能USART1,GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //开启复用时钟
//USART1_TX GPIOB.6
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PB.6
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB.6
//USART1_RX GPIOB.7初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//PB.7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB.7
//Usart1 NVIC 配置
// NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
// NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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(USART1, &USART_InitStructure); //初始化串口1
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_Cmd(USART1, ENABLE); //使能串口1
}
主函数
int main(void)
{
static u16 number=0;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
RS485_Init();
TIM5_Enable(999);
// RS485_RX_Service();
LED_Init();
IIC_Init();
TM1650_Init();
uart_init(115200);
USART_SendData(USART1,1);
printf("1111/r/n");
while(1)
{
USART_SendData(USART1,1);
printf("1111/r/n");
LED_RUN=1;
}
}
|
最佳答案
查看完整内容[请看2#楼]
有试过串口通信例子程序吗?
程序没发现问题,确定硬件连接好了?会不会助手的串口号选错了?
TXD脚用示波器看看是否有波形,也可直接将TXD脚用个东西短时间的接触到GND,看助手是否会收到数据.
|