STM32F407,利用原子哥的DMA控制串口打印输出源码,改写DMA控制PWM输出占空比,但是结果为有波形输出,无占空比变化。单独测试PWM的输出没有问题。
请大家帮忙看一下是哪里的错误~自己实在找不出问题出在哪儿了
我利用的是TIM3的通道1,PB4引脚
下面是我对于TIM3、DMA的配置和主函数的改写代码
- void TIM3_PWM_Init(u32 arr,u32 psc)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
-
- GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_TIM3);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //GPIOB4
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
-
-
- TIM_TimeBaseStructure.TIM_Prescaler=psc;
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_Period=arr;
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
-
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
-
- //TIM_ForcedOC1Config(TIM12, TIM_ForcedAction_InActive);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC1Init(TIM3, &TIM_OCInitStructure);
- TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
-
- TIM_ARRPreloadConfig(TIM3,ENABLE);
- TIM_Cmd(TIM3, ENABLE);
- }
- void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx,u32 chx,u32 par,u32 mar,u16 ndtr)
- {
-
- DMA_InitTypeDef DMA_InitStructure;
-
- if((u32)DMA_Streamx>(u32)DMA2)
- {
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);
- }else
- {
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);
- }
- DMA_DeInit(DMA_Streamx);
-
- while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}
-
- DMA_InitStructure.DMA_Channel = chx;
- DMA_InitStructure.DMA_PeripheralBaseAddr = par;
- DMA_InitStructure.DMA_Memory0BaseAddr = mar;
- DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
- DMA_InitStructure.DMA_BufferSize = ndtr;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
- DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
- DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
- DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
- DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
- DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
- DMA_Init(DMA_Streamx, &DMA_InitStructure);//³õʼ»¯DMA Stream
- }
- void MYDMA_Enable(DMA_Stream_TypeDef *DMA_Streamx,u16 ndtr)
- {
-
- DMA_Cmd(DMA_Streamx, DISABLE);
-
- while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}
-
- DMA_SetCurrDataCounter(DMA_Streamx,ndtr);
-
- DMA_Cmd(DMA_Streamx, ENABLE);
- }
- int main(void)
- {
- u16 i;
- u8 t=0;
- u8 j,mask=0;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- delay_init(168);
- uart_init(115200);
- LED_Init();
-
- TIM3_PWM_Init(84-1,1-1);
- MYDMA_Config(DMA1_Stream2,DMA_Channel_5,(u32)&TIM3->CCR1,(u32)SendBuff,SEND_BUF_SIZE);
-
- printf("DMA TEST\r\n");
-
- j=sizeof(TEXT_TO_SEND);
- for(i=0;i<SEND_BUF_SIZE;i++)
- {
- if(t>=j)
- {
- if(mask)
- {
- SendBuff[i]=10;
- t=0;
- }else
- {
- SendBuff[i]=20;
- mask++;
- }
- }else
- {
- mask=0;
- SendBuff[i]=TEXT_TO_SEND[t];
- t++;
- }
- }
-
- i=0;
- while(1)
- {
- TIM_DMACmd(TIM3,TIM_DMA_Update,ENABLE);
- MYDMA_Enable(DMA1_Stream2,SEND_BUF_SIZE);
- while(1)
- {
- if(DMA_GetFlagStatus(DMA1_Stream2,DMA_FLAG_TCIF2)!=RESET)
- {
- DMA_ClearFlag(DMA1_Stream2,DMA_FLAG_TCIF2);
- break;
- }
-
- }
-
- i++;
- delay_ms(10);
- if(i==2)
- {
- DS4=!DS4;
- i=0;
- }
-
- }
-
- }
复制代码
|
TIM_DMACmd(TIM3,TIM_DMA_Update,ENABLE);
MYDMA_Enable(DMA1_Stream2,SEND_BUF_SIZE);
这两句放循环里是不想重新使能DMA? DMA已经配置成循环模式了,DMA传输完成指定BufferSize的数量后,又会从头开始传输,不用再重新使能设置个数了。
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
//移到循环外好了
TIM_DMACmd(TIM3,TIM_DMA_Update,ENABLE);
MYDMA_Enable(DMA1_Stream2,SEND_BUF_SIZE);
while(1)
{
while(1)
{
if(DMA_GetFlagStatus(DMA1_Stream2,DMA_FLAG_TCIF2)!=RESET)
{
DMA_ClearFlag(DMA1_Stream2,DMA_FLAG_TCIF2);
break;
}
}
}