Tiva C launchpad Playing Music

Hola!


En este tutorial conectaremos a la Tiva C series TM4C123G  un piezo eléctrico (Buzzer) para hacer sonar divertidas melodías.

Materiales:

-Buzzer
-Tiva C launchpad
-Resistencia de  220 ohm o aproximado

Se interconectara el piezo de la siguiente manera a la tiva:

La positiva a la resistencia y esta al pin PF2 que viene siendo el pin digital 40 y el otro lado se conecta a tierra, si al conectar no sabes cual es la tierra simple no sonara al cambiar la posición sonara.



Figura 1. Esquema de conexión

En este caso escucharemos las melodías:

HBD- Cumpleaños feliz
Konami contra- soundtrack videogame
Imperial March- Star Wars
Let It Be- The Beatles

De los códigos 3 puedes encontrarlos en los ejemplos que trae el software Energía los cuales podrán modificar a su gusto

Figura 2. Energía Examples


Y Let it be puedes hacer una adaptacion del tradicional hecho para Arduino http://forum.arduino.cc/index.php?topic=3309.0

A continuación les dejo los que use y al final el vídeo de como funciona!

HBD

#include "pitches.h"
#define NOTE_C4_1 260

int buzzerPin = 40;

// notes in the melody:
int melody[] = {
   NOTE_C4_1,NOTE_C4, NOTE_D4, NOTE_C4,NOTE_F4,NOTE_E4,
   NOTE_C4_1,NOTE_C4,NOTE_D4,NOTE_C4,NOTE_G4,NOTE_F4,
   NOTE_C4_1,NOTE_C4,NOTE_C5,NOTE_A4,NOTE_F4,NOTE_F4, NOTE_E4,NOTE_D4,
   NOTE_AS4,NOTE_AS4,NOTE_A4,NOTE_F4,NOTE_G4,NOTE_F4};
   
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 4, 2, 2,2,1,
  4, 4, 2, 2,2,1,
  4, 4, 2, 2,4,4,2,1, 
  4, 4, 2, 2,2,1};

void setup() 
{
pinMode(buzzerPin,OUTPUT);
}
void loop() 
{
  for (int thisNote = 0; thisNote < 26; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(buzzerPin, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration + 50;      //delay between pulse
    delay(pauseBetweenNotes);
    
    noTone(buzzerPin);                // stop the tone playing
  }
}



Konami contra:

#include "pitches.h"


int buzzerPin = 40;


// notes in the melody:
int melody[] = {
  NOTE_F2, NOTE_C6, NOTE_B5, NOTE_G5, NOTE_A5, NOTE_E1, NOTE_B1, NOTE_E1, NOTE_B1, NOTE_E1, NOTE_B1, 
   
   
  NOTE_G6, NOTE_F6, NOTE_DS6, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_G5, NOTE_GS5, 
  NOTE_G5, NOTE_F5, NOTE_DS5, NOTE_F5, NOTE_AS4, NOTE_C5, NOTE_DS5, NOTE_F5,
  
  NOTE_C6, NOTE_NOTONE, NOTE_AS5, NOTE_C6, NOTE_D6, NOTE_DS6, NOTE_F5,
  NOTE_C6, NOTE_NOTONE, NOTE_AS5, NOTE_C6, NOTE_D6, NOTE_GS5, NOTE_F5,

// Repeat once   
  NOTE_C6, NOTE_NOTONE, NOTE_AS5, NOTE_C6, NOTE_D6, NOTE_DS6, NOTE_F5,
  NOTE_C6, NOTE_NOTONE, NOTE_AS5, NOTE_C6, NOTE_D6, NOTE_GS5, NOTE_F5,

  NOTE_C6, NOTE_NOTONE, NOTE_C6, NOTE_D6, NOTE_DS6, NOTE_NOTONE, NOTE_DS6, NOTE_NOTONE,
  NOTE_G5, NOTE_AS5, NOTE_C6, NOTE_D6, NOTE_NOTONE, NOTE_D6, NOTE_DS6,
  
  NOTE_C6, NOTE_NOTONE, NOTE_C6, NOTE_NOTONE, NOTE_DS6, NOTE_F6
  
  
};
   
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
2, 2, 2, 2,1, 2, 2, 2, 2, 2, 1,  // 11 start screen  notes



16, 16, 16, 16, 16, 16, 16, 16, 16, 16,  // 20  notes
16, 16, 16, 16, 16, 16, 16, 2,           // for both lines   

16, 16, 16, 8, 16, 2, 2,                  // 7
16, 16, 16, 8, 16, 2, 2,                  // 7

// Repeat once
16, 16, 16, 8, 16, 2, 2,                  // 7
16, 16, 16, 8, 16, 2, 2,                  // 7

16, 16, 4, 4, 16, 16, 16, 8,             // 8
16, 16, 16, 16, 16, 4, 4,                  // 7

16, 16, 16, 16, 16, 4                     // 6 

};

