Randy Carpenter

I cant figure out what is wrong

Discussion created by Randy Carpenter on May 15, 2009
Latest reply on May 15, 2009 by Daniel Lundin

I cant figure out what is wrong with my code.  Im pretty sure my getkey(), getnum() functions work, but when i run my program i just get clicking in my speaker.  I think i have the interrupts wrong where im not updating a compare register, or maybe its something simple like 0x0F should be 0xFF or i messed up on the math operation like &= or |= or ==... Any input would be greatly appreciated. Thanks

 

 


#include <hidef.h>      /* common defines and macros */#include <mc9s12c128.h>     /* derivative information */#include <math.h>#include <float.h>#pragma LINK_INFO DERIVATIVE "mc9s12c128"#define NUMBER_OF_BLACK_KEYS   36#define MAX_NUMBER             85#define MIN_NUMBER             20//INTS and BYTESvolatile int i = 0;volatile int j = 0;volatile int k = 0;volatile int l = 0;volatile int m = 0;volatile int once = 0;volatile byte hold = 0;volatile byte temp = 0;volatile byte temp1 = 0;word msCount = 0;word msCount1 = 0;word playchannel_6;word playchannel_7;//ARRAYSvolatile int number[3] = {0};volatile double freq[15] = {0};volatile int keys[15] = {0};int blackKeyArray[NUMBER_OF_BLACK_KEYS] =         { 22, 25, 27, 30, 32, 34, 37, 39, 42, 44, 46, 49, 51,           54, 56, 58, 61, 63, 66, 68, 70, 73, 75, 78, 80, 82,           85, 87, 90, 92, 94, 97, 99, 102, 104, 106 }; //STRUCTtypedef struct{   int noteNumber;   word start;   word stop; }Note;  Note record[101] = {0,0,0}; Note demo[38] = {0,0,0};    //********************************WAITINGvoid waiting() {for(k = 0; k < 5 ; k++){   }}//*****************************Reg Setupvoid setreg(){TSCR1 = 0x80; //Turn on Timer ADDRESS -046-TCTL1 = 0x50; //Timer Control ADDRESS -048-TSCR2 = 0x03;//SETS CLOCK TO 1MHZ TIMER CONTROL REGISTER ADDRESS -04D-DDRAD = 0XFF;//Set LED InputDDRT = 0xC0;DDRM = 0x0F;//KEYPAD SETPERT = 0x00;TIOS = 0x20;TIE = 0xE0;msCount = 0;temp1 = 0;i = 0;j = 0;}//**************************Get Input From Keyboardbyte getkey(){int test1 = 1;int test = 0;byte key1 = 0xFF;for(m=0;m<6;m++){key1 = 0xFF; if(test1 = 2) {  test = 1;}PERT = 0x00;//Test Row 1PTM_PTM0 = 0x01;waiting();if(PTT_PTT0 == 1)key1 = 0x00;if(PTT_PTT1 == 1)key1 = 0x01;if(PTT_PTT2 == 1)key1 = 0x02;if(PTT_PTT3 == 1)key1 = 0x03;//Test Row 2PTM = PTM << 1;waiting();if(PTT_PTT0 == 1)key1 = 0x04;if(PTT_PTT1 == 1)key1 = 0x05;if(PTT_PTT2 == 1)key1 = 0x06;if(PTT_PTT3 == 1)key1 = 0x0F;//Test Row 3PTM = PTM << 1;waiting();if(PTT_PTT0 == 1)key1 = 0x07;if(PTT_PTT1 == 1)key1 = 0x08;if(PTT_PTT2 == 1)key1 = 0x09;if(PTT_PTT3 == 1)key1 = 0x0A;//Test Row 4 PTM = PTM << 1;waiting();if(PTT_PTT0 == 1)key1 = 0x0B;if(PTT_PTT1 == 1)key1 = 0x0C;if(PTT_PTT2 == 1)key1 = 0x0D;if(PTT_PTT3 == 1)key1 = 0x0E;while(test == 1){    test = 0;   if(hold != key1){   test1--;   test1--;   m--;}} hold = key1; test1++; }return key1; }//***********************Control Button Pressedbyte Control_Pressed(byte key1){once = 0;key1 = getkey(); if(key1 == 0x0F){    once = 1;    PTAD = 0xFF; }  while(once == 1){  waiting();  key1 = getkey();        if(key1 == 0x0F){      //this ????    once = 2;     continue;    }    if(key1 != 0x0F)    break;       if(key1 == 0x0F){    continue;    } }  while(once == 2){ TIE = 0x00; key1 = getkey(); temp1 = 0;  j = 0; while((temp1 < 9)&&(key1 == 0xFF)){     j = j + pow(2,temp1); for(i = 0; i < 2000 ; i++){  PTAD = j;  }   key1 = getkey();   for(i = 0; i < 2000 ; i++){    PTAD = j; }temp1++;}j=0;while(temp1 != 0){     j = pow(2,temp1)-1 ; for(i = 0; i < 2000 ; i++){  PTAD = j;  }   key1 = getkey();   for(i = 0; i < 2000 ; i++){    PTAD = j; }temp1--;if(temp1 == 0) {  for(i = 0; i < 2000 ; i++){  PTAD = 0x00;  }   for(i = 0; i < 2000 ; i++){    PTAD = 0x00; }}}       if(key1 == 0x0F){      temp1 = 1;      return temp1;    }       if(key1 == 0x03){    temp1 = 2;    return temp1;    }       if(key1 == 0x0A){    temp1 = 3;    return temp1;    }       if(key1 == 0x0E){    //TIE = 0x00;    TIOS = 0x00;    temp1 = 4;    return temp1;    }       if(key1 == 0x00){    temp1 = 5;    return temp1;    }       if(key1 == 0x01){    temp1 = 6;    return temp1;    }    else    continue; }}//**********************************GetNumberint getnumber(volatile int midi){   volatile int midi1 = 0xFF;  once = 0;   // Clear the number array  number[0] = 0;  number[1] = 0;  number[2] = 0;   while(once == 0){  midi = midinumber();     if(midi == 0){       //this ???
  LEDFlash(midi1); //*******************************LED FLASHING  continue;  }   if(midi == 0x0E){  temp1 = 2;  break;     }   else{    number[0] = midi;    midi1 = number[0];    once = 1;  }  }    //Wait for user to unpress button   while(once == 1){   LEDFlash(midi1);    midi = midinumber();     if(midi == 0x0F)           //this???
        once = 2;    }     //Storing Second Number  while(once == 2){  midi = midinumber();   if(midi == 0x0F){           //this???    LEDFlash(number[0]);    continue;    }    if(midi == 0x0E){    temp1 = 2;     break;    }   else{    number[1] = midi;    midi1 = midi1*10 + number[1];    once = 3;   }  }       //Wait for user to unpress button    while(once == 3){   LEDFlash(number[1]);    midi = midinumber();     if(midi == 0x0F)          //???
     once = 4;    }       while(once == 4){    midi = midinumber();      if(midi == 0x0F){          //???
    LEDFlash(number[1]);    continue;    }       if(midi == 0x0E)    return midi1;    else    return midi1*10 + midi;    }   }int midinumber(){volatile int midi = 0xFF;PERT = 0x00;//Test Row 1PTM = PTM << 1;PTM_PTM0 = 0x01;waiting();if(PTT_PTT0 == 1)midi = 0x01;if(PTT_PTT1 == 1)midi = 0x02;if(PTT_PTT2 == 1)midi = 0x03;//Test Row 2PTM = PTM << 1;waiting();if(PTT_PTT0 == 1)midi = 0x04;if(PTT_PTT1 == 1)midi = 0x05;if(PTT_PTT2 == 1)midi = 0x06;//Test Row 3PTM = PTM << 1;waiting();if(PTT_PTT0 == 1)midi = 0x07;if(PTT_PTT1 == 1)midi = 0x08;if(PTT_PTT2 == 1)midi = 0x09;//Test Row 4 PTM = PTM << 1;waiting();if(PTT_PTT1 == 1)midi = 0x00;if(PTT_PTT2 == 1)midi = 0x0E;return midi; }//**************************************LED FlashLEDFlash(volatile int flash){ for(i = 0; i < 10000 ; i++){  PTAD = flash;  }  for(i = 0; i < 10000 ; i++){    PTAD = 0x00;}}//**************************************Set Frequencysetfreq(volatile int input){  // Verify the Midi Note Range  volatile int begin = 60;   for(j=0;j<15;++j){    for(i = 0; i < NUMBER_OF_BLACK_KEYS; ++i)   {       if(input == blackKeyArray[i]){      ++input;      i=0;   }   }      keys[j] = input;      ++input;    }   for(i=0;i<15;++i){ begin = keys[i];   if(i==0){ freq[i] = 27.5*pow(1.059463,(begin - 21)); continue; } freq[i] = 27.5 * pow(1.059463,(begin - 21));}begin = keys[0];for(i=0;i<15;++i){  freq[i] = 1000000/(2*freq[i]);   } }//**********************************Produce SOUNDvoid Produce_Sound(byte sound){ if (sound == 0){          //???  TIOS = 0x40;            //???           TIE = 0x40;}else if (sound == 0x0F){  TIOS = 0x40;                ///???       TIE = 0x40;} else{while (getkey() == sound){PTAD = keys[sound];TIOS = 0x40;TIE = 0x40;playchannel_6 = freq[sound];                       }}} //***************************INTERRUPTSvoid interrupt 13 ISR_Timer5(void) {        msCount++;       TC5 += 1000;
       TFLG1 = 0x20;      //???

      if((TFLG1 != 0x40)&&(TFLG1 != 0x80))                PTAD = msCount/100;                } void interrupt 14 ISR_Timer6(void) {  TC6 += playchannel_6;    TFLG1 = 0x40;                         //???
     }void interrupt 15 ISR_Timer7(void) { msCount1++;  TC7 += playchannel_7;    TFLG1 = 0x80;                           //??? }//LED RESETLED_reset(){   LEDFlash(0x01);  LEDFlash(0x02);  LEDFlash(0x04);  LEDFlash(0x08);  LEDFlash(0x10);  LEDFlash(0x20);  LEDFlash(0x40);  LEDFlash(0x80);  LEDFlash(0x80);  LEDFlash(0x80);  LEDFlash(0x40);  LEDFlash(0x20);  LEDFlash(0x10);  LEDFlash(0x08);  LEDFlash(0x04);  LEDFlash(0x02);  LEDFlash(0x01);  }void main(void) {volatile int key1;volatile int midi = 60;        Note scale[9] = {{60, 1000, 1150},{62, 1300, 1450},{64, 1600, 1750},{65, 1900, 2050},{67, 2200, 2350},{69, 2500, 2650},{71, 2800, 2950},{72, 3100, 3250},{0,0,0} };  setreg();setfreq(midi); EnableInterrupts;while(1){   key1 = getkey();   if(key1 != 0xFF)              //???
  PTAD = (key1 + (0x03 * key1)) + 4;   if(key1 == 0x0F){   TIOS = 0x00;  TFLG1 = 0x40;  Control_Pressed(key1);  }   //PTAD = msCount/100;   //**************************TEMP1 == 3 PLAY SCALE  if(temp1 == 3){    //PTAD = msCount/100;    key1 = getkey();     for(i=0;i<8;i++){   scale[i].noteNumber = freq[i];  }           msCount1 = 1000;                 for(i=0;i<9;i++){        while(msCount1 <= scale[i].start){       //PTAD = msCount/100;        if(key1 == 0xFF){      TIE = 0xA0;      TIOS = 0xA0;      }           if(key1 != 0xFF){      TIE = 0xE0;      TIOS = 0xE0;      PTAD = key1;      playchannel_6 = freq[key1];      }           if(key1 == 0x0F){      temp1 = 0;      break;      }           playchannel_7 = scale[i].noteNumber;      }       key1 = getkey();       while(msCount1 <= scale[i].stop){      msCount1++;       //PTAD = msCount/100;       if(key1 == 0x0F){      temp1 = 0;      break;      }          if(key1 == 0xFF){      TIE = 0x20;      TIOS = 0x20;      }           if(key1 != 0xFF){      TIE = 0x60;      TIOS = 0x60;      TFLG1 = 0x40;      PTAD = key1;      playchannel_6 = freq[key1];      }    }    if(scale[i].noteNumber == 0){        TIE = 0x00;   TIOS = 0x00;    }  }  }//***************temp1 == 4 RECORD MODE
   --TIE = 0x00--
   --TIOS = 0x00--                    if(temp1 == 4){    temp = 0;    while(temp == 0) {        key1 = getkey();     if(key1 != 0xFF)   temp = 0;     if(key1 == 0xFF){    TIOS = 0x20;    TIE = 0x20;   temp = 1;   }    }     //PTAD = msCount/100;    //*****************************CLEAR RECORD     for(i=0;i<101;i++){    record[i].noteNumber = 0;    record[i].start = 0;    record[i].stop = 0;         }       TIOS = 0x20;   TIE = 0x60;   msCount = 0;     LED_reset();     i=0;                         //*************RECORD STARTING   while(i < 101){      //PTAD = msCount/100;     key1 = getkey();      if(key1 == 0xFF){    TIOS = 0x20;    TIE = 0x20;   }   while(getkey() == 0xFF){  //PTAD = msCount/100;      }    key1 = getkey();     if(key1 == 0x0F){   record[i].stop = msCount;   i++;   record[i].noteNumber = freq[key1];           //???
   (scale[i])
   l=i;                                        if(i == 101)   break;     i = 100;   temp1 = 2;   break;   }    //PTAD = msCount/100;      if((key1 != 0xFF)&&(key1 != 0x0F)){     record[i].stop = msCount;   i++;   record[i].start = msCount;   record[i].noteNumber = freq[key1];   TIE = 0x60;   TIOS = 0x60;   }      while(getkey() == key1){   PTAD = key1;   playchannel_6 = freq[key1];   temp1 = 1;    }     key1 = getkey();    //PTAD = msCount/100;    if(temp1 == 1){   TIOS = 0x20;   TIE = 0x20;   record[i].stop = msCount;   i++;   record[i].start = msCount;   temp1 = 0;   }         }   //While(i < 101)     if(i == 100){   i++;   record[i].noteNumber = freq[key1];   //???
   (scale[i])   temp1 = 2;                   
   }    }  //Temp == 4   //PTAD = msCount/100;   while(temp1 == 1){   midi = getnumber(midi);   if((midi <= MIN_NUMBER)||(midi >= MAX_NUMBER)){  LED_reset();   continue;  }   setfreq(midi);  temp1 = 0;  }   Produce_Sound(key1);TIOS = 0x20;TIE = 0x20;//*****************************TEMP1 == 5 PLAY BACK RECORDif(temp1 == 5){   //PTAD = msCount/100;   key1 = getkey();   temp = 0;    while(temp == 0) {        key1 = getkey();     if(key1 != 0xFF)   temp = 0;     if(key1 == 0xFF)   temp = 1;     }       while(temp1 == 5){     //PTAD = msCount/100;    msCount = 0;                 key1 = getkey();      i=0;            TIE = 0xE0;      TIOS = 0x20;            for(i=0;i<l;i++){       //PTAD = key1;       TIOS = 0x20;           if(record[i].noteNumber == 0){          TIE = 0x00;           TIOS = 0x20;    }while((msCount <= record[i].stop) && (record[i].noteNumber == 0)){      //PTAD = msCount/100;       key1 = getkey();      if(key1 == 0x0F){        temp1 = 0;        break;      }           if(key1 == 0xFF){        TIE = 0x20;        TIOS = 0x20;      }           if(key1 != 0xFF){        TIE = 0x60;        TIOS = 0x60;        PTAD = key1;        playchannel_6 = freq[key1];      }    }    //PTAD = msCount/100;    while((msCount <= record[i].stop) && (record[i].noteNumber !=0)){      //PTAD = msCount/100;       key1 = getkey();           if(record[i].noteNumber == 0)      continue;            if (key1 == 0x0F){      temp1 = 0;      break;      }            if (key1 == 0xFF){      TIE = 0xA0;      TIOS = 0xA0;      }            if(key1 != 0xFF){      TIE = 0xE0;      TIOS = 0xE0;      PTAD = key1;      playchannel_6 = freq[key1];      PTAD = key1;      }                playchannel_7 = record[i].noteNumber;    }       if(key1 == 0x0F){      temp1 = 0;      break;      }           }      }}    //*****************************************temp1 == 6 PLAY DEMO   if(temp1 == 6){demo[0].noteNumber = 1275;demo[1].noteNumber = 1136;demo[2].noteNumber = 1012;demo[3].noteNumber = 851;demo[4].noteNumber = 955;demo[5].noteNumber = 851;demo[6].noteNumber = 758;demo[7].noteNumber = 851;demo[8].noteNumber = 851;demo[9].noteNumber = 637;demo[10].noteNumber = 675;demo[11].noteNumber = 637;demo[12].noteNumber = 851;demo[13].noteNumber = 1012;demo[14].noteNumber = 1275;demo[15].noteNumber = 1136;demo[16].noteNumber = 1012;demo[17].noteNumber = 851;demo[18].noteNumber = 851;demo[19].noteNumber = 851;demo[20].noteNumber = 758;demo[21].noteNumber = 851;demo[22].noteNumber = 1012;demo[23].noteNumber = 1136;demo[24].noteNumber = 1012;demo[25].noteNumber = 1275;demo[26].noteNumber = 1136;demo[27].noteNumber = 1275;demo[28].noteNumber = 1136;demo[29].noteNumber = 1703;demo[30].noteNumber = 1136;demo[31].noteNumber = 1136;demo[32].noteNumber = 851;demo[33].noteNumber = 1012;demo[34].noteNumber = 1136;demo[35].noteNumber = 1012;demo[36].noteNumber = 0; //PTAD = msCount/100;  demo[0].start = 0; demo[0].stop = 150;for(i=1;i<36;i++){ demo[i].start = demo[i-1].start + 325;  }for(i=1;i<36;i++){ demo[i].stop = demo[i-1].stop + 225;  } msCount = 0;   key1 = getkey();   for(i=0;i<36;i++){        while(msCount <= demo[i].start){        key1 = getkey();      if(key1 == 0xFF){      TIE = 0xA0;      TIOS = 0xA0;      }      //PTAD = msCount/100;       if(key1 != 0xFF){      TIE = 0xE0;      TIOS = 0xE0;      PTAD = key1;      playchannel_6 = freq[key1];      }           if(key1 == 0x0F){      temp1 = 0;      break;      }           playchannel_7 = demo[i].noteNumber;      }       key1 = getkey();       while(msCount <= demo[i].stop){      msCount1++;        key1 = getkey();      if(key1 == 0x0F){      temp1 = 0;      break;      }      //PTAD = msCount/100;      if(key1 == 0xFF){      TIE = 0x20;      TIOS = 0x20;      }           if(key1 != 0xFF){      TIE = 0x60;      TIOS = 0x60;      PTAD = key1;      playchannel_6 = freq[key1];      }    }    if(demo[i].noteNumber == 0){        TIE = 0x00;   TIOS = 0x00;    }  }}}}

 

 It is supposed to populate a Keypad array 123A,456B,789C,*0#D, with the midi note middle C and the rest of the notes in the scale.  B button is used as control mode to select either normal play, record, different starting note, and playback what was recorded.  I am using channel 6 for normal play modes and channel 7 for playing back what was recorded.  My record function should save the start time of the note, the stop time of the note, and the note number to the stack where it can be later recalled for playback.  On every entry into the record mode I try to clear what was previously recorded.

Outcomes