我用的板子是STM3210X-128K-EVAL .IAR 4.41A. 看了TIM的例子6,想用这个例子和USART的例子1综合起来,通过串口发送接收到的PWM到PC上面显示出来.可是显示出来的脉冲是乱码 想请教一下看看是不是那里有问题,或是串口的程序应该怎么样写.麻烦了.我在stm32f10x_it.c里面的void TIM2_IRQHandler(void) { /* Clear TIM2 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); /* Get the Input Capture value */ IC2_Value = TIM_GetCapture2(TIM2); if(IC2_Value != 0) { /* Duty cycle computation */ Duty_Cycle = (TIM_GetCapture1(TIM2) * 100) / IC2_Value; /* Frequency computation */ Frequency = 72000000 / IC2_Value; } else { Duty_Cycle = 0; Frequency = 0; } USART_SendData(USART2, Frequency); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) { } } 填加了发送函数,然后PC上面接收到的脉冲显示的是乱码. 程序如下: /* Includes ------------------------------------------------------------------*/ #include "stm32f10x_lib.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ #define TxBufferSize (countof(TxBuffer) - 1) #define RxBufferSize 0xFF /* Private macro -------------------------------------------------------------*/ #define countof(a) (sizeof(a) / sizeof(*(a))) /* Private variables ---------------------------------------------------------*/ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; ErrorStatus HSEStartUpStatus; USART_InitTypeDef USART_InitStructure; u8 TxBuffer[] ="AAAAAAAAAAAAA\r" ;//"\n\rUSART Example 1: USART - Hyperterminal ardware flow control\n\r"; u8 RxBuffer[RxBufferSize]; u8 NbrOfDataToTransfer = TxBufferSize; u8 TxCounter = 0; u8 RxCounter = 0; ErrorStatus HSEStartUpStatus; /* Private function prototypes -----------------------------------------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); /* Private functions ---------------------------------------------------------*/ /******************************************************************************* * Function Name : main * Description : Main program * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { #ifdef DEBUG debug(); #endif /* System Clocks Configuration */ RCC_Configuration(); /* NVIC configuration */ NVIC_Configuration(); /* Configure the GPIO ports */ GPIO_Configuration(); TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_PWMI; /*TIM使用输入PWM模式*/ TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; /*使用TIM2通道*/ TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; /*TIM输入捕获上升沿*/ TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; /*TIM输入2,3或4选择对应地与IC1或IC2或IC3或IC4相连*/ TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; /*TIM捕获在捕获输入上每探测到一个边沿执行一次*/ TIM_ICInitStructure.TIM_ICFilter = 0x0; /*选择输入比较滤波器 ,取值在0x0到0xF之间*/ TIM_ICInit(TIM2, &TIM_ICInitStructure); /* 选择TIM1输入触发源 TIM经滤波定时器输入2 */ TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); /* Select the slave Mode: Reset Mode */ TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); /* TIM主/从模式使能 */ TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); /* TIM enable counter */ TIM_Cmd(TIM2, ENABLE); /* 使能TIM捕获/比较2中断源 */ TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); 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_HardwareFlowControl = USART_HardwareFlowControl_None; //USART_HardwareFlowControl_RTS_CTS; 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; USART_Init(USART2, &USART_InitStructure); /* Enable the USART2 */ USART_Cmd(USART2, ENABLE); /* Communication hyperterminal-USART2 using hardware flow control -------------*/ /* Send a buffer from USART to hyperterminal */ while(NbrOfDataToTransfer--) { USART_SendData(USART2, TxBuffer[TxCounter++]); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) { } } /* Receive a string (Max RxBufferSize bytes) from the Hyperterminal ended by '\r' (Enter key) */ do { if((USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET)&&(RxCounter < RxBufferSize)) { RxBuffer[RxCounter] = USART_ReceiveData(USART2); USART_SendData(USART2, RxBuffer[RxCounter++]); } }while((RxBuffer[RxCounter - 1] != '\r')&&(RxCounter != RxBufferSize)); while (1) { } } /******************************************************************************* * Function Name : RCC_Configuration * Description : Configures the different system clocks. * Input : None * Output : None * Return : None *******************************************************************************/ void RCC_Configuration(void) { /* RCC system reset(for debug purpose) */ RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* PLLCLK = 8MHz * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } } /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_USART2, ENABLE); /* GPIOA clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); } /******************************************************************************* * Function Name : GPIO_Configuration * Description : Configure the GPIOD Pins. * Input : None * Output : None * Return : None *******************************************************************************/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* GPIOA Configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Enable the USART2 Pins Software Remapping */ GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); /* Configure USART2 RTS (PD.04) and USART2 Tx (PD.05) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //| GPIO_Pin_5; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Configure USART2 CTS (PD.03) and USART2 Rx (PD.06) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //| GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_InitStructure); } /******************************************************************************* * Function Name : NVIC_Configuration * Description : Configure the nested vectored interrupt controller. * Input : None * Output : None * Return : None *******************************************************************************/ void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; #ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif /* Enable the TIM2 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; /*TIM2全局中断*/ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; /*先占优先级0位*/ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; /*从优先级1位*/ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } #ifdef DEBUG |