小弟初入手STM32,跟着原子大哥的例程学习。但对于串口发送数据一直不理解,故特地提出串口发送的函数(自己根据串口实验例程修改的),但在串口调试助手一直得不到正确的值,下面是我的代码,有50几行吧,希望各位大神帮忙看看。
我感觉这个程序已经是最简化后的了,下面附上程序:
#include "stm32f10x.h"
void Delay(void);
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
SystemInit(); //系统初始化 ******
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX    A.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//串口的初始化
USART_InitStructure.USART_BaudRate = 9600;//一般设置为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_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE); //使能串口
while (1)
{
USART_SendData(USART1,0x02);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
Delay();
USART_SendData(USART1,0x04);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
Delay();
}
}
void Delay(void) //两个void意思分别为无需返回值,没有参数传递
{
unsigned int i,j; //定义无符号整数,最大取值范围65535
for(j=0;j<20;j++)
{
for(i=0;i<65000;i++) //做20000次空循环
; //什么也不做,等待一个机器周期
}
}
|