Guest User

ATtiny85 DAC example

a guest
Sep 16th, 2019
242
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Audio Sample Player v2
  2.  
  3.    David Johnson-Davies - www.technoblogy.com - 23rd October 2017
  4.    ATtiny85 @ 8 MHz (internal oscillator; BOD disabled)
  5.      
  6.    CC BY 4.0
  7.    Licensed under a Creative Commons Attribution 4.0 International license:
  8.    http://creativecommons.org/licenses/by/4.0/
  9. */
  10.  
  11. /* Direct-coupled capacitorless output */
  12.  
  13. #include <avr/pgmspace.h>
  14. #include <avr/sleep.h>
  15. #define adc_disable()  (ADCSRA &= ~(1<<ADEN))
  16.  
  17. // Audio encoded as unsigned 8-bit, 8kHz sampling rate// Enable 64 MHz PLL and use as source for Timer1
  18.   PLLCSR = 1<<PCKE | 1<<PLLE;    
  19.  
  20.   // Set up Timer/Counter1 for PWM output
  21.   TIMSK = 0;                              // Timer interrupts OFF
  22.   TCCR1 = 1<<PWM1A | 2<<COM1A0 | 1<<CS10; // PWM A, clear on match, 1:1 prescale
  23.   GTCCR = 1<<PWM1B | 2<<COM1B0;           // PWM B, clear on match
  24.   OCR1A = 128; OCR1B = 128;               // 50% duty at start
  25.  
  26.   // Set up Timer/Counter0 for 8kHz interrupt to output samples.
  27.   TCCR0A = 3<<WGM00;                      // Fast PWM
  28.   TCCR0B = 1<<WGM02 | 2<<CS00;            // 1/8 prescale
  29.   TIMSK = 1<<OCIE0A;                      // Enable compare match
  30.   OCR0A = 124;                            // Divide by 1000
  31.  
  32.   set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  33.   pinMode(4, OUTPUT);
  34.   pinMode(1, OUTPUT);
  35. }
  36.  
  37. void loop () { }
  38.  
  39. // Sample interrupt
  40. ISR (TIMER0_COMPA_vect) {
  41.   char sample = pgm_read_byte(&quack_wav[p++]);
  42.   OCR1A = sample; OCR1B = sample ^ 255;
  43.   // End of data? Go to sleep
  44.   if (p == quack_wav_len) {
  45.     TIMSK = 0;
  46.     adc_disable();
  47.     sleep_enable();
  48.     sleep_cpu();  // 1uA
  49.   }
  50. }
RAW Paste Data