Programming Microcontrollers In C:Internet Web Sites of Microcontroller Compilers

Internet Web Sites of Microcontroller Compilers

The amount of microcontroller software available on the Internet is huge and there are many different example programs. Internet web sites of some popular 8051 family microcontroller compilers and other useful sites are given below.

Programming-Microcontrollers-in-C-00[8]

Programming-Microcontrollers-in-C-00[7]

Further Reading

The following books and reference manuals are useful in learning to program in C.

Programming-Microcontrollers-in-C-00[5]

 

Programming Microcontrollers In C:Data Types

Data Types

The C51 compiler provides the standard C data types and in addition several extended data types are offered to support the 8051 microcontroller family. Table 2.1 lists the available data types (see the C51 reference manual for more information).

Some of the data types are described below in more detail.

Programming-Microcontrollers-in-C-00[16]

signed char/unsigned char

These data types are as in standard C language and are used todeclare signed and unsigned character variables. Each character variable is 1 byte long (8 bits). Signed character variables range from -128 to +127; unsigned character variables range from 0 to 255.

Programming-Microcontrollers-in-C-00[10]

Programming-Microcontrollers-in-C-00[15]

signed short/unsigned short

These data types are as in standard C language and are used todeclare signed and unsigned short variables. Each short variable is 2 bytes long (16 bits). Signed short variables range from -32 768 to +32 767 and unsigned short

variables range from 0 to 65 535.

Programming-Microcontrollers-in-C-00[4]

signed int/unsigned int

As in the standard C language, these data types are used todeclare signed and

unsigned integer variables. Integer variables are 2 bytes long (16 bits). Signed integers range from -32 768 to +32 767 and unsigned integers range from 0 to

65 535.

Example:

Programming-Microcontrollers-in-C-00[5]

sfr

This data type is similar tosbit but is used todeclare 8-bit variables.

Example:

Programming-Microcontrollers-in-C-00[8]

 

Microcomputer systems:Timer/Counters

Timer/Counters

The 8051 and AT89C2051 contain two timer/counters known as timer/counter 0 and timer/counter 1 (larger members of the 8051 family contain more timers/ counters). These timer/counters can be operated in several different modes depending upon the programming of two registers TCON and TMOD, as shown in Tables 1.2 and 1.3. These registers should be programmed before using any timer or counter facilities of the microcontroller.

Microcomputer-Systems0027_thumb

Microcomputer-Systems0028_thumb

For example, bit 4 of TCON is the counter 0 run control bit and setting this bit starts counter 0. TCON register is at address 88 (hex) and bits in this register can be accessed either by making reference to the address or by using compiler reserved names (e.g. TR0).

TMOD is the timer/counter mode control register. This register sets the operating modes of the two timer/counters as shown in Table 1.3. There are three operating modes, known as modes 0, 1, and 2. TMOD is not bit addressable and should be loaded by specifying all the 8 bits. For example, loading hexadecimal byte 01 into TMOD sets timer 0 into mode 1 which is a 16-bit timer and is turned on and off by bit TR0 of TCON. Also, timer 1 is set into mode 0 which is a 13-bit timer and is turned on and off by bit TR1 of TCON.

 

PROGRAMMING PIC MICROCONTROLLERS IN C:DELAYS IN C PROGRAMS

DELAYS IN C PROGRAMS

There are many real-time applications where the microcontroller is too fast and we need to slow it down to see, for example, the flashing of an LED. The PICC Lite compiler offers two types of delay functions: a microsecond delay function, and a millisecond delay function. The file <delay.c> must be included at the beginning of a program before these delay functions can be used. The function DelayUs(x ) is used to create delays in the range 1 to 255 μs. For example, the following function call generates a 200 μs delay:

DelayUs(200);

Similarly, function DelayMs(x ) is used to create delays in the range 1 to 255 ms. The following function call creates a 10 ms delay:

DelayMs(10);

