P2Dgizmo
Check-in [76d396e3a9]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added the source files as extracted from file Pulse2Tone_2012_01_22.zip
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:76d396e3a9157da2bd1d19a5f3a1aaf5257f94bf
User & Date: jos 2012-12-02 21:12:14
Context
2012-12-02
21:13
This application-note is referenced in the sources, so it's handy to include. check-in: 7893a1e1ee user: jos tags: trunk
21:12
Added the source files as extracted from file Pulse2Tone_2012_01_22.zip check-in: 76d396e3a9 user: jos tags: trunk
21:09
initial empty check-in check-in: ae687159eb user: jos tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added main.c.

























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//*****************************************************************************
// Title		: Pulse to tone (DTMF) converter
// Author		: Boris Cherkasskiy
// Created		: 2011-10-24
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
// DTMF generator logic is loosely based on the AVR314 app note from Atmel
//
//*****************************************************************************


//----- Include Files ---------------------------------------------------------
#include <avr/io.h>		// include I/O definitions (port names, pin names, etc)
#include <avr/interrupt.h>	// include interrupt support
#include <avr/sleep.h>
//#include <avr/wdt.h> 
#include <util/delay.h>
#include <stdbool.h>
#include <avr/eeprom.h>
#include "main.h" 


// EEPROM variables
// 7 speed dial number (dialed special function 3-9)
signed char EEMEM EEPROM_SpeedDial[7][SPEED_DIAL_SIZE] = {[0 ... 6][0 ... SPEED_DIAL_SIZE-1] = DIGIT_OFF};


// Global Variables 
volatile unsigned char cSWa = 0x00;               // step width of high frequency
volatile unsigned char cSWb = 0x00;               // step width of low frequency

volatile unsigned int iCurSinValA = 0;           // position freq. A in LUT (extended format)
volatile unsigned int iCurSinValB = 0;           // position freq. B in LUT (extended format)

volatile unsigned long ulDelayCounter = 0;		// Delay counter for sleep function

// Dial status structure
typedef struct struct_DialStatus
{	
	signed char iDialedDigit;					// Dialed/detected digit

	// SF dialed by holding rotor for few seconds (beep to indicate that SF is activated) before releasing it
	// SF defined as: 1:*; 2:#; 3-9: speed dial; 0: program speed dial number	
	bool bSF_Selected;							// Special Function selected
	
	signed char iSpeedDialDigitIndex;			// Speed dial digit index
	signed char iSpeedDialIndex; 				// Speed dial digi index (in the SD array)
	signed char arSpeedDial[SPEED_DIAL_SIZE];	// Selected speed dial arrays		
} type_DialStatus;

volatile type_DialStatus sDS;	// Global dial status structure


//----- BEGIN MAIN ------------------------------------------------------------
int main(void)
{
	// Program clock prescaller to divide +frequency by 1
	// Write CLKPCE 1 and other bits 0	
	CLKPR = (1<<CLKPCE);	
	// Write prescaler value with CLKPCE = 0
	CLKPR = 0x00;

	// Initialize I/O and global variables
	init();

	// Turn PWM OFF
	GenerateDigit(DIGIT_OFF, 0); 

	// Local dial status variables 
	volatile bool bPrevDialState = true;		// Rotor status
	volatile bool bPrevPulseState = false;	// Rotor pulse status
	volatile bool bCurDialState = true;		// Rotor status
	volatile bool bCurPulseState = false;	// Rotor pulse status
	volatile bool bSF_DetectionActive = false; // SF detection active


	// Main loop
  	while (1)
	{ 
		bCurDialState = bit_is_set (PINB, PIN_DIAL);
		bCurPulseState = bit_is_set (PINB, PIN_PULSE);


		if (bPrevDialState != bCurDialState) 
		{
			if (!bCurDialState) 
			{
				// Dial just started
				// Enabling special function detection
				bSF_DetectionActive = true;
				sDS.bSF_Selected = false;

				sDS.iDialedDigit = 0;
				SleepMS (50);	// Delay 50ms
			} 
			else 
			{
				// Disabling SF detection (should be already disabled)
				bSF_DetectionActive = false;

				// Check that we detect a valid digit
				if ((sDS.iDialedDigit <= 0) || (sDS.iDialedDigit > 10))
				{
					// Should never happen - no pulses detected OR count more than 10 pulses
					sDS.iDialedDigit = DIGIT_OFF;					

					// Do nothing
					SleepMS (50);	// Delay 50ms
				}
				else 
				{
					// Got a valid digit - process it			
					if (sDS.iDialedDigit == 10)
					{
						// 10 pulses => 0
						sDS.iDialedDigit = 0;
					}

					ProcessDialedDigit();
				}
					
				sDS.bSF_Selected = false;	// Reset SF flag
			}	
		} 
		else 
		{
			if (!bCurDialState) 
			{
				// Dial is running				
				if ((bPrevPulseState != bCurPulseState) && bCurPulseState)
				{
					// Disabling SF detection
					bSF_DetectionActive = false;

					// A pulse just started
					sDS.iDialedDigit++;
					SleepMS (50);	// delay 50ms
				}
			} 
			else
			{
				// Rotary dial at the rest position
				// Reset all variables
				bSF_DetectionActive = false;
				sDS.bSF_Selected = false;
				sDS.iDialedDigit = DIGIT_OFF;
			}
		}

		bPrevDialState = bCurDialState;
		bPrevPulseState = bCurPulseState;

		// Don't power down if special function detection is active		
		if (bSF_DetectionActive)
		{
			// SF detection in progress - we need timer to run (IDLE mode)
			set_sleep_mode(SLEEP_MODE_IDLE);		
			sleep_mode();

			// Special function mode detected?
			if (ulDelayCounter >= SF_DELAY_MS * T0_OVERFLOW_PER_MS)
			{
				// SF mode detected
				sDS.bSF_Selected = true;
				bSF_DetectionActive = false;

				// Indicate that we entered SF mode wit short beep
				GenerateDigit (DIGIT_BEEP, 200);
			}
		}
		else
		{
			// Don't need timer - sleep to power down mode
			set_sleep_mode(SLEEP_MODE_PWR_DOWN);
			sleep_mode();
		}
	}
		 
	return 0;
}
//----- END MAIN ------------------------------------------------------------



