#include "pwm_output.h"
static void TIM1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM1 clock enable */
//PCLK1经过2倍频后作为TIM1的时钟源等于72MHz
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
#define TIM1_CCR1_Address 0x40012C34
#define TIM1_CCR3_Address 0x40012C3C
#define TIM1_CCR4_Address 0x40012C40
#define DMA_Channel_TIM1_CCR1 DMA1_Channel2
#define DMA_Channel_TIM1_CCR2 DMA1_Channel3
#define DMA_Channel_TIM1_CCR3 DMA1_Channel6
#define DMA_Channel_TIM1_CCR4 DMA1_Channel4
uint16_t hBuff[4] = {50,400,150,200} ;
static void TIM1_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
DMA_InitTypeDef DMA_InitStructure ;
/* PWM信号电平跳变值 */
u16 CCR1_Val = 0;
u16 CCR2_Val = 0;
u16 CCR3_Val = 0;
u16 CCR4_Val = 500;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA1 channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA_Channel_TIM1_CCR1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM1_CCR1_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)hBuff;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 4;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA_Channel_TIM1_CCR1, &DMA_InitStructure);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA_Channel_TIM1_CCR1, ENABLE);
/* -----------------------------------------------------------------------
TIM1 Configuration: generate 4 PWM signals with 4 different duty cycles:
TIM1CLK = 72 MHz, Prescaler = 0x0, TIM1 counter clock = 72 MHz
TIM1 ARR Register = 999 => TIM1 Frequency = TIM1 counter clock/(ARR + 1)
TIM1 Frequency = 72 KHz.
TIM1 Channel1 duty cycle = (TIM1_CCR1/ TIM1_ARR)* 100 = 50%
TIM1 Channel2 duty cycle = (TIM1_CCR2/ TIM1_ARR)* 100 = 37.5%
TIM1 Channel3 duty cycle = (TIM1_CCR3/ TIM1_ARR)* 100 = 25%
TIM1 Channel4 duty cycle = (TIM1_CCR4/ TIM1_ARR)* 100 = 12.5%
----------------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 999; //当定时器从0计数到9999,即为1000次,为一个定时周期
TIM_TimeBaseStructure.TIM_Prescaler = 71; //设置预分频:不预分频,即为72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分频系数:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3; //上顶点与下低点 触发比较中断标志位
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0 ;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable ;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //设置跳变值,当计数器计数到这个值时,电平发生跳变
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High ;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //使能通道1
TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //设置通道2的电平跳变值,输出另外一个占空比的PWM
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //使能通道2
TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //设置通道3的电平跳变值,输出另外一个占空比的PWM
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //使能通道3
TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //设置通道4的电平跳变值,输出另外一个占空比的PWM
TIM_OC4Init(TIM1, &TIM_OCInitStructure); //使能通道4
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Disable);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Disable);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Disable);
TIM_DMAConfig(TIM1, TIM_DMABase_CCR1, TIM_DMABurstLength_4Bytes);
TIM_DMACmd(TIM1, TIM_DMA_CC1, ENABLE);
TIM_GenerateEvent(TIM1,TIM_EventSource_CC1);
TIM_UpdateRequestConfig(TIM1,TIM_UpdateSource_Global);
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
void TIM1_PWM_Init(void)
{
TIM1_GPIO_Config();
TIM1_Mode_Config();
}
/********************************/
以上代码,我的本意是想在TIM1->CCR1与计数值发生匹配的时候触发DMA更新TIM1->CCR1的值,但是实际情况却是在中心对齐的计数器顶端与底端才会触发DMA的传送,希望有人给我纠错。谢谢
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>