#include <avr/pgmspace.h> // by @tartakynov // programmed for ATtiny13 in Arduino IDE using core13 http://sourceforge.net/projects/ard-core13/ #define PIN_LED 1 #define PIN_BUZZER 0 #define COUNT_NOTES 39 word frequences[COUNT_NOTES] PROGMEM = { 392, 392, 392, 311, 466, 392, 311, 466, 392, 587, 587, 587, 622, 466, 369, 311, 466, 392, 784, 392, 392, 784, 739, 698, 659, 622, 659, 415, 554, 523, 493, 466, 440, 466, 311, 369, 311, 466, 392 }; word durations[COUNT_NOTES] PROGMEM = { 350, 350, 350, 250, 100, 350, 250, 100, 700, 350, 350, 350, 250, 100, 350, 250, 100, 700, 350, 250, 100, 350, 250, 100, 100, 100, 200, 100, 350, 250, 100, 100, 100, 200, 100, 350, 250, 100, 750 }; void setup() { pinMode(PIN_LED, OUTPUT); pinMode(PIN_BUZZER, OUTPUT); } void loop() { for (byte i = 0; i < COUNT_NOTES; i++) { buzz(PIN_BUZZER, pgm_read_word(&(frequences[i])), 2 * pgm_read_word(&(durations[i]))); delay(100); } delay(3000); } void buzz(unsigned char pin, word frequencyInHertz, word timeInMilliseconds) { long delayAmount = (long)(long(1000000) / (long)frequencyInHertz); long loopTime = (long)(((long)timeInMilliseconds * 500) / delayAmount); for (long i = 0; i < loopTime; i++) { digitalWrite(pin, HIGH); delayMicroseconds(delayAmount); digitalWrite(pin, LOW); delayMicroseconds(delayAmount); } }  // by @tartakynov: // http://youtu.be/5R7NeQkVS_8 // and me - vk.com/razniepodelki #define F_CPU 1200000L //     #include <avr/pgmspace.h> //   PROGMEM #define PIN_BUZZER 2 // PB2  #define BUTTON 4 // PB4  #define COUNT_NOTES 39 //   word frequences[COUNT_NOTES] PROGMEM = { //    392, 392, 392, 311, 466, 392, 311, 466, 392, 587, 587, 587, 622, 466, 369, 311, 466, 392, 784, 392, 392, 784, 739, 698, 659, 622, 659, 415, 554, 523, 493, 466, 440, 466, 311, 369, 311, 466, 392 }; word durations[COUNT_NOTES] PROGMEM = { //    350, 350, 350, 250, 100, 350, 250, 100, 700, 350, 350, 350, 250, 100, 350, 250, 100, 700, 350, 250, 100, 350, 250, 100, 100, 100, 450, 150, 350, 250, 100, 100, 100, 450, 150, 350, 250, 100, 750 }; //void setup() //{ int main( void ) //   void setup(),    { pinMode(PIN_BUZZER, OUTPUT); //   pinMode(BUTTON, INPUT); //    digitalWrite(BUTTON, HIGH); //    //LOW   //} //void loop() //{ while(1){ //  void loop()( ) if (digitalRead(BUTTON) == LOW) { //    for (byte i = 0; i < COUNT_NOTES; i++) //    { buzz(PIN_BUZZER, pgm_read_word(&(frequences[i])) * 3, 2 * pgm_read_word(&(durations[i]))); //  : // buzz(PIN_BUZZER, pgm_read_word(&(frequences[i])), 2 * pgm_read_word(&(durations[i]))); //      3 //(pgm_read_word(&(frequences[i])) * 3) //       //delay(100); //    } } //delay(3000); //   } //    return 0; // int main( void ) } //  while(1) void buzz(unsigned char pin, word frequencyInHertz, word timeInMilliseconds) //    { //   long delayAmount = (long)(long(1000000) / (long)frequencyInHertz); //  3  long loopTime = (long)(((long)timeInMilliseconds * 500) / delayAmount); // 1 -  for (long i = 0; i < loopTime; i++) // 2 -  { // 3 -  digitalWrite(pin, HIGH); //     delayMicroseconds(delayAmount); digitalWrite(pin, LOW); delayMicroseconds(delayAmount); } }  // by @tartakynov: // http://youtu.be/5R7NeQkVS_8 // and me - vk.com/razniepodelki #define F_CPU 1200000UL //     #include <avr/io.h> //    #include <util/delay.h> //      #include <avr/pgmspace.h> //   PROGMEM #define PIN_BUZZER 2 // PB2  #define BUTTON 4 // PB4  #define COUNT_NOTES 39 //   word frequences[COUNT_NOTES] PROGMEM = { //    392, 392, 392, 311, 466, 392, 311, 466, 392, 587, 587, 587, 622, 466, 369, 311, 466, 392, 784, 392, 392, 784, 739, 698, 659, 622, 659, 415, 554, 523, 493, 466, 440, 466, 311, 369, 311, 466, 392 }; word durations[COUNT_NOTES] PROGMEM = { //    350, 350, 350, 250, 100, 350, 250, 100, 700, 350, 350, 350, 250, 100, 350, 250, 100, 700, 350, 250, 100, 350, 250, 100, 100, 100, 450, 150, 350, 250, 100, 100, 100, 450, 150, 350, 250, 100, 750 }; //void setup() //{ int main( void ) //   void setup(),    { DDRB |= (1<<PIN_BUZZER); //   DDRB &= ~(1<<BUTTON); //    PORTB |= (1<<BUTTON); //    //0  () //} //void loop() //{ while(1){ //  void loop()( ) if (!(PINB & (1<<BUTTON))) { //    for (byte i = 0; i < COUNT_NOTES; i++) //    { buzz(pgm_read_word(&(frequences[i])) * 3, 2 * pgm_read_word(&(durations[i]))); //  : // buzz(PIN_BUZZER, pgm_read_word(&(frequences[i])), 2 * pgm_read_word(&(durations[i]))); //      3 //(pgm_read_word(&(frequences[i])) * 3) //       } //_delay_ms(100); //   } //_delay_ms(3000); //   } //    return 0; // int main( void ) } //  while(1) //void buzz(unsigned char pin, word frequencyInHertz, word timeInMilliseconds) void buzz(word frequencyInHertz, word timeInMilliseconds) //    { //   long delayAmount = (long)(long(1000000) / (long)frequencyInHertz); //  3  long loopTime = (long)(((long)timeInMilliseconds * 500) / delayAmount); // 1 -  for (long i = 0; i < loopTime; i++) // 2 -  { // 3 -  //digitalWrite(pin, HIGH); //     PORTB |= (1<<PIN_BUZZER); //       //delayMicroseconds(delayAmount); //     _delay_us(delayAmount); //   //digitalWrite(pin, LOW); PORTB &= ~(1<<PIN_BUZZER); //delayMicroseconds(delayAmount); //     _delay_us(delayAmount); //   } }  /* Attiny13 Star Wars melody */ #define F_CPU 1200000UL #include <avr/io.h> #include <avr/pgmspace.h> #include <util/delay.h> #include <avr/interrupt.h> #include <avr/sleep.h> #define COUNT_NOTES 39 //  const uint16_t frequences[COUNT_NOTES] PROGMEM = { 392, 392, 392, 311, 466, 392, 311, 466, 392, 587, 587, 587, 622, 466, 369, 311, 466, 392, 784, 392, 392, 784, 739, 698, 659, 622, 659, 415, 554, 523, 493, 466, 440, 466, 311, 369, 311, 466, 392 }; //  const uint16_t durations[COUNT_NOTES] PROGMEM = { 350, 350, 350, 250, 100, 350, 250, 100, 700, 350, 350, 350, 250, 100, 350, 250, 100, 700, 350, 250, 100, 350, 250, 100, 100, 100, 450, 150, 350, 250, 100, 100, 100, 450, 150, 350, 250, 100, 750 }; int main( void ) { /*    () = PB1 , ..      INT0 -   ,         Power-down    () = PB0, ..      PB0  PB1,       */ DDRB = (1 << DDB0); // PB0   /* -,       INT0  ,     RESET:      ,       ...        */ PORTB = (1 << PORTB1); //   PB1 GIMSK = (1 << INT0); //  INT0 (PB1) set_sleep_mode(SLEEP_MODE_PWR_DOWN); //   Power-down sei(); //  while(1) sleep_mode(); //    } //   static inline void doSound(void) { //    TCCR0A = (1 << COM0A0) | (1 << WGM01); //Toggle OC0A (PB0) on Compare Match ; mode = CTC TCCR0B = (1 << CS01) | (1 << CS00); //Clock Select = F_CPU/64 //   for (uint8_t i = 0; i < COUNT_NOTES; i++) { //    int16_t freq = pgm_read_word(&(frequences[i])); int16_t dur = pgm_read_word(&(durations[i])); //   1.5  dur *= 3; dur /= 2; //    PB0 OCR0A = (F_CPU / (2UL * 64UL * freq)) - 1; //,    while (dur > 0) { _delay_ms(10); dur -= 10; } } //  TCCR0A = 0; TCCR0B = 0; } //   INT0 ISR(INT0_vect) { doSound(); } Source: https://habr.com/ru/post/234763/
All Articles