Skip to content

Instantly share code, notes, and snippets.

@hellvesper
Created May 15, 2020 12:31
Show Gist options
  • Save hellvesper/260c3ecba1a0b7e4a3a66ad6bbe5d3b4 to your computer and use it in GitHub Desktop.
Save hellvesper/260c3ecba1a0b7e4a3a66ad6bbe5d3b4 to your computer and use it in GitHub Desktop.
void analogWrite(uint32_t ulPin, uint32_t ulValue)
{
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)
uint8_t do_init = 0;
#endif
PinName p = digitalPinToPinName(ulPin);
if (p != NC) {
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)
if (pin_in_pinmap(p, PinMap_DAC)) {
if (is_pin_configured(p, g_anOutputPinConfigured) == false) {
do_init = 1;
set_pin_configured(p, g_anOutputPinConfigured);
}
ulValue = mapResolution(ulValue, _writeResolution, DACC_RESOLUTION);
dac_write_value(p, ulValue, do_init);
} else
#endif //HAL_DAC_MODULE_ENABLED && !HAL_DAC_MODULE_ONLY
#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)
if (pin_in_pinmap(p, PinMap_PWM)) {
if (is_pin_configured(p, g_anOutputPinConfigured) == false) {
set_pin_configured(p, g_anOutputPinConfigured);
}
ulValue = mapResolution(ulValue, _writeResolution, _internalWriteResolution);
pwm_start(p, _writeFreq, ulValue, _internalWriteResolution);
} else
#endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */
{
//DIGITAL PIN ONLY
// Defaults to digital write
pinMode(ulPin, OUTPUT);
ulValue = mapResolution(ulValue, _writeResolution, 8);
if (ulValue < 128) {
digitalWrite(ulPin, LOW);
} else {
digitalWrite(ulPin, HIGH);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment