野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11281|回复: 12

如何配置TIM对外部脉冲计数啊

[复制链接]
发表于 2016-3-9 20:15:59 | 显示全部楼层 |阅读模式
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>

/* Private functions ---------------------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART1_configuration(void);
void TIM5_Configuration(void);
void Delay(__IO uint32_t nCount);
int fputc(int ch, FILE *f);
int fgetc(FILE *f);
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
        u32 oldcount = 0;
        RCC_Configuration();
        GPIO_Configuration();
        USART1_configuration();
        printf("\r\n This is a TIM external tigger counting expriment !\r\n");
  /* Add your application code here */
        TIM5_Configuration();
        //RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE );
        while(1)
        {
                Delay(0xfff);
                if(oldcount != TIM3->CNT)
                {
                        oldcount = TIM3->CNT;
                        printf("\r\n Now Number = %d\r\n",oldcount);
                }       
        }

}
/**
  * @brief  Configure the Clocks.
  * @param  None
  * @retval None
  */
void RCC_Configuration(void)
{
        /*Enable the TIM3 clock*/
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE );
       
        /* config USART1 clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
       
        /*Enable the GPIOA and GPIOC clocks*/
        RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE );

}
/**
  * @brief  Configure the different GPIO ports.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
                GPIO_InitTypeDef   GPIO_InitStructure;
                /* USART1 GPIO configration ------------------------------------------------------------*/
                /* 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);
                /*TIM3_CH2 GPIO configuration-------------------------------------------------------------*/
                /*Configure TIM5_CH1(PA.0) as floating input*/
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ;
                GPIO_Init(GPIOA, &GPIO_InitStructure);       
}
/**
  * @brief  Configure the USART1.
  * @param  None
  * @retval None
  */
void USART1_configuration(void)
{
                USART_InitTypeDef USART_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);
                USART_Cmd(USART1, ENABLE);
}
  /**
   * @brief  Configures the TIM3 .
   * @param  None
   * @retval None
         * @attention
                * TIM_Period / Auto Reload Register(ARR) = 1000   TIM_Prescaler--71
                * &#214;D&#182;&#207;&#214;ü&#198;ú&#206;a = 1/(72MHZ /72) * 1000 = 1ms
                *
                * TIMxCLK/CK_PSC --> TIMxCNT --> TIM_Period(ARR) --> &#214;D&#182;&#207; &#199;òTIMxCNT&#214;&#216;&#214;&#195;&#206;a0&#214;&#216;D&#194;&#188;&#198;êy
   */
void TIM5_Configuration(void)
{
        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
        //TIM_ICInitTypeDef TIM3_ICInitStructure;
         
        TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
        TIM_TimeBaseStructure.TIM_Prescaler = 0;
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInit(TIM5, & TIM_TimeBaseStructure);
          
/*Configures the External clock Mode2. */
TIM_ETRClockMode2Config (TIM5,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,  0x00);
        /*Sets the TIM3 Counter Register value as 0 */
        TIM_SetCounter( TIM5,0);

        TIM_Cmd(TIM5, ENABLE);

}
/**
  * @brief  redirect Function Print to USART1 .
  * @param  None
  * @retval None
  */
int fputc(int ch, FILE *f)
{
                /* ·¢&#203;íò&#187;&#184;&#246;×&#214;&#189;úêy&#190;Yμ&#189;USART1 */
                USART_SendData(USART1, (uint8_t) ch);
               
                /* μè′y·¢&#203;ííê±&#207; */
                while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);               
       
                return (ch);
}

/**
  * @brief  redirect Function Scanf to USART1 .
  * @param  None
  * @retval None
  */
int fgetc(FILE *f)
{
                /* μè′y′&#174;&#191;ú1ê&#228;è&#235;êy&#190;Y */
                while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

                return (int)USART_ReceiveData(USART1);
}
/**
         * @brief  Inserts a delay time.
   * @param  nCount: specifies the delay time length.
   * @retval None
   */
void Delay(__IO uint32_t nCount)
{
   for(; nCount != 0; nCount--);
}


我是照着网上别人的改的。。。但是不行啊...
请问定时器对外部脉冲计数 如何配置...?


回复

使用道具 举报

 楼主| 发表于 2016-3-9 20:18:30 | 显示全部楼层
@fire 没有找到库函数版的啊
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-9 20:19:14 | 显示全部楼层
火哥出个库函数版的吧
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-9 20:22:02 | 显示全部楼层
各位大神看看 哪里有问题
回复 支持 反对

使用道具 举报

发表于 2016-3-9 20:52:40 | 显示全部楼层
把定时器的时钟源改成外部输入
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-9 21:16:20 | 显示全部楼层
flyleaf 发表于 2016-3-9 20:52
把定时器的时钟源改成外部输入

怎么配置啊?用哪一个库函数啊
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-9 21:40:32 | 显示全部楼层
flyleaf 发表于 2016-3-9 20:52
把定时器的时钟源改成外部输入

void TIM_TIxExternalClockConfig  ( TIM_TypeDef *  TIMx,  
  uint16_t  TIM_TIxExternalCLKSource,  
  uint16_t  TIM_ICPolarity,  
  uint16_t  ICFilter  
)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-9 21:42:05 | 显示全部楼层
flyleaf 发表于 2016-3-9 20:52
把定时器的时钟源改成外部输入

是这个函数配置吗?我想用的是TIM5_CH1即PA0...依旧不行啊
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-9 22:09:15 | 显示全部楼层
我发现有个地方写错了。oldcount = TIM3->CNT;改成oldcount = TIM5->CNT
但依旧不行@fire
回复 支持 反对

使用道具 举报

发表于 2016-3-10 11:07:12 | 显示全部楼层
Zoepreal 发表于 2016-3-9 22:09
我发现有个地方写错了。oldcount = TIM3->CNT;改成oldcount = TIM5->CNT
但依旧不行@fire

你描述下,你具体想实现的功能
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-10 16:32:30 | 显示全部楼层
fire 发表于 2016-3-10 11:07
你描述下,你具体想实现的功能

就是想用一个定时器的计数模式实现对外部脉冲的计数。来一个脉冲就计个数...像51单片机的定时器不就有计数模式吗?stm32也有。。。但是我不知道用哪些库函数配...网上有的我试了不行,也不知道是我通道配置的不行还是?....网上还有就是原子的寄存器版本的一个程序,是选用TIM5——CH1..对应PA0 选用上拉输入...其中对寄存器的操作..看不懂...也不知道怎么找相应的库函数.....
回复 支持 反对

使用道具 举报

发表于 2016-3-10 16:42:58 | 显示全部楼层
Zoepreal 发表于 2016-3-10 16:32
就是想用一个定时器的计数模式实现对外部脉冲的计数。来一个脉冲就计个数...像51单片机的定时器不就有计 ...

直接用捕获模式,不就可以了嘛 ?
回复 支持 反对

使用道具 举报

发表于 2016-3-10 19:09:49 | 显示全部楼层
一直在关注这个问题。。。。。继续关注
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-9-22 19:20 , Processed in 0.047256 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表