Friday, 5 September 2014

Example Saw waveform generator with R2R DAC ATmega328

/*
Example Saw waveform generator with 8 bit R2R DAC pin 0-7
Timer1 Interrupt
*/

#define WAVE_SAMPLES 256
volatile uint16_t a;
uint8_t sample = 0;
boolean c;
boolean togg;
int timer1_counter;

void setup()
{
  DDRD = 0xFF; //b11111111 d255

  // initialize timer1
  cli();           // disable all interrupts
  TCCR1A = 0;
  TCCR1B = 0;

  // Set timer1_counter to the correct value for our interrupt interval
  timer1_counter = 65443;   // preload timer 65536-16MHz/8/21504 Hz

  TCNT1 = timer1_counter;   // preload timer
  TCCR1B |= (1 << CS11);    // clk/8 prescaler
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  sei();          // enable all interrupts
}

ISR(TIMER1_OVF_vect)        // interrupt service routine
{
  TCNT1 = timer1_counter;   // preload timer
  PORTD = sample;
  togg = !togg;
}

void loop()
{
  if (c != togg) {
    a++;
    if (a >= WAVE_SAMPLES) {
      a =0;
    }
    sample =a;
  } //
  c = togg;
}

No comments:

Post a Comment

Feel free to contact me with any suggestions, doubts or requests.

Bless