Like in a title. I simply want to measure voltage from ADC1 IN5 - PA5.
I want to measure it constantly in my thread. If it hits some value I'll have to set PAD to 1 or 0 but this is not a problem. A problem is that I don't have any measurement results.
I looked into ADC driver note + read examples. I don't have any idea what I'm doing wrong but I constantly get a 0 in my samples.
Can you help me? I think that I'm not understanding correctly configuration structure an here is a problem.
My code:
Code: Select all
/*
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ch.h"
#include "hal.h"
#include "halconf.h"
#include "pwm.h"
#include "pal.h"
#include "pwm_lld.h"
#include "pal_lld.h"
#include "hd44780.h"
#include "display.h"
//defines for ADC
//number of channels //
#define ADC_GRP1_NUM_CHANNELS 1
//depth of conversion buffer//
#define ADC_GRP1_BUF_DEPTH 1
// clock variables
uint16_t s1;
uint16_t s2;
uint16_t m1;
uint16_t m2;
uint16_t h1;
uint16_t h2;
// ADC measurement variables
double soil_voltage;
uint16_t voltage_buffer;
uint16_t sample[1] = {0};
// ADC samples buffer.
static adcsample_t samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
static const ADCConversionGroup adcgrpcfg = {
TRUE,
ADC_GRP1_NUM_CHANNELS,
NULL,
NULL,
/* HW dependent part. */
0, // cr1
ADC_CR2_SWSTART, // cr2
0,
ADC_SMPR1_SMP_AN10(ADC_SAMPLE_41P5),
ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
0,
ADC_SQR3_SQ2_N(ADC_CHANNEL_IN5)
};
// Voltage measure
static WORKING_AREA(waPomiar, 256);
static msg_t Pomiar(void *arg) {
adcStartConversion(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH);
chThdSleepMilliseconds(2);
voltage_buffer = *samples;
}
/* PWM */
static PWMConfig pwmcfg = {
10000, /* 10Khz PWM clock frequency */
10000, /* PWM period 1 second */
NULL, /* No callback */
/* Only channel 1 enabled */
{
{PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
},
0,
0,
#if STM32_PWM_USE_ADVANCED
0
#endif
};
/*
* clock thread
*/
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {
(void)arg;
chRegSetThreadName("clock");
while (TRUE) {
for(h1=0;h1<3;h1++)
{
for(h2=0;h1<5;h2++)
{
for(m1=0;m1<6;m1++)
{
for(m2=0;m2<10;m2++)
{
for(s1=0;s1<6;s1++)
{
for(s2=0;s2<10;s2++)
{
chThdSleepMilliseconds(1000);
}
}
}
}
}
}
}
return 0;
}
int main(void)
{
halInit();
chSysInit();
/* Enables PWM output (of TIM1, channel 1) on LED connected to PA8 */
palSetPadMode(GPIOA, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
/* Enables PWM output (of TIM1, channel 1N) on LED connected to PB13 */
palSetPadMode(GPIOB, 13, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
pwmStart(&PWMD1, &pwmcfg);
pwmEnableChannel(&PWMD1, 0, 5000);
/*
* Initializes the ADC driver 1 and enable the voltage probe
* The pin 5 on the port PA is programmed as analog input.*/
adcStart(&ADCD1, NULL);
/* INTRO */
LCD_Initialize();
disp_intro();
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
while (TRUE) {
Thread *tp = chThdCreateStatic(waPomiar, sizeof(waPomiar), NORMALPRIO, Pomiar, NULL);
chThdWait(tp);
//scale for mV
soil_voltage = ((double)voltage_buffer) *(3000.0/4095.0);
disp_time();
disp_measure();
chThdSleepMilliseconds(1000);
}
}
Thanks for help!
Regards
Matthew
