• AVR Freaks

Hot!PIC16f777 Global Variable Not Updating

Author
Noob001
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2019/06/19 22:38:20
  • Location: 0
  • Status: offline
2019/11/19 22:15:51 (permalink)
0

PIC16f777 Global Variable Not Updating

Hello,
I have written some code for a thermometer that uses an LCD screen and a small matrix keypad. I am not using any libraries. My issue is that my global variable, calData, is not staying changed after calibrate() changes its value. Thermometer() still sees calData as it was initially assigned. Here is my relevant code.
 
unsigned char calData=190;

//prototypes, functions etc.

void calibrate(void){
    static char digit1,digit2;
           //get user input from keypad and turn into numbers...
    
    calData=(digit1+digit2+100);
            
          //print out what was entered and new calData value
   }
 
void thermometer(){
    float temperature;
    
     //lcd stuff and formatting...
   
    while(1){
            line2();
            //get and scale adc value then some math...
            temperature=temperature*(-0.12)+calData; //temperature is now in degrees C
            //more math...parse and print
    }
}
When I print the value of calData inside of Calibrate, the value is the new user-inputted value. However,
When I call thermometer(), calData is still 190, and not what calibrate() changed it to. I have tried using pointers instead of manipulating calData directly, but the issue persists. I have also tried making calData volatile, and every other keyword with no avail. Please Help!
#1

8 Replies Related Threads

    ric
    Super Member
    • Total Posts : 24633
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC16f777 Global Variable Not Updating 2019/11/20 12:28:46 (permalink)
    +2 (2)
    We would need to see ALL the code to see what is going on, but my first guess is the Watchdog Timer triggering and resetting your code.
    We can't see if it is disabled because you did not show your CONFIG settings.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    Noob001
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/06/19 22:38:20
    • Location: 0
    • Status: offline
    Re: PIC16f777 Global Variable Not Updating 2019/11/21 22:01:46 (permalink)
    0
    Thanks for the reply, I appreciate it!
    I will attach my entire main.c to give some more clarity around the issue.
    WDTE is set to off in my configuration bits.
    Cheers!
    #3
    1and0
    Access is Denied
    • Total Posts : 10000
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16f777 Global Variable Not Updating 2019/11/21 22:25:38 (permalink)
    +1 (1)
    unsigned char poll(void){
       while(1){    
        PORTD=0x00;
          
        PORTDbits.RD1=1;                                  // Should this RD1
        if(PORTDbits.RD4==1){return 'd';}
        else if(PORTDbits.RD5==1){return '#';}
        else if(PORTDbits.RD6==1){return '0';}
        else if(PORTDbits.RD7==1){return '*';}
        else{PORTDbits.RD0=0;}                         // match this RD0 ?
     
        PORTDbits.RD0=1;                                  // Same here RD0
        if(PORTDbits.RD4==1){return 'c';}
        else if(PORTDbits.RD5==1){return '9';}
        else if(PORTDbits.RD6==1){return '8';}
        else if(PORTDbits.RD7==1){return '7';}
        else{PORTDbits.RD1=0;}                        // with this RD1 ?
     
        PORTDbits.RD2=1;
        if(PORTDbits.RD4==1){return 'b';}
        else if(PORTDbits.RD5==1){return '6';}
        else if(PORTDbits.RD6==1){return '5';}
        else if(PORTDbits.RD7==1){return '4';}
        else{PORTDbits.RD2=0;}
     
        PORTDbits.RD3=1;
        if(PORTDbits.RD4==1){return 'a';}
        else if(PORTDbits.RD5==1){return '3';}
        else if(PORTDbits.RD6==1){return '2';}
        else if(PORTDbits.RD7==1){return '1';}
        else{PORTDbits.RD3=0;}
       }
    }
     
     
    void printint(unsigned char * input){                   // Why pass in a pointer ??
        unsigned char digits[4],index=2,todisplay;
        todisplay=(unsigned char)(*input);
        digits[3]='\0';
        while(todisplay>0){
        digits[index]=(unsigned char)todisplay%10+'0';  //the cast truncates the decimal created in the next line      
        todisplay/=10;
        --index;
        }
        digits[index]=todisplay+'0';
        printstr(&digits[0]);
    }
     
    #4
    ric
    Super Member
    • Total Posts : 24633
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC16f777 Global Variable Not Updating 2019/11/21 22:32:24 (permalink)
    +2 (2)
    There's some serious misunderstanding in this line
       digits[index]=(unsigned char)todisplay%10+'0';  //the cast truncates the decimal created in the next line     

    There is no "decimal" created in the next line. You divide an unsigned char variable by ten. You can only ever end up with an unsigned char integer back in that variable...
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    Noob001
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/06/19 22:38:20
    • Location: 0
    • Status: offline
    Re: PIC16f777 Global Variable Not Updating 2019/11/22 01:30:15 (permalink)
    0
    Greetings,
    1and0: Thank you for catching that, the RD1's should match each other and the RD0's should match each other-I connected a new keypad and made some changes on the fly, forgot to fix that.
     
    Ric: Thank you for the refresher, I took out the casts. Do you have any diagnostic methods I could try to see what is going on?
     
    With regards to the initial problem, I had also disabled compiler optimizations (at the zero setting) while running into the global variable issue. It happens on all levels of compiler optimization-thought that might be important.
    Cheers.
    #6
    ric
    Super Member
    • Total Posts : 24633
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC16f777 Global Variable Not Updating 2019/11/22 01:58:13 (permalink)
    +2 (2)
    How are you testing your code?
    What you posted just runs once and exits the main() function, which is a big no-no in an embedded environment.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    1and0
    Access is Denied
    • Total Posts : 10000
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16f777 Global Variable Not Updating 2019/11/22 06:44:10 (permalink)
    +2 (2)
    ric
    How are you testing your code?
    What you posted just runs once and exits the main() function, which is a big no-no in an embedded environment.


    Yeah, I think that is the problem:
     
    menu() -> calibrate() to change calData
    main() exit and reset calData to 190
    menu() -> thermometer() and use calData of 190
     
     
    @OP: I noticed you're passing arguments to functions as pointer where it should not or not needed, like these
    void printint(unsigned char * input){
        todisplay=(unsigned char)(*input);

    void digitize(float *decimal){
        unsigned short to_display=(unsigned short)(*decimal);
    #8
    Noob001
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/06/19 22:38:20
    • Location: 0
    • Status: offline
    Re: PIC16f777 Global Variable Not Updating 2019/11/22 09:52:07 (permalink)
    0
    That fixed it! I relocated the contents of menu() into main with a while loop. Can't believe I didn't think of that. Also I have changed all unnecessary pointers, you can rest easy 1and0.
    Thank you both so much, I really appreciate your help!
    Best Regards
    #9
    Jump to:
    © 2019 APG vNext Commercial Version 4.5