我想做两对同时更新的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) ;
} |