我用寄存器方式设置ADC+DMA转换,如果在TIM1_CC4的中断中手动转换,则OK;但是我想把它转成自动转换,则死活不行,哪位高手有什么办**
ADC 初始化
void ADC_Init(void)
{
//1-打开ADC的时钟
RCC->APB2ENR |=RCC_APB2ENR_ADC1EN;
//2- ADC Calibration
ADC1->CR |=(1ul<<31);
while(ADC1->CR&(1ul<<31));
//3-clear
ADC1->CFGR1=0;
//4-连续模式-OFF
ADC1->CFGR1 |=(0<<13);
//6-右对齐
ADC1->CFGR1 |=(0<<5);
//7-12bit
ADC1->CFGR1 |=(0<<3);
//8-扫描方向
ADC1->CFGR1 |=(0<<2);
//9-采样时间
ADC1->SMPR =3;
//10-Vref
ADC1->CHSELR |=(1ul<<17);
ADC->CCR |=(1<<22)|(1<<23);
//11-采样通道0, 1,5,6,16,17
ADC1->CHSELR |=(1<<0)|(1<<1)|(1<<5)|(1<<6)|(1<<16)|(1<<17);
//12-rising
ADC1->CFGR1 |=(1<<10);
//13-TIM1-CC4 triger
ADC1->CFGR1 |=(1<<6);
//14-DMA
ADC1->CFGR1 |=(0<<1)|(1<<0);
//15-Enable ADC
ADC1->CR |=(1<<0);
//16-Start ADC
//ADC_Start();
ADC1->CR |=(1<<2);
}
//TIM1初始化
void TIM1_PWM_Init(void)
{
TIM_OCInitTypeDef TIM_OCst;
//1-打开PORTA的时钟
RCC->AHBENR|=RCC_AHBENR_GPIOAEN;
// //2-PA6--Brake input
// GPIOA->MODER |=(2<<(2*6)); //复用功能
// GPIOA->PUPDR &=~(3<<(2*6)); //无上拉下拉
// GPIOA->OTYPER |=(1<<6); //0-pushpull; 1-OD
// GPIOA->OSPEEDR |=(3<<(2*6)); //输出速度
// GPIOA->AFR[0] |=(2<<(4*6)); //PA6--T1_BKIN
//3-set PA7 as AF
GPIOA->MODER |=(CHN_MODE<<(2*7)); //AF function
GPIOA->OSPEEDR |=(3<<(2*7));
#if CHN_MODE==2
GPIOA->PUPDR |=(2<<(2*7)); //pull-down
GPIOA->AFR[0] |=(2<<(4*7)); //AF2--T1_CH1N
#endif
//3-PA8--T1_CH1
GPIOA->MODER |=(2<<(2*8)); //模式选择
GPIOA->OSPEEDR |=(3<<(2*8)); //输出速度
GPIOA->PUPDR |=(2<<(2*8)); //pull-down
GPIOA->AFR[1] |=(2<<(4*(8-8))); //AF2--T1_CH1
//4-PA9--T1_CH2
GPIOA->MODER |=(2<<(2*9));
GPIOA->OSPEEDR |=(3<<(2*9));
GPIOA->PUPDR |=(2<<(2*9)); //pull-down
GPIOA->AFR[1] |=(2<<(4*(9-8)));
//5-PA10--T1_CH3
GPIOA->MODER |=(2<<(2*10));
GPIOA->OSPEEDR |=(3<<(2*10));
GPIOA->PUPDR |=(2<<(2*10)); //pull-down
GPIOA->AFR[1] |=(2<<(4*(10-8)));
//打开PORTB的时钟
RCC->AHBENR|=RCC_AHBENR_GPIOBEN;
//set PB0 as AF function
GPIOB->MODER |=(CHN_MODE<<(2*0)); //AF function
GPIOB->OSPEEDR |=(3<<(2*0));
#if CHN_MODE==2
GPIOB->PUPDR |=(2<<(2*0));
GPIOB->AFR[0] |=(2<<(4*0)); //AF2-TIM1_CH2N
#endif
//set PB1 as AF function
GPIOB->MODER |=(CHN_MODE<<(2*1)); //AF function
GPIOB->OSPEEDR |=(3<<(2*1));
#if CHN_MODE==2
GPIOB->PUPDR |=(2<<(2*1));
GPIOB->AFR[0] |=(2<<(4*1)); //AF2-TIM1_CH3N
#endif
//打开TIM1的时钟
RCC->APB2ENR |=RCC_APB2ENR_TIM1EN;
/* Time 定时基础设置*/
TIM1->CR1 =(0<<8); //clock division
TIM1->CR1 |=(0<<5); //0-边沿对齐,1-中心对齐1
//预分频
TIM1->PSC =0;
/****** 重复频率 */
TIM1->ARR=ARR_DATA;
/* Set the Repetition Counter value */
TIM1->RCR = 0;
TIM_OCst.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCst.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCst.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCst.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCst.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCst.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCst.TIM_OCNIdleState = TIM_OCNIdleState_Set;
TIM_OCst.TIM_Pulse = 1000;
TIM_OC1Init(TIM1, &TIM_OCst);
TIM_OC2Init(TIM1, &TIM_OCst);
TIM_OC3Init(TIM1, &TIM_OCst);
/*config CC4 for ADC trigger*/
TIM_OCst.TIM_Pulse = 1000-20;
TIM_OC4Init(TIM1, &TIM_OCst);
//OC4REF as TRIGO
TIM1->CR2 =(7<<4);
//configure TIM1 as Master mode(updata)
TIM1->CR2 |=(2<<4);
//设置死区时间
TIM1->BDTR =(1<<10)|(1<<11)|(0<<12)|16;
SetTIMxOut(TIM1,0);
//设置break和CC4,UIF中断
TIM1->DIER =(0<<7)|(1<<4);
SetINT(TIM1_CC_IRQn,1);
SetINT(TIM1_BRK_UP_TRG_COM_IRQn,0);
//开始计数
TIM1->CR1 |= TIM_CR1_CEN;
} |