我的stm32串口使用printf函数总是不能成功发送数据啊,各位大哥大姐能帮忙看一下吗?实现的功能就是循环
发送“123”,每发一次就闪灯一下。程序如下:#include<stdio.h>#include<stm32f10x.h>//----------------------...
发送“123”,每发一次就闪灯一下。程序如下:
#include<stdio.h>
#include<stm32f10x.h>
//-------------------------------------------------------
#ifdef _GNUC_
#define PUTCHAR_PROTOTYPE int_io_putchar(int ch)
#define PUTCHAR_PROTOTYPE int fputc(int ch,FILE *f)
#endif
//-------------------------------------------------------
USART_InitTypeDef USART_InitStructure;//////////////////////////
//----------------------------------------------
GPIO_InitTypeDef GPIO_InitStructure;
#define RCC_GPIO_LED RCC_APB2Periph_GPIOF
#define GPIO_LED GPIOF
#define LED GPIO_Pin_6
//----------------------------------------------
void gpio_ini(void);
void usart_ini(void);
void delay(void);
//---------------------------------------------
int main(void)
{
gpio_ini();
usart_ini();
GPIO_SetBits(GPIO_LED,LED);
while(1)
{
printf("123");
GPIO_ResetBits(GPIO_LED,LED);
delay();
GPIO_SetBits(GPIO_LED,LED);
delay();
}
}
void gpio_ini(void)
{
RCC_APB2PeriphClockCmd(RCC_GPIO_LED,ENABLE); //////////////////////////////
GPIO_InitStructure.GPIO_Pin = LED;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_LED,&GPIO_InitStructure);
GPIO_SetBits(GPIO_LED,LED);
}
void usart_ini(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //////////////////////////////
USART_InitStructure.USART_BaudRate = 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_Tx | USART_Mode_Rx;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1, ENABLE);////////////////////////////
}
PUTCHAR_PROTOTYPE
{
USART_SendData(USART1,(uint8_t)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET)
{}
return ch;
}
void delay(void)
{u32 num;
for(num=0;num<=0xffe;num++);
} 展开
#include<stdio.h>
#include<stm32f10x.h>
//-------------------------------------------------------
#ifdef _GNUC_
#define PUTCHAR_PROTOTYPE int_io_putchar(int ch)
#define PUTCHAR_PROTOTYPE int fputc(int ch,FILE *f)
#endif
//-------------------------------------------------------
USART_InitTypeDef USART_InitStructure;//////////////////////////
//----------------------------------------------
GPIO_InitTypeDef GPIO_InitStructure;
#define RCC_GPIO_LED RCC_APB2Periph_GPIOF
#define GPIO_LED GPIOF
#define LED GPIO_Pin_6
//----------------------------------------------
void gpio_ini(void);
void usart_ini(void);
void delay(void);
//---------------------------------------------
int main(void)
{
gpio_ini();
usart_ini();
GPIO_SetBits(GPIO_LED,LED);
while(1)
{
printf("123");
GPIO_ResetBits(GPIO_LED,LED);
delay();
GPIO_SetBits(GPIO_LED,LED);
delay();
}
}
void gpio_ini(void)
{
RCC_APB2PeriphClockCmd(RCC_GPIO_LED,ENABLE); //////////////////////////////
GPIO_InitStructure.GPIO_Pin = LED;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_LED,&GPIO_InitStructure);
GPIO_SetBits(GPIO_LED,LED);
}
void usart_ini(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //////////////////////////////
USART_InitStructure.USART_BaudRate = 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_Tx | USART_Mode_Rx;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1, ENABLE);////////////////////////////
}
PUTCHAR_PROTOTYPE
{
USART_SendData(USART1,(uint8_t)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET)
{}
return ch;
}
void delay(void)
{u32 num;
for(num=0;num<=0xffe;num++);
} 展开
2个回答
2011-11-23
展开全部
可以 仿真 吗?
建议 去 我的领地 5d6d 的 proteus 论坛看看
那里有很多 单片机仿真实例,包括仿真图 和 源码
搜 5d6d 和 proteus 2个关键词
建议 去 我的领地 5d6d 的 proteus 论坛看看
那里有很多 单片机仿真实例,包括仿真图 和 源码
搜 5d6d 和 proteus 2个关键词
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询