程序如下:
USART.C的
#include "stm32f10x.h"
#include "Usart.h"
#include "led.h"
#include "stdio.h"
#include "Delay.h"
//加入以下代码,支持printf函数,而不需要选择use MicroLIB
#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;
}
#endif
void USART1_Init(void)
{
/**********串口1时钟配置**********/
USART_ClockInitTypeDef USART_ClockInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; //串口时钟禁止
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; //数据低电平有效
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;//配置CPHA使数据在第2个边沿的时候被捕获
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;// 禁用最后一位,使对应的时钟脉冲不会再输出到SCLK引脚
USART_ClockInit(USART1, &USART_ClockInitStructure); //配置USART与时钟相关的设置
/**********串口1GPIO配置**********/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA
|RCC_APB2Periph_AFIO, ENABLE); //USART1串口时钟及GPIOA时钟使能
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //USART1_TX初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1_RX PA.10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //USART1_RX初始化
/**********串口1配置**********/
USART_InitStructure.USART_BaudRate = 9600; //波特率9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止字节
USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无流控制
USART_InitStructure.USART_Mode = USART_Mode_Tx; //TX模式使能
// USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Rx模式和Tx模式使能
USART_Init(USART1,&USART_InitStructure); //串口1配置初始化
/**********串口1中断配置**********/
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);//开启串口1发送中断
USART_Cmd(USART1,ENABLE); //启动串口1
}
void USART1_IRQHandler()
{
if(USART_GetITStatus(USART1,USART_IT_TXE)!=RESET)// 检测是否开启串口1 TC发送中断
{
LED0_ON;
LED1_ON;
USART_SendData(USART1,0X02); //发送数据0X01
// while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=RESET); //等待数据发送完成
// USART_ClearFlag(USART1,USART_IT_TC);// 清除TC发送完成标志位
Delay_ms(500);
LED0_OFF;
LED1_OFF;
// USART_ClearITPendingBit(USART1,USART_IT_TXE); //清除TXE中断标志位
}
}
main.c如下
#include "stm32f10x.h"
#include "Delay.h"
#include "led.h"
#include "key.h"
#include "Usart.h"
#include "stdio.h"
int main()
{
u8 time=0;
SystemInit(); //系统时钟等初始化
Key_Init(); //
LED_Init();//
Delay_init(72);//
USART1_Init();//
while(1)
{
time++;
// USART_ClearFlag(USART1,USART_IT_TC);
printf("\n welcome to the world of STM32 \n");
USART_SendData(USART1,0X01);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=RESET);
USART_ClearFlag(USART1,USART_IT_TC);
while(time%5000==0)
{
printf("\n welcome to the world of STM32 \n");
printf("\n the data you have sent is :\n");
USART_SendData(USART1,0X01);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=RESET);
// USART_ClearFlag(USART1,USART_IT_TC);// 清除TC发送完成中断标志位
}
}
}
但是开启串口调试助手后,显示的始终是一个相同字符
|