AnsweredAssumed Answered

STM32F3 ADC without STD-Lib

Question asked by fischer.markus.002 on Aug 28, 2013
Latest reply on Aug 28, 2013 by Clive One
Hi,
I tried to program an ADC without using STD-Lib. But I have some problems with my initialization, I think. The compiler has no problems with my program, so there are no syntax errors, but the program get stuck at that line:

while((ADC3->ISR & ADC_ISR_EOC)==0);// wait until conversion is complete

I think I have a problem with my initialization, but I don't it exactly. 
I hope somebody can help me ;)

best regards
Markus

GPIO_CLKconfig();
    clock_config();
    //-----ADC_GPIO-----------------
    GPIOB->MODER |= GPIO_MODER_MODER1_1;
    GPIOB->MODER |= GPIO_MODER_MODER1_0; // Analoge MODE
    /*   Analog mode 11
    *    Alternate   10
    *    GPIO        01
    *    Input       00
    */
    GPIOB -> OSPEEDR |=  GPIO_OSPEEDER_OSPEEDR1_1| GPIO_OSPEEDER_OSPEEDR1_0;// 50 MHZ GPIO
    //--------------------------------
 
    //----LED_GPIO---------------------
    GPIOE->MODER |=GPIO_MODER_MODER8_0; // GPIO MODE
    GPIOE->MODER &=~GPIO_MODER_MODER8_1;
 
    /*   Analog mode 11
    *    Alternate   10
    *    GPIO        01
    *    Input       00
    */
    GPIOE -> OSPEEDR |=  GPIO_OSPEEDER_OSPEEDR8_1| GPIO_OSPEEDER_OSPEEDR8_0;// 50 MHZ GPIO
 
    GPIOE->MODER |=GPIO_MODER_MODER9_0;
    GPIOE -> OSPEEDR |=  GPIO_OSPEEDER_OSPEEDR9_1| GPIO_OSPEEDER_OSPEEDR9_0;
    //----------------------------------
 
 
    // ADC init
    RCC -> AHBENR |= RCC_AHBENR_ADC34EN; //CLockenable for ADC 3 und 4
    ADC3 -> CR &= ~ ADC_CR_ADEN; //disable AD
    ADC3->IER |= ADC_IER_EOC | ADC_IER_RDY; //enable EOC-Interrupt and ADC-Ready-Interrupt
 
    /*calibration*/
    ADC3 -> CR &=~ADC_CR_ADCALDIF; //clear register
    ADC3 -> CR |=(1<<28);// voltage regulator 01
    ADC3 -> CR |= ADC_CR_ADCAL;     //starting calibration
    while((ADC3->CR & ADC_CR_ADCAL)==0); //wait until calibration is ready
    /*end of calibration*/
 
    ADC3 -> CR |= ADC_CR_ADEN; //enable AD
    while((ADC3 -> ISR & ADC_ISR_ADRD)==1);//wait until ADC is ready
 
    /*conversion and channel selection*/
    ADC3-> SQR1 = 0;
    ADC3-> SQR1 |=ADC_SQR1_SQ1_0; //channel 1
    ADC3-> CFGR &= ~ADC_CFGR_RES;
    ADC3-> CFGR |= ADC_CFGR_RES_0; // 10 bit resolution
    /*
    * 00 => 12 bit
    * 01 => 10 bit
    * 10 => 8 bit
    * 11 => 6 bit
    */
    while(1)
    {
        ADC3-> SMPR1 |= ADC_SMPR1_SMP1_2; //sampling time to 19.5 clock cycles
        //SMPR1....for channel 1
        ADC3-> CFGR &= ~ADC_CFGR_CONT; //clear register
        ADC3-> CR |= ADC_CR_ADSTART; // ADC start
        while((ADC3->ISR & ADC_ISR_EOC)==0);// wait until conversion is complete
        GPIOE-> BSRR = GPIO_Pin_9; //set LED3 to high
 
        value=ADC_DR_RDATA;             //get value of the ADC
        ADC3-> CR |= ADC_CR_ADSTP;      // stop conversion
 
        if(value>=512)
        {
            GPIOE-> BRR = GPIO_Pin_8;   // low
        }
        else
        {
            GPIOE-> BSRR = GPIO_Pin_8;   //high
        }
 
    }
 
    return 0;
    }
 
 
these are the two librarys which I wrote:
 
void GPIO_CLKconfig()
{
    RCC -> AHBENR |= RCC_AHBENR_GPIOAEN | //enable clock for GPIOA
                     RCC_AHBENR_GPIOBEN | //enable clock for GPIOB
                     RCC_AHBENR_GPIOCEN | //enable clock for GPIOC
                     RCC_AHBENR_GPIODEN | //enable clock for GPIOD
                     RCC_AHBENR_GPIOEEN | //enable clock for GPIOE
                     RCC_AHBENR_GPIOFEN ; //enable clock for GPIOF
}
 
void GPIO_PinConfig(GPIO_TypeDef* port,uint16_t pin,uint8_t mode)
{
    port -> MODER &= ~(1<<(pin*2));
    port -> PUPDR &= ~(1<<(pin*2));
    port -> OSPEEDR &= ~(1<<(pin*2));
    port -> OTYPER &= ~(1<<pin);
 
    port -> MODER |= (uint32_t)((mode&0x03)<<(pin*2));
    port -> PUPDR |= (uint32_t)(((mode&0x03)>>2)<<(pin*2));
    port -> OSPEEDR |= (uint32_t)(((mode&0x03)>>4)<<(pin*2));
    port -> OTYPER |= (uint32_t)(((mode&0x03)>>6)<<pin);
}
 
The second library:
 
void clock_config(void)
{
    /*
     * ================================================================================
     * Initializing of PLL for using a higher internal clock-speed (64MHz)
     * ================================================================================
     */
    /* Enable Prefetch Buffer and set Flash Latency */
    /*These bits represent the ratio of the SYSCLK (system clock) period to the Flash
    access time. */
    /*Siehe RM0316 Seite 62 */
    FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY_1;
 
    /*configure prescaler */
    RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1 | //HCLK = SYSCLK
                 (uint32_t)RCC_CFGR_PPRE1_DIV2 | // prescaler APB1 = 2
                 (uint32_t)RCC_CFGR_PPRE2_DIV1; // prescaler APB2 =2
 
    /* PLL configuration */
    RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL));//clear register
    RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI_Div2 | //PLL gets HSI/2
                            RCC_CFGR_PLLMULL16); //PLL-factor is 16 => 64MHz
 
    RCC->CR |= RCC_CR_PLLON; /* Enable PLL */
    while((RCC->CR & RCC_CR_PLLRDY) == 0);/* Wait till PLL is ready */
 
    /* Select PLL as system clock source */
    RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));//clear register
    RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
    /* Wait till PLL is used as system clock source */
    while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL);
}

Outcomes