|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2012-1-21 15:27:25
|
显示全部楼层
以下程序,进不了 TIM1中断,帮忙看看是什么原因?谢谢
其中,PE9(TIM1.CH1),PE11(TIM1.CH2)是分别接的编码器的A,B相,STM是STM32F103VBT6
程序将TIM1设置成编码器模式,TIM2设置成1毫秒中断一次
/*------------------------------------------------------------------------------
Author : Liuzq
date : 1/21/2012
project : test
decribe: NONE
STM32F103VBT6是128K BYTES flash,20K BYTES SRAM,
所以FLASH地址:0x0000 0000 --0x0002 0000
SRAM地址: 0x2000 0000 --0x2000 4fff
------------------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#include "stm32f10x_systick.h"
#include "usart.h"
#include "common.h"
#include "stm32f10x_irqn.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
//extern pFunction Jump_To_Application;
//extern u32 JumpAddress;
//vu32 TimingDelay;
#define null 0
#define TIM1_UP_IRQn 25 //!< TIM1 Update Interrupt
#define ICx_FILTER (u8) 6 // 6<-> 670nsec
#define TIMx_PRE_EMPTION_PRIORITY 1
#define TIMx_SUB_PRIORITY 0
/*#pragma location=0x20002af0
__no_init u16 T_B[256],C_B[256]; //1K (0x2000 2af0 -- 0x2000 2ef0)
#pragma location=0x20002ef0
__no_init u8 last_logic_image[128],last_symbol_image[128]; //256 BYTES (0x2000 2ef0 -- 0x2000 2ff0)
#pragma location=0x20002ff0
__no_init u8 stack; //bit0->累加器,bit1->堆栈 ,1BYTE (0x2000 2ff0 -- 0x2000 2ff0)
#pragma location=0x20002ff2
__no_init u16 index1,index2; //4 BYTES (0x2000 2ff2 -- 0x2000 2ff2)
#pragma location=0x20003000
__no_init USEDATA_XRAM USEDATA; //8K (0x2000 3000 -- 0x2000 4fff)*/
__no_init vu32 capture @ ((u32)0x20002840);
char motor=0;
//#define ADC1_DR_Address ((u32)0x4001244C)
vu16 ADCConvertedValue; // v = volatile.
ErrorStatus HSEStartUpStatus;
//函数声明
void RCC_Configuration(void);
void NVIC_Configuration(void);
void TIM_Configuration(void);
void GPIO_Configuration(void);
//void USART_Configuration(void);
void Delay(u32 time);
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */ /* 将外设RCC寄存器重设为缺省值 */
RCC_DeInit();
/* Enable HSE */ /* 设置外部高速晶振(HSE),如果HSE被直接或者通过PLL用于系统时钟,那么它不能被停振 */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */ /* 该函数将等待直到HSE就绪,或者在超时的情况下退出 */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)//如果SUCCESS为真,则继续执行下列语句
{
/* Enable Prefetch Buffer *//* 选择FLASH预取指缓存的模式:预取指缓存使能*/
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */ /* 设置FLASH存储器延时时钟周期数为2延时周期 */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK *//* AHB时钟(HCLK) = 系统时钟 */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK *//* APB2时钟PCLK2) = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */ /* APB1时钟(PCLK1) = HCLK / 2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */ /* 设置PLL时钟源及倍频系数了PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */ /* 使能 PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready *//* 等待PLL就绪 */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */ /* 选择PLL作为系统时钟 */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */ /* 等待系统时钟的时钟源是用PLL作为系统时钟 */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* 下面部份是根据所使用的外设,使能相应外设的时钟 */
/* 使能 USART1 时钟*/
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* TIM2 clock enable [使能 TIM2定时器 时钟]*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* GPIOC,GPIOD,GPIOE clock enable *///GPIO归APB2管
RCC_APB2PeriphClockCmd(/*RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|*/RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD
|RCC_APB2Periph_GPIOE|RCC_APB2Periph_TIM1|RCC_APB2Periph_AFIO, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
// Set the Vector Table base location at 0x20000000
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else //VECT_TAB_FLASH
// Set the Vector Table base location at 0x08000000
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Deinitializes the NVIC */
NVIC_DeInit();
/* Configure the NVIC Preemption Priority Bits[配置优先级组] */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
// Enable the TIM1 Update Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //TIM1 更新中断源 /////////////////////////////////////
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the TIM2 gloabal Interrupt [允许TIM2全局中断]*/
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Configure the NVIC Preemption Priority Bits */
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART1 gloabal Interrupt [允许USART1全局中断]*/
/* NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);*/
}
//=========================================================================
// TIM_Configuration(void)
// 设置TIMER-2定时器 为计数溢出中断,1毫秒中断一次
// TIM2、3、4的时钟源是 APB1 即是 PCLK1 ( APB1 对应 PCLK1 )
// PCLK1 = APB1 = HCLK/2 = SYSCLK/2 = 36MHZ (36,000,000 HZ)
// 结果 自动倍频器*2;还原为72MHZ!!!!
//=========================================================================
//使用的TIm2
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM_OCInitTypeDef TIM_OCInitStructure ;
TIM_ICInitTypeDef TIM_ICInitStructure;
// Timer configuration in Encoder mode
TIM_DeInit(TIM1);/////////////////////////////////////////////////////////////
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling
TIM_TimeBaseStructure.TIM_Period = 11;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM1, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
TIM1->CNT = 0; //Reset counter
TIM_Cmd(TIM1, ENABLE);
/* ---------------------------------------------------------------
TIM2 Configuration: Output Compare Timing Mode:
TIM2CLK = 72 MHz, Prescaler = 72, TIM2 counter clock = 1 KHz
----------------------------------------------------------------
计算方法:
TIM2 frequency = TIM2CLK/(TIM_Prescaler+ 1) = 1 MHz
TIM2 counter clock = TIMxCLK / TIM_Period
72MHz/(71+1)=1MHz,1MHz/1000=1000,每1/1000=1ms产生一次中断
--------------------------------------------------------------- */
TIM_DeInit(TIM2);//复位TIM2定时器
/* TIM2 configuration */
TIM_TimeBaseStructure.TIM_Period = 1000; // 定时1毫秒
TIM_TimeBaseStructure.TIM_Prescaler = 71; // 分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//TIM_CKD_DIV1; // 时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数方向向上计数
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Clear TIM2 update pending flag[清除TIM2溢出中断标志] */
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
/* Enable TIM2 Update interrupt [TIM2溢出中断允许]*/
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* TIM2 enable counter [允许TIM2计数]*/
TIM_Cmd(TIM2, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* //PB1 为PLC的编程/运行设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//悬空输入//GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);*/
// LED1 LED2 LED3 selected. PC11 PC10 PC9 PC6 Pc7 为PLC的PLUSE输出口 POUT2-POUT3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
//PD3- PD10 为PLC的继电器输出口 OUTPOUT0-OUTPOUT7,
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出//GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* //PE1- PE11 为PLC的继电器输入口 INPUT0-INTPUT11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//悬空输入//GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);*/
//旋转编码器:PE9 -A相 PE11 -B相
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitStructure);
}
//#define VECT_TAB_RAM //重新remap中断向量地址
//n_Counter = TIM_GetCounter(TIM8);
void Delay(u32 time)
{
for(/*time*/;time>0;time--) ;
}
void Delay2(u32 time)
{
u32 temp=0;
{if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)//查询定时中断的发生
temp++;
}while(temp<time);
}
void Delay3(u32 time)
{
//TIM_Cmd(TIM2, ENABLE);
capture=0;
while(capture<time);
//TIM_Cmd(TIM2, DISABLE);
}
// Tim1中断函数
void TIM1_UP_IRQHandler(void)
{ /* Clear the interrupt pending flag */
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
if(motor)
GPIO_SetBits(GPIOD,GPIO_Pin_12); //关马达控制I/O口PD12
/*if(TIM_GetITStatus(TIM1,TIM_IT_Update) != RESET)
{
GPIO_SetBits(GPIOD,GPIO_Pin_12); //关马达控制I/O口PD12
TIM_ClearFlag(TIM1,TIM_FLAG_Update);
TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
}*/
}
void TIM2_IRQHandler(void)
{
//capture++;
// TIM_ClearITPendingBit(TIM2,TIM_FLAG_Update); //清中断//注意,这一语句不能少,否则中断会不循环进行
if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{
capture++;
TIM_ClearFlag(TIM2, TIM_FLAG_Update);//清中断//注意,这一语句不能少,否则中断会不循环进行
}
}
void main()
{
/* unsigned long i;*/
capture=0;
RCC_Configuration(); //初始化时钟与复位 //main clock init 72MHZ
NVIC_Configuration(); //初始化中断嵌套
TIM_Configuration(); //初始化定时器
GPIO_Configuration(); //初始化GPIOC,GPIOD,GPIOE
//USART_Configuration();
while(1)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11); //将端口置低,LED灯亮
GPIO_ResetBits(GPIOD,GPIO_Pin_12); //接通马达控制I/O口PD12
motor=1;
//Delay(1000);
Delay3(500);//延时500毫秒,让马达运转500毫秒
GPIO_SetBits(GPIOC,GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11); //将端口置高,LED灯熄灭
// GPIO_SetBits(GPIOD,GPIO_Pin_12); //关马达控制I/O口PD12
//Delay(1000);
Delay3(500);//延时500毫秒,让马达停止运转500毫秒
}
} |
|