Lots of bug fixes, everything works
This commit is contained in:
parent
bc9e3f6885
commit
cd65b8fd1f
@ -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;
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ extern "C" {
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
#include <avr/io.h>
|
||||
#define R_T0 10000
|
||||
#define R_T0 100E3
|
||||
#define T_0 298.15
|
||||
#define B 3950
|
||||
#define R_1 1000
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user