Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Melody is too long...???

Status
Not open for further replies.

Tricka90

Member level 1
Member level 1
Joined
Sep 2, 2013
Messages
40
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Visit site
Activity points
385
I'm trying to generate a little melody when my PIC 16F876 turns on. So I wrote in main(), before the while(1) cycle the following note sequence (I use MikroC):
Code:
Sound_Play(523,600);
Sound_Play(262,200);
Sound_Play(349,400);
Sound_Play(523,600);
Sound_Play(262,200);
Sound_Play(294,200);
Sound_Play(349,200);
Sound_Play(523,200);
Sound_Play(349,200);
Sound_Play(294,400);
Sound_Play(523,600);
Sound_Play(233,200);
Sound_Play(349,400);
Sound_Play(466,600);
Sound_Play(349,200);
Sound_Play(233,400);
Sound_Play(440,200);
Sound_Play(349,200);
Sound_Play(233,400);
Sound_Play(440,1400);
Delay_ms(1000);

...but it doesn't play the melody just one time! It repeat it forever! Also every repetition it seems like a little bit of code goes on! It looks like a PIC malfunction because if I reduce the lenght of the melody it works PERFECTLY! To be precise, if I stop at note number nine (Sound_Play(349,200);) all it's ok, the melody ends and the program goes on correctly. But if I try to add another note it repeats all the melody forever! Do you know why this happens? Do you know how to work around this strange problem?
 

I tried adding while(1) at the end of the melody and the melody does stop correctly!
But of course I need my program to go on after the melody ends.
So i tried adding a while which lasts some time:
Code:
long intro;
while(intro <= 100000) {intro++;}
But as soon as the while cycle ends the melody still repeats forever!
 

Is very long, now I post it, thank you.
You can find the melody in "void melody()" function:

Code:
void main();


 //VARIABLES
int red = 20;
int blue = 20;
int green = 20;
int adc = 0;
int stato = 0;
int mode_change = 0;
int colore = 0;
int clockON = 0;
int pwm_rgb = 0;
int mode = 1;
int j = 0;
int pwm = 0;
int flash = 0;
int lum = 20;
int lum_provv;
int clock = 0;
unsigned long intro = 0;



void interrupt()
{
if (INTCON.TMR0IF)
    {

        if(mode == 5) {
        j++;
        if(j >= flash*30+80)
                  {
                            if(pwm == 1)
                            {
                                        lum_provv = lum;
                                        lum=0;
                                        pwm = 0;
                            }
                            else
                            {
                                        lum = lum_provv;
                                        pwm = 1;
                            }
                                        j = 0;
                  }

                       }


        if(pwm_rgb >= 20)
               {
                pwm_rgb = 0;
                }
        else
                {
                 pwm_rgb++;
                 }
        if(green >= pwm_rgb && green != 0)
                {
                 PORTB.RB1 = 1;
                 }
        else
                 {
                  PORTB.RB1 = 0;
                  }
        if(red >= pwm_rgb && red != 0)
                {
                 PORTB.RB0 = 1;
                 }
        else
                 {
                  PORTB.RB0 = 0;
                  }
        if(blue >= pwm_rgb && blue != 0)
                {
                 PORTB.RB2 = 1;
                 }
        else
                 {
                  PORTB.RB2 = 0;
                  }

        INTCON.TMR0IF = 0;
    }
}


void melody()
{
Sound_Play(523,600);
 Sound_Play(262,200);
 Sound_Play(349,400);
 Sound_Play(523,600);
 Sound_Play(262,200);
 Sound_Play(294,200);
 Sound_Play(349,200);
 Sound_Play(523,200);
 Sound_Play(349,200);
 Sound_Play(294,400);
 Sound_Play(523,600);
 Sound_Play(233,200);
 Sound_Play(349,400);
 Sound_Play(466,600);
 Sound_Play(349,200);
 Sound_Play(233,400);
 Sound_Play(440,200);
 Sound_Play(349,200);
 Sound_Play(233,400);
 Sound_Play(440,1400);
 Delay_ms(1000);

 }


