www.pudn.com > IrDA.rar > dtmf.c


/*
  Copyright (C) 2002-2003 Gerd Rausch, BlauLogic (http://blaulogic.com)
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  3. Except as contained in this notice, neither the name of BlauLogic
     nor the name(s) of the author(s) may be used to endorse or promote
     products derived from this software without specific prior written
     permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHOR(S) OR BLAULOGIC BE LIABLE FOR ANY CLAIM,
  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include 
#include 
#include 
#include 
#include 

#include 

#ifndef AVR_OSC_FREQ
#error please define AVR_OSC_FREQ
#endif

#define TIMER_FREQ	(AVR_OSC_FREQ/510)
#define DIGIT_ON_TICKS	(TIMER_FREQ/8)
#define DIGIT_OFF_TICKS	(TIMER_FREQ/16)
#define FREQ2STEPS(f)	(((uint32_t)(f)*2*sizeof(sin_tab)+TIMER_FREQ/2)/TIMER_FREQ)

static prog_uchar sin_tab[]={
  0x3F, 0x43, 0x46, 0x49, 0x4C, 0x4F, 0x52, 0x55,
  0x58, 0x5B, 0x5E, 0x60, 0x63, 0x65, 0x68, 0x6A,
  0x6D, 0x6F, 0x71, 0x73, 0x75, 0x76, 0x78, 0x79,
  0x7A, 0x7C, 0x7D, 0x7D, 0x7E, 0x7F, 0x7F, 0x7F,
  0x7F, 0x7F, 0x7F, 0x7F, 0x7E, 0x7D, 0x7D, 0x7C,
  0x7A, 0x79, 0x78, 0x76, 0x75, 0x73, 0x71, 0x6F,
  0x6D, 0x6A, 0x68, 0x65, 0x63, 0x60, 0x5E, 0x5B,
  0x58, 0x55, 0x52, 0x4F, 0x4C, 0x49, 0x46, 0x43,
  0x3F, 0x3C, 0x39, 0x36, 0x33, 0x30, 0x2D, 0x2A,
  0x27, 0x24, 0x21, 0x1F, 0x1C, 0x19, 0x17, 0x14,
  0x12, 0x10, 0x0E, 0x0C, 0x0A, 0x09, 0x07, 0x06,
  0x04, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x03,
  0x04, 0x06, 0x07, 0x09, 0x0A, 0x0C, 0x0E, 0x10,
  0x12, 0x14, 0x17, 0x19, 0x1C, 0x1F, 0x21, 0x24,
  0x27, 0x2A, 0x2D, 0x30, 0x33, 0x36, 0x39, 0x3C
};

static uint8_t sin_step1, sin_step2;
static volatile uint16_t ticks;

SIGNAL(SIG_OVERFLOW1)
{
  static uint8_t idx1, idx2, next_pwm_val;

  outp(next_pwm_val, OCR1AL);

  idx1+=sin_step1;
  idx2+=sin_step2;

  next_pwm_val=PRG_RDB(sin_tab+(idx1>>1))+PRG_RDB(sin_tab+(idx2>>1));

  ticks++;
}

static void dial_digit(char digit)
{
  if(digit==',') {
    sleep_msec(2000);
    return;
  }

  switch(digit) {
  case '1':
  case '4':
  case '7':
  case '*':
    sin_step1=FREQ2STEPS(1209);
    break;
  case '2':
  case '5':
  case '8':
  case '0':
    sin_step1=FREQ2STEPS(1336);
    break;
  case '3':
  case '6':
  case '9':
  case '#':
    sin_step1=FREQ2STEPS(1477);
    break;
  default:
    return;
  }

  switch(digit) {
  case '1':
  case '2':
  case '3':
    sin_step2=FREQ2STEPS(697);
    break;
  case '4':
  case '5':
  case '6':
    sin_step2=FREQ2STEPS(770);
    break;
  case '7':
  case '8':
  case '9':
    sin_step2=FREQ2STEPS(852);
    break;
  case '*':
  case '0':
  case '#':
    sin_step2=FREQ2STEPS(941);
    break;
  default:
    return;
  }

  ticks=0;

#ifdef WGM10
  sbi(DDRB, DDB1);
  outp(1<