GlobLib
HAL and API libraries for MCUs and hardware.

This module contains low level functions for ADC interaction. More...

Files

file  stm32f103cb_adc.h
 Header file for stm32f103cb ADC API.
 

Macros

#define ADC_VOLT_REF   3.3f
 Typical voltage reference value used by ADC_volts()
 

Functions

mcu_error ADC_setup (gpio_port port, gpio_pin pin)
 Initialize ADC for a given port and pin. More...
 
uint16_t ADC_addChannel (gpio_port port, gpio_pin pin)
 Add an aditional channel to the ADC. More...
 
uint16_t ADC_sample (gpio_port port, gpio_pin pin)
 Get a sample from the ADC. More...
 
float ADC_volts (gpio_port port, gpio_pin pin)
 Get a sample from the ADC and convert it to volts. More...
 

Detailed Description

This module contains low level functions for ADC interaction.

The STM32f1x3 has one ADC with 10 channels on pins PA0 - PA7 and PB0 - PB1. This module containes basic routines to setup the ADC and obatin a sample from each of these channels.

Author
Stuart Ianna
Version
0.1
Date
June 2018
Warning
None
Bug:
None
Todo:
Compilers
  • arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
Minimum working example
#include <coms128.h>
void up(void);
volatile uint8_t flag;
int main(void){
//Clock setup
//Serial setup
COMS(USART_1);
//Setup systick for update at 20ms on function up()
SYSTICK_setup(20000,&up);
//Setup the ADC
//Add an additional channel
while (1){
if(flag){
flag = 0;
printt(ADC_sample(PORTA,PIN0)); //12-Bit sample
printl(ADC_volts(PORTA,PIN1)); //voltage sample
}
}
return 0;
}
//This is called by systick on timeout
void up(void){
flag = 1;
}

Function Documentation

uint16_t ADC_addChannel ( gpio_port  port,
gpio_pin  pin 
)

Add an aditional channel to the ADC.

This can be used after ADC_setup() has been called once to add additional ADC channels

Parameters
portThe port the pin is located on
pinThe pin of the port
Returns
Error number as defined in mcu_error, E_ADC_NOERROR if no error, E_ADC_PORT if port doesn't support ADC or E_ADC_PIN if given pin on port doesn't support ADC

Definition at line 56 of file stm32f103cb_adc.c.

uint16_t ADC_sample ( gpio_port  port,
gpio_pin  pin 
)

Get a sample from the ADC.

Parameters
portThe port the pin is located on
pinThe pin of the port
Returns
The 12-bit sample from the ADC, returns 0 if given port or pin isn't valid

Definition at line 172 of file stm32f103cb_adc.c.

mcu_error ADC_setup ( gpio_port  port,
gpio_pin  pin 
)

Initialize ADC for a given port and pin.

This must be called before any other API feature is used.

Parameters
portThe port the pin is located on
pinThe pin of the port
Returns
Error number as defined in mcu_error, E_ADC_NOERROR if no error, E_ADC_PORT if port doesn't support ADC or E_ADC_PIN if given pin on port doesn't support ADC

Definition at line 50 of file stm32f103cb_adc.c.

float ADC_volts ( gpio_port  port,
gpio_pin  pin 
)

Get a sample from the ADC and convert it to volts.

This uses ADC_VOLT_REF to convert the sample value to volts

Parameters
portThe port the pin is located on
pinThe pin of the port
Returns
The voltage sample from the ADC, returns 0 if given port or pin isn't valid

Definition at line 279 of file stm32f103cb_adc.c.