Got my Chip, Programmer & Software

Thread Starter

Chris15

Joined Apr 15, 2009
252
Yes there is

- C file
- Project File
- CProject File
- PREFS File
- D File
- ELF File
- LSS File
- MAP File
- O File
- (2) MK Files
- HEX File
 

hgmjr

Joined Jan 28, 2005
9,027
It just so happens that one of the files in the zip-file is the c source-code. That means that there is the possibility that you can go into the file and change the parameters to cause it to output a different tune.

hgmjr
 

Thread Starter

Chris15

Joined Apr 15, 2009
252
yes, but i have no idea at how to do that :(, i opened it in AVR Studio, i looked at it, and compiled it into a hex but to change the tune? i can mess with the numbers?
 

hgmjr

Joined Jan 28, 2005
9,027
Changing the code is feasible but reverse engineering a program, specially when there are a dirth of comments, is a very challenging undertaking.

hgmjr
 

thatoneguy

Joined Feb 19, 2009
6,359
You will need to hook up an LCD Display.

Here is the source code from your archive, the .c file, that's the heart of it, the rest are the libraries your compiler/(or OS in other systems) provide, they provide the "standard functions" of C.

--ETA: I do NOT see any routine that would produce sounds, only LCD Display output. It is well Commented Code, though.

Rich (BB code):
// initialize function prototypes
void initializePorts();
void initializeLCD();
void startGame();
int checkForHit();
void updateScore(int playerFlag, int score);
void showWinner();
int showMenu();

// function main
void main() {

  // initialize components
    initializePorts();
    initializeLCD();

  // start program proper
  do {
      startGame();
   } while( showMenu() );
} // end main

// start game proper
void startGame() {
  int player1Score = 0;
    int player2Score = 0;
    int playerFlag = 1;        // kun hino an natira
    /*
        1 -> player 1
        2 -> player 2
    */

    int yellowCtr = 0, greenCtr = 0, blueCtr = 0, redCtr = 0;
    int hitFlag;
    /*
        1 -> yellow
        2 -> green
        3 -> blue
        4 -> red
        5 -> switch player
    */

  LCD_Cmd(LCD_CLEAR);

    do {
        yellowCtr = 0;
        greenCtr = 0;
        blueCtr = 0;
        redCtr = 0;

        hitFlag = checkForHit(); // kitaon kun ano nga color an naigo

        switch( hitFlag ) {
            case 1:                    // yellow
                yellowCtr++;
                break;
            case 2:                    // green
                greenCtr++;
                break;
            case 3:                    // blue
                blueCtr++;
                break;
            case 4:                    // red
                redCtr++;
                break;
            case 5:
        playerFlag == 1? 2: 1;  // switch player
                continue;                      // balik ha start han do while loop
        }

        if( yellowCtr >= 2 || greenCtr >= 2 || blueCtr >= 2 || redCtr >= 2 ) {
            switch( playerFlag ) {
                case 1:
                    player1Score += 10;
                    break;
                case 2:
                    player2Score += 10;
                    break;
            }

            updateScore( playerFlag, playerFlag == 1? player1Score: player2Score ); // update display ha LCD

        }

    } while( player1Score == 50 || player2Score == 50 ); // check for winner

    showWinner();
}

void showWinner() {

}

int showMenu() {
     int hit = 0;

     LCD_Cmd(LCD_CLEAR);
     LCD_Out(1,1, " Press Any Key");
     LCD_Out(2,1, " To Start Game");
     
     hit = checkForHit();
     
     if( hit ) {
         LCD_Cmd(LCD_CLEAR);
         return 1;
     }
     
     return 0;
}

void updateScore( int playerFlag, int score ) {
    char *line1Text = "";
    char *line2Text = "";
    char *points = "";            // string value han score
    char *append = " points";    // igdudugtong ha line2Text

    IntToStr( score, points );
    strcpy( line2Text, strcat( points, append ) );
    
    LCD_Cmd(LCD_CLEAR);

    switch( playerFlag ) {
        case 1:
            strcpy(line1Text, "Player 1 Score");
            break;
        case 2:
            strcpy(line1Text, "Player 2 Score");
            break;
    }

    LCD_Out(1,1, line1Text);
    LCD_Out(2,4, line2Text);
}

int checkForHit() {
    int hit = 0;
    int oldState = 1;

    do {
        // for red
        if ( Button(&PORTD, 0, 1, 0) )                // detect logical one on RD0 pin
            oldState = 0;
        if ( oldState && Button(&PORTD, 0, 1, 1) ) {  // detect one-to-zero transition on RD0 pin
            hit = 4;
            oldState = 1;
        }

        //for green
        if ( Button(&PORTD, 1, 1, 0) )                // detect logical one on RD1 pin
            oldState = 0;
        if ( oldState && Button(&PORTD, 1, 1, 1) ) {  // detect one-to-zero transition on RD1 pin
            hit = 2;
            oldState = 1;
        }

        //for blue
        if ( Button(&PORTD, 2, 1, 0) )                // detect logical one on RD2 pin
            oldState = 0;
        if ( oldState && Button(&PORTD, 2, 1, 1) ) {  // detect one-to-zero transition on RD2 pin
            hit = 3;
            oldState = 1;
        }

        //for yellow
        if ( Button(&PORTD, 3, 1, 0) )                // detect logical one on RD3 pin
            oldState = 0;
        if ( oldState && Button(&PORTD, 3, 1, 1) ) {  // detect one-to-zero transition on RD3 pin
            hit = 1;
            oldState = 1;
        }

        //for Switch player button
        if ( Button(&PORTC, 1, 1, 0) )                // detect logical one on RC1 pin
            oldState = 0;
        if ( oldState && Button(&PORTC, 1, 1, 1) ) {  // detect one-to-zero transition on RC1 pin
            hit = 5;
            oldState = 1;
        }

    } while( !hit ); // ma-exit la kun non-zero it hit

    return hit; // return kun ano an napidlit
}

void initializePorts() {
    TRISB = 0;            // output
                   // initialize PORTB

    TRISD = 1;            // input
    PORTD = 0xFF;        // initialize PORTD

    TRISC = 1;            // input
    PORTC = 0xFF;        // initialize PORTC
}

void initializeLCD() {
    Lcd_Init(&PORTB);             // Lcd_Init_EP4, see Autocomplete

    LCD_Cmd(LCD_CLEAR);           // Clear display
    LCD_Cmd(LCD_CURSOR_OFF);      // Turn cursor off
  Lcd_Out(1, 4, "Let's Play");
  Delay_ms(1000);
  LCD_Cmd(LCD_CLEAR);
}
 

hgmjr

Joined Jan 28, 2005
9,027
Here is the C source code that I found in the zip file.

I believe this is the source code that implements the sound generation..

Rich (BB code):
 
#include <avr/io.h>
#include <avr/pgmspace.h>
// pin definitions
#define SPEAKER PB0
#define LED1 PB4
#define LED2 PB2
#define SENSOR PB3
#define GNDPIN PB1
#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define output_toggle(port,pin) port ^= (1<<pin)
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)
#define read_value(portdir,pin) ( portdir & (1<<pin) )
const uint8_t freq[] PROGMEM = {
113, 113, 113, 147, 113, 97, 197, 147, 197, 234, 170, 156,
166, 174, 197, 113, 98, 87, 107, 98, 113, 144, 129, 156,
147, 197, 234, 170, 156, 166, 174, 197, 113, 98, 87, 107,
98, 113, 144, 129, 156, 150, 98, 104, 110, 120, 115, 197,
174, 150, 174, 150, 131, 150, 98, 104, 110, 120, 115, 73,
73, 73, 197, 150, 98, 104, 110, 120, 115, 197, 174, 150,
174, 150, 131, 128, 136, 150, 197, 150, 150, 150, 150,
98, 104, 110, 120, 115, 197, 174, 150, 174, 150, 131,
150, 98, 104, 110, 120, 115, 73, 73, 73, 197, 150, 98,
104, 110, 120, 115, 197, 174, 150, 174, 150, 131, 128, 136,
150, 197, 150, 150, 150, 150, 150, 150, 150, 129, 113, 150,
174, 197, 150, 150, 150, 150, 129, 113, 86, 98, 150, 150,
150, 150, 129, 113, 150, 174, 197, 113, 113, 113, 147,
113, 97, 197
 
};
const uint8_t length[] PROGMEM = {
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 80, 100,
100, 100, 80, 50, 100, 80, 50, 80, 80, 80, 80, 100, 100, 100,
100, 80, 100, 100, 100, 80, 50, 100, 80, 50, 80, 80, 80, 80,
100, 100, 100, 100, 150, 150, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 150, 200, 80, 80, 80, 100, 100, 100, 100,
100, 150, 150, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 150, 150, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 150, 200, 80, 80, 80,
100, 100, 100, 100, 100, 150, 150, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 60, 80, 60, 80, 80, 80, 80,
80, 80, 60, 80, 60, 80, 80, 80, 80, 80, 60, 80, 60, 80, 80, 80,
80, 80, 80, 100, 100, 100, 100, 100, 100, 100
};
const int delay[] PROGMEM = {
150, 300, 300, 100, 300, 550, 575, 450, 400, 500, 300, 330,
150, 300, 200, 200, 150, 300, 150, 350, 300, 150, 150, 500,
450, 400, 500, 300, 330, 150, 300, 200, 200, 150, 300, 150,
350, 300, 150, 150, 500, 300, 100, 150, 150, 300, 300, 150,
150, 300, 150, 100, 220, 300, 100, 150, 150, 300, 300, 300,
150, 300, 300, 300, 100, 150, 150, 300, 300, 150, 150, 300,
150, 100, 420, 450, 420, 360, 300, 300, 150, 300, 300, 100,
150, 150, 300, 300, 150, 150, 300, 150, 100, 220, 300, 100,
150, 150, 300, 300, 300, 150, 300, 300, 300, 100, 150, 150,
300, 300, 150, 150, 300, 150, 100, 420, 450, 420, 360, 300,
300, 150, 300, 150, 300, 350, 150, 350, 150, 300, 150, 600,
150, 300, 350, 150, 150, 550, 325, 600, 150, 300, 350, 150,
350, 150, 300, 150, 600, 150, 300, 300, 100, 300, 550, 575
};
void sleep(int ms) {
int cnt;
for (cnt=0; cnt<(ms); cnt++) {
int i = 100;
while(i--) {
__asm("NOP");
}
}
}
 
 
int main(void) {
 
 
set_output(DDRB,SPEAKER);
set_output(DDRB,LED1);
set_output(DDRB,LED2);
// set the pin reading ir sensor for input w/o pull-up
set_input(DDRB,SENSOR);
output_high(PORTB,SENSOR);
// extra GND pin for LED
set_output(DDRB,GNDPIN);
output_low(PORTB,GNDPIN);
 
TCCR0A |= (1<<WGM01); // configure timer 1 for CTC mode
TCCR0A |= (1<<COM0A0); // toggle OC0A on compare match
TCCR0B |= (1<<CS01); // clk/8 prescale
 
 
for (;;) {
static uint8_t cnt=0;
static int time_delay=0;
static uint8_t led1 = 1; //initial value is on
static uint8_t led2 = 0; //initial value is off
if ( (PINB & 1<<SENSOR) && time_delay > 20) {
//start playing the tune
//restore LED values
(led1) ? (output_high(PORTB, LED1)) : (output_low(PORTB, LED1));
(led2) ? (output_high(PORTB, LED2)) : (output_low(PORTB, LED2));
//start timer
TCCR0B |= (1<<CS01); // clk/8 prescale
OCR0A=pgm_read_byte(&freq[cnt]);
output_toggle(PORTB,LED1);
output_toggle(PORTB,LED2);
sleep( pgm_read_byte(&length[cnt]) );
output_toggle(PORTB,LED1);
output_toggle(PORTB,LED2);
//save values
led1 = read_value(PORTB, LED1);
led2 = read_value(PORTB, LED2);
// stop timer
TCCR0B = 0;
sleep ( pgm_read_word(&delay[cnt]) );
// start timer
TCCR0B |= (1<<CS01); // clk/8 prescale
cnt++;
if (cnt > 155) cnt=0;
} else if (PINB & 1<<SENSOR) {
output_low(PORTB,LED1);
output_low(PORTB,LED2);
sleep(300);
output_high(PORTB,LED1);
output_high(PORTB,LED2);
sleep(300);
time_delay++;
} else {
time_delay = 0; // reset the time delay
//turn of leds
output_low(PORTB,LED1);
output_low(PORTB,LED2);
//stop timer
TCCR0B = 0;
}
 
 
}
 
return 0;
}
hgmjr
 