int RGB(int adc, int i) {
  if(adc >= 210)  {
                if(adc <= 280) { Delay_ms(80); adc = ADC_Read(0); if(adc <= 290 && adc >= 220) {
                               mode_change = 0;
                               stato = 0;
                               { PORTC.RC3 = 0; PORTC.RC4 = 0; PORTC.RC5 = 1; }
                               i = i-1;
                               if(i <= 0){ i = 0;
                                           Sound_Play(659, 20); }

                               Sound_Play(523, 20);
                               }}
               else            {
                                if(adc >= 940) {
                                                { PORTC.RC3 = 0; PORTC.RC4 = 1; PORTC.RC5 = 0; }
                                                i = i+1;
                                                if(i >= 20){ i = 20; Sound_Play(1319, 20); }
                                                Sound_Play(1046, 20);
                                                Delay_ms(80);
                                                }

                                if(adc <= 840 && adc >= 320) {
                                      { PORTC.RC3 = 1; PORTC.RC4 = 0; PORTC.RC5 = 0; stato = 1; }
                                      i = i;
                                      }
                                }
                }

if(adc <= 170)
              {
                      { PORTC.RC3 = 0; PORTC.RC4 = 0; PORTC.RC5 = 0; }
                      if(stato == 1)
                                    {
                                     mode_change++;
                                     }
                      if(mode_change == 1)
                                          {
                                           clockON = 1;
                                           }
                      stato = 0;

               }

                colore = i;
                return(colore);
}

void main()
{
ADCON0 = 0;
ADCON1 = 0b00001110;

//TRIS
{
TRISB = 0;
PORTB = 0;
TRISC = 0;
PORTC = 0;
TRISA = 0b00000001;
PORTA = 0;
PORTA.RA2 = 1;
}

Sound_Init(&PORTA, 1);

//INTRO MUSIC
melody();





//CCP_PWM (RC2, RC1)
{
PWM1_Init(5500);
PWM1_Set_Duty(210);
PWM1_Start();

PWM2_Init(1000);
PWM2_Set_Duty(lum*12+10);
PWM2_Start();
}

//TIMER/INTERRUPT
{
OPTION_REG = 0b10000010;
INTCON = 0b11100000;


}



while(1)
 {
 adc = ADC_Read(0);


 if(clockON == 1)
                {
                 clock++;
                 if(clock >= 6500)
                                 {

                                  if(mode_change >= 2) { if(mode_change <= 4)
                                               {
                                                if(mode_change == 2) {
                                                                      mode++;
                                                                      if(mode == 6)
                                                                                  {
                                                                                   mode = 1;
                                                                                   }
                                                                      }
                                                if(mode_change == 3 || mode_change == 4)
                                                                    {
                                                                     mode -= 1;
                                                                     if(mode == 0)
                                                                                  {
                                                                                   mode = 5;
                                                                                   }
                                                                     }
                                                if(mode == 4)
                                                             {
                                                              PORTB.RB4= 1;
                                                              PORTA.RA5= 0;
                                                              PORTB.RB5= 0;
                                                              }
                                                if(mode == 5)
                                                             {
                                                              PORTA.RA2= 0;
                                                              PORTB.RB4= 0;
                                                              PORTB.RB5= 1;
                                                              }
                                                if(mode == 1)
                                                             {
                                                              PORTA.RA2= 1;
                                                              PORTA.RA3= 0;
                                                              PORTB.RB5= 0;
                                                              }
                                                if(mode == 2)
                                                             {
                                                              PORTA.RA2= 0;
                                                              PORTA.RA3= 1;
                                                              PORTA.RA5= 0;
                                                              }
                                                if(mode == 3)
                                                             {
                                                              PORTA.RA5= 1;
                                                              PORTA.RA3= 0;
                                                              PORTB.RB4= 0;

                                                              }
                                                mode_change = 0;
                                                Delay_ms(50);
                                                Sound_Play(1046, 60);
                                                Delay_ms(15);
                                                Sound_Play(1568, 60);
                                                Delay_ms(15);
                                                Sound_Play(1046, 60);
                                                Delay_ms(700);
                                                }}
                                  mode_change = 0;
                                  clock = 0;
                                  clockON = 0;
                                  }
                 }


 switch(mode) {
              case 1:
                       RGB(adc, lum);
                       lum = colore;
                       PWM2_Set_Duty(lum*12+10);
                       break;
              case 2:
                       RGB(adc, blue);
                       blue = colore;
                       break;

              case 3:
                       RGB(adc, green);
                       green = colore;
                       break;
              case 4:
                       RGB(adc, red);
                       red = colore;
                       break;
              case 5:
                       RGB(adc, flash);
                       flash = colore;
                       PWM2_Set_Duty(lum*12+10);
                       break;

              }
 }

}
 

