This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

8051 C complier

Hello all,

I have been programming the 8051 chip using assembly for small programs and applications such as A to D converter, alarm clock, etc. In which I have learned that it is a great pain to do more complicated projects with assembly, as the number of lines of codes get larger and harder to track.

I have downloaded the uVision 4 C complier to try to replace assembly, but I'm having difficulties learning how to use it. I have had C/C++ programming before, but I must admit it has been a while since I used it.

Are there any tutorials on programming the 8051 using C?

Also, I have been trying to duplicate my counter project (counting from 0 to 99 using assembly code) with C. I have it count from 0 to 9, but I could not figure out how to do decimal adjust using C. Any idea? In assembly the code is simply DA

Thanks all,
Woozie

  • Note that uVision is not a compiler!

    uVision is an IDE; ie, it is a user-interface to the compiler - and all the other tools.

    See the diagram here for an illustration of how all the tools fit together: http://www.keil.com/c51/devproc.asp

    uVision overview here: http://www.keil.com/uvision/

    "I have had C/C++ programming before"

    What, exactly, do you mean by that?

    Do you mean you've just "dabbled", or are you actually a proficient 'C' user?

    If you're not a proficient 'C' user, then learning the 'C' programming language must be your first step - see: blog.antronics.co.uk/.../

    Once you've learned the language, then you need to study the Keil Manuals for the implementation-specific details - in particular, the language extensions provided to access the 8051's particular features:

    http://www.keil.com/support/man/docs/c51/c51_extensions.htm

    "Are there any tutorials on programming the 8051 using C?"

    Have you spent time exploring the Keil website?

    Have you askied your tool supplier what training they can offer you - or if they can recommend local training providers...?

    http://www.keil.com/books/

  • It is a common mistake to try to write 'C' the same way you wrote assembly - ie, to treat 'C' as just a glorified assembler.

    "I could not figure out how to do decimal adjust using C"

    'C' is a high-level language.
    The whole point of high-level lamguage (HLL) is that you do not mess about with the low-level machine details like that!

    I think this confirms that what you really need to do first is to spend time learning the 'C' programming language.

  • In C, you don't have the concept of BCD. So no decimal adjust. You either count 0..99 and then handle splitting into two digits when you present the information.

    Or you use two variables and let the least significant variable count 0..9 and then explicitly increment the most significant variable whenever the low digit would overflow.

    Or you manually implement BCD logic, where you look at the low nibble (bit-and) and decide if it needs to be reset to zero and the high nibble incremented (+ 0x10). You obviously also need to make sure that the high nibble doesn't get larger than 9, i.e. if byte gets larger than 0x99 you need to clear it again.

    As Andy notes, C is a high-level (or at least reasonably high level) language, so you just should not try any one-to-one between assembler and C. You are much better off by letting the C variables store numbers without any BCD twist, and then handle the presentation of the numbers separately - i.e. if you want them presented on 7-segment digits, on serial port or LEDs, and if you want presentation in decimal, binary, octal, hexadecimal or whatever.

    For the majority of programs, the speed needed to convert from a binary number into the presentable digits, is not an issue. So it would be logical to just use a C variable of a suitable type to fit the largest numeric value you want your counter to reach.

  • Thanks for all the replies.

    I did the code to make it count from 0 to 99, but it counts in Hex, where it went from 09 to 0F, and it does not display the value in the 7-Seg display.

    The C variables I have been using are the 8051 Header file, where it labels P0,P1,P2 etc.. And it got me into thinking I'm programming it in assembly. Thus I was looking for the DA command.

  • I mistaken uVision as compiler is because I use it to create the hex file I need and program it into the chip using another tool.

    I suppose I would consider myself beginner of C programming; I know the basic of it, variables, loops, etc.

    After I posted this thread, I found a page in this site with 8051 tutorial, I will be looking at that for a while, as well as getting more help in this forum.

    No, I don't have training from tool supplier, I downloaded the free version of uVision.

  • "I mistaken uVision as compiler is because I use it to create the hex file"

    Do you now understand that it's not uVision that does this?
    uVision simply coordinates all the other tools - compiler, assmebler, linker, object converter - to do all the necessary processes in the appropriate order.

    "I did the code to make it count from 0 to 99, but it counts in Hex"

    You have either done it wrong, or misunderstood (or both).

    Counting is (or should be) about numeric values - it is independent of the representation in decimal, octal, binary hex, riman numerals, or whatever.

    "it went from 09 to 0F"

    That's a bug, then!

    "it does not display the value in the 7-Seg display"

    Driving the display is (or should be) entirely separate from the couting.

    "The C variables I have been using are the 8051 Header file"

    That does not make sense

    "it labels P0,P1,P2 etc.."

    Obviously any 8051 program - whether in 'C' or assembly or any other languare - is going to need some way to access the ports, etc!

    "I found a page in this site with 8051 tutorial"

    I thought you said you were already an assembly programmer - so you shouldn't need a tutorial on the 8051.
    What you need is a course in 'C' programming!

    "I don't have training from tool supplier"

    I strongly recommend that you should get some training!
    This will save you from wasting lots more time with more false assumptions & misinterpretations.

    It needn't be from a tool supplier - but the tool supplier should certainly be able to recommend training providers.

  • "The whole point of high-level lamguage (HLL) is that you do not mess about with the low-level machine details like that!"

    agreed.

    but that can be incredibly difficult to understand / implement even for seasoned embedded programmers.

  • I have tried using while-statement to increment my variable's value as long as it is less than 99. Then output the variable to P1. It counts 0-9, then A-F.

    What I meant with the variables is I used the declared variables in the header file, instead of creating variables. So instead of variable Count, I used ACC.

    The 8051 tutorial I found in here is the 8051 C programming tutorial....

  • Through all the replies and readings here. Perhaps I have misunderstood certain things.

    So, If I want to program the 8051, should I be using the variables and functions that only works for the 8051?? When I did C/C++ programming, all I ever done was wrote a program that does something, and simulated it. It was never programed/embedded into a chip. So to me, it kinda make sense to write the code only specific to the chip. Correct?

    If that's true, then the hardware configuration that interface with the 8051 should also be the same as if I were to do it in assembly?

    Now I want the 8051 to drive the 4511BCD chip to drive the 7-segment displays, I would have the ACC variable to increment every second and output to port 1.

    Before I program it into the chip, I debugged it and ran in uVision. I opened port 1 and monitored the count. It went from 0 to 9, then A to F on the single digit. Once I program it in the chip, it does the same thing, except the 4511BCD did not output the A to F values in the 7-segment. Thus it leads me into the decimal adjust question, because that's the only thing left out when I compared with my assembly code.

  • ACC is not a variable - it is the CPU's ACC register.

    The compiler will use this for its own purposes - so you should not use it directly in your 'C' programs.

    You really, really do need to get some proper training!

  • OK, here goes my crack at this.

    Your program should look something like the following:

    #include <8051.h>
    
    void main (void)
    {
    unsigned char i;               // The loop counter
    
    while (1)                      // Most all embedded programs require this
      {
      for (i=0; i<100; i++)        // Loop from 0 to 99
        {
        display_write(i);          // Send value of i to the display
        delay_ms (250);            // Delay for 250 milliseconds
        }
      }
    }
    

    display_write is a function you create to send the number to the 7-segment display. delay_ms is a function you create that delays the specified number of ms--so the display doesn't count too fast to read.

    The display_write function MIGHT look like this:

    void display_write (
      unsigned char x)             // Value to write to display
    {
    // Assumptions:  The 7-seg display is a 2-digit display wired to P1
    //               The LSN is the ones digit
    //               The MSN is the tens digit
    
    P1 = (x % 10) | (((x / 10) % 10) << 4);
    }
    

    Hopefully, this will help you get started.

    Jon

  • Jon,

    I understand the first part of the code, but I'm not sure about the second part where P1 = (x % 10) | (((x / 10) % 10) << 4);

    Would you mind explain it to me?

    Andrew,

    Yes, I figured I couldn't use ACC directly; I used it to trial-and-error.


  • I'm not sure about the second part where P1 = (x % 10) | (((x / 10) % 10) << 4);

    That's the part that's actually C code. If you don't understand that, it's time to break out a C text book and learn about the C programming language. If you don't understand that, you don't know C. It seems that's what several other posters have been trying to tell you.

    Jon

  • I know, I never denied I don't need to learn C, I also didn't say I'm a pro at C. Hence I'm here to ask for help. If I knew the answers to my questions are "you don't know C, learn it", then I would probably just went straight there to begin with, and answer the questions myself sooner or later. Don't get me wrong, I'm not saying all these replies are not helpful, it really strike me hard to make myself think how little do I know about C versus embedded. Although I would be much more appreciated if anyone could give me a hand and guide me to the right direction while pointing out my problems, than just pointing out my problems without additional help to correct my problem.

    I really appreciated your example code, I will run your code and see what happens while I will learn C to stop embarrassing myself.

    Thanks.

  • "I would be much more appreciated if anyone could give me a hand and guide me to the right direction while pointing out my problems"

    But your fundamental problem is precisely that you need to spend time learning the 'C' language!

    That is absolutely fundamental. We cannot build anything further until you have laid that foundation.

    You've just shown that yourself: Jon gave you some 'C' code, but you couldn't uderstand it - because you haven't got that basic foundation to enable you to understand 'C' code!