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
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.
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!
I mainly used a language reference manual (definitions, not attempts at sample code) and lots and lots of downloaded examples when learning C.
Since it wasn't embedded programming, I looked for really large programs. If they were easy to understand, I tried to duplicate the constructs and way of writing. If they where not easy to read, I ignored the style but looked for intresting constructs.
Reading code is a very good way to learn. But it takes lots of code, until our pattern-matching brain starts to be able to detect and categorize what is good and what isn't good.
One bad thing with books is that lots of book authors don't seem to be good programmers. They know how to produce many pages of text, but can't present elegant code. And are so focused on describing a specific keyword or specific function call that they write "simplified" code that doesn't really do anything. All the time missing all the extras needed for use in an commercial-grade environment.
I have this C/C++ pocket reference with definitions of syntax and examples. I could probably do this counter in just C/C++ without any problem. My biggest problem is understanding how to use C/C++ to program the micro controller to do what I wanted to do. Which I learned I needed 8051 headers, and from the way it looks, it seems like the header is a file with all the 8051 registers declared into C variables per the addresses. Hence why I tried to use ACC to do my counter calculation like I did with embedded. But since I can't use ACC directly, I wonder why it is in the header file. Something I need to do some study on.
As for classes, they can only teach so much. I have not yet taken a class where they teaches me how to program in C to interact with micro controller. Neither are books available.. Anyone knows? Any recommendations?
I tried to use ACC to do my counter calculation like I did with embedded. But since I can't use ACC directly, I wonder why it is in the header file.
For writing in-line assembly.
Jon
So what's all this, then: http://www.keil.com/books
"Neither are books available."
there are so many (too many?) of them out there.
but that's 2ndary. programming a micro controller is no different from programming a regular pc/large computer in that you need to have thought out a structure to your code (aka how are you going to have the computer solve a problem?), and then program individual pieces to actually achieve that goal.
the distinction here is that with a mcu, you are communicating with the outside world solely with their pins: you create certain patterns on those pins, across time or across multiple pins.
it is that simple - that's why embedded programmers don't make much money.