我考虑的有点局限所以都没考虑到中断,版主说的太好了。不过我要做的没有中断,嘿嘿。 我已经用这种方法编好了程序,测了两次,前后两次的数据还是差不多的,接收到的数据也是对的。 int main(void) { #ifdef DEBUG debug(); #endif
/* System Clocks Configuration */ RCC_Configuration();
/* NVIC configuration */ NVIC_Configuration();
/* Configure the GPIO ports */ GPIO_Configuration(); USART_InitStructure.USART_BaudRate = 300; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_Even; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_Clock = USART_Clock_Disable; USART_InitStructure.USART_CPOL = USART_CPOL_Low; USART_InitStructure.USART_CPHA = USART_CPHA_2Edge; USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
/* Configure USART2 */ USART_Init(USART2, &USART_InitStructure); /* Enable the USART2 */ USART_Cmd(USART2, ENABLE); TIM_PrescalerConfig(TIM2, 10, TIM_PSCReloadMode_Immediate); TIM_SetCounter(TIM2,0);
while(count<1500000) { if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_10)==1) count++; else count=0; } while(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_6)==1) ; TIM_Cmd(TIM2, ENABLE); //开定时器 while(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_6)==0) ; time=TIM_GetCounter(TIM2); TIM_Cmd(TIM2, DISABLE); TIM_PrescalerConfig(TIM2, 10, TIM_PSCReloadMode_Immediate); while(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_6)==1) ; TIM_Cmd(TIM2, ENABLE); //开定时器 while(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_6)==0) ; time1=TIM_GetCounter(TIM2); TIM_Cmd(TIM2, DISABLE); while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET) { }
/* Store the received byte in RxBuffer */ RxBuffer[1] = (USART_ReceiveData(USART2) & 0xFF); |