1

Short version:

Need help with C programming to select channel/s for ADC in STM32F3(Dicovery board). Please find the code that I have written as of now at the end of the question. It would be great if anyone can refer to some resources for bare metal programming.

Long Version:

I am learning programming ARM controllers; what I have is a ST32F303 Discovery board and I believe the method I am trying is called bare metal programing. I have experience in writing plain C code for AVR microcontrollers but that was quite straight forward and almost all the registers were same throughout the series. But when it came to ARM I was totally surprised to see the number of things(files) that we have to get right for even compiling the code properly. By the way I am having a Linux system so I put together the gnu arm toolchain with Eclipse.

After some research and numerous Youtube videos I finalized on two materials

  1. STM32Snippets
  2. NewbieHack - This chaps Youtube series which is for

Of course I am referring the reference manual of the controller in hand too. But the problem is that both of the above resources are not straight forward ones for stm32F3, But I am using the same workflow for writing for F3. Right now I am stuck with ADC as I cant figure out how to select a channel for conversion and some advice and guidance would be helpful

Here is the code I have written so far

  /*
   * ADC in ARM
   *
   * Author : Easwaran
   *
   */
  #include "stm32f30x_conf.h"

  int main (void)
  {
      //ADC calibration
      ADC1->CR &= ~ADC_CR_ADEN; //Disables ADC; 0 in ADCEN to disable ADC.

  //    ADC1->CR
      ADC1->CR |= ADC_CR_ADCAL; //put a 1 in calibration register |  CR is the control register for ADC1
      while ((ADC1->CR & ADC_CR_ADCAL) != 0)
        {
          //waiting till the calibration is over; ie the bit turns 0
        }

      //select a clock source
      RCC->AHBENR |= RCC_AHBENR_ADC12EN; // enables both ADC1 & 2
      RCC->CR |= RCC_CR_HSEON;
      while((RCC->CR & RCC_CR_HSERDY) != 1)
        {
          //to make sure the clock has started
        }
      ADC1->CR = ADC12_CCR_CKMODE; // this is weird

      // enable ADC

      ADC1-> CR = ADC_CR_ADEN;
      while((ADC1->ISR & ADC_ISR_ADRD) == 0)
        {
          //waiting to get ready
        }

      //sampling time

      ADC1->SMPR1 |= ADC_SMPR1_SMP0_0 | ADC_SMPR1_SMP0_1 | ADC_SMPR1_SMP0_2;

      //set the channel - here internal temp register

      while(1)
      {
          //start conversion
      }

  }
easwaran
  • 51
  • 4
  • Did you read the [reference manual](http://www.st.com/content/ccc/resource/technical/document/reference_manual/4a/19/6e/18/9d/92/43/32/DM00043574.pdf/files/DM00043574.pdf/jcr:content/translations/en.DM00043574.pdf), page 321, chapter 15.3.11? – Bence Kaulics May 09 '18 at 08:32
  • Yes I did. From what I uderstand it describes that there are 18 channels and what all are what. I checked the *SQRx* register and all four of them have 4bit sequence selectors with the same description **These bits are written by software with the channel number (1..18) assigned as the 1st in the regular conversion sequence.** what does that mean ? does it mean I have to represnt the channel number in 4 bit form and then set the number of conversions ? I didn't get *regular channel sequence length* too - the first 4 bits of every **SQRx** register. – easwaran May 09 '18 at 08:58
  • Could you also comment if the code and approach are correct till now ? I am taking ques from the reference manual itslef. – easwaran May 09 '18 at 09:01

0 Answers0