d2jsp
d2jsp Forums > Programmer's Haven > C/C++/C# > Need Help Programming A Game > Have Code Built In C
Add Reply New Topic New Poll
neweras
#1 Dec 4 2019 01:06pm
Group: Member
Posts: 3,244
Joined: May 30 2006
Gold: 10,122.00
i need some assitance building a code using msp430 in C i can send you what i have so far its like 50 lines or so .
Yakir
#2 Dec 5 2019 06:20am
Group: Trade Moderator
Posts: 22,526
Joined: Dec 20 2005
Gold: 22,925.00
Trader: Mediator
What exactly you need?
neweras
#3 Dec 5 2019 11:29am
Group: Member
Posts: 3,244
Joined: May 30 2006
Gold: 10,122.00
play music while my game is running is not coming out this is my code.

#include <msp430.h>
#include "lcdLib.h"
int value;
int tic=0;
int row = 0;
int col;
int bottom = 15;
int score = 0;
int array[8];
int size = 8;
int i;
int level = 1;
int LevelTest = 1;
int total;
int num = 123;
int speed = 25000;
void printLcd();
void testArray();
void HexCheck(int Hex, int x, int y);
unsigned short int randomNumber(unsigned short int x);
unsigned short int random(unsigned short int z);



#define c 261
#define d 294
#define e 329
#define f 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

void delay_msa(unsigned int msa )
{
unsigned int j;
for (j = 0; j<= msa; j++)
__delay_cycles(500); //Built-in function that suspends the execution for 500 cicles
}

void delay_usa(unsigned int usa )
{
unsigned int j;
for (j = 0; j<= usa/2; j++)
__delay_cycles(1);
}

//This function generates the square wave that makes the piezo speaker sound at a determinated frequency.
void beep(unsigned int note, unsigned int duration)
{
int j;
long delay = (long)(10000/note); //This is the semiperiod of each note.
long time = (long)((duration*100)/(delay*2)); //This is how much time we need to spend on the note.
for (j=0;j<time;j++)
{
P1OUT |= BIT6; //Set P1.2...
delay_usa(delay); //...for a semiperiod...
P1OUT &= ~BIT6; //...then reset it...
delay_usa(delay); //...for the other semiperiod.
}
delay_ms(20); //Add a little delay to separate the single notes
}

void play()
{
beep(a, 500);
beep(a, 500);
beep(a, 500);
beep(a, 350);
beep(a, 150);
beep(a, 500);
beep(a, 350);
beep(a, 150);
beep(a, 650);

delay_ms(150);
//end of first bit

beep(a, 500);
beep(a, 500);
beep(a, 500);
beep(a, 350);
beep(a, 150);
beep(a, 500);
beep(a, 350);
beep(a, 150);
beep(a, 650);

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

beep(a, 500);
beep(a, 300);
beep(a, 150);
beep(a, 400);
beep(a, 200);
beep(a, 200);
beep(a, 125);
beep(a, 125);
beep(a, 250);

delay_ms(250);

beep(a, 250);
beep(a, 400);
beep(a, 200);
beep(a, 200);
beep(a, 125);
beep(a, 125);
beep(a, 250);

delay_ms(250);

beep(a, 125);
beep(a, 500);
beep(a, 375);
beep(a, 125);
beep(a, 500);
beep(a, 375);
beep(a, 125);
beep(a, 650);

//end of third bit... (Though it doesn't play well)
//let's repeat it

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

delay_ms(250);

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

delay_ms(250);

beep(f, 250);
beep(gS, 500);
beep(f, 375);
beep(cH, 125);
beep(c, 500);
beep(f, 375);
beep(cH, 125);
beep(c, 650);
//end of the song

}




int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT


P1DIR|=BIT6;

col = random(num);
lcdInit();
lcdClear();
printLcd();

TA0CTL = TASSEL_2 ;
TA0CCTL0 = CCIE;
TA0CCR0 = 1000;

ADC10CTL0 = ADC10ON + ADC10IE + ADC10SHT_0;
ADC10CTL1 = INCH_5;
ADC10AE0 |= 0x20;
ADC10CTL0 |= ENC + ADC10SC;
_enable_interrupts();
while(1){
play();
delay_ms(2000);

}
}


#pragma vector = ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
value = ADC10MEM;

