butterfly board help

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Book: C Programming for Microcontrollers
Program: Play it again sam

I need help with a modification to the Play it again sam program. I just simply want to the sirene1 program to start without utilizing PC_Comm. I would just like to download this program and the piezo speaker to start buzzing. Could someone please help me.

OR

A simple program that enables a piezo speaker.

I just prefer the sound sirene1 makes...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well you posted this as hijacks on two other threads so I assume you don't know how to use a forum and that is the first thing you should learn.

Please read:
How To Ask Questions The Smart Way
http://www.catb.org/~esr/faqs/sm...

Newbie? Start Here!
https://www.avrfreaks.net/index.p...

As far as that program is concerned it follows on other programs from the book and is pretty simple as is. Have you done the precursor programs? Are you asking for someone to write a program for you that outputs a sirene sound on a Butterfly - but doesn't use the code already provided? I'm really not following what you are trying to do here.

Smiley

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I will delete the two hijacks and let it evolve here.

A GIF is worth a thousend words   They are called Rosa, Sylvia and Tessa, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In the original program (play it again sam) the PC selects a tune. I would like to use the code already provided but modify it so that there is no selection via pc_com. I would simply like the butterfly to play Sirene1. I have tired to delete all the other song choices and just use the sirene1. I have deleted the following pc_com.c, pc_com.h (I moved the functions to demonstrator.h) and messages.h. I have tried to modifiey the code myself but I keep struggling with it. This is the modified Demonstrator.c:

// Demonstrator.c Play it again Sam version


#include "sound.h"
#include "Demonstrator.h"
void initializer()
{

	
	// Initialize timer0 to play a tune
	Timer0_Init();
	
	// initialize piezo-element
    sbi(DDRB, 5);               // set OC1A as output
    sbi(PORTB, 5);              // set OC1A high	
	


}



void stopTune()
{
    cbi(TCCR1B, 0);    // stop Playing
    TCCR1A = 0;
    TCCR1B = 0;
    sbi(PORTB, 5);     // set OC1A high
}


void startTune(char tune)
{
	const int Sirene1[] = 
        {
            0,
            32,400, 32,397, 32,394, 32,391, 32,388, 32,385, 32,382, 32,379,
            32,376, 32,373, 32,370, 32,367, 32,364, 32,361, 32,358, 32,355,
            32,352, 32,349, 32,346, 32,343, 32,340, 32,337, 32,334, 32,331, 
            32,328, 32,325, 32,322, 32,319, 32,316, 32,313, 32,310, 32,307, 
            32,304, 32,301, 32,298, 32,298, 32,301, 32,304, 32,307, 32,310, 
            32,313, 32,316, 32,319, 32,322, 32,325, 32,328, 32,331, 32,334, 
            32,337, 32,340, 32,343, 32,346, 32,349, 32,352, 32,355, 32,358, 
            32,361, 32,364, 32,367, 32,370, 32,373, 32,376, 32,379, 32,382, 
            32,385, 32,388, 32,391, 32,394, 32,397, 32,400,
            0, 1
        };


 
	stopTune();        

	Sound_Init();
}


void Sound_Init(void)
{
    TCCR1A = (1<<COM1A1); 		// Set OC1A when upcounting, clear when downcounting
    TCCR1B = (1<<WGM13);       // Phase/Freq-correct PWM, top value = ICR1
    
    sbi(TCCR1B, CS10);         // start Timer1, prescaler(1)    
    
   
}

void Play_Tune(void)
{
    int temp_hi;
    
    char loop;
    
    if(!Tone)
    {
        Duration = 0;   
		
		Tempo = ( + 0); 
		Tempo <<= 1; // compensate for using 2 MHz clock 
        Tone = 1;   //Start the song from the beginning
    }
    
    if(!Tempo)
    {
        if(Duration)        // Check if the length of the tone has "expired"
        {   
            Duration--;
        }
        else if(Sirene1 + Tone)  // If not the end of the song
        {
            Duration = ( DURATION_SEED / (Sirene1 + Tone) );  // store the duration
			Duration <<= 1;// compensate for using 2 MHz clock
        
			Tone++;                     // point to the next tone in the Song-table        
                        
            if((Sirene1 + Tone) == p) | ((Sirene1 + Tone) == P) ) // if pause
                cbi(TCCR1B, CS10);             // stop Timer1, prescaler(1)    
            else 
                sbi(TCCR1B, CS10);             // start Timer1, prescaler(1)  
                
            cli(); 
            
			temp_hi = (Sirene1 + Tone);      // read out the PWM-value

            temp_hi >>= 7;// compensate for 2 MHz clock 8; // move integer 8 bits to the right
                
            TCNT1H = 0;                     // reset TCNT1H/L
            TCNT1L = 0;
            
            ICR1H = temp_hi;                // load ICR1H/L       
			ICR1L = (Sirene1 + Tone);
			ICR1L <<= 1; // compensate for using 2 MHz clock
            
            sei(); 
            
            Tone++;                     // point to the next tone in the Song-table
        }
        else    // the end of song
        {
            Tone++;         // point to the next tone in the Song-table        
            
			loop = (Sirene1 + Tone); // get the byte that tells if the song should loop or not
            
            if( loop )  
            {
                Tone = 1;
            }
            else        // if not looping the song
            {
                Tone = 0;
                //gPlaying = FALSE;
                cbi(TCCR1B, 0);                     // stop Playing
                TCCR1A = 0;
                TCCR1B = 0;
                sbi(PORTB, 5);              // set OC1A high
            }
        }
        
		Tempo = (pSong + 0);
    }
    else
        Tempo--;
 
}  


void Timer0_Init(void)
{
 	// Initialize Timer0.
	// Used to give the correct time-delays in the song

    // Enable timer0 compare interrupt
	TIMSK0 = (1<<OCIE0A);

    // Sets the compare value
	OCR0A = 38;

	// Set Clear on Timer Compare (CTC) mode, CLK/256 prescaler
	TCCR0A = (1<<WGM01)|(0<<WGM00)|(4<<CS00);
}

this is my demonstartor.h:

// Demonstrator.h Play it again Sam version


#include 
#include 
#include 
#include 
#include 
#include 
void initializer(void);

void startTune(char);

void stopTune(void);

void Sound_Init(void);
void Timer0_Init(void);

The tunes.h is the same. I greatly appreciate your time.

[moderator: [code] tags added - please get into the habit of doing it yourself in future]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Given that:

 const int Sirene1[]

is defined inside

void startTune(char tune)

where the only two statements that can "see" it are:

   stopTune();        

   Sound_Init(); 

yet neither of those make reference to this limited name scope local variable exactly how are you expecting anything else within this program to be able to "see" and play the tune?!?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I'm mainly concerned why you want to be so far ahead of where you are in your learning? Why do you want to play the sirene on a Butterfly without reading the lessons that lead up to that example?

If you want a sirene without learning, just buy one.

If you start with the first lesson in the book and do each exercise, you'll learn how to do this.

Why do you want to skip ahead?

Smiley

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks clawson.. I understand now :)....