新手上路
- 积分
- 29
- 金钱
- 29
- 注册时间
- 2018-8-2
- 在线时间
- 27 小时
|
#include "stm32f10x.h"
GPIO_InitTypeDef GPIO_LED_InitStructure;
GPIO_InitTypeDef GPIO_USART_TX_InitStructure;
GPIO_InitTypeDef GPIO_USART_RX_InitStructure;
USART_InitTypeDef USART_InitStructure;
int main()
{
unsigned short int temp;
// 配置时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// 配置USART_TX
GPIO_USART_TX_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_USART_TX_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_USART_TX_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_USART_TX_InitStructure);
// 配置USART_RX
GPIO_USART_RX_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_USART_RX_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_USART_RX_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_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
//配置灯
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_LED_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_LED_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_LED_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_LED_InitStructure);
//关闭灯
GPIO_SetBits(GPIOB, GPIO_Pin_5);
while (1)
{
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET)
{
temp = USART_ReceiveData(USART1);
USART_SendData(USART1,temp);
GPIOB->ODR ^=GPIO_Pin_0;
}
}
}
|
|