if (value > 550)
{
col++;
if(col > 7)
col = 7;
}
else if(value < 450)
{
col--;
if(col < 0)
col = 0;
}
else if( value>450 && value<550)
{
speed -= 2000;
TA0CCR0 = speed;
}
printLcd();
TA0CTL |= MC_1;
}
#pragma vector=TIMER0_A0_VECTOR//FALLING BIT TIMER
__interrupt void TA_ISR(void)
{
tic++;
if (tic == 10)
{
row++;
//row--;
if(row > bottom - 1 )
{
// row--;
if(array[col] == 1)

bottom--;
else
array[col] = 1;
row = 0;
col = random(num++);
speed =(int) 50000;
TA0CCR0 = speed;
}
tic=0;
TA0CTL ^= MC_1;
printLcd();
testArray();

}
}
void testArray()
{
if(bottom <= 0)
{
lcdClear();
lcdSetText("You Lost!", 0,0);
bottom = 0;
return;
}
total = 0;
for(i = 0; i < 8; i++)
total += array;

if(total == 8)
{
score++;
bottom++;
if(bottom > 15)
bottom = 15;
for(i = 0; i < 8; i++)
array = 0;
}
else if(score == 32)
{
lcdClear();
lcdSetText("Hooray",0,0);
lcdSetText("Bombs Away!!", 0,1);
return;
}
ADC10CTL0 |= ENC + ADC10SC;
}
void HexCheck(int Hex, int x, int y)
{
switch (Hex){
case 10:
lcdSetText("A", x,y);
break;
case 11:
lcdSetText("B",x,y);
break;
case 12:
lcdSetText("C", x,y);
break;
case 13:
lcdSetText("D",x,y);
break;
case 14:
lcdSetText("E",x,y);
break;
case 15:
lcdSetText("F",x,y);
break;
default:
lcdSetInt(Hex, x,y);
break;
}
}

unsigned short int randomNumber(unsigned short int x)
{
unsigned short int i, y;
for(i = 0, y= 997; i < 113; i++)
y += x;

for( ; y > 1237; y -=1237)
;

return y;

}

unsigned short int random(unsigned short int z) //0->65K
{
unsigned short int h;

z = randomNumber(z);
h = z;
for(; h >= 8; h -= 8)
;

return h;
}
void printLcd()
{
lcdSetText(" ",0,0);
lcdSetInt(1, col, 0);
for(i = 0; i < size; i++)
lcdSetInt(array, i, 1); // lcdSetInt(print, 0-15, 0-1)
lcdSetText("R:",8,0);
lcdSetText("B:",8,1);
lcdSetText("S:",12,0);
lcdSetText("Lvl",12,1);
HexCheck(row,10,0);
HexCheck(bottom, 10,1);
lcdSetInt(score, 14,0);
lcdSetInt(level, 15,1);
return;
}
Klexmoo
#4 Dec 5 2019 12:45pm
Group: Member
Posts: 10,347
Joined: May 17 2013
Gold: 53,243.00
Code
#include <msp430.h>
#include "lcdLib.h"
int value;
int tic=0;
int row = 0;
int col;
int bottom = 15;
int score = 0;
int array[8];
int size = 8;
int i;
int level = 1;
int LevelTest = 1;
int total;
int num = 123;
int speed = 25000;
void printLcd();
void testArray();
void HexCheck(int Hex, int x, int y);
unsigned short int randomNumber(unsigned short int x);
unsigned short int random(unsigned short int z);



#define c 261
#define d 294
#define e 329
#define f 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

void delay_msa(unsigned int msa )
{
unsigned int j;
for (j = 0; j<= msa; j++)
__delay_cycles(500); //Built-in function that suspends the execution for 500 cicles
}

void delay_usa(unsigned int usa )
{
unsigned int j;
for (j = 0; j<= usa/2; j++)
__delay_cycles(1);
}

//This function generates the square wave that makes the piezo speaker sound at a determinated frequency.
void beep(unsigned int note, unsigned int duration)
{
int j;
long delay = (long)(10000/note); //This is the semiperiod of each note.
long time = (long)((duration*100)/(delay*2)); //This is how much time we need to spend on the note.
for (j=0;j<time;j++)
{
P1OUT |= BIT6; //Set P1.2...
delay_usa(delay); //...for a semiperiod...
P1OUT &= ~BIT6; //...then reset it...
delay_usa(delay); //...for the other semiperiod.
}
delay_ms(20); //Add a little delay to separate the single notes
}

void play()
{
beep(a, 500);
beep(a, 500);
beep(a, 500);
beep(a, 350);
beep(a, 150);
beep(a, 500);
beep(a, 350);
beep(a, 150);
beep(a, 650);

delay_ms(150);
//end of first bit

beep(a, 500);
beep(a, 500);
beep(a, 500);
beep(a, 350);
beep(a, 150);
beep(a, 500);
beep(a, 350);
beep(a, 150);
beep(a, 650);

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

beep(a, 500);
beep(a, 300);
beep(a, 150);
beep(a, 400);
beep(a, 200);
beep(a, 200);
beep(a, 125);
beep(a, 125);
beep(a, 250);

delay_ms(250);

beep(a, 250);
beep(a, 400);
beep(a, 200);
beep(a, 200);
beep(a, 125);
beep(a, 125);
beep(a, 250);

delay_ms(250);

beep(a, 125);
beep(a, 500);
beep(a, 375);
beep(a, 125);
beep(a, 500);
beep(a, 375);
beep(a, 125);
beep(a, 650);

//end of third bit... (Though it doesn't play well)
//let's repeat it

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

delay_ms(250);

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

delay_ms(250);

beep(f, 250);
beep(gS, 500);
beep(f, 375);
beep(cH, 125);
beep(c, 500);
beep(f, 375);
beep(cH, 125);
beep(c, 650);
//end of the song

}




