研究生
最后登录1970-1-1
在线时间 小时
注册时间2015-12-4
|
一样的代码怎么TIM3就不行呢?????????????????????
#include "stm32f10x.h"
#include "encode.h"
#include "misc.h"
#include "nvic.h"
#include "sys.h"
#include "delay.h"
void TIM4_Mode_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
//PB6 ch1 A,PB7 ch2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//使能TIM4时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能GPIOA时钟
GPIO_StructInit(&GPIO_InitStructure);//将GPIO_InitStruct中的参数按缺省值输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//PB6 PB7浮空输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_Config(2);
TIM_DeInit(TIM4);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //设定计数器重装值 TIMx_ARR = 359*4
TIM_TimeBaseStructure.TIM_Prescaler = 0; //TIM3时钟预分频值
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;//设置时钟分割 T_dts = T_ck_int
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);//使用编码器模式3,上升下降都计数
TIM_ICStructInit(&TIM_ICInitStructure);//将结构体中的内容缺省输入
TIM_ICInitStructure.TIM_ICFilter = 6; //选择输入比较滤波器
TIM_ICInit(TIM4, &TIM_ICInitStructure);//将TIM_ICInitStructure中的指定参数初始化TIM3
// TIM_ARRPreloadConfig(TIM4, ENABLE);//使能预装载
TIM_ClearFlag(TIM4, TIM_FLAG_Update);//清除TIM3的更新标志位
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);//运行更新中断
//Reset counter
TIM4->CNT = 0;//
TIM_Cmd(TIM4, ENABLE); //启动TIM4定时器
}
void TIM3_Mode_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
//PA6 ch1 A,PA7 ch2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//使能TIM3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能GPIOA时钟
GPIO_StructInit(&GPIO_InitStructure);//将GPIO_InitStruct中的参数按缺省值输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//PB6 PB7浮空输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_Config(3);
TIM_DeInit(TIM3);
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //设定计数器重装值 TIMx_ARR = 359*4
TIM_TimeBaseStructure.TIM_Prescaler = 0; //TIM3时钟预分频值
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;//设置时钟分割 T_dts = T_ck_int
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);//使用编码器模式3,上升下降都计数
TIM_ICStructInit(&TIM_ICInitStructure);//将结构体中的内容缺省输入
TIM_ICInitStructure.TIM_ICFilter = 6; //选择输入比较滤波器
TIM_ICInit(TIM3, &TIM_ICInitStructure);//将TIM_ICInitStructure中的指定参数初始化TIM3
TIM_ARRPreloadConfig(TIM3, ENABLE);//使能预装载
TIM_ClearFlag(TIM3, TIM_FLAG_Update);//清除TIM3的更新标志位
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);//运行更新中断
//Reset counter
TIM3->CNT =1000;//
TIM_Cmd(TIM3, ENABLE); //启动TIM3定时器
}
/*void TIM3_Mode_Config(void)
{
RCC->APB1ENR|=1<<1;//TIM3时钟使能
RCC->APB2ENR|=1<<2; //使能PORTA时钟
TIM3->ARR=0xFFFF/4; //设定计数器自动重装值//刚好1ms
//这两个东东要同时设置才可以使用中断
TIM3->DIER|=1<<0; //允许更新中断
TIM3->DIER|=1<<6; //允许触发中断
GPIOA->CRL&=0XF0FFFFFF;//PA6
GPIOA->CRL|=0X04000000;//浮空输入
GPIOA->CRL&=0X0FFFFFFF;//PA7
GPIOA->CRL|=0X40000000;//浮空输入
TIM3->CCMR1 |= 1<<0; //CC1S='01' IC1FP1映射到TI1
TIM3->CCMR1 |= 1<<8; //CC2S='01' IC2FP2映射到TI2
TIM3->CCER &= ~(1<<1); //CC1P='0' IC1FP1不反相,IC1FP1=TI1
TIM3->CCER &= ~(1<<5); //CC2P='0' IC2FP2不反相,IC2FP2=TI2
TIM3->CCMR1 |= 1<<4; // IC1F='0001' 输入捕获1滤波器
TIM3->CCMR1 |= 1<<12; // IC2F='0001' 输入捕获2滤波器
TIM3->SMCR |= 3<<0; //SMS='011' 所有的输入均在上升沿和下降沿有效
//TIM3->EGR|=1<<0; //计数器清零,会产生一次更新事件
TIM3->CR1|=0x01; //使能定时器3
}*/
void TIM4_Init(void)
{
TIM4_Mode_Config();
}
void TIM3_Init(void)
{
TIM3_Mode_Config();
}
#include "stm32f10x.h"
#include "misc.h"
#include "nvic.h"
#include "sys.h"
#include "delay.h"
/*
中断分配说明,优先级由高向低排
顺序 类型 作用 抢占优先级 响应优先级
1 EXTI1 编码器Z相归0 0 0
2 TIM4 定时器的编码器模式 1 1
3 TIM3 定时器的编码器模式 2 1
4 USART1 串口通信中断 3 1
*/
//1.-----Z信号归零 PA1
void PA_NVIC_Config(NVIC_InitTypeDef NVIC_InitStructure1)
{
NVIC_InitStructure1.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure1.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure1.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure1.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure1);
}
//2----定时器的编码器接口模式,对编码器进行计数
void TIM4_NVIC_Config(NVIC_InitTypeDef NVIC_InitStructure2)
{
NVIC_InitStructure2.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure2);
}
void TIM3_NVIC_Config(NVIC_InitTypeDef NVIC_InitStructure3)
{
NVIC_InitStructure3.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure3.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure3.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure3.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure3);
}
//3----串口1通信,用于Modbus-Rtu-485S
void USART1_NVIC_Config(NVIC_InitTypeDef NVIC_InitStructure4)
{
NVIC_InitStructure4.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure4.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure4.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure4.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure4);
}
void NVIC_Config(u8 Interrupt_flag)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 配置中断使用组合 抢占式3位(0-7),响应式1位(0-1) */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
if(Interrupt_flag == 1)
{
/*外部中断1,Z相归零----优先级最高 0 0*/
PA_NVIC_Config(NVIC_InitStructure);
}
if(Interrupt_flag == 2)
{
/*TIM4中断,编码器解码----优先级 1 1*/
TIM4_NVIC_Config(NVIC_InitStructure);
}
if(Interrupt_flag == 3)
{
/*TIM4中断,编码器解码----优先级 1 1*/
TIM3_NVIC_Config(NVIC_InitStructure);
}
if(Interrupt_flag == 4)
{
/* USART1中断,数据接收----优先级 2 1*/
USART1_NVIC_Config(NVIC_InitStructure);
}
}
|
|