1. You can now buy finished microcontroller project from us, Check out the Store for the complete list of projects.
  2. Need a custom project, Send us some details about your project. So that we can quote the price for it.

A very basic example of writing C code for the 8051

A very basic example of writing C code for the 8051

  1. Binu
    Code:
    Code (Text):
    1. /*************************************************************************
    2. * basic.c - The basics of writing C code for the 8051 using the Keil
    3. *  development environment.  In this case, a simple program will be
    4. *  constructed to make a binary counter on Port 0.
    5. */
    6.  
    7. /*
    8. * As always with C, the included header files should come first.  Most
    9. * every project for the 8051 will want to include the file reg51.h.  This
    10. * header file contains the mapping of registers to names, such as setting
    11. * P0 (port 0) to address 0x80.  This allows the coder to use the keyword
    12. * "P0" in their code whenever they wish to access Port 0.  For the complete
    13. * list of registers named, view the file.
    14. */
    15.  
    16. #include
    17.  
    18. /*
    19. * Other header files may be included after reg51.h, including any headers
    20. * created by the user.
    21. */
    22.  
    23. /*
    24. * The C program starts with function main().  In the case of a program
    25. * written using multiple .c files, main can only occur in one of them.
    26. * Unlike in programming user applications for a standard computer, the
    27. * main() function in a Keil program for the 8051 takes no inputs and
    28. * returns no output, thus the declaration has implied void types.
    29. */
    30.  
    31. /*************************************************************************
    32. * main - Program entry point
    33. *
    34. * INPUT: N/A
    35. * RETURNS: N/A
    36. */
    37.  
    38. main()
    39. {
    40.     unsigned int i;        /* will be used for a delay loop */
    41.  
    42.     /* First, Port 0 will be initialized to zero */
    43.  
    44.     P0 = 0;
    45.  
    46.     /*
    47.       * Now the counter loop begins.  Because this program is intended
    48.       * to run in an embedded system with no user interaction, and will
    49.       * run forever, the rest of the program is placed in a non-exiting
    50.       * while() loop.
    51.       */
    52.  
    53.     while (1==1)
    54.     {
    55.           /*
    56.                   * This is a very unpredictable method of implementing a delay
    57.                   * loop, but remains the simplest.  More reliable techniques
    58.                   * can be done using the using the built-in timers.  In this
    59.                   * example, though, the for() loop below will run through 60000
    60.                   * iterations before continuing on to the next instruction.
    61.                   * The amount of time required for this loop varies with the
    62.                   * clock frequency and compiler used.
    63.                   */
    64.  
    65.           for (i = 0; i < 60000; i++) {;}
    66.  
    67.         /* Increment Port 0 */
    68.  
    69.         P0 = P0 + 1;
    70.     }
    71.  
    72. }
Loading...