int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT


P1DIR|=BIT6;

col = random(num);
lcdInit();
lcdClear();
printLcd();

TA0CTL = TASSEL_2 ;
TA0CCTL0 = CCIE;
TA0CCR0 = 1000;

ADC10CTL0 = ADC10ON + ADC10IE + ADC10SHT_0;
ADC10CTL1 = INCH_5;
ADC10AE0 |= 0x20;
ADC10CTL0 |= ENC + ADC10SC;
_enable_interrupts();
while(1){
play();
delay_ms(2000);

}
}


#pragma vector = ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
value = ADC10MEM;

if (value > 550)
{
col++;
if(col > 7)
col = 7;
}
else if(value < 450)
{
col--;
if(col < 0)
col = 0;
}
else if( value>450 && value<550)
{
speed -= 2000;
TA0CCR0 = speed;
}
printLcd();
TA0CTL |= MC_1;
}
#pragma vector=TIMER0_A0_VECTOR//FALLING BIT TIMER
__interrupt void TA_ISR(void)
{
tic++;
if (tic == 10)
{
row++;
//row--;
if(row > bottom - 1 )
{
// row--;
if(array[col] == 1)

bottom--;
else
array[col] = 1;
row = 0;
col = random(num++);
speed =(int) 50000;
TA0CCR0 = speed;
}
tic=0;
TA0CTL ^= MC_1;
printLcd();
testArray();

}
}
void testArray()
{
if(bottom <= 0)
{
lcdClear();
lcdSetText("You Lost!", 0,0);
bottom = 0;
return;
}
total = 0;
for(i = 0; i < 8; i++)
total += array[I];

if(total == 8)
{
score++;
bottom++;
if(bottom > 15)
bottom = 15;
for(i = 0; i < 8; i++)
array[I]= 0;
}
else if(score == 32)
{
lcdClear();
lcdSetText("Hooray",0,0);
lcdSetText("Bombs Away!!", 0,1);
return;
}
ADC10CTL0 |= ENC + ADC10SC;
}
void HexCheck(int Hex, int x, int y)
{
switch (Hex){
case 10:
lcdSetText("A", x,y);
break;
case 11:
lcdSetText("B",x,y);
break;
case 12:
lcdSetText("C", x,y);
break;
case 13:
lcdSetText("D",x,y);
break;
case 14:
lcdSetText("E",x,y);
break;
case 15:
lcdSetText("F",x,y);
break;
default:
lcdSetInt(Hex, x,y);
break;
}
}

unsigned short int randomNumber(unsigned short int x)
{
unsigned short int i, y;
for(i = 0, y= 997; i < 113; i++)
y += x;

for( ; y > 1237; y -=1237)
;

return y;

}

unsigned short int random(unsigned short int z) //0->65K
{
unsigned short int h;

z = randomNumber(z);
h = z;
for(; h >= 8; h -= 8)
;

return h;
}
void printLcd()
{
lcdSetText(" ",0,0);
lcdSetInt(1, col, 0);
for(i = 0; i < size; i++)
lcdSetInt(array[I], i, 1); // lcdSetInt(print, 0-15, 0-1)
lcdSetText("R:",8,0);
lcdSetText("B:",8,1);
lcdSetText("S:",12,0);
lcdSetText("Lvl",12,1);
HexCheck(row,10,0);
HexCheck(bottom, 10,1);
lcdSetInt(score, 14,0);
lcdSetInt(level, 15,1);
return;
}
neweras
#5 Dec 5 2019 01:48pm
Group: Member
Posts: 3,244
Joined: May 30 2006
Gold: 10,122.00
???
SCVonSteroids
#6 Jan 1 2020 05:27am
Group: Member
Posts: 3,574
Joined: Aug 17 2013
Gold: 275.00
They formatted your code.

So this used to work and now it doesn't?
I ask because of this comment in your code:
Code
//end of third bit... (Though it doesn't play well)


Have you actively broken into this code and confirmed it all executes as it should? We're talking micro controllers here eh? (Just what I found when I looked up MSP430) Never worked with those before...
3oDAtlas
#7 Jan 1 2020 07:12am
Group: Member
Posts: 4,353
Joined: Apr 16 2019
Gold: 13,010.95
Quote (SCVonSteroids @ Jan 1 2020 05:27am)
They formatted your code.

So this used to work and now it doesn't?
I ask because of this comment in your code:
Code
//end of third bit... (Though it doesn't play well)


Have you actively broken into this code and confirmed it all executes as it should? We're talking micro controllers here eh? (Just what I found when I looked up MSP430) Never worked with those before...


Yeah, looks like Arduino to me.
Go Back To C/C++/C# Topic List
Add Reply New Topic New Poll