Newbie’s guide into programming - 00000001 - Programs and programming languages
Let’s start with a basic understanding of what program actually is. Program is a set of instructions for microcontroller to follow. A particular program is usually written for microcontroller to perform particular task. This leads us to next important statement:
You should be certain, at least on general level, what you want your robot to do.
That means that statement like “I want my robot to behave cool” has no meaning in terms of programming. You should know how exactly it should behave. So, instead of “I want my robot to behave cool” you should come up with something like “My robot should blink eyes and make sounds like R2D2”. This last statement is almost ready to be rewritten. Yes, you probably suspected that microcontroller will not understand English, and you had probably heard something about programming languages. All programming languages could be divided in two major groups: low-level languages and high-level languages. Here I will refer to the history of computer programming languages as microcontroller is a computer on its own. So, each computer has native language. It is a set of all instructions it can understand and perform. All these instructions are written in binary and would look like:
…000010 00110 00000 00000 10000 000000…
We will discuss the binary number system later in this chapter. Native languages in general are also called machine languages. Machine language is a low level language and very difficult to program in. Long time ago all computers were programmed in machine language. As you can imagine, programming was very hard and programs were far from readable. Even the author of a program could not understand his own code if he came back to it after some time. So, clever programmers thought “Why don’t we write a program that will allow us write other programs in more understandable way and compile it into native code?” This is how assembler language and first compiler were born. Assembler code looks something like:
start: mov al, 1 ;value to write to port
mov dx, 201h ;port number
out dx, al ;write to port
mov cx, 0F00h ;# of loops
And it is also a low level programming language. It is much easier than machine language but still very difficult. Then programmers decided to go even further and came up with high level languages like Basic and C and many others that look almost like English. Piece of code in C would look this way:
float earth_years , martian_years;
printf("Please enter the amount of earth years to convert.\n");
High level languages are much easier to learn and use. That is why we will use them in this book.
Earlier I have promised to talk about binary system. People use decimal system which has a base of 10 and 10 different symbols (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) to represent any number. Binary system has a base of two and, as you guessed, has only two symbols (0, 1). Let me try to describe how all it works. Say we are counting in decimal from zero. Easy, isn’t it? 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, what happens next? Aha we ran out of symbols and have to use more digits to represent next number: 10, 11, 12, …. 99, and again: 100. The same is with binary system the only difference is that we have only two symbols to play with. So, let’s try to count in binary: 0, 1,… aha, we already have to use next digit: 10, 11, and again: 100… See how it works? This should be enough for our needs so far. But if you feel like knowing more, feel free to visit wikipedia. Let’s have some more information before we move to next chapter. The binary digits 0 and 1 are called the bits. Eight bits make up a byte. If you count how far you can go with 8 bits, you will find out that we have 256 different combinations which can represent numbers from 0 to 255. Please remember these numbers. They will become handy very soon.
Here are few links to Wikipedia content:
See other chapters of this series here.