diff --git a/main.c b/main.c index 4437bf9..1fd57fc 100644 --- a/main.c +++ b/main.c @@ -96,24 +96,45 @@ int fetchClimate(int8_t* humidity, int8_t* temperature) { } #define __delay_s(s) {uint8_t __delay_s_i = (s); while(__delay_s_i > 0){ __delay_s_i--; __delay_ms(1000); }} +#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) { - CTRL_1 = 1; - uint8_t delay_on = rate / 16; // Total elapsed time is 16s - __delay_s(delay_on); - CTRL_1 = 0; - uint8_t delay_off = 16 - rate/16; - __delay_s(delay_off); + // 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_1 = 1; + __delay_ms2(delay_on); + CTRL_1 = 0; + __delay_ms2(delay_off); + } + } + } +// Global heating at about 100% if 255, and 0% if 0. void heat2(uint8_t rate) { - CTRL_2 = 1; - uint8_t delay_on = rate / 4; // Total elapsed time is 32s - __delay_s(delay_on); - CTRL_2 = 0; - uint8_t delay_off = 32 - rate/4; - __delay_s(delay_off); + // 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); + CTRL_2 = 0; + __delay_ms2(delay_off); + } + } } +uint8_t hist_step_count = 0; +uint8_t hist_count = 0; +int8_t temp_hist[72] __at(0xA0); +int8_t hum_hist[72] __at(0x120); + + void main() { // 8MHz internal clock OSCCON = 0b01110000; @@ -125,7 +146,7 @@ void main() { LATAbits.LATA4 = 0; // Declare RA4 output value as 0 (only effective when DHT22_PIN_DIR = 0) CTRL_1 = 0; CTRL_2 = 0; - APFCONbits.T1GSEL = 1; // Prevent T1G function to be assigned to RA4. + //APFCONbits.T1GSEL = 1; // Prevent T1G function to be assigned to RA4. // Timer 0 config OPTION_REGbits.PSA = 0; // Enable prescaler @@ -133,25 +154,36 @@ void main() { OPTION_REGbits.TMR0CS = 0; // Use internal clock OPTION_REGbits.TMR0SE = 0; // Increment on rising edge - // PWM config -// PWM2CON = 0b00000000; // Module enable - Output enable - Output state 0 (off) - Output active state is high - Standard PWM mode -// PWM2CLKCON = 0b01110000; // - Divide source clock by 128 -> 62500Hz - - FOSC clock source -// PWM2PRH = 0xff; // Count to 65535 -> 1Hz full loop -// PWM2PRL = 0xff; -// PWM2PHH = 0; // No phase count -// PWM2PHL = 0; -// PWM2OFH = 0; // No offset -// PWM2OFL = 0; -// PWM2DCH = 0; // Comparator set to 0 -> always off -// PWM2DCL = 0; -// APFCONbits.P1SEL = 1; // PWM1 on RA5 - // PWM is not working. + // 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) 30); + int8_t temp_target = ((int8_t) 40); while (1) { int8_t hum = 0; @@ -159,22 +191,28 @@ void main() { int is_error = fetchClimate(&hum, &temp); __delay_ms(100); + + hist_step_count++; + if(hist_step_count == 10) { + hist_step_count = 0; + temp_hist[hist_count] = temp; + hum_hist[hist_count] = hum; + hist_count++; + } + if(is_error){ - // Reconfigure DHT as output to clean it up at 0v -// DHT22_PIN_DIR = 0; -// LATAbits.LATA4 = 0; LATAbits.LATA4 = 0; __delay_ms(1000); }else{ if(hum < hum_target){ - heat1(128); + heat1(128); // 50% Power }else if(temp < temp_target){ heat2(128); }else { - __delay_ms(5000); + __delay_s(HEAT_DURATION); } } -// PWM2DCH = 128; // 50% power -// PWM2DCL = 128; // 50% power + //PWM3DCH = 128; // 50% power + //PWM1DCH = 128; // 50% power } }