Longer delays can be created by using the above delay functions in loops. These delay functions are by default based on the assumption that the microcontroller operates with a 4 MHz clock frequency. If the clock rate is different, then the new clock rate must be specified at the beginning of the program. For example, if the clock rate is 2 MHz, then the following lines must be included at the beginning of the program:

clip_image001#undef XTAL FREQ

clip_image002#define XTAL FREQ 2MHZ

 

PROGRAMMING PIC MICROCONTROLLERS IN C:ACCESSING THE EEPROM MEMORY

ACCESSING THE EEPROM MEMORY

clip_image001Some PIC microcontrollers (e.g. PIC16F84) have EEPROM memories which can be used to store nonvolatile data. PICC Lite provides instructions for reading and writing to this memory. The EEPROM WRITE command is used to write a data byte to the EEPROM memory. For example, the following command writes 0x2E to address 2 of the EEPROM memory:

Programming PIC Microcontrollers in C-0141clip_image002

clip_image001[1]Similarly, the EEPROM READ command is used to read a data byte from the EEPROM memory. For example, the following command reads a byte from address 5 of the EEPROM memory and loads it into the variable k:

Programming PIC Microcontrollers in C-0142clip_image003

INTERUPTS IN C PROGRAMS

Interrupts are very important in real-time programs. Most PIC microcontrollers offer internal and external interrupt facilities. Internal interrupts are usually timer-generated interrupts, while the external interrupts are triggered by activities on the I/O pins. When an interrupt occurs the processor jumps to the interrupt service routine where the interrupt is serviced. Medium-range microcontrollers (such as the PIC16F84) have only one ISR, which is at address 0x04 of the program memory. If interrupts from multiple sources are expected then the interrupting device can be found by inspecting the INTCON register bits.

clip_image004Interrupts can be used in PICC Lite programs in the form of special functions. When an interrupt occurs the program jumps to this special function where the interrupt is handled. ISR function must have the name interrupt followed by a user-selected function name, and the function must not return any value and must not have any arguments. For example, an ISR function named my int is given below:

Programming PIC Microcontrollers in C-0143

Interrupts must be enabled before they can be recognized by the microcontroller. The ei() and di() instructions enable and disable global interrupts, respectively. Additionally, the interrupt control bit of each interrupt source must be enabled in the appropriate SFR register (e.g. T0IE bit in INTCON must be set to 1 to enable timer interrupts).

The PIC microcontroller only saves the program counter on stack when an interrupt occurs. The PICC Lite compiler determines which registers and objects are used by the interrupt function and saves these appropriately.

 

PROGRAMMING PIC MICROCONTROLLERS IN C:POINTERS IN C

POINTERS IN C

Pointer are widely used in C programs. A pointer is a variable which stores the address of another variable. A pointer is declared by preceding it with the ‘*’ character. For example, a character pointer called p is declared as follows:

char *p;

Similarly, an integer pointer called pnt is declared by writing:

int *pnt;

In the above example, although pnt is declared as a pointer, currently it is not assigned any value. The address of a variable is obtained by preceding the variable name with the ‘&’ character. For example, the address of the integer variable sum is obtained as follows:

pnt = &sum;

We can also make the above declaration by writing:

int *pnt = &sum;

Now, the pointer pnt holds the address of the variable sum. We can access the value of a variable whose address is known by preceding its pointer with the ‘*’ character. Thus, in the above example, we can set the value of the variable sum to 10 as follows:

*pnt = 10;

In C whenever variables are passed as arguments to a function, their values are copied to the corresponding function parameters, and the variables themselves are not changed in the calling environment. This is referred to as the call-by-value mechanism. Most other languages provide call-by-reference mechanisms so that the values of variables can be changed in functions. In C this is done using pointers where the addresses of the variables are passed to a function and not their values. An example function is given below which swaps the values of two of its arguments:

Programming PIC Microcontrollers in C-0136

