打印

请香主看看有没有更好的方法实现两对同时更新的PWM波形

[复制链接]
2473|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
jw_andy|  楼主 | 2010-12-22 13:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我想做两对同时更新的PWM波形,一对是原数据,一对是调整后的数据,它们更新要同步,即四通道的PWM脉冲同时更新,内容不一样但数组的索引是相同的,TIM1只有三通道,不能满足要求,所以用TIM1和TIM8的CCR1和CCR2寄存器,程序如下,请问香主有没有更好的方法呢?
设置如下:
#define TIM1_DMAR_Address    0x40012C4C
#define Max_Samples  30  //定义最大数组值
uint32_t  Sin_Table[Max_Samples];
uint32_t  Cos_Table[Max_Samples];
void DMA_Configuration(void)
{
  DMA_InitTypeDef DMA_InitStructure;
  /* DMA1 Channel5 Config */
  DMA_DeInit(DMA1_Channel5);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM1_DMAR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sin_Table[0];// SRC1_Buffer;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  DMA_InitStructure.DMA_BufferSize =30;
  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(DMA1_Channel5, &DMA_InitStructure);
  DMA_Cmd(DMA1_Channel5, ENABLE);
  
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Cos_Table[0];// SRC1_Buffer;
  DMA_Init(DMA2_Channel1, &DMA_InitStructure);
  DMA_Cmd(DMA2_Channel1, ENABLE);
  
}
int main(void)
{
int  i;
unsigned int dwVal;
for( i=0;i<30;i++)
{
  dwVal = i*10;
  Sin_Table[i]= ((dwVal<<16)|(600-dwVal));
  Cos_Table[i]= ((dwVal<<16)|(600-dwVal));
}
  
  RCC_Configuration();
  GPIO_Configuration();
  DMA_Configuration();
  TIM_TimeBaseStructure.TIM_Prescaler =0;
  TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_CenterAligned2;
  TIM_TimeBaseStructure.TIM_Period = 600;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter =1;// 0;
  
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
  TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
  
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OutputNState =TIM_OutputNState_Enable;
  TIM_OCInitStructure.TIM_Pulse = (unsigned short)(Sin_Table[0]);
  TIM_OCInitStructure.TIM_OCPolarity =  TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
  TIM_OC1Init(TIM1, &TIM_OCInitStructure);
  TIM_OC1Init(TIM8, &TIM_OCInitStructure);
  
  TIM_OCInitStructure.TIM_Pulse = (unsigned short)(Sin_Table[0]>>16);
  TIM_OC2Init(TIM1, &TIM_OCInitStructure);
  TIM_OC2Init(TIM8, &TIM_OCInitStructure);
  //设置 Burst 模式,分别传输一个HalfWord给TIM1_CCR1和TIM1_CCR2
  TIM_DMAConfig(TIM1, TIM_DMABase_CCR1, TIM_DMABurstLength_2Bytes );  
  TIM_DMAConfig(TIM8, TIM_DMABase_CCR1, TIM_DMABurstLength_2Bytes );  
  
  TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE);
  TIM_OC1FastConfig(TIM1, TIM_OCFast_Enable );
  TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
  TIM_OC2FastConfig(TIM1, TIM_OCFast_Enable );
  TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig(TIM1, ENABLE);//ARR预装载缓冲器
  TIM_Cmd(TIM1, ENABLE);
  TIM_DMACmd(TIM8, TIM_DMA_Update, ENABLE);
  TIM_OC1FastConfig(TIM8, TIM_OCFast_Enable );
  TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);
  TIM_OC2FastConfig(TIM8, TIM_OCFast_Enable );
  TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig(TIM8, ENABLE);//ARR预装载缓冲器
  TIM_Cmd(TIM8, ENABLE);
  
  TIM_CtrlPWMOutputs(TIM1, ENABLE);
  TIM_CtrlPWMOutputs(TIM8, ENABLE);
  while (1) ;
  
}
沙发
香水城| | 2010-12-22 14:48 | 只看该作者
你看错了吧,TIM1有4个通道,不是只有3个通道,刚好够你用,:lol

使用特权

评论回复
板凳
jw_andy|  楼主 | 2010-12-22 15:06 | 只看该作者
忘了告诉你,四个通道都要互补和死区输出的,TIM1的第四通道没有这个功能,一直为这个事情烦恼,香主帮忙想想办法吧

使用特权

评论回复
地板
香水城| | 2010-12-22 15:19 | 只看该作者
忘了告诉你,四个通道都要互补和死区输出的,TIM1的第四通道没有这个功能,一直为这个事情烦恼,香主帮忙想想办法吧
jw_andy 发表于 2010-12-22 15:06


也就是说你要4对互补输出通道。

除了使用2个定时器(TIM1和TIM8),我没有办法克服硬件的限制。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

47

帖子

1

粉丝