Setup universal heating function & heating strategy

This commit is contained in:
Clément Grennerat 2025-08-13 23:22:58 +02:00
parent 3ef9b13ce7
commit 068ce0b865

79
main.c
View File

@ -99,30 +99,21 @@ int fetchClimate(int8_t* humidity, int8_t* temperature) {
#define __delay_ms2(ms) {uint8_t __delay_ms_i = (ms); while(__delay_ms_i > 0){ __delay_ms_i--; __delay_us(1000); }}
#define HEAT_DURATION 60 // When heating, heating for a minute
// Water heating at about 100% if 255, and 0% if 0.
void heat1(uint8_t rate) {
// For each tenth of second of HEAT_DURATION, heats in the proportion of rate.
uint8_t delay_on = (uint8_t) (rate / 2.55); // Total elapsed time is 16s
uint8_t delay_off = 101 - delay_on;
// Heating at about 50% if 255, and 0% if 0. Do not exceed 128.
// This function can?t be used to heat at more than 50% power.
void heat(uint8_t rate_water, uint8_t rate_heat) {
// Calculate time repartition for 100 ms
uint8_t delay_on_water = (uint8_t) (rate_water / 5.1);
uint8_t delay_on_heat = (uint8_t) (rate_heat / 5.1);
uint8_t delay_off = 101 - delay_on_water - delay_on_heat;
for(int i = 0; i < HEAT_DURATION; i++){
for(int j = 0; j < 10; j++){
CTRL_1 = 1;
__delay_ms2(delay_on);
__delay_ms2(delay_on_water);
CTRL_1 = 0;
__delay_ms2(delay_off);
}
}
}
// Global heating at about 100% if 255, and 0% if 0.
void heat2(uint8_t rate) {
// For each tenth of second of HEAT_DURATION, heats in the proportion of rate.
uint8_t delay_on = (uint8_t) (rate / 2.55); // Total elapsed time is 16s
uint8_t delay_off = 101 - delay_on;
for(int i = 0; i < HEAT_DURATION; i++){
for(int j = 0; j < 10; j++){
CTRL_2 = 1;
__delay_ms2(delay_on);
__delay_ms2(delay_on_water);
CTRL_2 = 0;
__delay_ms2(delay_off);
}
@ -154,36 +145,12 @@ void main() {
OPTION_REGbits.TMR0CS = 0; // Use internal clock
OPTION_REGbits.TMR0SE = 0; // Increment on rising edge
// Not using PWM as it is not working.
// PWM config PWM1 on RA5 and PWM3 on RA2
PWM3CLKCON = 0b01110000; // - Divide source clock by 128 -> 62500Hz - - FOSC clock source
PWM3PRH = 0xff; // Count to 65535 -> 1Hz full loop
PWM3PRL = 0xff;
PWM3PHH = 0; // No phase count
PWM3PHL = 0;
PWM3OFH = 0; // No offset
PWM3OFL = 0;
PWM3DCH = 0; // Comparator set to 0 -> always off
PWM3DCL = 0;
//PWM3CON = 0b11000000; // Module enable - Output enable - Output state 0 (off) - Output active state is high - Standard PWM mode
APFCONbits.P1SEL = 1; // PWM1 on RA5
PWM1CLKCON = 0b01110000; // - Divide source clock by 128 -> 62500Hz - - FOSC clock source
PWM1PRH = 0xff; // Count to 65535 -> 1Hz full loop
PWM1PRL = 0xff;
PWM1PHH = 0; // No phase count
PWM1PHL = 0;
PWM1OFH = 0; // No offset
PWM1OFL = 0;
PWM1DCH = 0; // Comparator set to 0 -> always off
PWM1DCL = 0;
//PWM1CON = 0b11000000; // Module enable - Output enable - Output state 0 (off) - Output active state is high - Standard PWM mode
// Target (except from nov. to feb.: 15°):
// temp = 24 to 28°
// hum = 60 to 80%
int8_t hum_target = ((int8_t) 80);
int8_t temp_target = ((int8_t) 40);
int8_t hum_target = ((int8_t) 62);
int8_t temp_target = ((int8_t) 25);
int8_t temp_max = ((int8_t) 28);
while (1) {
int8_t hum = 0;
@ -193,26 +160,28 @@ void main() {
__delay_ms(100);
hist_step_count++;
if(hist_step_count == 10) {
if(hist_step_count == 30) { // Stores one value per 30min.
hist_step_count = 0;
temp_hist[hist_count] = temp;
hum_hist[hist_count] = hum;
hist_count++;
if (hist_count < 72) {
temp_hist[hist_count] = temp;
hum_hist[hist_count] = hum;
hist_count++;
}
}
if(is_error){
LATAbits.LATA4 = 0;
__delay_ms(1000);
}else{
if(hum < hum_target){
heat1(128); // 50% Power
if(hum < hum_target && temp < temp_target){
heat(127, 127); // 25% humidify, 25% heat
}else if(hum < hum_target && temp < temp_max){
heat(127, 0); // 25% humidify
}else if(temp < temp_target){
heat2(128);
heat(0, 127); // 25% heat
}else {
__delay_s(HEAT_DURATION);
}
}
//PWM3DCH = 128; // 50% power
//PWM1DCH = 128; // 50% power
}
}