Thank you.
The PIC I'm using is a 16F876, I said it in the first post.
Clock frequency is 12MHz, with a crystal oscillator, clock set on HS.
I attach the zip file:

- - - Updated - - -

UPDATE: I think I found the reason of the issue.
If I remove WHICHEVER function the melody just repeat once, so the problem is solved!
For example when I remove the two PWM declarations (PWM_Init, PWM_Set_Duty and PWM_Start) the melody infinite repetition doesn't occurs!
So i tried adding those functions later, inside the "while(1)" cycle, but the problem returns...
Maybe is a memory problem, or a CPU issue, I don't know exactly. I think so because WHATEVER big function I remove the problem is solved (for example if I remove the void interrupt() function or the int RGB() function the problem doesn't occur).
But RAM is just 18% used and ROM just 30%, that I can read on the compiler log...
Do you know how I can work around the problem? Maybe with some code simplification (removing useless routines)?

- - - Updated - - -

THE PROBLEM IS SOLVED!!!
I found that the problem wasn't occurring when used ROM was less than 2050, so I just correct some "int" with "unsigned short" and do some other little corrections and I manage to make used ROM under 2000, now it works perfectly!!!
But I still have a question: WHY the damn PIC start going crazy if the used ROM is just about 30%??? Do you know why?
 

Attachments

  • melody problem.rar
    50.3 KB · Views: 75

I don't know exactly what your code does. I am attaching a new file. See if it works fine. Post your circuit and explain the working of your code in detail.

Maybe your compiler is creating wrong hex file. Burn my hex file and try.
 

Attachments

  • melody problem.rar
    70 KB · Views: 68

You probably used the whole RAM memory with allocations, and at some point proc get restarted. So it seemed to you that your melody runs indefinite.
 

I think any linker would fail create binary if there are two main functions. It is probably copy/paste issue.
 

jayanth.devarayanadurga YOUR HEX WORKS PERFECTLY!!! And the ROM that you used is 2153, a value so high doesn't worked for my code! So i can say YOU TOTALLY SOLVED THE PROBLEM!
But there is a strange thing: if I try to create the hex myself, using YOUR project, the one who you just attach, it doesn't work! So I think there is a problem with the HEX creation of my compiler. Do you have any idea?
 

I used PIC16F876 and 12 MHz Clock. Did you change anything in code or settings? You have the hex file which I sent in the rar file. Compile my code without making any changes. Compare the hex file you get with the hex file in my .rar file. See if they are same or different. If different then your Compiler is creating wrong hex file. I had similar problem. Uninstalling and reinstalling the Compiler solves the problem.
 
Yes the two hex files are slightly different, I've just found that. Me too I'm using PIC16F876 and 12MHz clock. Now a try unistalling and reinstalling MikroC, I'll let you know. Thank you again for all the help
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top