新手入门
- 积分
- 15
- 金钱
- 15
- 注册时间
- 2017-8-12
- 在线时间
- 1 小时
|
1金钱
很基本的一个程序,最开始似乎是硬件问题,换了个接口好了。然后我想写从PC发到32再发回来的程序,没反应,改回之前的程序也没反应了。弄出来打算调ADC的。
小白一只,恳请各位大佬赐教。已经搞了几个小时了。现在都凌晨4点了= =
以下是配置程序:
#include<stm32f10x.h>
#include<stdio.h>
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_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(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(unsigned char)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
return (ch);
}
main函数:
#include<stm32f10x.h>
#include<stdio.h>
#include<usart1.h>
int main(void)
{
USART1_Config();
printf("\r\n this is a printf demo \r\n");
}
串口助手没收到数据,串口打开正常
|
最佳答案
查看完整内容[请看2#楼]
想成功发送在前面加上#if 1 #pragma import(__use_no_semihosting) //标准库需要的支持函数 struct __FILE { int handle; }; FILE __stdout; //定义_sys_exit()以避免使用半主机模式 _sys_exit(int x) { x = x; } //重定义fputc函数 int fputc(int ch, FILE *f) { while((USART1->SR&0X40)==0);//循环发送,直到发送完毕 USART1->DR = (u8) ch; return ch; } # ...
|