добавлена функция изменения разрешения ацп и соответствующие изменения в функции analogread()
This commit is contained in:
parent
b94459ebae
commit
2321bfcad7
@ -12,6 +12,9 @@ extern void ErrorMsgHandler(const char * msg);
|
||||
|
||||
// -------------------------- Analog read -------------------------- //
|
||||
#define ADC_SAMPLES_QTY 10 // samples quantity for averaging adc results
|
||||
#define ADC_DEFAULT_RESOLUTION 10 // resolution for arduino compatibility
|
||||
|
||||
uint8_t currentResolution = ADC_DEFAULT_RESOLUTION; // resolution used for output results
|
||||
|
||||
// structure for ADC channel initialization. Only the channel number
|
||||
// changes, everything else is the same
|
||||
@ -23,6 +26,16 @@ static ADC_HandleTypeDef hadc =
|
||||
.Init.Sel = 0
|
||||
};
|
||||
|
||||
void analogReadResolution(uint8_t resolution)
|
||||
{
|
||||
// resolution limits
|
||||
if (resolution > 32) resolution = 32;
|
||||
if (resolution < 1) resolution = 1;
|
||||
|
||||
// save new resolution
|
||||
currentResolution = resolution;
|
||||
}
|
||||
|
||||
// initialize the channel, run a single measurement, wait for the result
|
||||
uint32_t analogRead(uint32_t PinNumber)
|
||||
{
|
||||
@ -57,8 +70,20 @@ uint32_t analogRead(uint32_t PinNumber)
|
||||
HAL_ADC_Single(&hadc);
|
||||
acc += HAL_ADC_WaitAndGetValue(&hadc);
|
||||
}
|
||||
// get value by averaging
|
||||
// get value by averaging with MCU_ADC_RESOLUTION
|
||||
value = acc/ADC_SAMPLES_QTY;
|
||||
|
||||
// map value to selected resolution
|
||||
if (currentResolution > MCU_ADC_RESOLUTION)
|
||||
{
|
||||
// extra least significant bits are padded with zeros
|
||||
value = (value << (currentResolution - MCU_ADC_RESOLUTION));
|
||||
}
|
||||
else
|
||||
{
|
||||
// extra least significant bits read from the ADC are discarded
|
||||
value = (value >> (MCU_ADC_RESOLUTION - currentResolution));
|
||||
}
|
||||
}
|
||||
else
|
||||
ErrorMsgHandler("analogRead(): invalid analog pin number");
|
||||
|
||||
@ -16,6 +16,13 @@ extern "C" {
|
||||
*/
|
||||
uint32_t analogRead(uint32_t PinNumber);
|
||||
|
||||
/*
|
||||
* \brief Set the resolution of adc results. Default is 10 bits (range from 0 to 1023).
|
||||
*
|
||||
* \param resolution 1...32
|
||||
*/
|
||||
void analogReadResolution(uint8_t resolution);
|
||||
|
||||
/*
|
||||
* \brief Writes an analog value (PWM wave) to a pin.
|
||||
*
|
||||
|
||||
@ -71,6 +71,7 @@ volatile uint32_t* portInputRegister(GPIO_TypeDef* GPIO_x);
|
||||
#define SERIAL_PORT_QTY 2
|
||||
|
||||
// ADC
|
||||
#define MCU_ADC_RESOLUTION 12 // bits
|
||||
// determines the ADC channel number by the board pin number
|
||||
uint32_t analogInputToChannelNumber(uint32_t PinNumber);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user