Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
12/15/04 08:37
Read: times


 
#83121 - code
Responding to: ???'s previous message
typedef unsigned char tChar;
typedef unsigned int  tInt;
typedef unsigned long tLong;
Using your own typedefs like this, rather than just char, int, etc, is a good idea as it allows you to "encapsulate" compiler-dependencies.
However, your names are not very informative; "tChar" tells you nothing explicitly about the size or signed-ness of the type.
Therefore, I prefer names like:
U8 - Unsigned, 8-bits;
S8 - Signed, 8-bits;
U16 - Unsigned, 16-bits;
S16 - Signed, 8-bits;
etc, etc.
These explicitly & succinctly indicate both size & signed-ness.
You may prefer something slightly less terse, but you get the idea...
void msDelay(tInt msec)
{

tInt cntr_1, cntr_2;

for (cntr_1 = 0; cntr_1 <= msec; ++cntr_1 )
    {
      for ( cntr_2 = 0; cntr_2 <= 5000; ++cntr_2);
    }
}
You should define cntr_1, cntr_2 as volatile - otherwise the compiler may completely optimise this code away!
Also, beware that you have absolutely no guarantee whatsoever as to what specific machine code will be generated - so any change to build options, and certainly a compiler update, could very easily change the timing - possibly drastically.
Therefore, you need to keep an eye on the generated assembler.
However, since you need to keep an eye on the generated assembler, you might as well write it in assembler in the first place!
This is definitely one of those places where assembler is better than 'C'!


List of 22 messages in thread
TopicAuthorDate
Help LCD--89C51 using KEIL            01/01/70 00:00      
   RE            01/01/70 00:00      
   Keil?            01/01/70 00:00      
      Keil App Notes            01/01/70 00:00      
   Assembler programmer learning C?            01/01/70 00:00      
   So what happened to the USB, then?            01/01/70 00:00      
   The wanted code...            01/01/70 00:00      
      code            01/01/70 00:00      
         Tips and Tricks.....Andy            01/01/70 00:00      
            volatile.            01/01/70 00:00      
               Not so volatile ?            01/01/70 00:00      
                  optimiser            01/01/70 00:00      
                     Volatile warning?            01/01/70 00:00      
                        not here            01/01/70 00:00      
                        re: volatile            01/01/70 00:00      
                           volatile            01/01/70 00:00      
      Another comment - symbolic names            01/01/70 00:00      
   c code for lcd            01/01/70 00:00      
      code            01/01/70 00:00      
         symbolic Port names            01/01/70 00:00      
   There's Gratitude for you! :-(            01/01/70 00:00      
   A better method in C            01/01/70 00:00      

Back to Subject List