void setup() 
{
pinMode(buzzerPin,OUTPUT);
}
void loop() 
{
  for (int thisNote = 0; thisNote < 80; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    // Had to down tempo to 100/150 
    int noteDuration = 1500/noteDurations[thisNote];
    tone(buzzerPin, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration + 50;      //delay between pulse
    delay(pauseBetweenNotes);
    
    noTone(buzzerPin);                // stop the tone playing
  }
}

Star Wars


#include "pitches.h"
#define cc 261
#define dd 294
#define ee 329
#define ff 349
#define g 391
#define gS 415
#define a 440
#define aS 455
#define b 466
#define cH 523
#define cSH 554
#define dH 587
#define dSH 622
#define eH 659
#define fH 698
#define fSH 740
#define gH 784
#define gSH 830
#define aH 880

int buzzerPin = 40;

void beep(int note, int duration)
{
  tone(buzzerPin, note, duration/2);
  delay(duration/2);
  noTone(buzzerPin);
  delay(duration/2 + 20);  
}
void setup() 
{
pinMode(buzzerPin,OUTPUT);
}
void loop() 
{
   beep(a, 500);
beep(a, 500);
beep(a, 500);
beep(ff, 350);
beep(cH, 150);  
beep(a, 500);
beep(ff, 350);
beep(cH, 150);
beep(a, 650);

    delay(150);
    //end of first bit   

beep(eH, 500);
beep(eH, 500);
beep(eH, 500);   
beep(fH, 350);
beep(cH, 150);
beep(gS, 500);
beep(ff, 350);
beep(cH, 150);
beep(a, 650);

    delay(150);
    //end of second bit...  

beep(aH, 500);
beep(a, 300);
beep(a, 150);
beep(aH, 400);
beep(gSH, 200);
beep(gH, 200); 
beep(fSH, 125);
beep(fH, 125);    
beep(fSH, 250);

    delay(250);

beep(aS, 250); 
beep(dSH, 400); 
beep(dH, 200);  
beep(cSH, 200);  
beep(cH, 125);  
beep(b, 125);  
beep(cH, 250);  

    delay(250);

beep(ff, 125);  
beep(gS, 500);  
beep(ff, 375);  
beep(a, 125);
beep(cH, 500);
beep(a, 375);  
beep(cH, 125);
beep(eH, 650);


beep(aH, 500);
beep(a, 300);
beep(a, 150);
beep(aH, 400);
beep(gSH, 200);
beep(gH, 200);
beep(fSH, 125);
beep(fH, 125);    
beep(fSH, 250);

    delay(250);

beep(aS, 250);  
beep(dSH, 400);  
beep(dH, 200);  
beep(cSH, 200);  
beep(cH, 125);  
beep(b, 125);  
beep(cH, 250);      

    delay(250);

beep(ff, 250);  
beep(gS, 500);  
beep(ff, 375);  
beep(cH, 125);
beep(a, 500);   
beep(ff, 375);   
beep(cH, 125); 
beep(a, 650);   
    //end of the song
    //end of the song
}

Let it Be


int speakerPin = 40;

int length = 56; // the number of notes
char notes[] = "ggaeggCDEEEDDCC gEEFEED EDDC EDCEGAGGEDCagE EEEFEED EDDC"; // a space represents a rest
int beats[] = { 2, 1, 2, 2, 3, 2, 1, 2, 1, 1, 4, 1, 2, 1, 2, 2, 1, 1, 2, 2, 1, 2, 1, 1, 1, 1, 2, 2, 8, 1, 2, 5, 1, 2, 5, 1, 1, 1, 1, 4, 1, 2, 5, 5, 1, 1, 1, 2, 1, 1, 4, 1, 1, 1, 2, 2};
int tempo = 200;

void playTone(int tone, int duration) {
 for (long i = 0; i < duration * 1000L; i += tone * 2) {
   digitalWrite(speakerPin, HIGH);
   delayMicroseconds(tone);
   digitalWrite(speakerPin, LOW);
   delayMicroseconds(tone);
 }
}

void playNote(char note, int duration) {
 char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B' };
 int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 851, 760, 716, 628, 568, 507 };
 
 // play the tone corresponding to the note name
 for (int i = 0; i < 14; i++) {
   if (names[i] == note) {
     playTone(tones[i], duration);
   }
 }
}

void setup() {
 pinMode(speakerPin, OUTPUT);
}

void loop() {
 for (int i = 0; i < length; i++) {
   if (notes[i] == ' ') {
     delay(beats[i] * tempo); // rest
   } else {
     playNote(notes[i], beats[i] * tempo);
   }
   
   // pause between notes
   delay(tempo / 2); 
 }
}




ÉXITOS!


Maryori Sabalza Mejía 
Cartagena-Colombia






Comentarios

  1. Hola! Una pregunta podrás compartir el código entero? No he podido replicarlo en mi TIVA.....

    ResponderBorrar

Publicar un comentario

Entradas más populares de este blog

CPLD Max II Blink Led

Max II EPM240T100C5 CPLD Altera- Music Buzzer