1、程序实现功能:可以直接接收 USART1 的数据,并通过串口调试输出显示#include“stm32f10x_lib.h“void NVIC_Configuration(void);void RCC_Configuration(void);void GPIO_Configuration(void);ErrorStatus HSEStartUpStatus;USART_InitTypeDef USART_InitStructure;USART_ClockInitTypeDef USART_ClockInitStructure;int main()#ifdef DEBUGdebug#endifRC
2、C_Configuration();NVIC_Configuration();GPIO_Configuration();/*串口传输速率的大小必须与 RCC 所设定的时钟相对应起来*/USART_InitStructure.USART_BaudRate = 9600; /设置 USART 的传输速率/*设定数据的接收发送模式 */USART_InitStructure.USART_WordLength = USART_WordLength_8b;/在一帧中传输或接受 8 位数据位USART_InitStructure.USART_StopBits = USART_StopBits_1; /定义
3、在帧的结尾传输一个停止位USART_InitStructure.USART_Parity = USART_Parity_No; /奇偶失能USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /指定硬件流控制模式 RTS 和 CTS 使能USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /指定使能或失能发送和接受模式 Tx 发送使能和 Rx 接收使能USART_ClockInitStructure.USART_C
4、lock = USART_Clock_Disable; /提升 USART时钟时使能还是失能,钟低电平活动USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; /指定 SLCK 引脚上时钟的极性USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; /时钟第二个边缘进行数据捕获USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; /在 SCLK 引脚上输出最后发送的那个数据字的脉冲不从 SCLK 输出USART_
5、ClockInit(USART1, USART_Init(USART1, /*输入输出的中断使能*/ USART_ITConfig(USART1, USART_IT_TXE, ENABLE);USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);USART_Cmd(USART1, ENABLE); /启动串口 使能 USART1 外设while(1)/*USART_SendData(USART1, 0X26); /发送数据while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) = RESET)/等待发送结束*/v
6、oid NVIC_Configuration(void)NVIC_InitTypeDef NVIC_InitStructure;#ifdef VETB_TAB_RAMNVYC_SetVectorTable(NVIC_VectTab_RAM,0x0);#elseNVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif/* Enable the USART1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; /通道设置为串口 1 中断(故后面应选择在“void U
7、SART1_IRQHandler(void)”开中断)NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; /中断占先等级 0NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; /中断响应优先级 0NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; /打开中断NVIC_Init(void RCC_Configuration(void)RCC_DeInit();RCC_HSEConfig(RCC_HSE_ON);HSEStartUpStatu
8、s=RCC_WaitForHSEStartUp();if(HSEStartUpStatus=SUCCESS)FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);FLASH_SetLatency(FLASH_Latency_2);RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK2Config(RCC_HCLK_Div1); /串口波特率的确定RCC_PCLK1Config(RCC_HCLK_Div2);RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);RC
9、C_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY=RESET) RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); while(RCC_GetSYSCLKSource()!=0x08)RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE|RCC_APB2Periph_USART1, ENABLE); /RCC 中打开相应的串口 void GPIO_Configuration(void)GPIO_InitTypeDef G
10、PIO_InitStructure;/ GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE); /改变指定管脚脚的映射 Changes the mapping of the specified pin/* Configure USART1 RTS (PA12) and USART1 Tx (PA9) as alternate function push-pull 根据资料可查得各管脚对应*/GPIO_InitStructure.GPIO_Pin =GPIO_Pin_9;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_
11、50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; /推挽输出GPIO_Init(GPIOA, /* Configure USART2 CTS (PA11) and USART1 Rx (PA10) as input floating */GPIO_InitStructure.GPIO_Pin =GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOA, GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GP
12、IO_Pin_0|GPIO_Pin_2|GPIO_Pin_3;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_Init(GPIOE, 中断函数: (可在 stm32f10x_.it.c 中调用)void USART1_IRQHandler(void)u8 RX_dat; /定义字符变量if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) /判断发生接收中断GPIO_WriteBit(GPIOE
13、, GPIO_Pin_1, (BitAction)0x01); /LED MRX_dat=(USART_ReceiveData(USART1) /接收数据,整理除去前两位 USART_ClearITPendingBit(USART1, USART_IT_RXNE); /清除中断标志while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) = RESET)/等待接收结束/ USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); /关中断USART_SendData(USART1,RX_dat); /发送数据 GPIO_WriteBit(GPIOE, GPIO_Pin_2, (BitAction)0x01); /LED M