thatoneguy

Joined Feb 19, 2009
6,359
Here is the C source code that I found in the zip file.

I believe this is the source code that implements the sound generation..
Yeah, that's it.

I must have grabbed a different archive above. :confused:

I took out the LED stuff.

The first array, freq, defines the frequency
The second array, length, defines how long to play that frequency

Delay seems like the pause in between notes, but some of the numbers are pretty high compared to the duration of the sounds....

The one thing I do not see is where PB0 (Speaker) is toggled after initialization to create the frequency, it might be an AVR timer trick. I mostly work with PIC stuff.


Rich (BB code):
 
#include <avr/io.h>
#include <avr/pgmspace.h>
// pin definitions
#define SPEAKER PB0
#define SENSOR PB3
#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define output_toggle(port,pin) port ^= (1<<pin)
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)
#define read_value(portdir,pin) ( portdir & (1<<pin) )
const uint8_t freq[] PROGMEM = {
113, 113, 113, 147, 113, 97, 197, 147, 197, 234, 170, 156,
166, 174, 197, 113, 98, 87, 107, 98, 113, 144, 129, 156,
147, 197, 234, 170, 156, 166, 174, 197, 113, 98, 87, 107,
98, 113, 144, 129, 156, 150, 98, 104, 110, 120, 115, 197,
174, 150, 174, 150, 131, 150, 98, 104, 110, 120, 115, 73,
73, 73, 197, 150, 98, 104, 110, 120, 115, 197, 174, 150,
174, 150, 131, 128, 136, 150, 197, 150, 150, 150, 150,
98, 104, 110, 120, 115, 197, 174, 150, 174, 150, 131,
150, 98, 104, 110, 120, 115, 73, 73, 73, 197, 150, 98,
104, 110, 120, 115, 197, 174, 150, 174, 150, 131, 128, 136,
150, 197, 150, 150, 150, 150, 150, 150, 150, 129, 113, 150,
174, 197, 150, 150, 150, 150, 129, 113, 86, 98, 150, 150,
150, 150, 129, 113, 150, 174, 197, 113, 113, 113, 147,
113, 97, 197
 
};
const uint8_t length[] PROGMEM = {
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 80, 100,
100, 100, 80, 50, 100, 80, 50, 80, 80, 80, 80, 100, 100, 100,
100, 80, 100, 100, 100, 80, 50, 100, 80, 50, 80, 80, 80, 80,
100, 100, 100, 100, 150, 150, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 150, 200, 80, 80, 80, 100, 100, 100, 100,
100, 150, 150, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 150, 150, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 150, 200, 80, 80, 80,
100, 100, 100, 100, 100, 150, 150, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 60, 80, 60, 80, 80, 80, 80,
80, 80, 60, 80, 60, 80, 80, 80, 80, 80, 60, 80, 60, 80, 80, 80,
80, 80, 80, 100, 100, 100, 100, 100, 100, 100
};
const int delay[] PROGMEM = {
150, 300, 300, 100, 300, 550, 575, 450, 400, 500, 300, 330,
150, 300, 200, 200, 150, 300, 150, 350, 300, 150, 150, 500,
450, 400, 500, 300, 330, 150, 300, 200, 200, 150, 300, 150,
350, 300, 150, 150, 500, 300, 100, 150, 150, 300, 300, 150,
150, 300, 150, 100, 220, 300, 100, 150, 150, 300, 300, 300,
150, 300, 300, 300, 100, 150, 150, 300, 300, 150, 150, 300,
150, 100, 420, 450, 420, 360, 300, 300, 150, 300, 300, 100,
150, 150, 300, 300, 150, 150, 300, 150, 100, 220, 300, 100,
150, 150, 300, 300, 300, 150, 300, 300, 300, 100, 150, 150,
300, 300, 150, 150, 300, 150, 100, 420, 450, 420, 360, 300,
300, 150, 300, 150, 300, 350, 150, 350, 150, 300, 150, 600,
150, 300, 350, 150, 150, 550, 325, 600, 150, 300, 350, 150,
350, 150, 300, 150, 600, 150, 300, 300, 100, 300, 550, 575
};


