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.
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;
}
}
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;
}
}
}