// Processing dialed digit
void ProcessDialedDigit (void)
{
	// Special functions 1 and 2 (* and #)
	if (sDS.bSF_Selected && (sDS.iDialedDigit == 1))				
	{
		// SF 1-*
		sDS.iDialedDigit = DIGIT_STAR;
	}
	else if (sDS.bSF_Selected && (sDS.iDialedDigit == 2))
	{
		// SF 2-#
		sDS.iDialedDigit = DIGIT_POUND;
	}

	// Speed dial functionality - entering and leaving SD mode
	if (sDS.bSF_Selected && (sDS.iDialedDigit == 0))
	{
		// SF 0 - write speed dial

		// SP programming already in progress?
		if (sDS.iSpeedDialDigitIndex < 0)
		{
			// Just entered SD mode						
			sDS.iSpeedDialDigitIndex = 0;

			// At this point we don't know SD index yet
			sDS.iSpeedDialIndex = -1;

			// Clear selected SD array
			for (unsigned char i=0; i<SPEED_DIAL_SIZE; i++)
			{
				sDS.arSpeedDial[i] = DIGIT_OFF;
			}

			// Beep upon entering SD mode, user has to enter SD index
			GenerateDigit (DIGIT_TUNE_ASC, 700);
			GenerateDigit (DIGIT_TUNE_DESC, 700);
		}
		else
		{
			// SD in progress and user entered SF 0 - save SD and exit SD mode
		
			// Save speed dial number
			WriteCurrentSpeedDial(sDS.iSpeedDialIndex);

			// Leave SD mode
			sDS.iSpeedDialIndex = -1;
			sDS.iSpeedDialDigitIndex = -1;

			// Beep to indicate that we done
			GenerateDigit (DIGIT_TUNE_DESC, 800);
		}
	}
	// Programming SD number
	else if (sDS.iSpeedDialDigitIndex >= 0)
	{
		// First digit dialed after selecting SD mode. SD index not set yet
		if (sDS.iSpeedDialIndex < 0)
		{
			// SD index supposed to be between 3 and 9
			if ((sDS.iDialedDigit >= 3) && (sDS.iDialedDigit <= 9))
			{
				sDS.iSpeedDialIndex = sDS.iDialedDigit;
			
				// Beep to indicate that we are in the SD mode
				GenerateDigit (DIGIT_TUNE_ASC, 800);
			}
			else
			{
				// Wrong SD index - beep and exit SD mode
			
				// Leave SD mode
				sDS.iSpeedDialIndex = -1;
				sDS.iSpeedDialDigitIndex = -1;
			
				// Long Beep to indicate error
				GenerateDigit (DIGIT_BEEP, 1000);
			}
		}
		else
		{
			// Programming SD already in progress

			// Do we have too many digits entered?
			if (sDS.iSpeedDialDigitIndex >= SPEED_DIAL_SIZE)
			{
				// YES - finish and save speed dial number

				// Save speed dial number
				WriteCurrentSpeedDial(sDS.iSpeedDialIndex);

				// Leave SD mode
				sDS.iSpeedDialIndex = -1;
				sDS.iSpeedDialDigitIndex = -1;

				// Beep to indicate that we done
				GenerateDigit (DIGIT_TUNE_DESC, 800);
			} 
			else
			{
				// All good - set new digit to the array
				sDS.arSpeedDial[sDS.iSpeedDialDigitIndex] = sDS.iDialedDigit;

				// Generic beep - do not gererate DTMF code
				GenerateDigit(DIGIT_BEEP_LOW, DTMF_DURATION_MS);

				// Next digit
				sDS.iSpeedDialDigitIndex++;
			}
		}
	}
	// Call SD stored number
	else if (sDS.bSF_Selected && (sDS.iDialedDigit >= 3) && (sDS.iDialedDigit <= 9))
	{
		// SF 3-9 -> Call speed dial number
		Dial_SpeedDialNumber(sDS.iDialedDigit);
	} 
	// Standard (non speed dial functionality)
	else
	{
		// Standard (no speed dial, no special function) mode
		// Generate DTMF code
		GenerateDigit(sDS.iDialedDigit, DTMF_DURATION_MS);  
	}
}


