/*
* * 软件功用: 串口试验(软件延时方法)* */#include "stm32f10x.h"#include#include "delay.h"u8 uart1_buf[20];int count=0; //字符串长度int flag=0; void RCC_Configuration(void);void GPIO_Configuration(void);void NVIC_Configuration(void);void USART1_Configuration(void);void Uart1_PutChar(u8 ch);void Uart1_PutString(u8* buf , u8 len);int USART1_ReceivedStrCmp(const char *str);/*函数: int main(void)功用: main主函数参数: 无回来: 无/int main(void){RCC_Configuration();NVIC_Configuration();GPIO_Configuration();delay_init(72);USART1_Configuration();GPIO_ResetBits(GPIOA,GPIO_Pin_0);//灭while(1){ //GPIO_ResetBits(GPIOA,GPIO_Pin_0);//灭//delay_ms(500);//延时//Uart1_PutString("Hello",5);//GPIO_SetBits(GPIOA,GPIO_Pin_0);//亮// delay_ms(500);//延时/* if(flag==1&&count!=0){Uart1_PutString(uart1_buf,count);count=0;flag=0;} */}}/*函数: void RCC_Configuration(void)功用: 复位和时钟操控 装备参数: 无回来: 无/void RCC_Configuration(void){ErrorStatus HSEStartUpStatus; //界说外部高速晶体发动状况枚举变量RCC_DeInit(); //复位RCC外部设备寄存器到默许值RCC_HSEConfig(RCC_HSE_ON); //翻开外部高速晶振HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等候外部高速时钟准备好if(HSEStartUpStatus == SUCCESS) //外部高速时钟现已准别好{FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //舱位FLASH预读缓冲功用,加快FLASH的读取。一切程序中有必要的用法.方位:RCC初始化子函数晒干,时钟起振之后FLASH_SetLatency(FLASH_Latency_2); //flash操作的延时RCC_HCLKConfig(RCC_SYSCLK_Div1); //装备AHB(HCLK)时钟等于==SYSCLKRCC_PCLK2Config(RCC_HCLK_Div1); //装备APB2(PCLK2)钟==AHB时钟RCC_PCLK1Config(RCC_HCLK_Div2); //装备APB1(PCLK1)钟==AHB1/2时钟RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //装备PLL时钟 == 外部高速晶体时钟 * 9 = 72MHzRCC_PLLCmd(ENABLE); //使能PLL时钟while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //等候PLL时钟安排妥当{}RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //装备体系时钟 = PLL时钟while(RCC_GetSYSCLKSource() != 0x08) //查看PLL时钟是否作为体系时钟{}}RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE); //答应 GPIOA、USART1、AFIO时钟}/*函数: void GPIO_Configuration(void)功用: GPIO装备参数: 无回来: 无/void GPIO_Configuration(void){GPIO_InitTypeDef GPIO_InitStructure; //界说GPIO初始化结构体GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); //PA0输出操控LED灯GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复合推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); //PA9串口输出GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入GPIO_Init(GPIOA, &GPIO_InitStructure); //PA10用于串口读入}void NVIC_Configuration(void) //中止分组和优先级装备 详见《STM32的函数阐明(中文).pdf》P165{NVIC_InitTypeDef NVIC_InitStructure;//NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); // Set the Vector Table base location at 0x08000000 装备中止向量偏移表的.默许是FLASH的,所以你不设置也无联系. RAM调试的时分,有必要设置.NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //0组,先占优先级1位,从优先级3位NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //USART1 大局中止//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //中止呼应优先级1NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能NVIC_Init(&NVIC_InitStructure); //初始化装备}/*函数名:USART1_Configuration输 入:输 出:功用阐明:初始化串口硬件设备,启用中止装备过程:(1)翻开GPIO和USART1的时钟(2)设置USART1两个管脚GPIO形式(3)装备USART1数据格式、波特率等参数(4)使能USART1接纳中止功用(5)最终使能USART1功用*/void USART1_Configuration(void) //串口装备 详见《STM32的函数阐明(中文).pdf》P346{USART_InitTypeDef USART_InitStructure;USART_InitStructure.USART_BaudRate=9600; //波特率为9600USART_InitStructure.USART_WordLength=USART_WordLength_8b; //数据位为8USART_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 | USART_Mode_Rx; //USART_Mode 指定了使能或许失能发送和接纳形式:发送使能|接纳失能USART_Init(USART1, &USART_InitStructure); //初始化装备USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能或许失能指定的 USART 中止 ,此处为接纳中止USART_Cmd(USART1,ENABLE); //使能或许失能 USART 外设USART_ClearFlag(USART1, USART_FLAG_TC);//铲除传输完结标志位,不然可能会丢掉第1个字节的数据.USART_FLAG_TC为发送完结标志位}/*中止服务程序*/void USART1_IRQHandler(void){ u8 dat;//u8 uart1_buf[6];if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET) //若接纳完结{//GPIO_SetBits(GPIOA,GPIO_Pin_0);//点亮LED //delay_ms(500);//延时//GPIO_ResetBits(GPIOA,GPIO_Pin_0);//灭 dat=USART_ReceiveData(USART1);//Uart1_PutChar(dat);//uart1_buf[count++]=dat;dat = USART_ReceiveData(USART1);if(dat!=0x23) //!=#在发送的信息结尾应包括#号{uart1_buf[count++]=dat;if(count>19){Uart1_PutString("超出长度![最长20个字符/10个汉字]",32);count=0;}}else if(count!=0){//Uart1_PutString("Received Data:",14);//Uart1_PutString(uart1_buf,count);if(USART1_ReceivedStrCmp("我喜欢你")) Uart1_PutString("我也爱你",8);if(USART1_ReceivedStrCmp("Hello")) Uart1_PutString("Hi",2);count=0;} USART_ClearFlag(USART1,USART_IT_RXNE);} //溢出处理-假如产生溢出需求先铲除ORE,再读DR寄存器 则可铲除不断入中止的问题if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET){USART_ClearFlag(USART1,USART_FLAG_ORE); //铲除OREUSART_ReceiveData(USART1); //读DR}}//发送一个字符void Uart1_PutChar(u8 ch){USART_SendData(USART1, (u8) ch);while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等候发送完结}//发送一个字符串 Input : buf为发送数据的地址 , len为发送字符的个数void Uart1_PutString(u8* buf , u8 len){ u8 i;for(i=0;i