Showing posts with label FREE ARTICLES. Show all posts
Showing posts with label FREE ARTICLES. Show all posts

Computer/Microcontroller Programming Primer-Part 1

This article,prepared with inputs from some web articles, covers basic concepts of computer/microcontroller programming, going under the presumption that the reader is a complete novice.

A computer program is a sequence of step-by-step instructions for the computer to follow. The computer will do exactly what you tell it to do, no more no less. The computer only knows what's in the program, not what you intended. Thus the origin of the phrase, "Garbage in, garbage out".

The set of valid instructions comes from the particular programming language used. There are many languages, including C, C++, Java, Ada, Lisp, Fortran, Basic, Pascal, Perl, and a thousand others.The Arduino  uses a simplified variation of the C programming language.

For any programming language, the instructions must be entered in a specific syntax in order for the computer to interpret them properly. Typically, the interpretation is a two step process. A compiler takes the language specific text you enter for the program and converts it into a machine readable form that is downloaded into the processor. When the program executes, the processor executes the machine code line by line.
Fundamentals of Programming Languages

All sequential programming languages have four categories of instructions. First are operation commands that evaluate an expression, perform arithmetic, toggle states of I/O lines, and many other operations. Second are jump commands that cause the program to jump immediately toanother part of the program that is tagged with a label. Jumps are one way to break out of the normal line-by-line processing mode. For example, if you want a program to repeat over and over without stopping, have the last line of the program be a jump command that takes the program back to its first line. Third are branch commands that evaluate a condition and jump if the condition is true. For example, you might want to jump only if a number is greater than zero.Or, you might want to jump only if the state of an i/o line is low. Fourth are loop commands that repeat a section of code a specified number of times. For example, with a loop you can have a light flash on and off exactly six times.Most programming languages contain a relatively small number of commands. The complexity of computers comes from combining and repeating the instructions several million times a second.

Here's a generic program.

1. Do this

2. Do that

3. Jump to instruction 6

4. Do the other thing

5. All done, sleep

6. If switch closed, do that thing you do

7. Jump to instruction 4

The computer will execute this line by line. The art of programming is simply a matter of translating your intent into a sequence of instructions that match. Here is an example of a for loop command followed by a branch command that uses an IF statement.

for (i=0;i<6,i++) {

instructions

}

if (j > 4) goto label

instructions

The commands inside the loop will be repeated six times. Following this, if the value of the variable j is greater than 4, the program will skip to the instruction tagged with the specified label, and if not, the line following the if statement will be executed. In addition to the basic commands, languages have the ability to call functions which are independent sections of code that perform a specific task. Functions are a way of calling a section of code from a number of different places in the program and then returning from that section to the line that follows the calling line.

Here's one example

apples();

instructions

apples();

more instructions

void apples() {

instructions

}

The function apples is everything between the set of braces that follows “apples()”. When the function completes, the program jumps back to the line following the line that called the function.

Digital Numbers

When working with a microcontroller that interacts with the real world, you have to dig a little below the surface to understand numbering systems and data sizes. A binary (base 2) variable has two states, off and on, or 0 and 1, or low and high. At their core, all computers work in binary since their internal transistors can only be off or on and nothing between. Numbers are built up from many digits of binary numbers, in much the same way that in the base 10 system we create numbers greater than 9 by using multiple digits. A bit is one binary digit that can take on values of either 0 or 1. A byte is a number comprised of 8 bits, or 8 binary digits. By convention, the bits that make up a byte are labeled right to left with bit 0 being the rightmost.

In a computer, variables are used to store numbers. A bit variable can take on two values, 0 and 1, and is typically used as a true/false flag in a program. A byte variable can take on integer values 0-255 decimal while a 16-bit word variable can take on integer values 0-65,535. Variables can be either signed (positive and negative values) or unsigned (positive only).

(To be continued)