初级会员
- 积分
- 190
- 金钱
- 190
- 注册时间
- 2019-3-17
- 在线时间
- 43 小时
|
1金钱
PWM初始化
//播放语音时,产生PWM
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//
//TIM_ICInitTypeDef TIM_ICInitStructure;
//NVIC_InitTypeDef NVIC_InitStructure;
//
TIM_DeInit(TIM3);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//
/* time3的时钟:72mhz*/
TIM_TimeBaseStructure.TIM_Prescaler = 0; /*预分频值*/
//
TIM_TimeBaseStructure.TIM_Period = (PLAY_PWM_CYCLE - 1); /*重装载值*/
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; /*时钟分频*/
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; /*向上计数*/
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_SetCounter(TIM3,0x0000);
TIM_ARRPreloadConfig(TIM3,ENABLE);
//
/* tim3->ch4 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; /*PWM2*/
TIM_OCInitStructure.TIM_Pulse = PLAY_PWM_CYCLE/2; /* 50% */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; /* 指定TIM输出比较状态使能*/
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; /* 指定TIM输出极性低*/
TIM_OC4Init(TIM3,&TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable);
//
TIM_ITConfig(TIM3,TIM_IT_CC2,DISABLE);
//
DEBUG_STRING("timer3 init ok\r\n");
更新PWm数据TIM初始化
//定时器,PWM,AD采样
//
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//
TIM_DeInit(TIM1);//Deinitializes the TIM1 peripheral registers to their default reset values
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); /* tim1挂在APB2上,最高时钟72mhz */
//
/* time1的时钟:8mhz,0.125us */
TIM_TimeBaseStructure.TIM_Prescaler = TICK_PRESCALER_USE_PLL_12MHZ;//设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_Period = 999; /* 0-1000:125us *///设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x00;//指定重复计数器值。 每次RCR递减计数器达到零时,都会产生一个更新事件并从RCR值(N)重新开始计数。
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);//根据指定的参数初始化TIMx的时间基数单位
TIM_SetCounter(TIM1,0x0000);//设置TIMx计数器寄存器值
TIM_ARRPreloadConfig(TIM1,ENABLE);//启用或禁用ARR上的TIMx外设预加载寄存器。
//
// ch4,播放语音时更新数据
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStructure.TIM_Pulse = 750; /* 62.5us中断一次 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC4Init(TIM1,&TIM_OCInitStructure);
TIM_ClearFlag(TIM1, TIM_FLAG_CC4);
TIM_ITConfig(TIM1,TIM_IT_CC4,ENABLE);
//
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //0-3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//
DEBUG_STRING("timer1 init ok\r\n");
然后在中断里更新音频数据
|
|