void sleep(int ms) {
int cnt;
for (cnt=0; cnt<(ms); cnt++) 
{
int i = 100;
while(i--) 
{
__asm("NOP");
}
}
}
 
int main(void) {
set_output(DDRB,SPEAKER);
// set the pin reading ir sensor for input w/o pull-up
set_input(DDRB,SENSOR);
output_high(PORTB,SENSOR);
 
TCCR0A |= (1<<WGM01); // configure timer 1 for CTC mode
TCCR0A |= (1<<COM0A0); // toggle OC0A on compare match
TCCR0B |= (1<<CS01); // clk/8 prescale
 
for (;;) {
static uint8_t cnt=0;
static int time_delay=0;
if ( (PINB & 1<<SENSOR) && time_delay > 20) {
//start playing the tune
//start timer
TCCR0B |= (1<<CS01); // clk/8 prescale
OCR0A=pgm_read_byte(&freq[cnt]);
sleep( pgm_read_byte(&length[cnt]) );
// stop timer
TCCR0B = 0;
sleep ( pgm_read_word(&delay[cnt]) );
// start timer
TCCR0B |= (1<<CS01); // clk/8 prescale
cnt++;
if (cnt > 155) cnt=0;
} 
else if (PINB & 1<<SENSOR)
{
sleep(300);
time_delay++;
} 
else 
{
time_delay = 0; // reset the time delay
TCCR0B = 0;
}
}
return 0;
}
 

Thread Starter

Chris15

Joined Apr 15, 2009
252
Ok, so what does this mean? I appreciate the time you guys put towards extracting the sound part of the C file, but what can i do with it? how do i make my own tune if i dont know what im writing sounds like, just numbers
 
Top