domingo, 14 de mayo de 2017

ATtiny85 Tutorial

1. FLASH LED


Download: Flash LED
_______________________________________________________________________

2. PUSH BUTTON



Download: Push Button
_______________________________________________________________________

3. FLASHING THERMOMETER




#include <avr/sleep.h>                                                                                                                                                                                                                                                                                                                                                                             

const int red = 0;                         // PB0
const int green = 2;                       // PB2

int ReadADC() {
  ADCSRA = ADCSRA | 1<<ADEN;               // Enable ADC
  set_sleep_mode(SLEEP_MODE_ADC);
  sleep_enable();
  sleep_cpu();                             // Go into ADC noise reduction mode
  int low, high;                           // Return here on ADC interrupt
  low = ADCL;
  high = ADCH;
  ADCSRA = ADCSRA & ~(1<<ADEN);            // Disable ADC to save power
  return (high<>i & 1;
    if (show || b || (i==0)) {
      if (value>>i & 1) colour = red; else colour = green;
      digitalWrite(colour, HIGH);
      WDDelay(0);                              // 12msec flash
      digitalWrite(colour, LOW);
      WDDelay(6);                              // 1 second gap
      show = true;
    }
    i--;
  } while (i>=0);
}

// Watchdog timer **********************************************

// Use Watchdog for time delay; n=0 is 16ms; n=6 is 1sec ; n=9 is 8secs, 
void WDDelay(int n) {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  WDTCR = 1<<WDIE | (n & 0x8)<<2 | 1<<WDE | (n & 0x7);
  sleep_enable();
  sleep_cpu();
}

ISR(WDT_vect) {
  // Special sequence to disable watchdog timer
  WDTCR = 1<<WDCE | 1<<WDE;
  WDTCR = 0;
}

// Setup and main loop **********************************************

void setup() {
  // Turn off Timer/Counter0, Timer/Counter1, and USI to save power
  PRR = 1<<PRTIM1 | 1<<PRTIM0 | 1<<PRUSI;
  // Define inputs and outputs
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  // Set up ADC for temperature
  ADMUX = 0<<REFS2 | 2<<REFS0 | 15<<MUX0;  // temperature and 1.1V reference
  ADCSRA = 1<<ADEN | 1<<ADIE | 3<<ADPS0;   // enable ADC, interrupt, 125kHz ADC clock
}

void loop() {
  int Temperature;
  // Go to sleep to save power until the watchdog timer wakes us up
  WDDelay(9);                              // 8 second delay
  WDDelay(9);                              // and another 8 second delay
  Temperature = (ReadADC()*31 - 8837)/33;  // In degrees Celsius
  Flash(NegaBinary(Temperature));
}


Download: Flashing Thermmeter
_______________________________________________________________________

4. TWO PWM





Download: Two PWM
_______________________________________________________________________ 

5. WAVEFORM GENERATOR







SQUARE WAVE


SAWTOOTH WAVE


TRIANGLE WAVE

_______________________________________________________________________ 

6. DIGITAL MUSIC





int Scale[] = { 
  680,  721,  764,  809,  857,  908,  962, 1020, 1080, 1144, 1212, 1284, 
 1361, 1442, 1528, 1618, 1715, 1817, 1925, 2039, 2160, 2289, 2425, 2569,
 2722, 2884, 3055, 3237, 3429, 3633, 3849, 4078 };
 
const int Channels = 4;
const int Tempo = 4;      // 4 = 4 beats per second
const int Decay = 9;      // Length of note decay; max 10

volatile unsigned int Acc[Channels];
volatile unsigned int Freq[Channels];
volatile unsigned int Amp[Channels];

// Play Happy Birthday

