初级会员

- 积分
- 174
- 金钱
- 174
- 注册时间
- 2014-3-5
- 在线时间
- 0 小时
|
发表于 2014-5-9 12:30:54
|
显示全部楼层
实现功能:定时1s计PA0,PA12输入的脉冲数,并转换成转速
实现条件:PE.5通过软件延时产生外部脉冲,或者由信号发生器产生脉冲,此脉冲接入PA0,PA12管脚。。
需要的函数:main,timer.c
主函数:int main(void)
{
u8 i=0;
u8 *s=tab;
RCC_Configuration();
NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
GPIO_Configuration();
LED_Init();//PB5指示灯
LCD_init(); //lcd初始化
LCD_prints(4,0,s); //lcd第一行输出Lcd Test!
TIM1_Configuration(); //TIM1对PA12输入脉冲计数
TIM2_Configuration(); //TIM2对PA0输入脉冲计数
TIM3_Int_Init(10000-1,7200-1);//10Khz的计数频率,计数到10000为1s
while(1)
{
for(i = 0; i < 4; i ++) // 软件延时产生pwm,tim1 正常计数
{ //
GPIO_SetBits(GPIOE, GPIO_Pin_5); //200us
Delay(800); //1200-148, 1000-163,800-181,600-203,
GPIO_ResetBits(GPIOE, GPIO_Pin_5); //
Delay(1000);
} //
}
}
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);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); //使能PD端口时钟
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* PA0,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_5; //PE.8 端口配置
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PE.8 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOE, &GPIO_InitStructure);
//LCD1602(DO~D7)对应STM32的GPIO口(PB8~PB15)
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//LCD1602的(RS、RS、E)对应STM32的GPIO口(PD11、PD10、PD9)
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_8|GPIO_Pin_9 | GPIO_Pin_10);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_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);
}
定时器3定时1s输出脉冲个数timer.c
#include "timer.h"
#include "led.h"
#include "stm32f10x.h"
#include "lcd1602.h"
extern u16 counter;
u16 Pmo=2000;//转一圈的脉冲数
u16 Vic=0;//转速值
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
//定时器TIM3初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIMx
}
//定时器3中断服务程序
void TIM3_IRQHandler(void) //TIM3中断
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查TIM3更新中断发生与否
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx更新中断标志
LED0=!LED0; ///
counter=TIM_GetCounter(TIM2); //
TIM3_Vic(counter);
LCD_dispnum(Vic);
TIM_SetCounter(TIM2, 0);
}
}
void TIM3_Vic(u16 counter)
{
Vic=60*counter/Pmo;
}
|
|