// Initialization
void init (void)
{
	TIMSK  = (1<<TOIE0);                // Int T0 Overflow enabled

	TCCR0A = (1<<WGM00) | (1<<WGM01);   // 8Bit PWM; Compare/match output mode configured later
	TCCR0B = TIMER_PRESCALE_MASK0 & TIMER_CLK_DIV1;
	TCNT0 = 0;
	OCR0A = 0;
	
	// Configure I/O pins
	PORTB = 0;	// Reset all outputs. Force PWM output (PB0) to 0
	DDRB   = (1 << PIN_PWM_OUT);	// PWM output (supposed to be OC0A pin)
	PORTB  = (1 << PIN_DIAL) | (1 << PIN_PULSE);  // Enable Dial/Pulse pull-up resistors

	// Disable unused modules to save power
	PRR = (1<<PRTIM1) | (1<<PRUSI) | (1<<PRADC);
	ACSR = (1<<ACD);

	// Configure pin change interrupt
	GIMSK = (1<<PCIE);
	PCMSK = (1 << PIN_DIAL) | (1 << PIN_PULSE);

	// Initialize (global) dial status structure (sDS)
	sDS.iDialedDigit = DIGIT_OFF;

	// Variables to detect special functions (SF)
	// SF dialed by holding rotor for few seconds (beep to indicate that SF activated) before releasing it
	// SF defined as: 1:*; 2:#; 3-9: speed dial; 0: program speed dial number
	sDS.bSF_Selected = false;	// Special Function selected

	// Speed dial stuff
	sDS.iSpeedDialDigitIndex = -1;	// Speed dial digit index
	sDS.iSpeedDialIndex = -1; // Speed dial digi index (in the SD array)	
	for (unsigned char i=0; i<SPEED_DIAL_SIZE; i++)	// Clear selected SD array
	{
		sDS.arSpeedDial[i] = DIGIT_OFF;
	}

	// Interrupts enabled
	sei();                     	     
}


// Generate DTMF tone, duration x ms
void GenerateDigit (signed char scDigit, unsigned int uiDuarationMS)
{
	if (scDigit >= 0 && scDigit <= DIGIT_POUND)
	{
		// Standard digits 0-9, *, #
		cSWa = auc_frequency[scDigit][0];  
		cSWb = auc_frequency[scDigit][1]; 
		EnablePWM();

		// Wait x ms
		SleepMS(uiDuarationMS);
	} 
	else if (scDigit==DIGIT_BEEP)
	{
		// Beep ~1000Hz (66)
		cSWa = 66;  
		cSWb = 0;
		EnablePWM();

		// Wait x ms
		SleepMS(uiDuarationMS);
	}
	else if (scDigit==DIGIT_BEEP_LOW)
	{
		// Beep ~500Hz (33)
		cSWa = 33;  
		cSWb = 0;
		EnablePWM();

		// Wait x ms
		SleepMS(uiDuarationMS);
	}
	else if (scDigit==DIGIT_TUNE_ASC)
	{
		cSWa = 34;	// C=523.25Hz  
		cSWb = 0;
		EnablePWM();
		
		SleepMS(uiDuarationMS/3);
		cSWa = 43;	// E=659.26Hz
		SleepMS(uiDuarationMS/3);
		cSWa = 51;	// G=784Hz
		SleepMS(uiDuarationMS/3);
	}
	else if (scDigit==DIGIT_TUNE_DESC)
	{
		cSWa = 51;	// G=784Hz
		cSWb = 0;
		EnablePWM();

		SleepMS(uiDuarationMS/3);
		cSWa = 43;	// E=659.26Hz
		SleepMS(uiDuarationMS/3);
		cSWa = 34;	// C=523.25Hz  
		SleepMS(uiDuarationMS/3);
	}


	// Stop DTMF transmitting
	// Disable PWM output (compare match mode 0) and force it to 0
	cbi(TCCR0A, COM0A1);
	cbi(TCCR0A, COM0A0);
	cbi(PORTB, PIN_PWM_OUT);
	cSWa = 0;
	cSWb = 0;
}


