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.
"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.
View all questions in Keil forum