switch problems...

This is a discussion on switch problems... within the C Programming forums, part of the General Programming Boards category; can anyone find an error in here that would prevent it from switching states? Code: static unsigned char state; static ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    7

    switch problems...

    can anyone find an error in here that would prevent it from switching states?

    Code:
    static unsigned char state;
       static unsigned char countdown;
       TMR3CN &= ~(0x80);                     // clear TF3
       countdown = countdown-1;
       if (countdown == 0){
       		state = state + 1;
    		}
    
       if (state > 4){
    		state = 1;
    		}
       switch (state){
       	case 1:
       		P1 = 0x44;
    		countdown = 100;
    		break;
       	case 2:
    		P1 = 0x06;
    		countdown = 16;
    		break;
       	case 3:
    		P1 = 0x11;
    		countdown = 100;
    		break;
     	case 4:
    		P1 = 0x09;
    		countdown = 16;
    		break;
     	}

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    initial value of countdown is 0 because globals and static variables are all initialized to 0 by default. decrementing the unsigned will cause data underflow, an undefined behavior. Most compilers will do data wrap-around to 255 but that is not guarenteed.

    Short answer: the value of countdown = 255.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Ha ha... you two might communicate better if you stopped deleting posts AFTER the other person read and replied to them.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    but i cant just have
    Code:
    countdown = 255;
    countdown = countdown-1;
    will it need to be set elsewhere? like in the switch statement?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by sean_mackrory
    Ha ha... you two might communicate better if you stopped deleting posts AFTER the other person read and replied to them.

    Yea, I know that is a problem, there there is no way to know if the post has already been read. So when I post something that I later discover to be untrue I just delete it, hoping nobody notices

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by rexnprogress
    but i cant just have
    Code:
    countdown = 255;
    countdown = countdown-1;
    will it need to be set elsewhere? like in the switch statement?

    First you need to decide what value you want countdown to have initiallly, what is the minimum value (apparently 0) and what is its maximum value (is 255 ok?) Set the initial value when it is declared
    Code:
      static unsigned char countdown  = 1;

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    maybe a prob with my case wording, as i havent associated it with "state", so there may be no connection between the two

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  2. switch() inside while()
    By Bleech in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 03:34 AM
  3. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM
  4. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  5. switch and string copy problems ?
    By Neildadon in forum C Programming
    Replies: 5
    Last Post: 06-12-2002, 08:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21