function variable return problems

Thread Starter

SavageSean995

Joined Jul 13, 2015
1
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:
#include <stdio.h>
#include <stdlib.h>
#include <pic16f639.h>
#include <xc.h>

#define left PORTBbits.RB0
#define right PORTBbits.RB1

int getportValue(int portValue);
int delay(int portValue);

int portValue=0;

void main()
{
//Osc set to 4MHz so tosc = 1MHz
OSCCONbits.IRCF = 0b1101;
OSCCONbits.SCS = 0b10;
OPTION_REGbits.TMR0CS = 0;
OPTION_REGbits.TMR0SE = 0;
OPTION_REGbits.PSA = 0;
//Prescaler set to 1:4
OPTION_REGbits.PS = 0b001;

LATB = 0x00;
ANSELB = 0x00;
TRISB = 0x01;
LATC = 0x00;
TRISC=0;

   
 
    while(1)
   {
         portValue = getportValue(portValue);

         PORTC = portValue;
   }    
}


int delay(int portValue)
{
       for(int i = 1; i <= portValue; ++i)
       {
       //Delay is 1mS
  
       TMR0 = 6;
       while(INTCONbits.TMR0IF == 0);
       INTCONbits.TMR0IF = 0;     
       }    
}

int getportValue(int portValue)
{
    if(left)
    {
        while(left)
        {
            ;
        }
            if(portValue==0)
            {
            portValue=0;
            return portValue;
            }
            if(portValue!=0)
            {
            portValue--;
            return portValue;
            }
    }

    if(right)
    {
        while(right)
        {
            ;
        }

        if(portValue==3)
        {
            portValue==3;
            return portValue;
        }

        if(portValue!=3)
        {
            portValue++;
            return portValue;
        }
      
    }
}

This is program 2

Code:
#include <stdio.h>
#include <stdlib.h>
#include <pic16f639.h>
#include <xc.h>

#define left PORTBbits.RB0
#define right PORTBbits.RB1

int getportValue(int portValue);
int delay(int portValue);

int portValue=0;

void main()
{
//Osc set to 4MHz so tosc = 1MHz
OSCCONbits.IRCF = 0b1101;
OSCCONbits.SCS = 0b10;
OPTION_REGbits.TMR0CS = 0;
OPTION_REGbits.TMR0SE = 0;
OPTION_REGbits.PSA = 0;
//Prescaler set to 1:4
OPTION_REGbits.PS = 0b001;

LATB = 0x00;
ANSELB = 0x00;
TRISB = 0x01;
LATC = 0x00;
TRISC=0;

   
 
    while(1)
   {
         portValue = getportValue(portValue);

         //PORTC = portValue;

        PORTCbits.RC3 = 1;
        delay(portValue);
     
        PORTCbits.RC3 = 0;
        delay(portValue);
   }    
}


int delay(int portValue)
{
       for(int i = 1; i <= portValue; ++i)
       {
       //Delay is 1mS
  
       TMR0 = 6;
       while(INTCONbits.TMR0IF == 0);
       INTCONbits.TMR0IF = 0;     
       }    
}

int getportValue(int portValue)
{
    if(left)
    {
        while(left)
        {
            ;
        }
            if(portValue==0)
            {
            portValue=0;
            return portValue;
            }
            if(portValue!=0)
            {
            portValue--;
            return portValue;
            }
    }

    if(right)
    {
        while(right)
        {
            ;
        }

        if(portValue==3)
        {
            portValue==3;
            return portValue;
        }

        if(portValue!=3)
        {
            portValue++;
            return portValue;
        }
      
    }
}
 

ErnieM

Joined Apr 24, 2011
8,415
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.
 

hunterage2000

Joined May 2, 2010
487
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.
 
Top