初中生
最后登录1970-1-1
在线时间 小时
注册时间2015-7-25
|
用TIM3捕获输入的PWM波,然后用串口输出数据,现在输出数据总是0- 主函数:
- #include "stm32f10x.h"
- #include "bsp_TiMbase.h"
- #include "bsp_usart1.h"
- uint16_t IC2Value;
- uint16_t DutyCycle;
- uint32_t Frequency;
- /**
- * @brief Ö÷oˉêy
- * @param ÎT
- * @retval ÎT
- */
- int main(void)
- {
- TIM3_GPIO_Config();
-
- USART1_Config();
-
- NVIC_Configuration();
- /* TIM3 ¶¨ê±ÅäÖà */
- TIM3_Configuration();
- NVIC_Configuration();
- /* êμÕ½¶¨ê±Æ÷μÄÖD¶ÏóÅÏ輶 */
- TIM3_NVIC_Configuration();
- while(1)
- {
- }
- }
- /**
- ******************************************************************************
- * @file bsp_usart1.c
- * @author fire
- * @version V1.0
- * @date 2013-xx-xx
- * @brief usartó|óÃbsp
- ******************************************************************************
- * @attention
- *
- * êμÑéƽì¨:ò°»e iSO STM32 ¿a·¢°å
- * ÂÛì3 :http://www.firebbs.cn
- * ìÔ±| :http://firestm32.taobao.com
- *
- ******************************************************************************
- */
-
- #include "bsp_usart1.h"
- /**
- * @brief USART1 GPIO ÅäÖÃ,1¤×÷Ä£ê½ÅäÖá£9600 8-N-1
- * @param ÎT
- * @retval ÎT
- */
- void USART1_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
-
- /* config USART1 clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
-
- /* USART1 GPIO config */
- /* Configure USART1 Tx (PA.09) as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* Configure USART1 Rx (PA.10) as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- /* USART1 mode config */
- 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_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART1, &USART_InitStructure);
-
- /* ê1Äü′®¿ú1½óêÕÖD¶Ï */
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
-
- USART_Cmd(USART1, ENABLE);
- }
- /// ÅäÖÃUSART1½óêÕÖD¶Ï
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Configure the NVIC Preemption Priority Bits */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
-
- /* Enable the USARTy Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- int fputc(int ch, FILE *f)
- {
- USART_SendData(USART1, (uint8_t) ch);
-
- /* μè′y·¢Ëííê±Ï */
- while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
-
- return (ch);
- }
- int fgetc(FILE *f)
- {
- /* μè′y′®¿ú1êäèëêy¾Y */
- while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
- return (int)USART_ReceiveData(USART1);
- }
- /**
-
- #include "bsp_TiMbase.h"
- void TIM3_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* éèÖÃTIM3CLK Îa 72MHZ */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- /* GPIOA clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- }
- /// TIM3ÖD¶ÏóÅÏ輶ÅäÖÃ
- void TIM3_NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void TIM3_Configuration(void)
- {
- TIM_ICInitTypeDef TIM_ICInitStructure;
- TIM_ICInitStructure.TIM_Channel=TIM_Channel_2;
- TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
- TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
- TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
- TIM_ICInitStructure.TIM_ICFilter=0x3;
- TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
-
- TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
- /* Select the slave Mode: Reset Mode */
- TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
- /* Enable the Master/Slave Mode */
- TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
- TIM_Cmd(TIM3,ENABLE);
- TIM_ITConfig(TIM3,TIM_IT_CC2,ENABLE);
- }
- 中断服务函数:
- void TIM3_IRQHandler(void)
- {
- /* Clear TIM3 Capture compare interrupt pending bit */
- TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
- /* Get the Input Capture value */
- IC2Value = TIM_GetCapture2(TIM3);
- if (IC2Value != 0)
- {
- /* Duty cycle computation */
- DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value;
- /* Frequency computation */
- Frequency = 72000000 / IC2Value;
- }
- else
- {
- DutyCycle = 0;
- Frequency = 0;
- }
- printf("Frequency:%d\n",Frequency);
- printf("DutyCycle:%d\n",DutyCycle);
- }
- void USART1_IRQHandler(void)
- {
- USART_ClearITPendingBit(USART1, USART_IT_TC);
- }
复制代码
|
|