Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-dev by
Diff: targets/hal/TARGET_STM/TARGET_STM32F0/analogout_api.c
- Revision:
- 8:69ce7aaad4c4
- Parent:
- 0:9b334a45a8ff
- Child:
- 144:ef7eb2e8f9f7
--- a/targets/hal/TARGET_STM/TARGET_STM32F0/analogout_api.c Fri Oct 16 07:45:35 2015 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F0/analogout_api.c Mon Oct 26 09:00:10 2015 +0000 @@ -36,6 +36,7 @@ #include "PeripheralPins.h" #define DAC_RANGE (0xFFF) // 12 bits +#define DAC_NB_BITS (12) static DAC_HandleTypeDef DacHandle; @@ -80,12 +81,12 @@ pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); } -static inline void dac_write(dac_t *obj, uint16_t value) { +static inline void dac_write(dac_t *obj, int value) { if (obj->pin == PA_4) { - HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value); + HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE)); HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1); - } else { // PA_5 - HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value); + } else if (obj->pin == PA_5) { + HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE)); HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2); } } @@ -93,36 +94,34 @@ static inline int dac_read(dac_t *obj) { if (obj->pin == PA_4) { return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); - } else { // PA_5 + } else if (obj->pin == PA_5) { return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2); } + return 0; /* Just silented warning */ } void analogout_write(dac_t *obj, float value) { if (value < 0.0f) { dac_write(obj, 0); // Min value } else if (value > 1.0f) { - dac_write(obj, (uint16_t)DAC_RANGE); // Max value + dac_write(obj, (int)DAC_RANGE); // Max value } else { - dac_write(obj, (uint16_t)(value * (float)DAC_RANGE)); + dac_write(obj, (int)(value * (float)DAC_RANGE)); } } void analogout_write_u16(dac_t *obj, uint16_t value) { - if (value > (uint16_t)DAC_RANGE) { - dac_write(obj, (uint16_t)DAC_RANGE); // Max value - } else { - dac_write(obj, value); - } + dac_write(obj, value >> (16 - DAC_NB_BITS)); } float analogout_read(dac_t *obj) { uint32_t value = dac_read(obj); - return (float)((float)value * (1.0f / (float)DAC_RANGE)); + return (float)value * (1.0f / (float)DAC_RANGE); } uint16_t analogout_read_u16(dac_t *obj) { - return (uint16_t)dac_read(obj); + uint32_t value = dac_read(obj); + return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits } #endif // DEVICE_ANALOGOUT