用班子自带的程序可以,但是用下面的程序就只能Keil仿真了。
寄存器方式:
int main()
{
float Div;
u16 M,F,BRR;
u32 baud;
unsigned long cnt;
GPIO_InitTypeDef GPIO_InitStructure;
/*配置串口1输出管脚*/
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);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
USART1 ->CR1 |= 1 << 13;
USART1 ->CR1 &= ~(1<<12);
USART1 ->CR2 &= ~(3<<12);
USART1 ->CR1 |= 1<<3;
baud = 9600;
Div = (float)72 * 1000 * 1000 / (baud * 16);
M = Div;
F = (Div - M) * 16;
BRR = (M << 4) + F;
USART1 ->BRR = BRR;
USART1 ->DR = 'A';
while(cnt++ < 30)
{
while(0 == USART1 ->SR & 1<<6);
USART1 ->DR = 'A' + cnt;
}
while(1);
}
库函数方式:
int main()
{
u8 data;
u16 cnt = 0;
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/*配置串口1输出管脚*/
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);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
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);
data = 'A';
while(cnt++ < 30)
{
USART_SendData(USART1,data);
data++;
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
}
}
|