Skip to content
Snippets Groups Projects
Commit 3b282bee authored by Karol Hennessy's avatar Karol Hennessy
Browse files

set pwm resolution to 16 bit

parent 077ed2b2
Branches
No related merge requests found
......@@ -37,4 +37,4 @@ const int pin_lcd_d5 = 28;
const int pin_lcd_d6 = 30;
const int pin_lcd_d7 = 32;
const int pwm_resolution = 8; // 8 bit resolution; up to 12 possible
\ No newline at end of file
const int pwm_resolution = 12; // 8 bit resolution; up to 12 possible
\ No newline at end of file
......@@ -41,5 +41,5 @@ const int pin_spare_2 = 23; // I2C SCL
// PWM channels
const int pwm_chan_inhale = 0;
const int pwm_chan_exhale = 1;
const int pwm_resolution = 8; // 8 bit resolution; up to 16 possible
const int pwm_resolution = 16; // 8 bit resolution; up to 16 possible
const int pwm_frequency = 900; // frequency in Hz
\ No newline at end of file
......@@ -43,5 +43,5 @@ const int pin_sda = 22;
// PWM channels
const int pwm_chan_inhale = 0;
const int pwm_chan_exhale = 1;
const int pwm_resolution = 8; // 8 bit resolution; up to 16 possible
const int pwm_resolution = 16; // 8 bit resolution; up to 16 possible
const int pwm_frequency = 900; // frequency in Hz
......@@ -46,23 +46,23 @@ ValvesController::ValvesController()
ValvesController::~ValvesController()
{ ; }
int ValvesController::calcValveDutyCycle(int pwm_resolution, float frac_open)
uint32_t ValvesController::calcValveDutyCycle(uint32_t pwm_resolution, float frac_open)
{
// Here the duty cycle is an integer in the range of the PWM resolution
// Here the duty cycle is an uint32_teger in the range of the PWM resolution
// - for 8 bit, we have range 0-255
// => duty_cycle = frac_open * 255
// there's a hard limit set by MAX_VALVE_FRAC_OPEN
int range_upper_val = pow(2, pwm_resolution) - 1;
uint32_t range_upper_val = pow(2, pwm_resolution) - 1;
if (frac_open > MAX_VALVE_FRAC_OPEN)
return (int)(range_upper_val * MAX_VALVE_FRAC_OPEN);
return (int)(range_upper_val * frac_open);
return (uint32_t)(range_upper_val * MAX_VALVE_FRAC_OPEN);
return (uint32_t)(range_upper_val * frac_open);
}
void ValvesController::setPWMValve(int pin, float frac_open)
{
#ifdef CHIP_ESP32
int duty_cycle = calcValveDutyCycle(pwm_resolution, frac_open);
uint32_t duty_cycle = calcValveDutyCycle(pwm_resolution, frac_open);
int chan = _pin_to_chan[pin];
//if (pin == pin_valve_exhale)
// chan = pwm_chan_exhale;
......
......@@ -32,7 +32,7 @@ public:
uint8_t vexhale, bool vpurge);
void getValves(bool &vin_air, bool &vin_o2, uint8_t &vinhale,
uint8_t &vexhale, bool &vpurge);
int calcValveDutyCycle(int pwm_resolution, float frac_open);
uint32_t calcValveDutyCycle(uint32_t pwm_resolution, float frac_open);
uint8_t getValveInhalePercent();
uint8_t getValveExhalePercent();
uint8_t valveAirInEnabled();
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment