怎样利用STM32L476的TIM1输出脉冲??

2019-03-23 15:23发布

各位,我想用单独的TIM1通过PA7(TIM1_CH1N)输出指定个数的脉冲。参考了很多例程,网上的,还有STM32CUBE库自带的例程。都没有输出,只是CNT计数器可以计数。请大家指点指点。ST的技术支持也不是狠好。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
10条回答
ZHANGXUEJIE
2019-03-24 20:53
huo_hu 发表于 2018-9-6 12:22
最近刚好弄这个,cube生成的代码里几个地方要操作一下,一个是   TIM_OC_InitStruct.OCState 和 OCNState  ...

CNT可以计数,就是没有脉冲输出。能否看看你的代码,只要有脉冲输出就可以。
/* Peripheral clock enable */
  RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
       
/* Configure the Time base :
       + Counter mode : UP Counter
       + Clock Division : 1
       + Period : 5000
       + Pulse : 2000  
       + Prescaler : 7
       + Repetition counter : PULSE_NUMBER - 1
*/

/* Set the Timer prescaler to get 1MHz as counter clock */
Prescaler = (uint16_t) (SystemCoreClock / 1000000) - 1;

/* Select the up counter mode */
TIM1->CR1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);
TIM1->CR1 |= TIM_COUNTERMODE_UP;
  
/* Set the clock division to 1*/
TIM1->CR1 &= ~TIM_CR1_CKD;         
TIM1->CR1 |= TIM_CLOCKDIVISION_DIV1;
       
/* Set the Autoreload value */
TIM1->ARR = PERIOD;      

/* Set the Pulse value */
TIM1->CCR1 = PULSE;

/* Set the Prescaler value */
TIM1->PSC = Prescaler;

/* Set the Repetition counter value */
TIM1->RCR = PULSE_NUMBER - 1;   

/* Generate an update event to reload the Prescaler and the repetition counter
  value immediately */       
TIM1->EGR = TIM_EGR_UG;            
       
/******** Configure the Internal Clock source *********************************/
/* Disable slave mode to clock the prescaler directly with the internal clock
if the TIM_SMCR is in the reset value, we can delete the following instruction*/
TIM1->SMCR =  RESET;

一周热门 更多>