function variable return problems

Discussion in 'Embedded Systems and Microcontrollers' started by SavageSean995, Jul 13, 2015.

  1. SavageSean995

    Thread Starter New Member

    Jul 13, 2015
    1
    0
    Hello, I have written the following program and I am wondering why the portValue variable holds an incremented value but the second program doesnt hold the value when sent to a delay function as an argument. I am trying to increment a value when a button is pressed then put that value in a delay function that increments the delay by 1ms after every button press.

    Code (Text):
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <pic16f639.h>
    4. #include <xc.h>
    5.  
    6. #define left PORTBbits.RB0
    7. #define right PORTBbits.RB1
    8.  
    9. int getportValue(int portValue);
    10. int delay(int portValue);
    11.  
    12. int portValue=0;
    13.  
    14. void main()
    15. {
    16. //Osc set to 4MHz so tosc = 1MHz
    17. OSCCONbits.IRCF = 0b1101;
    18. OSCCONbits.SCS = 0b10;
    19. OPTION_REGbits.TMR0CS = 0;
    20. OPTION_REGbits.TMR0SE = 0;
    21. OPTION_REGbits.PSA = 0;
    22. //Prescaler set to 1:4
    23. OPTION_REGbits.PS = 0b001;
    24.  
    25. LATB = 0x00;
    26. ANSELB = 0x00;
    27. TRISB = 0x01;
    28. LATC = 0x00;
    29. TRISC=0;
    30.  
    31.    
    32.  
    33.     while(1)
    34.    {
    35.          portValue = getportValue(portValue);
    36.  
    37.          PORTC = portValue;
    38.    }    
    39. }
    40.  
    41.  
    42. int delay(int portValue)
    43. {
    44.        for(int i = 1; i <= portValue; ++i)
    45.        {
    46.        //Delay is 1mS
    47.  
    48.        TMR0 = 6;
    49.        while(INTCONbits.TMR0IF == 0);
    50.        INTCONbits.TMR0IF = 0;    
    51.        }    
    52. }
    53.  
    54. int getportValue(int portValue)
    55. {
    56.     if(left)
    57.     {
    58.         while(left)
    59.         {
    60.             ;
    61.         }
    62.             if(portValue==0)
    63.             {
    64.             portValue=0;
    65.             return portValue;
    66.             }
    67.             if(portValue!=0)
    68.             {
    69.             portValue--;
    70.             return portValue;
    71.             }
    72.     }
    73.  
    74.     if(right)
    75.     {
    76.         while(right)
    77.         {
    78.             ;
    79.         }
    80.  
    81.         if(portValue==3)
    82.         {
    83.             portValue==3;
    84.             return portValue;
    85.         }
    86.  
    87.         if(portValue!=3)
    88.         {
    89.             portValue++;
    90.             return portValue;
    91.         }
    92.      
    93.     }
    94. }
    95.  
    96.  
    97.  
    98.  
    99.  
    100.  

    This is program 2

    Code (Text):
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <pic16f639.h>
    4. #include <xc.h>
    5.  
    6. #define left PORTBbits.RB0
    7. #define right PORTBbits.RB1
    8.  
    9. int getportValue(int portValue);
    10. int delay(int portValue);
    11.  
    12. int portValue=0;
    13.  
    14. void main()
    15. {
    16. //Osc set to 4MHz so tosc = 1MHz
    17. OSCCONbits.IRCF = 0b1101;
    18. OSCCONbits.SCS = 0b10;
    19. OPTION_REGbits.TMR0CS = 0;
    20. OPTION_REGbits.TMR0SE = 0;
    21. OPTION_REGbits.PSA = 0;
    22. //Prescaler set to 1:4
    23. OPTION_REGbits.PS = 0b001;
    24.  
    25. LATB = 0x00;
    26. ANSELB = 0x00;
    27. TRISB = 0x01;
    28. LATC = 0x00;
    29. TRISC=0;
    30.  
    31.    
    32.  
    33.     while(1)
    34.    {
    35.          portValue = getportValue(portValue);
    36.  
    37.          //PORTC = portValue;
    38.  
    39.         PORTCbits.RC3 = 1;
    40.         delay(portValue);
    41.      
    42.         PORTCbits.RC3 = 0;
    43.         delay(portValue);
    44.    }    
    45. }
    46.  
    47.  
    48. int delay(int portValue)
    49. {
    50.        for(int i = 1; i <= portValue; ++i)
    51.        {
    52.        //Delay is 1mS
    53.  
    54.        TMR0 = 6;
    55.        while(INTCONbits.TMR0IF == 0);
    56.        INTCONbits.TMR0IF = 0;    
    57.        }    
    58. }
    59.  
    60. int getportValue(int portValue)
    61. {
    62.     if(left)
    63.     {
    64.         while(left)
    65.         {
    66.             ;
    67.         }
    68.             if(portValue==0)
    69.             {
    70.             portValue=0;
    71.             return portValue;
    72.             }
    73.             if(portValue!=0)
    74.             {
    75.             portValue--;
    76.             return portValue;
    77.             }
    78.     }
    79.  
    80.     if(right)
    81.     {
    82.         while(right)
    83.         {
    84.             ;
    85.         }
    86.  
    87.         if(portValue==3)
    88.         {
    89.             portValue==3;
    90.             return portValue;
    91.         }
    92.  
    93.         if(portValue!=3)
    94.         {
    95.             portValue++;
    96.             return portValue;
    97.         }
    98.      
    99.     }
    100. }
    101.  
    102.  
    103.  
    104.  
    105.  
    106.  
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    It is best to cut your code down to a minimum amount that still shows the problem.

    A better description is necessary, I have no idea what your problem is.

    I do note you use the name “portValue” in several places but they refer to different things. One is a global value, others are local to functions, all are confusing.
     
  3. hunterage2000

    Active Member

    May 2, 2010
    400
    0
    yeah sorry it is a bit too much. The idea is to press a button called right that increments the portValue integer by 1 and returns it to portValue = getportValue(portValue); and make PORTC = portValue; . For the 1st program the portValue holds the value, say I press 3 times I get portValue = 3 as leds on bits 0 and 1 of PORTC are lit up.

    But for the 2nd program, I am expecting portValue to hold at 3 when it is sent to the delay function as an argument. It does increment the value and it sends 3 to the delay function but once the function is exited the portValue returns to 0.

    I'm assuming that the 1st program holds the value at 3 as the returned value is used within the curly braces it was called from but then again it doesn't hold in the 2nd program between the curly braces.
     
Loading...