diff --git a/prosjekt.X/command-handler.c b/prosjekt.X/command-handler.c index 50b3378..25ab39e 100644 --- a/prosjekt.X/command-handler.c +++ b/prosjekt.X/command-handler.c @@ -127,7 +127,7 @@ void parse_command(uint8_t command[], uint8_t command_len) { } // Store the parameter - param = command[2]; + param = command[1]; context.conf = param; @@ -149,8 +149,8 @@ void parse_command(uint8_t command[], uint8_t command_len) { uint8_t bytes[2]; } config_value; - config_value.bytes[0] = command[3]; - config_value.bytes[1] = command[4]; + config_value.bytes[0] = command[2]; + config_value.bytes[1] = command[3]; // Store the value context.conf_val = config_value.value; @@ -165,7 +165,7 @@ uint8_t route_command(int pos) { switch (context.conf) { case SAMPLE_TIME: // Overwrite the config value - config.fanspeed_sample_rate = context.conf_val; + config.ms_fanspeed_sample_rate = context.conf_val; // Set the flag to store it in the EEPROM store_config = true; @@ -190,7 +190,7 @@ uint8_t route_command(int pos) { uint16_t value; uint8_t bytes[2]; } config_value; - config_value.value = config.fanspeed_sample_rate; + config_value.value = config.ms_fanspeed_sample_rate; // Return the corresponding data byte return config_value.bytes[1-pos]; @@ -202,6 +202,9 @@ uint8_t route_command(int pos) { case READ_VOLTAGE: { + // Validate that pos is within range + if (pos >= 2) { return 0; } + // Create a union to store the data union { int16_t v; @@ -225,7 +228,7 @@ uint8_t route_command(int pos) { } // Send the data - return voltage.bytes[pos]; + return voltage.bytes[1-pos]; } case READ_TERMPERATURE: { @@ -239,10 +242,28 @@ uint8_t route_command(int pos) { } break; case READ_FAN_SPEED: + // Validate that pos is within range + if (pos >= 2) { return 0; } + + // Union to hold the u16 if (context.fan == FAN1) { - return fan1_history[fan1_history_index]; + union { + uint16_t val; + uint8_t bytes[2]; + } speed; + + speed.val = fan1_history[fan1_history_index]; + + return speed.bytes[1-pos]; } else if (context.fan == FAN2) { - return fan2_history[fan2_history_index]; + union { + uint16_t val; + uint8_t bytes[2]; + } speed; + + speed.val = fan2_history[fan2_history_index]; + + return speed.bytes[1-pos]; } else { return 0; } diff --git a/prosjekt.X/eeprom.h b/prosjekt.X/eeprom.h index b4fe331..e6d9cae 100644 --- a/prosjekt.X/eeprom.h +++ b/prosjekt.X/eeprom.h @@ -21,7 +21,7 @@ extern "C" { // Struct for information on the controller. typedef struct { - uint16_t fanspeed_sample_rate; + uint16_t ms_fanspeed_sample_rate; } config_t; // Check if EEPROM is ready to be written to diff --git a/prosjekt.X/fan-speed.c b/prosjekt.X/fan-speed.c index 9ba3b89..0fa2085 100644 --- a/prosjekt.X/fan-speed.c +++ b/prosjekt.X/fan-speed.c @@ -1,7 +1,7 @@ #include "fan-speed.h" #include "uart.h" -uint16_t timer_period = 1; +uint16_t timer_period_ms = 1000; uint16_t fan_speed = 0; volatile uint16_t fan1_edge_counter = 0; volatile uint16_t fan2_edge_counter = 0; @@ -12,15 +12,19 @@ void init_TCA0() { TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */ } -void TCA0_update_period(uint16_t timer_period_s) { - TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_s) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */ +void TCA0_update_period(uint16_t period_ms) { + float period_s = period_ms / 1E3; // Convert the ms to s + float frequency = 1/ period_s; // convert the period to a frequency + float perbuf_val = frequency * F_CPU / 1024; // F_IQR * F_CPU / TCA_prescaler + TCA0.SINGLE.PERBUF = (uint16_t)perbuf_val; + timer_period_ms = period_ms; } // COUNTINGS / TIME = FREQUENCY // FREQ / 2 = GIVES ACTUAL FREQ // FREQ * SEC-IN-A-MINUTE(60) / FAN-BLADES uint16_t RPM_calculation(uint16_t edge_counter, uint16_t time_ms) { - fan_speed = (edge_counter / time_ms); + fan_speed = (edge_counter / (float)((float)time_ms/1000.0) ); fan_speed = fan_speed/2; fan_speed = fan_speed * 60/5; edge_counter = 0; @@ -112,8 +116,8 @@ ISR (TCA0_OVF_vect) { } // Calculate the fanspeed - fan1_history[fan1_history_index] = RPM_calculation(fan1_edge_counter,timer_period/1000); - fan2_history[fan2_history_index] = RPM_calculation(fan2_edge_counter,timer_period/1000); + fan1_history[fan1_history_index] = RPM_calculation(fan1_edge_counter, timer_period_ms); + fan2_history[fan2_history_index] = RPM_calculation(fan2_edge_counter, timer_period_ms); // Reset the edge counter fan1_edge_counter = 0; diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index ea0d4a1..b0612b7 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -52,7 +52,7 @@ int main() { init_AC0(); init_AC1(); init_TCA0(); - TCA0_update_period(config.fanspeed_sample_rate); + TCA0_update_period(config.ms_fanspeed_sample_rate); // Only enable UART when required for debugging #ifdef ENABLE_UART @@ -69,7 +69,7 @@ int main() { while (1) { // If we have made a config change, store it and recalculate the period. if (store_config) { - TCA0_update_period(config.fanspeed_sample_rate); + TCA0_update_period(config.ms_fanspeed_sample_rate); write_struct_from_EEPROM(config); store_config = false; } diff --git a/prosjekt.X/themistor-temp.h b/prosjekt.X/themistor-temp.h index fe671b9..46ec2aa 100644 --- a/prosjekt.X/themistor-temp.h +++ b/prosjekt.X/themistor-temp.h @@ -19,7 +19,7 @@ extern "C" { #include #include #include -#define R_T0 10000 +#define R_T0 100E3 #define T_0 298.15 #define B 3950 #define R_1 1000 diff --git a/prosjekt.X/thermistor-temp.c b/prosjekt.X/thermistor-temp.c index 1b3bd2f..dccc95f 100644 --- a/prosjekt.X/thermistor-temp.c +++ b/prosjekt.X/thermistor-temp.c @@ -11,7 +11,7 @@ float calculate_thermistor_temp(float thermistor_voltage){ float ln; float T_thermistor; float V_thermistor; - #define V_TOT 5 + #define V_TOT 3.3 // Calculate Voltage over thermistor V_thermistor = (V_TOT/1024)*thermistor_voltage; diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index a3eb335..6e1162b 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -3,10 +3,10 @@ void ADC0_init(void) { /* Initializing ADC0 pin*/ - /*Voltage reading on pin pd6*/ - PORTD.PIN6CTRL &= ~PORT_ISC_gm; - PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ - PORTD.PIN6CTRL &= PORT_PULLUPEN_bm; + /*Voltage reading on pin pd2*/ + PORTD.PIN2CTRL &= ~PORT_ISC_gm; + PORTD.PIN2CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ + PORTD.PIN2CTRL &= PORT_PULLUPEN_bm; /* Thermistor */ PORTD.PIN3CTRL &= ~PORT_ISC_gm; @@ -42,7 +42,7 @@ uint16_t thermistor_voltage_read() { } // Gets the value over thermistor uint16_t external_voltage_read() { - ADC0.MUXPOS = 0x06; // Read PD6 + ADC0.MUXPOS = 0x02; // Read PD6 uint16_t adc_val = ADC0_read(); return adc_val;