中级会员
- 积分
- 336
- 金钱
- 336
- 注册时间
- 2016-7-3
- 在线时间
- 87 小时
|
发表于 2020-3-13 16:41:07
|
显示全部楼层
我用STM32401CC TIM2 定时器通道1 PA15脚输出 进不了DMA中断,看了下DMA_GetCurrDataCounter(DMA1_Stream5);返回的值不变.好奇怪,搞了好几天了.一点头绪都没有.程序如下:
#include "tim_dma_pwm_pulse_output.h"
#include "delay.h"
#include "led.h"
u16 ZhanKongBi=0;
u16 sss=100;
void TIM2_Dma_Pwm_Pulse_Output_Test(void)
{
u8 i=0;
TIM2_Dma_Pwm_Pulse_Output_Init();
delay_ms(5); //延时一点,给下面的正式脉冲有个间隔,便于观察
TIM2_Dma_Pwm_Pulse_Output(200-1,83,10,100);//周期=200*72/72000000=200us 占空比10% 10个脉冲
while(1)
{
sss=DMA_GetCurrDataCounter(DMA1_Stream5);
delay_ms(1000);
}
}
void TIM2_Dma_Pwm_Pulse_Output_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); //设置缺省值,这一步最好加上防止放到串口初始化后出问题
TIM_OCStructInit(&TIM_OCInitStructure); //设置缺省值,这一步最好加上防止放到串口初始化后出问题
//TIM_ICStructInit(&TIM_ICInitStructure); //设置缺省值,这一步最好加上防止放到串口初始化后出问题
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能定时器3时钟
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
// RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_DMA1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);//DMA1时钟使能
GPIO_PinAFConfig(GPIOA,GPIO_PinSource15,GPIO_AF_TIM2); //GPIOF9复用为定时器14
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //GPIOF9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PF9
//初始化TIM3
TIM_TimeBaseStructure.TIM_Period = 65535; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =0; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//初始化TIM3 Channe1 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC1Init(TIM2, &TIM_OCInitStructure); //初始化通道1
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM3在通道1上的预装载寄存器
DMA_DeInit(DMA1_Stream5); //将DMA的通道1寄存器重设为缺省值
while (DMA_GetCmdStatus(DMA1_Stream5) != DISABLE){}//等待DMA1_Stream4可配置
DMA_ClearITPendingBit(DMA1_Stream5,DMA_IT_FEIF5|DMA_IT_DMEIF5|DMA_IT_TEIF5|DMA_IT_HTIF5|DMA_IT_TCIF5);//清空DMA1_Stream4上所有中断标志
DMA_InitStructure.DMA_Channel = DMA_Channel_3; //通道0 SPI2_TX通道
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&TIM2->CCR1; //DMA外设基地址
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)&ZhanKongBi; //DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //DMA_DIR_PeripheralSRC:从外设读 DMA_DIR_PeripheralDST:从内存读
DMA_InitStructure.DMA_BufferSize = 0; //DMA通道的DMA缓存的大小,实测不加1少一个,应该是最后一个还没输出就中断关闭定时器了,+1就好了
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址寄存器不变
DMA_InitStructure.DMA_MemoryInc = DMA_PeripheralInc_Disable; //内存地址寄存器递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//数据宽度为8位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //数据宽度为8位
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //工作在正常模式
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //DMA通道 x拥有中优先级
// DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
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_DoubleBufferModeConfig(DMA1_Stream3,(u32)buf1,DMA_Memory_0);//双缓冲模式配置
DMA_DoubleBufferModeCmd(DMA1_Stream5,DISABLE);//双缓冲模式开启
//是否开启内存到内存传输(关闭表示只有事件产生才传输一次数据,开启表示一直传输)DMA_M2M_Disable:非存储器到存储器模式(关闭内存到内存模式) DMA_M2M_Enable:启动存储器到存储器模式(开启内存到内存模式)
DMA_Init(DMA1_Stream5, &DMA_InitStructure); //根据DMA_InitStruct中指定的参数初始化DMA的通道USART1_Tx_DMA_Channel所标识的寄存器
// TIM_DMAConfig(TIM2,TIM_DMABase_CCR1,1);
// DMA_Cmd(DMA1_Stream5, ENABLE); //DMA1_3使能
TIM_DMACmd(TIM2, TIM_DMA_Update, ENABLE); //定时器3TIM_DMA_Update使能
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// DMA_ClearITPendingBit(DMA1_Stream5,DMA_IT_FEIF5|DMA_IT_DMEIF5|DMA_IT_TEIF5|DMA_IT_HTIF5|DMA_IT_TCIF5);//清空DMA1_Stream4上所有中断标志
DMA_ITConfig(DMA1_Stream5,DMA_IT_TC,ENABLE); //开传输完成中断
TIM_SetCompare1(TIM2,0); //通道1
TIM_Cmd(TIM2, ENABLE); //使能TIM3
}
void TIM2_Dma_Pwm_Pulse_Output(u16 arr,u16 psc,u8 zhankongbi_temp,u16 geshu)
{
TIM_SetAutoreload(TIM2,arr); //设置重装载寄存器
TIM_PrescalerConfig(TIM2,psc,TIM_PSCReloadMode_Immediate); //设置定时器时钟分频,立即生效
ZhanKongBi=(arr+1)*zhankongbi_temp/100; //计算占空比
TIM_SetCompare1(TIM2,ZhanKongBi); //占空比填入比较寄存器
DMA_Cmd(DMA1_Stream5, DISABLE); //DMA失能才能设置
while (DMA_GetCmdStatus(DMA1_Stream5) != DISABLE){} //确保DMA可以被设置
DMA_SetCurrDataCounter(DMA1_Stream5,geshu+1); //设置DMA长度,即脉冲数量
DMA_Cmd(DMA1_Stream5, ENABLE); //开启
}
void DMA1_Stream5_IRQHandler(void)//脉冲输出完毕,关闭定时器
{
if(DMA_GetITStatus(DMA1_Stream5,DMA_IT_TCIF5))
{
TIM_SetCompare1(TIM2, 0);
RED=!RED;
}
DMA_ClearITPendingBit(DMA1_Stream5,DMA_IT_FEIF5|DMA_IT_DMEIF5|DMA_IT_TEIF5|DMA_IT_HTIF5|DMA_IT_TCIF5) ;
}
|
|