本例使用了TIM2 Channel1, 2, 3 and 4 in Output, corresponding GPIOA0,1,2,3, and pin 10~13,所以可不用修改直接使用,还是遇到一些问题,先把大致的程序附录一下:
/General TIM: 16bit, 4 independent channels, TIM2,3,4 are normal Timer, TIM1 is advanced timer
//工作在输出比较模式
//Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
vu16 CCR1_Val = 0x8000;
vu16 CCR2_Val = 0x4000;
vu16 CCR3_Val = 0x2000;
vu16 CCR4_Val = 0x1000;
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG // this DEBUG is defined in the "option - c complier - Preprocessor - Defined symbols"
debug();
#endif
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC Configuration */
NVIC_Configuration();
/* GPIO Configuration */
GPIO_Configuration();
/* ---------------------------------------------------------------
TIM2 Configuration: Output Compare Toggle Mode:
TIM2CLK = 36 MHz, Prescaler = 0x2, TIM2 counter clock = 12 MHz 36/(2+1)MHz
CC1 update rate = TIM2 counter clock / CCR1_Val = 366.2 Hz 这是计时器=比较寄存器 事件的频率
CC2 update rate = TIM2 counter clock / CCR2_Val = 732.4 Hz
CC3 update rate = TIM2 counter clock / CCR3_Val = 1464.8 Hz
CC4 update rate = TIM2 counter clock / CCR4_Val = 2929.6 Hz
--------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
//period的值是设定计数器的最大值,从0数到这个值又返回0重新计数
TIM_TimeBaseStructure.TIM_Prescaler = 0x02;
//TIM_Prescaler:预分频器 TIMx 的计数器到时频率为TIMxCLK/(Prescaler+1)
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // up counting mode
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Output Compare Toggle Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
//output compare toggle mode 触发模式, 计数器与比较寄存器相同时,输出翻转
TIM_OCInitStructure.TIM_Channel = TIM_Channel_1;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //pulse value loaded in capture compare register
//Pulse=0x8000,即计数脉冲到0x8000时作操作(P148 参考手册),本例是输出从0-1或相反.
//12M/0x8000=366.2,而实际的波形频率为366.2/2=183.1Hz
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
//TIM_OCPolarity就是设定CC1P的值, 为H就是输出不反相,为L就是输出反相。
圈中右边的梯形表示一个多路开关,多路开关受TIMx_CCER寄存器的CC1P位控制,CC1P=0时OC1REF不经反相直接输出,CC1P=1时先将OC1REF反相在输出。
TIM_OCInit(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); //??? TIM2 preload register disable
......
/* TIM enable counter */
TIM_Cmd(TIM2, ENABLE);
/* TIM IT enable */
TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
while(1)
{
}
}
中断程序: stm32f10x_it.c
u16 capture = 0;
extern vu16 CCR1_Val; //"extern" tell the compiler the variant is defined in the other file
extern vu16 CCR2_Val;
extern vu16 CCR3_Val;
extern vu16 CCR4_Val;
/*******************************************************************************
* Function Name : TIM2_IRQHandler
* Description : This function handles TIM2 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void TIM2_IRQHandler(void)
{
/* TIM2_CH1 toggling with frequency = 183.1 Hz */
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) //check which specific interrupt has occured
//if TIM_GetITStatus()==SET, interrupt happened
{ //TIM_IT_CC1 is capture/compare 1 interrupt source
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 ); //clear the interrupt pending bit
capture = TIM_GetCapture1(TIM2);
//get the capture1 value (CCR1) of TIM2, 脉冲计数器的值即是与CCR1 的值比较,相等则触发TIMx事件
TIM_SetCompare1(TIM2, capture + CCR1_Val );
/*set capture/compare 1 register value,CCR1的值增加,使得下一个TIM事件也需要CCR1_Val个脉冲,另一种方式我想是清零脉冲计数器,*/
//TIM_SetCounter(TIM2,0x0000);
......
}
还有些问题没有搞懂,希望有明白的高人指点一二:
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); 是什么含义??
(感谢STM32给予一下疑问的解答!)
文章评论(0条评论)
登录后参与讨论