Re: 8051 and a C switch statement



On Sun, 30 Dec 2007 18:45:20 +0100, Martin Griffith
<mart_in_medina@xxxxxxxx> wrote:

On Sun, 30 Dec 2007 19:18:32 +0200, in comp.arch.embedded Anton
Erasmus <nobody@xxxxxxxxxxxxxxxx> wrote:

On Fri, 28 Dec 2007 23:39:04 +0100, Martin Griffith
<mart_in_medina@xxxxxxxx> wrote:

Got a 4k8 serial biphase signal being decoded by a at85c52 with an
external non retrig monostable (hc221), and I'm running out of time
occasionally waiting for the generic 2*8 LCD to clear it's busy flag.
The LCD is fed with the info from the incoming biphase

To reduce the "reaction time" I could use a long switch statement on
the signal's clock interrupt bitcount from the sync word, 80 per
frame, do just a little bit each interrupt.

The real question is

" how big can I make the switch statement?"
Whats the limit?
I'm using the rasionance C compiler.

There can be another problem, depending on your compiler.
The older IAR C51 Compilers generated thread unsafe code for
a switch statement for more than about 3 items, hence if one used
big switch statements inside an interrupt routine and outside, the
code crashed. This was for IAR C51 V4 and V5. I do not know whether
this has been fixed, or if an option had been added to force the
compiler to generate thread safe code.

You can always split a big switch statement into multiple smaller
ones. This is a bit slower than one big one.

i.e.
switch (var) /* Check bottom bits */
{
case 0:
break;


case maxsize:
break;
default:
switch(var)
{
case maxsize+1:
break;
}


}


Regards
Anton Erasmus
Ah, there was I happily writing a 16 case switch.

Generally speaking, should you keep code in a case statement short?

If it is in an interrupt routine, then it normally should be short,
not because it is in a switch statement, but because it is executed
in an interrupt routine.
I personally keep the segments in a switch statement reasonably short.
I find that the code becomes difficult to maintain if one has huge
sections in each case statement, especially if it is a switch
statement with many cases. Also the switch statement is normally
for decision making, while the contents of the case blocks are for
controlling/action. I find it good practice to split these concepts.

[snipped]

Regards
Anton Erasmus
.



Relevant Pages

  • Re: hi i am a girl who were trying a bit on C can someone help?
    ... You may want to take a look at the compiler warnings that get issued. ... your code, with those tabs set at ... >empty, empty); ... You may want to convert the if/else ladder into a switch statement. ...
    (comp.lang.c)
  • Re: Handling 5000 different functionalities - Optimised way in C
    ... the jump table that's typically used to implement a switch statement. ... Perhaps that's not technically a jump table, but AFAIK it's the closest thing* one can implement in C, therefore the name is descriptive enough. ... I agree that the compiler _could_ generate something faster, by jumping to code inside the same function instead of calling another function, but that shouldn't be a noticeable difference. ... that assumes the table is sorted, which may not always be true, my first attempt was obviously wrong and I decided getting the point across was more important than figuring out how to code a binary search on a non-power-of-two-sized array, and a binary search may have a larger I-cache footprint, bomb the branch predictor, and/or confuse the data prefetcher.) ...
    (comp.lang.c)
  • Re: [OT] Switch question
    ... On Sat, 21 Feb 2004, Sean Kenwrick wrote: ... > a byte code interpreter I am working on, and I am wondering if I should take ... Each compiler can do it differently. ... If a switch statement used branching (a parallel ...
    (comp.lang.c)
  • Re: why doesnt this work? final assignments and switches
    ... whatever, however, it seems that the compiler only performs a very ... This should be sufficient to work a switch statement - I believe. ... Sure, there are work arounds, but since java offered modest developer ... conveniences to sdk1.5 - how about this one which helps the "hand coder". ...
    (comp.lang.java.programmer)
  • Re: Sort of Gray code to binary converter
    ... >>> as there exists no assembly equivalent for a switch statement, ... >machine code, I can too. ... Frank's point is that there is no exact equivalent, so the compiler at ...
    (sci.electronics.design)