最近在使用STM32F0做一个AVR控制简单逻辑,比较奇怪的是,只开启前8个通道一切正常,大于8个以上会进入死EOC死等待,读取AD使用同一个函数,具体如下:
void STM32_RCC_Init(void)
{
RCC->CR |= RCC_CR_HSION;
while((RCC->CR&RCC_CR_HSIRDY)== 0);
// RCC->CFGR |= RCC_CFGR_SWS_PLL;
// RCC->CFGR |= RCC_CFGR_PLLXTPRE_HSE_PREDIV_DIV2;
RCC->CFGR |= RCC_CFGR_PLLSRC_HSI_DIV2;
RCC->CFGR |= RCC_CFGR_SW_PLL;
RCC->CFGR |= RCC_CFGR_PLLMUL12;
RCC->CR |= RCC_CR_PLLON;
while((RCC->CR&RCC_CR_PLLRDY) == 0);
// RCC->APB2RSTR |= RCC_APB2RSTR_tiM16RST;
// RCC->APB2RSTR |= RCC_APB2RSTR_TIM1RST;
// RCC->APB2RSTR |= RCC_APB2RSTR_ADCRST;
RCC->AHBENR = RCC->AHBENR|RCC_AHBENR_GPIOAEN|RCC_AHBENR_GPIOBEN
|RCC_AHBENR_GPIOCEN|RCC_AHBENR_GPIODEN;
RCC->APB2ENR = RCC->APB2ENR|RCC_APB2ENR_SYSCFGCOMPEN
|RCC_APB2ENR_ADCEN|RCC_APB2ENR_TIM16EN|RCC_APB2ENR_TIM1EN;
RCC->CR2 |= RCC_CR2_HSI14ON;
while(RCC_CR2_HSI14RDY == 0);
}
void STM32_GPIO_Init(void)
{
GPIOA->MODER = 0x6955ffff;
// GPIOA->MODER = 0x2800ffff;
GPIOB->MODER = 0x55565557;
GPIOB->AFR[1] = 0x02;
GPIOC->MODER = 0x00115fff;
GPIOD->MODER = 0x00000010;
}
void STM32_ADC_Init(void)
{
ADC1->SMPR |= ADC_SMPR_SMP_2|ADC_SMPR_SMP_0;
ADC1->CR |= ADC_CR_ADCAL;
while(((ADC1->CR)&ADC_CR_ADCAL) != 0);
ADC1->CFGR1 |= ADC_CFGR1_OVRMOD;
ADC1->CR |= ADC_CR_ADEN;
while(((ADC1->ISR)&ADC_ISR_ADRDY) == 0);
}
//读取AD值函数
unsigned short ADConvertValues(unsigned char ADChanel)
{
unsigned char ADC_DelayCNT = 0;
while(((ADC1->ISR)&ADC_ISR_ADRDY) == 0);
ADC1->CHSELR = (1<<adchanel);
ADC1->CR |= ADC_CR_ADSTART;
while (!((ADC1->ISR) & ADC_ISR_EOC));
/*
{
ADC_DelayCNT++;
if(ADC_DelayCNT>250)
{
return ADC1->DR;
}
}
*/
return ADC1->DR;
}
//前面8通道读取在定时器TIM1里面做的,一切正常。
void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{
static unsigned short PowButtonCheckcnt;
static unsigned short PageDownCheckcnt;
static unsigned short LcdCnt;
unsigned short DataTep;
unsigned short DataTepA;
unsigned short DataTepB;
unsigned short DataTepC;
unsigned long DataComp;
unsigned char ADCCH;
TIM1->SR = 0x00;
McLedBoostOn;
for(ADCCH=0;ADCCH<9;ADCCH++)
{
DataTep = ADConvertValues(ADCCH);
if(DataTep >= AdOffSetValue)
{
DataTep = DataTep - AdOffSetValue;
}
else
{
DataTep = AdOffSetValue - DataTep;
}
DataComp = DataTep;
DataComp = DataComp*DataTep;
switch(ADCCH)
{
//InputVoltageRMS/
case 0:
NewInPutAPhaseVoltageBuff = NewInPutAPhaseVoltageBuff+DataComp;
break;
case 1:
NewInPutBPhaseVoltageBuff = NewInPutBPhaseVoltageBuff+DataComp;
break;
case 2:
NewInPutCPhaseVoltageBuff = NewInPutCPhaseVoltageBuff+DataComp;
break;
//OutputVoltageRMS/
case 3:
NewOutPutAPhaseVoltageBuff = NewOutPutAPhaseVoltageBuff+DataComp;
DataTepA = DataTep;
break;
case 4:
NewOutPutBPhaseVoltageBuff = NewOutPutBPhaseVoltageBuff+DataComp;
DataTepB = DataTep;
break;
case 5:
NewOutPutCPhaseVoltageBuff = NewOutPutCPhaseVoltageBuff+DataComp;
DataTepC = DataTep;
break;
//OutputVoltageCurrent/
case 6:
NewOutPutAPhaseCurrentBuff = NewOutPutAPhaseCurrentBuff+DataComp;
DataComp = DataTepA;
DataComp = DataComp*DataTep;
NewOutPutAPhaseWatBuff = NewOutPutAPhaseWatBuff+DataComp;
break;
case 7:
NewOutPutBPhaseCurrentBuff = NewOutPutBPhaseCurrentBuff+DataComp;
DataComp = DataTepB;
DataComp = DataComp*DataTep;
NewOutPutBPhaseWatBuff = NewOutPutBPhaseWatBuff+DataComp;
break;
case 8:
NewOutPutCPhaseCurrentBuff = NewOutPutCPhaseCurrentBuff+DataComp;
DataComp = DataTepC;
DataComp = DataComp*DataTep;
NewOutPutCPhaseWatBuff = NewOutPutCPhaseWatBuff+DataComp;
break;
}
}
AD_CONV_CNT++;
NTC_Check_Funcion();
if(AD_CONV_CNT == 0x36)
{
GPIOA->BRR = (~(unsigned short)(AVRControl_REG<<8))&0xff00;
}
if(AD_CONV_CNT == 0x4a)
{
if(New_mode != OutRang_mode)
{
GPIOA->BSRR = (unsigned short)(AVRControl_REG<<8)&0xff00;
}
}
}
//后面8通道用于NTC采样,只要开启一组就有问题,无论该函数放在中断也好,主函数也好,采取单通道扫描也会出现一样问题。
void NTC_Check_Funcion(void)
{
switch(NTC_AD_CNT)
{
case 9:
NTC_RSCR_Ad_Buff = NTC_RSCR_Ad_Buff+ADConvertValues(NTC_AD_CNT);
break;
case 10:
NTC_SSCR_Ad_Buff = NTC_SSCR_Ad_Buff+ADConvertValues(NTC_AD_CNT);
break;
case 11:
NTC_TSCR_Ad_Buff = NTC_TSCR_Ad_Buff+ADConvertValues(NTC_AD_CNT);
break;
case 12:
NTC_RTX_Ad_Buff = NTC_RTX_Ad_Buff+ADConvertValues(NTC_AD_CNT);
break;
case 13:
NTC_STX_Ad_Buff = NTC_STX_Ad_Buff+ADConvertValues(NTC_AD_CNT);
break;
case 14:
NTC_TTX_Ad_Buff = NTC_TTX_Ad_Buff+ADConvertValues(NTC_AD_CNT);
break;
case 15:
NTC_ENV_Ad_Buff = NTC_ENV_Ad_Buff+ADConvertValues(NTC_AD_CNT);
break;
}
NTC_AD_CNT++;
if(NTC_AD_CNT == 10)
{
NTC_AD_CNT = 9;
NTC_CheckCnt ++;
}
if(NTC_CheckCnt == 16)
{
NTC_CheckCnt = 0;
NTC_RSCR_Ad_Buff = NTC_RSCR_Ad_Buff>>4;
NTC_SSCR_Ad_Buff = NTC_SSCR_Ad_Buff>>4;
NTC_TSCR_Ad_Buff = NTC_TSCR_Ad_Buff>>4;
NTC_RTX_Ad_Buff = NTC_RTX_Ad_Buff>>4;
NTC_STX_Ad_Buff = NTC_STX_Ad_Buff>>4;
NTC_TTX_Ad_Buff = NTC_TTX_Ad_Buff>>4;
NTC_ENV_Ad_Buff = NTC_ENV_Ad_Buff>>4;
NTC_RSCR_Level = NTC_Level_Dect(NTC_RSCR_Ad_Buff);
NTC_SSCR_Level = NTC_Level_Dect(NTC_SSCR_Ad_Buff);
NTC_TSCR_Level = NTC_Level_Dect(NTC_TSCR_Ad_Buff);
NTC_RTX_Level = NTC_Level_Dect(NTC_RTX_Ad_Buff);
NTC_STX_Level = NTC_Level_Dect(NTC_STX_Ad_Buff);
NTC_TTX_Level = NTC_Level_Dect(NTC_TTX_Ad_Buff);
NTC_ENV_Level = NTC_Level_Dect(NTC_ENV_Ad_Buff);
NTC_RSCR_Ad_Buff = 0;
NTC_SSCR_Ad_Buff = 0;
NTC_TSCR_Ad_Buff = 0;
NTC_RTX_Ad_Buff = 0;
NTC_STX_Ad_Buff = 0;
NTC_TTX_Ad_Buff = 0;
NTC_ENV_Ad_Buff = 0;
if((NTC_RSCR_Level == 0xff)||(NTC_SSCR_Level == 0xff)||(NTC_TSCR_Level == 0xff)
||(NTC_RTX_Level == 0xff)||(NTC_STX_Level == 0xff)||(NTC_TTX_Level == 0xff)
||(NTC_ENV_Level == 0xff))
{
STAT_FLAGE.TepError = 1;
}
}
}</adchanel);
|