-1

So I created this program in MPLABX, I am using it mostly to learn to code in C for future projects. I wanted to put some hardware/ register stuff into functions for neatness and ease of reading. For some reason my PWM function keeps repeating and despite what i do i cant get it to run then stop.

Here is what I have:

    #include <xc.h>








        const int sineval[320] = {125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 146, 148, 150, 152, 
154, 156, 158, 160, 161, 163, 165, 167, 169, 170, 172, 174, 176, 177, 179, 181, 182, 184, 185, 187, 
188, 190, 191, 193, 194, 196, 197, 198, 200, 201, 202, 204, 205, 206, 207, 208, 209, 210, 211, 212, 
213, 214, 215, 216, 217, 217, 218, 219, 219, 220, 221, 221, 222, 222, 223, 223, 223, 224, 224, 224, 
225, 225, 225, 225, 225, 225, 225, 225, 225, 225, 225, 224, 224, 224, 223, 223, 223, 222, 222, 221, 
221, 220, 219, 219, 218, 217, 217, 216, 215, 214, 213, 212, 211, 210, 209, 208, 207, 206, 205, 204, 
202, 201, 200, 198, 197, 196, 194, 193, 191, 190, 188, 187, 185, 184, 182, 181, 179, 177, 176, 174, 
172, 170, 169, 167, 165, 163, 161, 160, 158, 156, 154, 152, 150, 148, 146, 145, 143, 141, 139, 137, 
135, 133, 131, 129, 127, 125, 123, 121, 119, 117, 115, 113, 111, 109, 107, 105, 104, 102, 100, 98, 
96, 94, 92, 90, 89, 87, 85, 83, 81, 80, 78, 76, 74, 73, 71, 69, 68, 66, 65, 63, 62, 60, 59, 57, 56, 
54, 53, 52, 50, 49, 48, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 33, 32, 31, 31, 30, 
29, 29, 28, 28, 27, 27, 27, 26, 26, 26, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 27, 
27, 27, 28, 28, 29, 29, 30, 31, 31, 32, 33, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 
48, 49, 50, 52, 53, 54, 56, 57, 59, 60, 62, 63, 65, 66, 68, 69, 71, 73, 74, 76, 78, 80, 81, 83, 85,
87, 89, 90, 92, 94, 96, 98, 100, 102, 104, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123};

        //function prototypes
        void fast_sine();
        void fast_sine_off();

        int main() {






    OSCCONbits.SCS0 = 0;    //This as 1 sets the micro to use the internal 32KHz RC Oscillator, 0 and it uses external OSC.
    OSCCONbits.SCS1 = 1;
    TRISA = 0xFF;           //This shit has to be inside main!!!! Setting the whole 8 bit register A to inputs.
    TRISD = 0xFF;
    TRISCbits.RC0 = 0;
    TRISCbits.RC1 = 0;
    TRISCbits.RC4 = 1;
    TRISCbits.RC5 = 1;
    TRISCbits.RC6 = 1;
    TRISB = 0x00;












    //Setup Buttons      
    //int pb1 = PORTDbits.RD2;
    //int pb2 = PORTDbits.RD3;
    //int pb3 = PORTCbits.RC4;
    //int pb4 = PORTCbits.RC5;     

    //define for delay funtion
    #define _XTAL_FREQ 20000000  




    //sinewave function test

    //for (int z = 0; z < 20; z++){
    fast_sine();

    //}

    fast_sine_off();





    /*
    while (1) {

        //sinewave without function
        #define _XTAL_FREQ 20000000
        //Sine wave
        PORTCbits.RC1 = 0;
        PR2 = 250; //Timer2 reset value.
        T2CON = 4; //TIMER 2 ON.
        CCP2CON = 60;
        CCPR2L = 0;
        for (int i = 0; i < 319; i++) {
            __delay_us(49);
            CCPR2L = sineval[i];
        }
    }
    */



    //CCP2CON = 0;
    //__delay_ms(10);
    //fast_sine();


    /*

     if (pb4 == 1 && pb1 == 1 ){
        fast_sine();
     __delay_ms(100);  
    }  

     else if (pb1 == 1  && CCPR2L <= 13) {
     CCPR2L = CCPR2L + 1; 
     LATCbits.LATC0 = 1;
     __delay_ms(100); 
     }


     else if (pb2 == 1 && CCPR2L >= 1  ){
     CCPR2L = CCPR2L - 1;
     LATCbits.LATC0 = 0;
    __delay_ms(100); 
     }

     else if (pb3 == 1 && PR2 >= 40){             
     PR2 = PR2 - 1;
     LATCbits.LATC0 = 1;
     __delay_ms(40); 
     }

    else if (pb4 == 1 && PR2 <= 133 ){           
     PR2 = PR2 + 1; 
     LATCbits.LATC0 = 0; 
     __delay_ms(40); 
    }

     */

    //} 

}

