changeset 54:58f1ec46bff6

Add a beeper when the data is stale (only when USB is disabled as they share a pin) Move the time of day timer to OCR2 so we can use PB3/OC0 for PWM.
author darius@Inchoate
date Wed, 29 Oct 2008 17:41:04 +1030
parents 98b5a43fc748
children 6b1057409d9a
files tempctrl.c testavr.c
diffstat 2 files changed, 68 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/tempctrl.c	Wed Oct 29 16:11:15 2008 +1030
+++ b/tempctrl.c	Wed Oct 29 17:41:04 2008 +1030
@@ -52,7 +52,6 @@
     uint8_t	ambient_ROM[8];
     int16_t	target_temp;
     uint16_t	hysteresis;
-
     /* How much to under/overshoot on heating/cooling */
     int16_t	minheatovershoot;
     int16_t	mincoolovershoot;
@@ -80,6 +79,9 @@
     /* Check/stale times */
     int16_t	check_interval;
     int16_t	stale_factor;
+
+    /* Beep if stale */
+    int8_t	dobeep;
 } __attribute__((packed)) settings_t;
 
 /* Current settings in RAM */
@@ -109,11 +111,16 @@
     .heatbits = _BV(7),
     .idlebits = 0x00,
     .check_interval = 10,
-    .stale_factor = 3    
+    .stale_factor = 3,
+    .dobeep = 1
 };
 
 /* Local variable declarations */
 volatile static time_t	now;
+#ifndef WITHUSB
+volatile static uint8_t beeping = 0;
+volatile static uint8_t lasttoggle = 0;
+#endif
 
 /* Local function prototypes */
 static void		tempctrl_load_or_init_settings(void);
@@ -135,13 +142,13 @@
     /* 16Mhz / 1024 = 15625 Hz / 125 = 125 Hz = IRQ every 8 ms */
 
     /* CTC mode, no output on pin, Divide clock by 1024 */
-    TCCR0 = _BV(WGM01)| _BV(CS02) | _BV(CS00);
+    TCCR2 = _BV(WGM21)| _BV(CS22) | _BV(CS21) | _BV(CS20);
     
     /* Compare with ... */
-    OCR0 = 125;
+    OCR2 = 125;
     
     /* Enable interrupt for match on A */
-    TIMSK = _BV(OCIE0);
+    TIMSK = _BV(OCIE2);
 
     now.sec = 0;
     now.usec = 0;
@@ -150,12 +157,12 @@
 }
 
 /*
- * Timer 0 Compare IRQ
+ * Timer 2 Compare IRQ
  *
  * Update time counter
  */
 
-ISR(TIMER0_COMP_vect) {
+ISR(TIMER2_COMP_vect) {
     wdt_reset();
 
     now.usec += 8000; /* 1000000 * 1 / F_CPU / 1024 / 125 */
@@ -163,6 +170,20 @@
 	now.usec -= 1000000;
 	now.sec++;
     }
+
+#ifndef WITHUSB
+    if (beeping) {
+	lasttoggle++;
+	// 63 * 8ms = ~0.5s
+	if (lasttoggle > 63) {
+	    DDRB ^= _BV(PB3);
+	    lasttoggle = 0;
+	}
+    } else {
+	DDRB &= ~_BV(PB3);
+	lasttoggle = 0;
+    }
+#endif    
 }
 
 /* 
@@ -265,6 +286,12 @@
     if (stale)
 	nextstate = 'i';
     
+    /* Handle beeping */
+    if (settings.dobeep && stale)
+	beeping = 1;
+    else
+	beeping = 0;
+	
     /* Handle state forcing */
     if (settings.mode != TC_MODE_AUTO)
 	forced = 1;
@@ -439,6 +466,9 @@
 		     "                 h    Always heat\r\n"
 		     "                 i    Always idle\r\n"
 		     "                 n    Like idle but don't log anything\r\n"
+#ifndef WITHUSB
+		     "tc beep [01]    Enable/disable beeping when data is stale\r\n"
+#endif
 		     "tc X Y          Set X to Y where X is one of\r\n"
 		     "                 targ Target temperature\r\n"
 		     "                 hys  Hysteresis range\r\n"
@@ -477,7 +507,8 @@
 		      "Mode - %c, Target - %d, Hystersis - %d\r\n"
 		      "Min heat overshoot - %d, Min cool overshoot - %d\r\n"
 		      "Min cool on time - %d, Min cool off time - %d\r\n"
-		      "Min heat on time - %d, Min heat off time - %d\r\n"),
+		      "Min heat on time - %d, Min heat off time - %d\r\n"
+		      "Beep on stale - %S\r\n"),
 		 settings.fermenter_ROM[0], settings.fermenter_ROM[1], settings.fermenter_ROM[2], settings.fermenter_ROM[3], 
 		 settings.fermenter_ROM[4], settings.fermenter_ROM[5], settings.fermenter_ROM[6], settings.fermenter_ROM[7], 
 		 settings.fridge_ROM[0], settings.fridge_ROM[1], settings.fridge_ROM[2], settings.fridge_ROM[3], 
@@ -487,7 +518,8 @@
 		 settings.mode, settings.target_temp, settings.hysteresis,
 		 settings.minheatovershoot, settings.mincoolovershoot,
 		 settings.mincoolontime, settings.minheatontime,
-		 settings.minheatontime, settings.minheatofftime);
+		 settings.minheatontime, settings.minheatofftime,
+		 settings.dobeep ? PSTR("yes") : PSTR("no"));
 	return;
     }
     if (!strcasecmp_P(cmd, PSTR("mode"))) {
@@ -506,6 +538,15 @@
 	}
 	return;
     }
+    if (!strcasecmp_P(cmd, PSTR("beep"))) {
+	if (buf[8] == '1')
+	    settings.dobeep = 1;
+	else if (buf[8] == '0')
+	    settings.dobeep = 0;
+	else
+	    printf_P(PSTR("Expected a 0 or 1\r\n"));
+	return;
+    }
     if (!strcasecmp_P(cmd, PSTR("ferm")) ||
 	!strcasecmp_P(cmd, PSTR("frg")) ||
 	!strcasecmp_P(cmd, PSTR("amb"))) {
--- a/testavr.c	Wed Oct 29 16:11:15 2008 +1030
+++ b/testavr.c	Wed Oct 29 17:41:04 2008 +1030
@@ -84,6 +84,10 @@
 #else
     DDRA = 0xff;
     PORTA = 0x00;
+
+    DDRB = 0x00;
+    PORTB = 0x00;
+
 #endif    
     /* GPIO (0:7) */
     DDRC = 0xff;
@@ -93,6 +97,20 @@
     DDRD = 0xf7;
     PORTD = 0xf7;
 
+#ifndef WITHUSB
+    /* Beep
+     *
+     * Fpwm = Fclk / (N * 510)
+     *      = 16e6 / (8 * 510)
+     *      = 3921Hz
+     *
+     * Behind ifndef because PB3 is A0 on PDI chip
+     *
+     * Phase correct PWM, non-inverted output, divide by 8 */
+    TCCR0 = _BV(WGM00) | _BV(WGM11) | _BV(COM00) | _BV(COM01) | _BV(CS01);
+    OCR0 = 253;
+#endif
+
     /* Set up the one wire stuff */
     OWInit();