// Enable PWM output by configuring compare match mode - non inverting PWM
void EnablePWM (void)
{
	sbi(TCCR0A, COM0A1);
	cbi(TCCR0A, COM0A0);
}


// Wait x ms
void SleepMS(unsigned int uiMsec)
{	
	ulDelayCounter = 0;
	
	set_sleep_mode(SLEEP_MODE_IDLE);		
	while(ulDelayCounter <= uiMsec * T0_OVERFLOW_PER_MS)
	{
		sleep_mode();
	}
}


// Dial speed dial number (it erases current SD number in the global structure)
void Dial_SpeedDialNumber (unsigned char iSpeedDialIndex)
{
	if ((iSpeedDialIndex >= 3) && (iSpeedDialIndex <= 9))
	{
		// If dialed index 3 => using array index 0
		eeprom_read_block (&sDS.arSpeedDial, &EEPROM_SpeedDial[iSpeedDialIndex-3][0], SPEED_DIAL_SIZE);

		for (unsigned char i=0; i<SPEED_DIAL_SIZE; i++)
		{
			// Dial the number
			// Skip dialing invalid digits
			if ( (sDS.arSpeedDial[i] >= 0) && (sDS.arSpeedDial[i] <= DIGIT_POUND) )
			{
				GenerateDigit(sDS.arSpeedDial[i], DTMF_DURATION_MS);  

				// Pause between DTMF tones
				SleepMS (DTMF_DURATION_MS);    
			}
		}
	}
}


// Write current speed dial array (from the global strucutre) to the EEPROM
void WriteCurrentSpeedDial(unsigned char iSpeedDialIndex)
{
	if ((iSpeedDialIndex >= 3) && (iSpeedDialIndex <= 9))
	{
		// If dialed index 3 => using array index 0
		eeprom_update_block (&sDS.arSpeedDial, &EEPROM_SpeedDial[iSpeedDialIndex-3][0], SPEED_DIAL_SIZE);
	}
}


// Timer overflow interrupt service routine
TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW0)
{ 
	unsigned char ucSinA;
	unsigned char ucSinB;

	// A component (high frequency) is always used
	// move Pointer about step width ahead
	iCurSinValA += cSWa;      
	// normalize Temp-Pointer 
	unsigned int i_TmpSinValA = (char)(((iCurSinValA + 4) >> 3) & (0x007F)); 
	ucSinA = auc_SinParam[i_TmpSinValA];


	//	B component (low frequency) is optional
	if (cSWb > 0)
	{
		// move Pointer about step width ahead
		iCurSinValB += cSWb;	
		// normalize Temp-Pointer	
		unsigned int i_TmpSinValB = (char)(((iCurSinValB + 4) >> 3) & (0x007F));		
		ucSinB = auc_SinParam[i_TmpSinValB];
	}
	else
	{
		ucSinB = 0;
	}

	// calculate PWM value: high frequency value + 3/4 low frequency value
	OCR0A = (ucSinA + (ucSinB - (ucSinB >> 2)));

	ulDelayCounter++;
}


// Pin change interrupt 
PIN_INTERRUPT_HANDLER(SIG_PIN_CHANGE)
{ 
	// Do nothing, just wake up MCU
	_delay_us(100);
}

Added main.h.























































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
//*****************************************************************************
// Title		: Pulse to tone (DTMF) converter
// Author		: Boris Cherkasskiy
// Created		: 2011-10-24
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
// DTMF generator logic is loosely based on the AVR314 app note from Atmel
//
//*****************************************************************************


#ifndef MAIN_H
#define MAIN_H


#ifndef TIMER_INTERRUPT_HANDLER
#define TIMER_INTERRUPT_HANDLER		SIGNAL
#endif

#ifndef PIN_INTERRUPT_HANDLER
#define PIN_INTERRUPT_HANDLER		SIGNAL
#endif


