Thread: Closing / stopping a switch statement

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    4

    Closing / stopping a switch statement

    I am currently writing a piece of code for a PIC18F1229 microcontroller using the software MXLAB v3.45 .
    I have been tasked with flashing the middle segment at a speed of 1 flash per second and to stop it after 4 seconds using a switch stamement. I have managed to get the middle segment to flash at one flash per second but I am unsure how to close the switch statement. Would this be with a else statement and if so else what? Else false as all my other values are "true" . I have trialled using the "end" statement which flags an error as undefined indentifier. would this be a local indentifier or global. Please see my code below and perhaps make you might see where I have made a simple mistae.

    This is my timer loop :
    insert
    Code:
    WriteTimer0(0);
     INTCONbits.TMR0IF=0;
     while(TRUE) //Loop forever
     {
            Display(ucCount);
      ucCount++;
      if(ucCount>7) ucCount=0;
      while (INTCONbits.TMR0IF==0);
      INTCONbits.TMR0IF=0;
            WriteTimer0(46004);  
            //1/10Mhz x 4 x 64 = 25.6 us. 500000/25.6 = 19531. 65545 - 19531 = 46004
            
        }
            
        CloseTimer0();
    this is my switch statement:
    insert
    Code:
    {
     switch (ucHexDigit)      
     {
      case 0:
       LATB=0b00101011;
       LATA=0b11111111;
       break;
                
            case 1:
       LATB=0b11111111;
       LATA=0b11111111;
       break;
                
            case 2:
       LATB=0b00101011;
       LATA=0b11111111;
       break;
                
            case 3:
       LATB=0b11111111;
       LATA=0b11111111;
       break;
                
            case 4:
       LATB=0b00101011;
       LATA=0b11111111;
       break;
                
            case 5:
       LATB=0b11111111;
       LATA=0b11111111;
       break;
                
            case 6:
       LATB=0b00101011;
       LATA=0b11111111;
       break;
                
            case 7:
       LATB=0b11111111;
       LATA=0b11111111;
       break;
                              
      default: //display turned off
       LATB=0b11111111;
       LATA=0b11111111;
                break;     
                        
     }
    }
    As can be seen above The last statement deault turns all LEDS off and after this I would like to stop the switch statement from looping around again.

    Any advice would be appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of writing while(TRUE), write while(display_turned_on), which is set to 1 before the loop, and then in the default case of the switch set display_turned_on to 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    This is not a very good switch. Firstly, LATA is set to the same thing in every branch. Secondly, LATB is assigned just two different values based on whether or not it's even. Also, binary constants are not standard. We would usually use hex.
    Code:
    ucHexDigit = get_it();  // "get_it" represents whatever needs to be done to set ucHexDigit
    while (ucHexDigit < 8) {  // I'm assuming "uc" means unsigned char so it can't be < 0
        LATB = ucHexDigit % 2 == 0 ? 0x2B : 0xFF;
        LATA = 0xFF;
        ucHexDigit = get_it();
    }
    Or if your method of setting ucHexDigit allows it:
    Code:
    while ((ucHexDigit = get_it()) < 8) {
        LATB = ucHexDigit % 2 == 0 ? 0x2B : 0xFF;
        LATA = 0xFF;
    }

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You can create a label and use goto to jump to it. Some consider this bad style, but sometimes it is very useful.

    Code:
    while(1)
    {
        ...
        {
            switch (ucHexDigit)
            {
            case 0:
                ...
                break;
            case 1:
                ...
                break;
            ...
            default:
                LATB = 0xff;
                LATA = 0xff;
                goto done;
            }
        }
    }
    
    done:
    /* Rest of code. */

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    4
    HI Laserlight

    I modified the code as suggested but I still have my LED continuously flashing

    while(DisplayTurnedOn) //Loop forever
    {
    Display(ucCount);

    case 7:
    LATB=0b11111111;
    LATA=0b11111111;
    break;

    default: //display turned off
    //LATB=0b11111111;
    //LATA=0b11111111;
    DisplayTurnedOff ;
    break;

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    4
    Quote Originally Posted by c99tutorial View Post
    You can create a label and use goto to jump to it. Some consider this bad style, but sometimes it is very useful.

    Code:
    while(1)
    {
        ...
        {
            switch (ucHexDigit)
            {
            case 0:
                ...
                break;
            case 1:
                ...
                break;
            ...
            default:
                LATB = 0xff;
                LATA = 0xff;
                goto done;
            }
        }
    }
    
    done:
    /* Rest of code. */
    Hi C99 I have tried doing the above but when you have declared "done" have you done this as a global or local void? as while 1 in my case is true but if i create a done void should this allow my LED to only flash 4 times?

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by lyndonrobertson View Post
    Hi C99 I have tried doing the above but when you have declared "done" have you done this as a global or local void?

    The label should be local to a function. It does not have a type, it is just a label. e.g.

    Code:
    int my_function()
    {
       ...
       goto some_label;
       ...
    
    some_label:
       ...
    }
    Using labels means you understand the logic already and are taking full control of the code. If you don't know how many times your body will be executed, don't use goto.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  2. Switch statement
    By Programmer_P in forum C++ Programming
    Replies: 9
    Last Post: 07-24-2010, 03:11 AM
  3. switch statement
    By crash88 in forum C Programming
    Replies: 4
    Last Post: 06-29-2006, 12:49 AM
  4. help with switch statement?
    By Kristina in forum C++ Programming
    Replies: 10
    Last Post: 05-14-2002, 11:25 AM
  5. Closing & Stopping Port Connections
    By (TNT) in forum Windows Programming
    Replies: 1
    Last Post: 10-21-2001, 11:31 PM

Tags for this Thread