Either of these definitions is independent of the size of the array being passed, and the array size is in general not known by the function. In a function definition, a formal parameter that is defined as an array is actually a pointer. When an array is passed as an argument to a function, the base address is passed and the array elements themselves are not copied. An array bracket notation is used to declare pointers as parameters. An example is given below where it is assumed that array a has 10 elements:

Programming PIC Microcontrollers in C-0137

Programming PIC Microcontrollers in C-0138

 

PROGRAMMING PIC MICROCONTROLLERS IN C:FUNCTIONS IN C

FUNCTIONS IN C

Almost all programming languages support functions or some similar concepts. Some lan- guages call them subroutines, some call them procedures. Some languages distinguish between functions which return variables and those which do not.

In almost all programming languages functions are of two kinds: user functions and built-in functions. User functions are developed by programmers, while built-in functions are usually general purpose routines provided with the compiler. Functions are independent program codes and are usually used to return values to the main calling programs.

In this section we shall look at both types of functions.

User Functions

These functions are developed by the programmer. Every function has a name and optional arguments, and a pair of brackets must be used after the function name in order to declare the arguments. The function performs the required operation and can return values to the main calling program if required. Not all functions return values. Functions whose names start with the keyword void do not return any values, as shown in the following example:

Programming PIC Microcontrollers in C-0130

Built-in Functions

The PICC Lite compiler provides a large number of built-in functions which are available to the programmer. A list of all the available functions can be obtained from the PICC Lite User’s Guide. Some examples are given below.

abs

This function calculates the absolute value of a given number. For example, the absolute value of the variable total can be obtained as follows:

Programming PIC Microcontrollers in C-0131

cos

This function returns the trigonometric cosine of an angle. The angle must be in radians. For example, the cosine of 45◦ can be calculated as follows:

Programming PIC Microcontrollers in C-0132

sqrt

The sqrt function returns the square root of a given number. In the following example, the square root of number 14.5 is stored in the variable no:

Programming PIC Microcontrollers in C-0133

isupper

This function checks whether or not a given character is upper case between A and Z. If it is, a 1 is returned, otherwisea0 is returned. An example is given below:

Programming PIC Microcontrollers in C-0134

isalnum

This function checks whether or not a given character is an alphanumeric character between 0 and 9, a and z, or A and Z. An example is given below:

Programming PIC Microcontrollers in C-0135

 

PROGRAMMING PIC MICROCONTROLLERS IN C:PROGRAM FLOW CONTROL

PROGRAM FLOW CONTROL

The PICC Lite language supports the following flow control commands:

Programming PIC Microcontrollers in C-0123

The if statement can be used together with the else statement when it is required to execute alternative set of statements when a condition is not satisfied. The general format is:

Programming PIC Microcontrollers in C-0124

Switch–Case Statement

This is another form of flow control where statements are executed depending on a multi-way decision. The switch–case statement can only be used in certain cases where:

• only one variable is tested and all branches depend on the value of that variable;

• each possible value of the variable can control a single branch.

The general format of the switch–case statement is as follows. Here, variable number is tested. If number is equal to n1, statements between n1 and n2 are executed. If number is equal to n2, statements between n2 and n3 are executed, and so on. If number is not equal to any of the condi- tion then the statements after the default case are executed. Notice that each block of statement is terminated with a break statement so that the program jumps out of the switch–case block.

Programming PIC Microcontrollers in C-0125

For Statement

The for statement is used to create loops in programs. The for loop works well where the number of iterations of the loop is known before the loop is entered. The general format of the for statement is:

Programming PIC Microcontrollers in C-0127

While Statement

The while loop repeats a statement until the condition at the beginning of the statement becomes false. The general format of this statement is:

Programming PIC Microcontrollers in C-0128

Programming PIC Microcontrollers in C-0129

Continue Statement

The continue statement is similar to the break statement but is used less frequently. The continue statement causes a jump to the loop control statement. In a while loop, control jumps to the condition statement, and in a for loop, control jumps to the beginning of the loop.