// Define constants
#define TIMER_CLK_STOP			0x00	///< Timer Stopped
#define TIMER_CLK_DIV1			0x01	///< Timer clocked at F_CPU
#define TIMER_CLK_DIV8			0x02	///< Timer clocked at F_CPU/8
#define TIMER_CLK_DIV64			0x03	///< Timer clocked at F_CPU/64
#define TIMER_CLK_DIV256		0x04	///< Timer clocked at F_CPU/256
#define TIMER_CLK_DIV1024		0x05	///< Timer clocked at F_CPU/1024

#define TIMER_PRESCALE_MASK0		0x07	///< Timer Prescaler Bit-Mask
#define TIMER_PRESCALE_MASK1		0x0F	///< Timer Prescaler Bit-Mask

#define  N_samples  128              // Number of samples in lookup table

#define DIGIT_BEEP	-10
#define DIGIT_BEEP_LOW	-13
#define DIGIT_TUNE_ASC	-11
#define DIGIT_TUNE_DESC	-12
#define DIGIT_OFF	-1
#define DIGIT_STAR	10
#define DIGIT_POUND	11

#define DTMF_DURATION_MS 100

// PWM frequency = 4Mhz/256 = 15625Hz; overflow cycles per MS = 15
#define T0_OVERFLOW_PER_MS 15
#define SF_DELAY_MS 2000
#define SPEED_DIAL_SIZE 30

// PB0 (OC0A) as PWM output
#define PIN_PWM_OUT PB0

#define PIN_DIAL PB2
#define PIN_PULSE PB1


// Helper functions
#ifndef BV
	#define BV(bit)			(1<<(bit))
#endif
#ifndef cbi
	#define cbi(reg,bit)	reg &= ~(BV(bit))
#endif
#ifndef sbi
	#define sbi(reg,bit)	reg |= (BV(bit))
#endif


//************************** SIN TABLE *************************************
// Samples table : one period sampled on 128 samples and
// quantized on 7 bit
//**************************************************************************
const unsigned char auc_SinParam [N_samples] = {
	64, 67,  70, 73,
	76, 79,  82, 85,
	88, 91,  94, 96,
	99, 102, 104,106,
	109,111, 113,115,
	117,118, 120,121,
	123,124, 125,126,
	126,127, 127,127,
	127,127, 127,127,
	126,126, 125,124,
	123,121, 120,118,
	117,115, 113,111,
	109,106, 104,102,
	99, 96,  94, 91,
	88, 85,  82, 79,
	76, 73,  70, 67,
	64, 60,  57, 54,
	51, 48,  45, 42,
	39, 36,  33, 31,
	28, 25,  23, 21,
	18, 16,  14, 12,
	10, 9,   7,  6,
	4,  3,   2,  1,
	1,  0,   0,  0,
	0,  0,   0,  0,
	1,  1,   2,  3,
	4,  6,   7,  9,
	10, 12, 14,  16,
	18, 21, 23,  25,
	28, 31, 33,  36,
	39, 42, 45,  48,
	51, 54, 57,  60
};


//***************************  x_SW  ***************************************
// Fck=Xtal/prescaler
// Table of x_SW (excess 8): x_SW = ROUND(8 * N_samples * f * 510 / Fck)
//**************************************************************************

//high frequency
//1209hz  ---> x_SW = 79
//1336hz  ---> x_SW = 87
//1477hz  ---> x_SW = 96
//1633hz  ---> x_SW = 107
//
//low frequency
//697hz  ---> x_SW = 46
//770hz  ---> x_SW = 50
//852hz  ---> x_SW = 56
//941hz  ---> x_SW = 61
//
// 		|  1209 | 1336 | 1477 | 1633
//  697 |   1	|  2   |   3  |   A
//  770 |   4	|  5   |   6  |   B
//  852 |   7	|  8   |   9  |   C
//  941 |	*	|  0   |   #  |   D


const unsigned char auc_frequency [12][2] = {
	{87, 61},	// 0
	{79, 46},	// 1
	{87, 46},	// 2
	{96, 46},	// 3
	{79, 50},	// 4
	{87, 50},	// 5
	{96, 50},	// 6
	{79, 56},	// 7
	{87, 56},	// 8
	{96, 56},	// 9
	{79, 61},	// *
	{96, 61},	// #
};


// Function prototypes
int main(void);

void ProcessDialedDigit (void);

void init (void);

void GenerateDigit(signed char scDigit, unsigned int uiDuarationMS);

void EnablePWM (void);
void SleepMS (unsigned int uiMsec);

void Dial_SpeedDialNumber(unsigned char ucSpeedDialIndex);
void WriteCurrentSpeedDial(unsigned char ucSpeedDialIndex);

#endif