Close

Printing ADC values over UART

A project log for STM32F030F4P6 breakout board

Not as bulky as the nucleo boards, after all.

christophChristoph 02/10/2015 at 20:442 Comments

Sounds simple? Not quite. The available flash is very limited, so fancy stuff like snprintf() simply won't fit - at least with the newlib I'm using. I'm really not sure how newlib has been built (to be honest I'm not even sure if I did it myself and screwed it up) and if I could trim it a bit.

itoa() maybe? No sign of it.

But I just wanted to see the ADC work and wrote a simple conversion from uint8_t to ascii, which is quick, easy and won't use huge amounts of flash.The ADC is very simple to set up using CubeMX. I've set it to 8 bits, right aligned, continuous conversion mode, overrun data (i.e. discard previous value once a conversion has finished). Every thing I had to add was

MX_ADC_Init(); // <- actually added by CubeMX
HAL_ADC_Start(&hadc);
// Other peripherals ommitted
while(1)
{
  HAL_UART_Transmit(&huart1, "ADC: 0x", 7, HAL_MAX_DELAY);

  uint8_t adc = HAL_ADC_GetValue(&hadc);
  // convert to hex string
  uint8_t nibble;
  char c;
  nibble = (adc >> 4) & 0x0F;
  if (nibble > 9)
  {
    c = 'A' + nibble - 0xA;
  }
  else
  {
    c = '0' + nibble;
  }
  HAL_UART_Transmit(&huart1, &c, 1, HAL_MAX_DELAY);
  nibble = (adc >> 0) & 0x0F;
  if (nibble > 9)
  {
    c = 'A' + nibble - 0xA;
  }
  else
  {
    c = '0' + nibble;
  }
  HAL_UART_Transmit(&huart1, &c, 1, HAL_MAX_DELAY);
  HAL_UART_Transmit(&huart1, "\r", 1, HAL_MAX_DELAY);
  HAL_Delay(100);
}

Surely not the most effective code, but it does the job. A "nibble to ascii" function would certainly make it more readable. DMA should also be easy.

Now the 10k pot, connected as a voltage divider, can "just be read".

After all I've read about CubeMX, it's surprisingly not-that-bad. Writing code for simple tasks like this is just as easy as writing for arduino platforms. Sure, there might be bugs in ST's HAL, but I'm to my knowledge that's also true for arduino.

Discussions

rasyoung wrote 02/12/2015 at 05:12 point

Wondering if you have you're codebase posted somewhere? I'm very excited to see someone shoe-horning functionality onto this little chip...

  Are you sure? yes | no

Christoph wrote 02/12/2015 at 09:16 point

I actually got snprintf() to fit into the available flash by simply using gcc correctly - after reading the readme that came with it. I'll post another project log with more information, but in short:

- generated CubeMX output for Atollic TrueStudio

- imported source files in Code::Blocks

- wrote a linker script

- compiled with gcc-arm-none-eabi 4.9 q4

- linked with newlib-nano

The chip isn't that small, after all. I've also used the ATtiny10 before, which is equally capable of printing an ADC value over a serial line. It's just a bit different and not as convenient.

  Are you sure? yes | no