const uint32_t Tune[] PROGMEM = {
//_*_*__*_*_*__*_*__*_*_*__*_*__*_
//C D EF G A BC D EF G A BC D EF G
0b00000000000000000100000000000000,
0b00000000000000000001000000000000,

0b10000000000000000000010000000000,
0b00000000000100000000000000000000,
0b00001001000000000001000000000000,
0b00000000000000000000000000000000,
0b00000000000000000000000010000000,
0b00000000000000000000000000000000,

0b00100000000000000000000100000000,
0b00000000000000000000000000000000,
0b00000101000000000000000000000000,
0b00000000000010000000000000000000,
0b00000000000000000001000000000000,
0b00000000000000000001000000000000,

0b10000000000000000000010000000000,
0b00000000000000000000000000000000,
0b00000101000000000001000000000000,
0b00000000000001000000000000000000,
0b00000000000000000000000000100000,
0b00000000000000001000000000000000,

0b10000000000000000000000010000000,
0b00000000000000000000000000000000,
0b00001001000000000000000000000000,
0b00000000000000000000000000000000,
0b00000000000000000001000000000000,
0b00000000000000000001000000000000,

0b10000000000000000000000000000001,
0b00000000000000000100000000000000,
0b00100000000000000000000000001000,
0b00000000000000000000000000000000,
0b00001000000000000000000010000000,
0b00000000000000000010000000000000,

0b00000100000000000000000100000000,
0b00000000000000000000000000000000,
0b00000000000000000000010000000000,
0b00000000000000000001000000000000,
0b00000000000000000000000000000100,
0b00000000000000000000000000000100,

0b00000001000000000000000000001000,
0b00000000000000000000000000000000,
0b00000000000000000000000010000000,
0b00000000000010000000000000000000,
0b00000101000000000000000000100000,
0b00000000010000000000000000000000,

0b10001001000000000000000010000000,
0xFF}; // End of tune

//Globals persist throughout tune
int TunePtr = 0, Chan = 0;

// Watchdog interrupt plays notes
ISR(WDT_vect) {
  sei();   // Allow interrupts
  WDTCR |= 1<<WDIE;
  unsigned long Chord = pgm_read_dword(&Tune[TunePtr]);
  if (Chord == 0xFF) return;
  TunePtr++;
  // Read the bits in Chord
  for (int Note = 0; Note < 32; Note++) {
    if ((Chord & 0x80000000) != 0) {
      Freq[Chan] = Scale[Note];
      Amp[Chan] = 1<<(Decay+5);
      Chan = (Chan + 1) % Channels;
    }
    Chord = Chord<<1;
  }
}

// Generate square waves on 4 channels
ISR(TIMER0_COMPA_vect) {
  signed char Temp, Mask, Env, Note, Sum=0;
  for (int c = 0; c < Channels; c++) {
    Acc[c] = Acc[c] + Freq[c];  
    Amp[c] = Amp[c] - (Amp[c] != 0);
    Temp = Acc[c] >> 8;
    Mask = Temp >> 7;
    Env = Amp[c] >> Decay;
    Note = (Env ^ Mask) + (Mask & 1);
    Sum = Sum + Note;
  }
  OCR1B = Sum + 128;
}

void setup() {
  // Enable 64 MHz PLL and use as source for Timer1
  PLLCSR = 1<<PCKE | 1<<PLLE;     

  // Set up Timer/Counter1 for PWM output
  TIMSK = 0;                     // Timer interrupts OFF
  TCCR1 = 1<<CS10;               // 1:1 prescale
  GTCCR = 1<<PWM1B | 2<<COM1B0;  // PWM B, clear on match

  OCR1B = 128;
  DDRB = 1<<DDB4;                // Enable PWM output on pin 4

  // Set up Timer/Counter0 for 20kHz interrupt to output samples.
  TCCR0A = 3<<WGM00;             // Fast PWM
  TCCR0B = 1<<WGM02 | 2<<CS00;   // 1/8 prescale
  OCR0A = 99;                    // Divide by 100
  TIMSK = 1<<OCIE0A;             // Enable compare match, disable overflow
 
  // Set up Watchdog timer for 4 Hz interrupt for note output.
  WDTCR = 1<<WDIE | Tempo<<WDP0; // 4 Hz interrupt
}

void loop() {
}

Download: Digital Music
 
https://www.amazon.com/Guillermo%20Perez/e/B01EZOTUVQ/

guillengap@gmail.com


No hay comentarios:

Publicar un comentario