Thread: (Beginner) Nested C for PIC MCU - Problem displaying 0

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    2

    (Beginner) Nested C for PIC MCU - Problem displaying 0

    Hello Everyone

    (Please excuse if this is in a wrong section)

    So I have strted my journey with C programming for PIC.
    I got stuck when I want to display a 0 when counting down from 9 in a while loop

    I have an array of numbers that I send to display ports of my MCU and when I count from 0 to 9 it is all good but not when counting down.

    I think the problem is somewhere in here:

    Code:
     if (digit<0) digit=9;
    but I do not know how to correct it.

    I want to display 0 and then start from 9 again.

    Full code:

    Code:
    unsigned char const SEGMENT_MAP[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
    
    void main(void) {
         TRISC = 0;                   //Set PortC to all outputs
         TRISBbits.RB6=1;             //Set Port B7 as an input
         OSCCON = 0X76;                  //SET fOR INTERNAL OSCCILATOR
         char digit=9;
         
      while(1){
    
          if (SW1)       //Check if switch SW1 is closed
          {
            for(int c=0; c<=20; c++)__delay_ms(5); //debouncing wait for 100ms
            
             if (SW1)       //Check if switch SW1 is still closed
             {
            PORTC = (SEGMENT_MAP[digit]);
    
       for (int count=0; count<20; count++) __delay_ms(50); //Delay 1 second
            
       digit--;
       if (digit<0) digit=9;
       
                  }
          }
          else
          {
            PORTC = (SEGMENT_MAP[digit]);
          }
        }
    }
    Thank you all for your help and your expertise

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    char might be signed or unsigned (the result of digit-- is undefined behaviour if its value is 0)



    The rest looks like you've been trying random changes hoping they might work... dunno... messy though

    Changing
    Code:
    char digit = 9;
    to

    Code:
    int digit = 9;
    might work, but who knows

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    2
    HODOR! HODOR! HODOR! HODOR!

    It Works !!!! Such simple beginners things!
    Massive thank you HODOR! As always you carry others to victory
    Wasted almost 3 days myself on it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested if Statement problem
    By Cochise in forum C Programming
    Replies: 3
    Last Post: 08-03-2016, 02:19 AM
  2. Displaying prime numbers using nested loops?
    By Tech2011 in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2015, 07:25 PM
  3. Beginner. Help with displaying highest and lowest numbers
    By SkywardTaco in forum C++ Programming
    Replies: 9
    Last Post: 04-27-2015, 12:34 PM
  4. Displaying a table using nested loops
    By leviterande in forum C Programming
    Replies: 13
    Last Post: 09-29-2009, 04:42 PM
  5. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 10:16 AM

Tags for this Thread