初级会员

- 积分
- 174
- 金钱
- 174
- 注册时间
- 2014-3-5
- 在线时间
- 0 小时
|
发表于 2014-4-4 16:59:22
|
显示全部楼层
回复【楼主位】怡然叮咚:
---------------------------------
#include "stm32f10x.h"
#include "platform_config.h"
TIM_OCInitTypeDef TIM_OCInitStructure;
/*  rivate function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void Delay(__IO uint32_t nCount);
void TIM1_Configuration(void);
void TIM2_Configuration(void);
void Tim4_confi(void);
u16 Count_pulse(u16 c);
u16 counter=0;
u16 COUN1=0;
u16 COUN2=0;
/*  rivate functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval: None
*/
int main(void)
{
u8 i=0;
RCC_Configuration();
GPIO_Configuration();
TIM1_Configuration();
TIM2_Configuration();
// Tim4_confi(); //
while (1)
{
Delay(10000);
COUN1=TIM1->CNT;
COUN2=TIM2->CNT;
}
}
void Delay(__IO uint32_t nCount) //0.1us
{
for(; nCount != 0; nCount--);
}
void RCC_Configuration(void)
{
RCC_ClocksTypeDef RCC_ClockFreq;
SystemInit();
RCC_GetClocksFreq(&RCC_ClockFreq);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //使能PD端口时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能定时器TIM4时钟
GPIO_PinRemapConfig (GPIO_Remap_TIM4,ENABLE ); //重映射
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*  A0,PA12-> 左右脉冲输入 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void TIM1_Configuration(void) //只用一个外部脉冲端口
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//配置TIMER1作为计数器
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;//设置预分频可以将外部信号分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); // Time base configuration
TIM_ITRxExternalClockConfig(TIM2,TIM_TS_ETRF); //我测试中一直不能用的原因是缺少这句话,缺少后,timer的驱动时钟源默认是RCC,需要更改为外部ETR输入才行。
TIM_ETRClockMode2Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
TIM_SetCounter(TIM1, 0);
TIM_Cmd(TIM1, ENABLE);
}
void TIM2_Configuration(void) //只用一个外部脉冲端口
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//配置TIMER2作为计数器
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); // Time base configuration
TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
TIM_SetCounter(TIM2, 0);
TIM_Cmd(TIM2, ENABLE);
}
void Tim4_confi(void) //TIM4初始化
{
// TIM4-CH2基本计数器设置(设置PWM频率)
// 频率=TIM1_CLK/(ARR+1)
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_DeInit( TIM4 ) ;
TIM_BaseInitStructure.TIM_Period = 100; //100us
TIM_BaseInitStructure.TIM_Prescaler = 72-1;
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4, &TIM_BaseInitStructure);
//启用ARR的影子寄存器(直到产生更新事件才更改设置)
TIM_ARRPreloadConfig(TIM4, ENABLE);
//TIM4_OC2模块设置
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_Pulse = 50;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
//TIM4开启
TIM_Cmd(TIM4, ENABLE);
//TIM4_OC通道输出PWM(一定要加)
TIM_CtrlPWMOutputs(TIM4, ENABLE);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval: None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
这是我的程序,pa0接的10k脉冲,计数器值为0 |
|