-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
546 lines (451 loc) · 13.6 KB
/
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//*****************************************************************************
// Title : Pulse to tone (DTMF) converter
// Author : Boris Cherkasskiy
// http://boris0.blogspot.ca/2013/09/rotary-dial-for-digital-age.html
// Created : 2011-10-24
// Modified : Arnie Weber 2015-06-22
// https://bitbucket.org/310weber/rotary_dial/
// NOTE: This code is not compatible with Boris's original hardware
// due to changed pin-out (see Eagle files for details)
//
//
// 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
volatile bool bSF_DetectionActive = false; // SF detection active [AW] Moved from local main() variables
volatile bool bCurDialState = true; // Rotor status [AW] Moved from local main() variables
// 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 bCurPulseState = false; // Rotor pulse status
// 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
// [AW] functions moved to INT0 routine
}
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 (OC0A pin)
PORTB = 0; // [AW] Disable Pull-ups - external HW debounce
// Disable unused modules to save power
PRR = (1<<PRTIM1) | (1<<PRUSI) | (1<<PRADC);
ACSR = (1<<ACD);
// Configure pin change interrupt
MCUCR = (1 << ISC01) | (0 << ISC00); // [AW] Set INT0 for falling edge detection
GIMSK = (1 << INT0) | (1 << PCIE); // [AW] Added INT0
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 structure) 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++;
}
// [AW] Handler for external interrupt on INT0 (PB2, pin 7)
ISR(INT0_vect)
{
if (!bCurDialState)
{
// Disabling SF detection
bSF_DetectionActive = false;
// A pulse just started
sDS.iDialedDigit++;
}
}
// [AW] Interrupt handlers updated to new code convention
// Interrupt initiated by pin change on any enabled pin
ISR(PCINT0_vect)
{
// Do nothing, just wake up MCU
_delay_us(100);
}
// [AW] Handler for any unspecified 'bad' interrupts
ISR(BADISR_vect)
{
// Do nothing, just wake up MCU
_delay_us(100);
}