PWM波可以输出,但是占空比改变不了,哪位大佬看一下
代码:
void TIM3_Init(void)
{
//定义初始化结构体
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//使能定时器时钟 TIM4
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
//使能GPIOb外设时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//初始化GPIO
//设置服用输出功能TIM4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //TIM4 PB6,7,8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化时基
//设置在下一个更新事件装入活动的自动重装载寄存器周期值50HZ
TIM_TimeBaseStructure.TIM_Period = 125-1; //125-1 3-1
//设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_Prescaler =720-1; //720-112 30-1
//设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
//TIM向上计数模式
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//输出模式配置
//选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
//比较输出使能
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//输出极性:TIM输出比较级性高
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 72;//设置待装入捕获比较寄存器的脉冲值
//根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
// TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //CH1预装载使能
//使能ARR、MOE及定时器
// TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_DMACmd(TIM3, TIM_DMA_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE); //使能TIM4
}
void DMA_Tx_Init( DMA_Channel_TypeDef* DMA_CHx, u32 ppadr, u32 memadr, u16 bufsize)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_DMA1, ENABLE );
delay_ms(5);
DMA_DeInit(DMA_CHx);
//设置DMA源地址:*/
DMA_InitStructure.DMA_PeripheralBaseAddr = ppadr;
//内存地址
DMA_InitStructure.DMA_MemoryBaseAddr = memadr;
//方向:从内存到外设
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
//传输大小
DMA_InitStructure.DMA_BufferSize = bufsize;
//外设地址不增
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_VeryHigh;
//禁止内存传输
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init( DMA_CHx, &DMA_InitStructure ); //初始化DMA1
DMA_Cmd(DMA1_Channel3, ENABLE); //DMA1_3使能
}
extern u32 buf[4];
int main(void)
{
TIM3_Init();
DMA_Tx_Init(DMA1_Channel3, (u32)&TIM3->ARR, (u32)&buf,4);
}
输出的波形
|