Skip to content

Instantly share code, notes, and snippets.

@wgbartley
Last active January 1, 2016 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wgbartley/8184541 to your computer and use it in GitHub Desktop.
Save wgbartley/8184541 to your computer and use it in GitHub Desktop.
Custom analogRead function for testing different ADC_SAMPLING_TIME values
#define MY_ADC_SAMPLING_TIME ADC_SampleTime_239Cycles5
uint8_t My_adcInitFirstTime = true;
uint8_t My_adcChannelConfigured = NONE;
PinMode My_digitalPinModeSaved = (PinMode)NONE;
void My_ADCInit()
{
ADC_InitTypeDef ADC_InitStructure;
// ADCCLK = PCLK2/4
// RCC_ADCCLKConfig(RCC_PCLK2_Div4);
// ADCCLK = PCLK2/6 = 72/6 = 12MHz
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
// Enable ADC1 clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
// Put everything back to power-on defaults
ADC_DeInit(ADC1);
// ADC1 Configuration
// ADC1 operate independently
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
// Disable the scan conversion so we do one at a time
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
// Don't do continuous conversions - do them on demand
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
// Start conversion by software, not an external trigger
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
// Conversions are 12 bit - put them in the lower 12 bits of the result
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
// Say how many channels would be used by the sequencer
ADC_InitStructure.ADC_NbrOfChannel = 1;
// Now do the setup
ADC_Init(ADC1, &ADC_InitStructure);
// Enable ADC1
ADC_Cmd(ADC1, ENABLE);
// Enable ADC1 reset calibration register
ADC_ResetCalibration(ADC1);
// Check the end of ADC1 reset calibration register
while(ADC_GetResetCalibrationStatus(ADC1));
// Start ADC1 calibration
ADC_StartCalibration(ADC1);
// Check the end of ADC1 calibration
while(ADC_GetCalibrationStatus(ADC1));
}
int32_t My_analogRead(uint16_t pin)
{
// Allow people to use 0-7 to define analog pins by checking to see if the values are too low.
if (pin < FIRST_ANALOG_PIN)
{
pin = pin + FIRST_ANALOG_PIN;
}
// SPI safety check
if (SPI.isEnabled() == true && (pin == SCK || pin == MOSI || pin == MISO))
{
return -1;
}
// I2C safety check
if (Wire.isEnabled() == true && (pin == SCL || pin == SDA))
{
return -1;
}
// Serial1 safety check
if (Serial1.isEnabled() == true && (pin == RX || pin == TX))
{
return -1;
}
if (pin >= TOTAL_PINS || PIN_MAP[pin].adc_channel == NONE )
{
return -1;
}
if (My_adcChannelConfigured != PIN_MAP[pin].adc_channel)
{
My_digitalPinModeSaved = PIN_MAP[pin].pin_mode;
pinMode(pin, AN_INPUT);
}
if (My_adcInitFirstTime == true)
{
My_ADCInit();
My_adcInitFirstTime = false;
}
if (My_adcChannelConfigured != PIN_MAP[pin].adc_channel)
{
ADC_RegularChannelConfig(ADC1, PIN_MAP[pin].adc_channel, 1, MY_ADC_SAMPLING_TIME);
My_adcChannelConfigured = PIN_MAP[pin].adc_channel;
}
//Start ADC1 Software Conversion
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
// Wait until conversion completion
// while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
// Get the conversion value
return ADC_GetConversionValue(ADC1);
}
void setup() {
Serial.begin(9600);
pinMode(A0, INPUT);
}
void loop() {
Serial.println(My_analogRead(A0));
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment