~toykeeper/flashlight-firmware/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * NANJG 105C Diagram
 *           ---
 *         -|   |- VCC
 *  Star 4 -|   |- Voltage ADC
 *  Star 3 -|   |- PWM
 *     GND -|   |- Star 2
 *           ---
 
 * CPU speed is 4.8Mhz without the 8x divider when low fuse is 0x75
 *
 * define F_CPU 4800000  CPU: 4.8MHz  PWM: 9.4kHz       ####### use low fuse: 0x75  #######
 *                             /8     PWM: 1.176kHz     ####### use low fuse: 0x65  #######
 * define F_CPU 9600000  CPU: 9.6MHz  PWM: 19kHz        ####### use low fuse: 0x7a  #######
 *                             /8     PWM: 2.4kHz       ####### use low fuse: 0x6a  #######
 * 
 * !!Above PWM speeds are for phase-correct PWM.  This program uses Fast-PWM, which when the CPU is 4.8MHz will be 18.75 kHz
 *
 * FUSES
 *		I use these fuse settings
 *		Low:  0x75
 *		High: 0xff
 *
 * STARS
 *		Star 2 = Moon if connected		//changed moon is now standard if not soldered
 *		Star 3 = H-L if connected, L-H if not //changed H-L is now standard...more POWER!!!
 *		Star 4 = Capacitor for off-time  PB3 is leg #2
 *
 * VOLTAGE
 *		Resistor values for voltage divider (reference BLF-VLD README for more info)
 *		Reference voltage can be anywhere from 1.0 to 1.2, so this cannot be all that accurate
 *
 *           VCC
 *            |
 *           Vd (~.25 v drop from protection diode)
 *            |
 *          1912 (R1 19,100 ohms)
 *            |
 *            |---- PB2 from MCU
 *            |
 *          4701 (R2 4,700 ohms)
 *            |
 *           GND
 *
 *		ADC = ((V_bat - V_diode) * R2   * 255) / ((R1    + R2  ) * V_ref)
 *		125 = ((3.0   - .25    ) * 4700 * 255) / ((19100 + 4700) * 1.1  )
 *		121 = ((2.9   - .25    ) * 4700 * 255) / ((19100 + 4700) * 1.1  )
 *
 *		Well 125 and 121 were too close, so it shut off right after lowering to low mode, so I went with
 *		130 and 120
 *
 *		To find out what value to use, plug in the target voltage (V) to this equation
 *			value = (V * 4700 * 255) / (23800 * 1.1)
 *      
 */







#define F_CPU 4800000UL

//#define F_CPU 9600000



/*
 * =========================================================================
 * Settings to modify per driver
 */

#define VOLTAGE_MON			// Comment out to disable

#define MEMORY				// Comment out to disable

// Levels should start around 10 with Fast PWM

#define MODE_MOON			10	// Can comment out to remove mode, but should be set through soldering stars
#define MODE_LOW			65//35  // Can comment out to remove mode
//#define MODE_MED			130	// Can comment out to remove mode
#define MODE_HIGH_W_TURBO	150	// MODE_HIGH value when turbo is enabled
#define MODE_HIGH			255	// Can comment out to remove mode
#define MODE_TURBO			255	// Can comment out to remove mode
#define TURBO_TIMEOUT		180 // How many WTD ticks before before dropping down (.5 sec each)

#define ADC_LOW				130	// When do we start ramping
#define ADC_CRIT			124 // When do we shut the light off

#define CAP_THRESHOLD		200  // Value between 1 and 255 corresponding with cap voltage (0 - 1.1v) where we consider it a short press to move to the next mode
								 // Not sure the lowest you can go before getting bad readings, but with a value of 70 and a 1uF cap, it seemed to switch sometimes
								 // even when waiting 10 seconds between presses.

/*
 * =========================================================================
 */

#ifdef MODE_TURBO
#undef  MODE_HIGH
#define MODE_HIGH	MODE_HIGH_W_TURBO
#endif

//#include <avr/pgmspace.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>	
#include <avr/eeprom.h>
#include <avr/sleep.h>
//#include <avr/power.h>

#define STAR2_PIN   PB0
#define STAR3_PIN   PB4
#define CAP_PIN     PB3
#define CAP_CHANNEL 0x03	// MUX 03 corresponds with PB3 (Star 4)
#define CAP_DIDR    ADC3D	// Digital input disable bit corresponding with PB3
#define PWM_PIN     PB1
#define VOLTAGE_PIN PB2
#define ADC_CHANNEL 0x01	// MUX 01 corresponds with PB2
#define ADC_DIDR 	ADC1D	// Digital input disable bit corresponding with PB2
#define ADC_PRSCL   0x06	// clk/64

#define PWM_LVL		OCR0B	// OCR0B is the output compare register for PB1

/*
 * global variables
 */

// Mode storage
uint8_t eepos = 0;
uint8_t eep[32];
uint8_t memory = 0;

// Modes (gets set when the light starts up based on stars)
static uint8_t modes[10];  // Don't need 10, but keeping it high enough to handle all
volatile uint8_t mode_idx = 0;
int     mode_dir = 0; // 1 or -1. Determined when checking stars. Do we increase or decrease the idx when moving up to a higher mode.
uint8_t mode_cnt = 0;

uint8_t lowbatt_cnt = 0;

void store_mode_idx(uint8_t lvl) {  //central method for writing (with wear leveling)
	uint8_t oldpos=eepos;
	eepos=(eepos+1)&31;  //wear leveling, use next cell
	// Write the current mode
	EEARL=eepos;  EEDR=lvl; EECR=32+4; EECR=32+4+2;  //WRITE  //32:write only (no erase)  4:enable  2:go
	while(EECR & 2); //wait for completion
	// Erase the last mode
	EEARL=oldpos;           EECR=16+4; EECR=16+4+2;  //ERASE  //16:erase only (no write)  4:enable  2:go
}
inline void read_mode_idx() {
	eeprom_read_block(&eep, 0, 32);
	while((eep[eepos] == 0xff) && (eepos < 32)) eepos++;
	if (eepos < 32) mode_idx = eep[eepos];//&0x10; What the?
	else eepos=0;
}

inline void next_mode() {
	if (mode_idx == 0 && mode_dir == -1) {
		// Wrap around
		mode_idx = mode_cnt - 1;
	} else {
		mode_idx += mode_dir;
		if (mode_idx > (mode_cnt - 1)) {
			// Wrap around
			mode_idx = 0;
		}
	}
}

inline void check_stars() {
	// Load up the modes based on stars
	// Always load up the modes array in order of lowest to highest mode
	// 0 being low for soldered, 1 for pulled-up for not soldered
	// Moon
#ifdef MODE_MOON
	if ((PINB & (1 << STAR2_PIN)) == 1) {		//changed MOON is standard
		modes[mode_cnt++] = MODE_MOON;
	}
#endif
#ifdef MODE_LOW
	modes[mode_cnt++] = MODE_LOW;
#endif
#ifdef MODE_MED
	modes[mode_cnt++] = MODE_MED;
#endif
#ifdef MODE_HIGH
	modes[mode_cnt++] = MODE_HIGH;
#endif
#ifdef MODE_TURBO
	modes[mode_cnt++] = MODE_TURBO;
#endif
	if ((PINB & (1 << STAR3_PIN)) == 0) {
		// High to Low
		mode_dir = 1;			//changed
	} else {
		mode_dir = -1;			//changed H_L is standard now
	}
}

inline void WDT_on() {
	// Setup watchdog timer to only interrupt, not reset, every 500ms.
	cli();							// Disable interrupts
	wdt_reset();					// Reset the WDT
	WDTCR |= (1<<WDCE) | (1<<WDE);  // Start timed sequence
	WDTCR = (1<<WDTIE) | (1<<WDP2) | (1<<WDP0); // Enable interrupt every 500ms
	sei();							// Enable interrupts
}

inline void WDT_off()
{
	cli();							// Disable interrupts
	wdt_reset();					// Reset the WDT
	MCUSR &= ~(1<<WDRF);			// Clear Watchdog reset flag
	WDTCR |= (1<<WDCE) | (1<<WDE);  // Start timed sequence
	WDTCR = 0x00;					// Disable WDT
	sei();							// Enable interrupts
}

inline void ADC_on() {
	DIDR0 |= (1 << ADC_DIDR);							// disable digital input on ADC pin to reduce power consumption
	ADMUX  = (1 << REFS0) | (1 << ADLAR) | ADC_CHANNEL; // 1.1v reference, left-adjust, ADC1/PB2
	ADCSRA = (1 << ADEN ) | (1 << ADSC ) | ADC_PRSCL;   // enable, start, prescale
}

inline void ADC_off() {
	ADCSRA &= ~(1<<7); //ADC off
}

#ifdef VOLTAGE_MON
uint8_t low_voltage(uint8_t voltage_val) {
	// Start conversion
	ADCSRA |= (1 << ADSC);
	// Wait for completion
	while (ADCSRA & (1 << ADSC));
	// See if voltage is lower than what we were looking for
	if (ADCH < voltage_val) {
		// See if it's been low for a while
		if (++lowbatt_cnt > 8) {
			lowbatt_cnt = 0;
			return 1;
		}
	} else {
		lowbatt_cnt = 0;
	}
	return 0;
}
#endif

ISR(WDT_vect) {
	static uint8_t ticks = 0;
	if (ticks < 255) ticks++;
	
#ifdef MODE_TURBO	
	//if (ticks == TURBO_TIMEOUT && modes[mode_idx] == MODE_TURBO) { // Doesn't make any sense why this doesn't work
	if (ticks == TURBO_TIMEOUT && mode_idx == (mode_cnt - 1)) {
		// Turbo mode is always at end
		PWM_LVL = modes[--mode_idx];
		store_mode_idx(mode_idx);
	}
#endif

}

int main(void)
{	
	// All ports default to input, but turn pull-up resistors on for the stars (not the ADC input!  Made that mistake already)
	PORTB = (1 << STAR2_PIN) | (1 << STAR3_PIN);
	
	// Determine what mode we should fire up
	// Read the last mode that was saved
	read_mode_idx();
	
	check_stars(); // Moving down here as it might take a bit for the pull-up to turn on?
	
	// Start up ADC for capacitor pin
	DIDR0 |= (1 << CAP_DIDR);							// disable digital input on ADC pin to reduce power consumption
	ADMUX  = (1 << REFS0) | (1 << ADLAR) | CAP_CHANNEL; // 1.1v reference, left-adjust, ADC3/PB3
	ADCSRA = (1 << ADEN ) | (1 << ADSC ) | ADC_PRSCL;   // enable, start, prescale
	
	// Wait for completion
	while (ADCSRA & (1 << ADSC));
	// Start again as datasheet says first result is unreliable
	ADCSRA |= (1 << ADSC);
	// Wait for completion
	while (ADCSRA & (1 << ADSC));
	if (ADCH > CAP_THRESHOLD) {
		// Indicates they did a short press, go to the next mode
		next_mode(); // Will handle wrap arounds
		store_mode_idx(mode_idx);
	} else {
		// Didn't have a short press, keep the same mode
	#ifndef MEMORY
		// Reset to the first mode
		mode_idx = ((mode_dir == 1) ? 0 : (mode_cnt - 1));
		store_mode_idx(mode_idx);
	#endif
	}
	// Turn off ADC
	ADC_off();
	
	// Charge up the capacitor by setting CAP_PIN to output
	DDRB  |= (1 << CAP_PIN);	// Output
    PORTB |= (1 << CAP_PIN);	// High
	
    // Set PWM pin to output
    DDRB |= (1 << PWM_PIN);

    // Set timer to do PWM for correct output pin and set prescaler timing
    TCCR0A = 0x23; // phase corrected PWM is 0x21 for PB1, fast-PWM is 0x23
    TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)
	
	// Turn features on or off as needed
	#ifdef VOLTAGE_MON
	ADC_on();
	#else
	ADC_off();
	#endif
	ACSR   |=  (1<<7); //AC off
	
	// Enable sleep mode set to Idle that will be triggered by the sleep_mode() command.
	// Will allow us to go idle between WDT interrupts
	set_sleep_mode(SLEEP_MODE_IDLE);
	
	uint8_t prev_mode_idx = mode_idx;
	
	WDT_on();
	
	// Now just fire up the mode
	PWM_LVL = modes[mode_idx];
	
	uint8_t i = 0;
	uint8_t hold_pwm;
	while(1) 
	{								//main loop
	  #ifdef VOLTAGE_MON								///VOLTAGE MONITOR!!!
		if (low_voltage(ADC_LOW)) 
	      {
			// We need to go to a lower level
			if (mode_idx == 0 && PWM_LVL <= modes[mode_idx]) 
			  {
				// Can't go any lower than the lowest mode
				// Wait until we hit the critical level before flashing 10 times and turning off
				while (!low_voltage(ADC_CRIT));			//low_voltage(x) 1 if longer low zero is normal
				i = 0;
				while (i++<10) {
					PWM_LVL = 0;
					_delay_ms(250);
					PWM_LVL = modes[0];
					_delay_ms(500);
								}
				// Turn off the light
				PWM_LVL = 0;
				// Disable WDT so it doesn't wake us up
				WDT_off();
				ADC_off();
				// Power down as many components as possible
				set_sleep_mode(SLEEP_MODE_PWR_DOWN);
				sleep_mode();
			   }
			 else 
			  {
				// Flash 2 times before lowering
				hold_pwm = PWM_LVL;
				i = 0;
				while (i++<2) 
				  { 
					PWM_LVL = 0;
					_delay_ms(250);
					PWM_LVL = hold_pwm;
					_delay_ms(500);
			      }
				// Lower the mode by half, but don't go below lowest level
				if ((PWM_LVL >> 1) < modes[0]) 
				 {
					PWM_LVL = modes[0];
					mode_idx = 0;
				 } 
				 else 
				 	{					
					  PWM_LVL = (PWM_LVL >> 1);
				     }					
				// See if we should change the current mode level if we've gone under the current mode.
				if (PWM_LVL < modes[mode_idx])
				 {
					// Lower our recorded mode
					mode_idx--;
				  }
			}
			// Wait 3 seconds before lowering the level again
			_delay_ms(3000);
		}//endiflowvoltagemon
	#endif
		sleep_mode();
	}

    return 0; // Standard Return Code
}