void fast_sine() {

        //#define _XTAL_FREQ 20000000
        //Sine wave
        PORTCbits.RC1 = 0;
        PR2 = 250; //Timer2 reset value.
        T2CON = 4; //TIMER 2 ON.
        CCP2CON = 60;
        CCPR2L = 0;
        for (int i = 0; i < 319; i++) {
        __delay_us(49);
        CCPR2L = sineval[i];
        }
    LATCbits.LATC0 = 0;


 }



void fast_sine_off(){

        //Sine wave off
        PORTCbits.RC1 = 0;
        LATCbits.LATC0 = 0;
        TRISCbits.RC1 = 1;
        PR2 = 0; //Timer2 reset value.
        T2CON = 0x00; //TIMER 2 ON.
        CCP2CON = 0x00;
        CCPR2L = 0;
        LATCbits.LATC0 = 1; 
        __delay_ms(10);
        LATCbits.LATC0 = 0; 
        __delay_ms(10);



}
TeslaCeph
  • 1
  • 1

1 Answers1

0

You have not told us how do you deduce the sine is not stopping.

You have declared two functions (sorry if I err but the code is not very clear, a lot of commented code), fast_sine and fast_sine_off.

1st test: You expect to execute first fast_sine and then fast_sine_off, you have not defined what happens after, as you let the program reach the end of main function.

In HiTech and new XC compilers this leads to a end of execution but as this is not a program that is called from a terminal, the end of execution normally leads to a controlled or uncontrolled reset. As the PIC you are using will restart, you are probably seeing the main code repeating endlessly.

If you want to be sure to control this, I would suggest to place an instruction like this after calling fast_sine_off:

while(true){ // if true is not recognized you an place while(1){
 CLRWDT(); // just in case you have enabled watchdog timer, refresh it so you don't let it reset the CPU
};

2nd test: On the other side you could have enabled the watchdog timer with a short enough time that it is triggered before reaching the end of fast_sine function execution, and what you would see would have similar results, endlessly executing fast_sine.

Try disabling whatchdog timer.

In any of both cases I would recommend you to try to emulate the code on a PICKIT3 or similar tools, or fall back to the simulator of MPLAB, and verify that your code reaches the sine_off function. You'll probably discover the issue very fast if you debug with any ICD tool or even the Simulator.

  • I was watching the sine wave on an oscope to see if it stops. The only way I could get it to stop was to add an endless empty loop at the bottom of the main but to me that seems like a workaround not a true answer. – TeslaCeph Nov 03 '15 at 18:35
  • Yes my code is a mess and I should have deleted the commented out stuff. That was because I was experimenting with so many things trying to get it to stop plus other nonsense. I thought I had tried both with and without watchdog but I think it looped either way. I am new to the whole hardware aspect of PICs I was compiling in basic before and there were many built in functions. I think you may have actually solved my problem already. I still don't understand why it only loops when I use a function though. Thanks so much for your help. – TeslaCeph Nov 03 '15 at 20:21
  • My whole response was erased, I meant to also say that I wanted the code to do the sine function a few times then stop at the end of the Main(). The thing was is that it worked fine till i made it into a function. Main loops or resets constantly with sine as a function but with it just coded into main it does not. – TeslaCeph Nov 03 '15 at 20:24