0% found this document useful (0 votes)
31 views10 pages

Sheet 1

Uploaded by

abdo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views10 pages

Sheet 1

Uploaded by

abdo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

4th computer&control

Sheet NO.1
16F876 microcontroller
-------------------------
We will use PIC 16f876 microcontroller from Microchip Company in all experiments in this
course so, I will give you some features for the microcontroller:

1-Pin diagram of pic 16f876 is:

1
2-Block diagram of PIC 16f876:

2
3-Specifications for the microcontroller:

3
4-Oscillator configurations for the microcontroller:

5- RC OSCILLATOR
For timing insensitive applications, the “RC” device option offers additional cost savings. The RC
oscillator frequency is a function of the supply voltage, the resistor (REXT) and capacitor (CEXT) values and
the operating temperature. In addition to this, the oscillator frequency will vary from unit to unit due to
normal process parameter variation. Furthermore, the difference in lead frame capacitance between package
types will also affect the oscillation frequency, especially for low CEXT values. The user also needs to take
into account variation due to tolerance of external R and C components used. Figure 14-3 shows how the R/C
combination is connected to the PIC16F87XA.

4
Benefits of C language in Embedded Systems:
The direct benefits of using C in Embedded Systems design are as follows:
• You will not be overwhelmed by details. Working with the limited resources of
microcontrollers through a C compiler helps to abstract the architecture and keep from
miring you down in opcode sequences and silicon bugs.
• You will learn the basics of portability. The cost of modifying assembly language code to
allow a program written for one microcontroller to run on a different microcontroller may
remove any incentive to make the change.
• You can reduce costs through traditional programming techniques. Details relating to
specific hardware implementations can be placed in separate library functions and header
files. Using C library functions and header files ensures that application source code can be
recompiled for different microcontroller targets.
• You can spend more time on algorithm design and less time on implementation. C is a high
level language. You will be able to program your applications quickly and easily using C.
each line of code written in C can replace many lines of assembly language. Debugging and
maintaining code written in C is much easier than in code written in assembly language.

Intrinsic types used in the PICC compiler:

5
Types of files in the PICC compiler:
Source File: A program written in a language the compiler and you understand. The source file
has to be processed before the Microcontroller will understand it.

Object File: This is a file produced by the Assembler / Compiler and is in a form which the
microcontroller understands to enable it to perform the task. File extension is .OBJ or .HEX
depending on the assembler directive.

List File: This is a file created by the Assembler / Compiler and contains all the instructions from
the Source file together with their hexadecimal values alongside and comments you have written.
The file extension is .LST

Other Files as The error file (.ERR) contains a list of errors.

C Fundamentals
In this Part, we will present some of the key aspects of the C programming language. A quick
overview of each of these aspects will be given.

1-The Structure of C Programs:


All C programs contain preprocessor directives, declarations, definitions, expressions, statements
and functions.

 Preprocessor directive
A preprocessor directive is a command to the C preprocessor (which is automatically invoked as
the first step in compiling a program). The two most common preprocessor directives are the
#define directive, which substitutes text for the specified identifier, and the #include directive,
which includes the text of an external file into a program.

 Declaration
A declaration establishes the names and attributes of variables, functions, and types used in the
program. Global variables are declared outside functions and are visible from the end of the
declaration to the end of the file. A local variable is declared inside a function and is visible from
the end of the declaration to the end of the function.

 Function
A function is a collection of declarations, definitions, expressions, and statements that
performs a specific task. Braces enclose the body of a function.

 main Function
All C programs must contain a function named main where program execution begins. The braces
that enclose the main function define the beginning and ending point of the program.

6
Example: General C program structure

2- Components of a C program
All C programs contain essential components such as statements and functions. Statements are the
parts of the program that actually perform operations. All C programs contain one or more
functions. As:
#include <stdio.h>
/* my first C program */
main ()
{
printf (“Hello world!”);
}

The statement #include <stdio.h> tells the compiler to include the source code from the file
‘stdio.h’ into the program. The extension .h stands for header file. A header file contains
information about standard functions that are used in the program.

3-printf Function:
The printf function is a standard library function which allows the programmer to send printable
information. The general format for a printf () statement is:

printf(“control_string”, argument_list);

A control_string is a string with double quotes at each end. Inside this string, any combination of
letters, numbers and symbols can be used. Special symbols call format specifiers are denoted with
a %. The control_string must always be present in the printf () function. An argument_list may
not be required if there are no format specifiers in the format string. The argument_list can be
composed of constants and variables. The following two examples show printf () statements using a
constant and then a variable.

7
printf (“Hello world!”);
printf(“Microchip® is #%d!”,1);

The format specifier (%d) is dependent on the type of data being displayed. The table below shows
all of the format specifiers in C and the data types they affect.

Format Specifiers printf ()


%c single character
%d signed decimal integer
%f floating point (decimal notation)
%e floating point (exponential or scientific notation)
%u unsigned decimal integer
%x unsigned hexadecimal integer (lower case)
%X unsigned hexadecimal integer (upper case)
l Prefix used with %d, %u, %x to specify long integer

NOTE: A 0 (zero) following a % character within a format string forces leading zeros to be printed
out. The number following specifies the width of the printed field.
printf (“The Hex of decimal 12 is %02x\n”, 12);
This would be print out as:
The Hex of decimal 12 is 0c
Escape Sequences
\n newline \t horizontal tab
\r carriage return \” double quote
\’ single quote \0 Null character
\\ backslash

The format specification can also be shown as % [flags][width][.precision], so in a previous


example the line:
printf(“Area is %6.4f square units\n”, area);

Will print out the value area in a field width of 6, with a precision of 4 decimal places.

4-Functions:
There are two methods used to inform the compiler what type of value a function returns.
In The first way the function and its definition are written before the main function. The second
way to inform the compiler about the return value of a function is the function prototype. A
function prototype not only gives the return value of the function, but also declares the number
and type of arguments that the function accepts. The prototype must match the function
declaration exactly. The general format for a function prototype is shown here:

type function_name (type var1, type var2, type var3);

In the above example, the type of each variable can be different. An example of a function
prototype is shown in the following program. The function calculates the volume defined by
length, width, and height.

8
int volume (int s1, int s2, int s3);
void main()
{
int vol;
vol = volume(5,7,12);
printf(“volume: %d\n”,vol);
}
int volume(int s1, int s2, int s3)
{
return s1*s2*s3;
}
Notice that the return uses an expression instead of a constant or variable.
The importance of prototypes may not be apparent with the small programs that we have been doing
up to now. However, as the size of programs grows from a few lines to many thousands of lines, the
importance of prototypes in debugging errors is evident.

5-One-Dimensional Arrays
An array is a list of variables that are all of the same type and can be referenced through the same
name. An individual variable in the array is called an array element. The general form for declaring
one-dimensional arrays is:

type var_name [size];


As:
int height [50];
What happens if you have an array with ten elements and you accidentally write to the eleventh
element? C has no bounds checking for array indexes. Therefore, you may read or write to an
element not declared in the array. However, this will generally have disastrous results. Often this
will cause the program to crash and sometimes even the computer to crash as well. C does not
allow you to assign the value of one array to another simply by using an assignment like:

int a [10],b[10];
.
.
a=b;

The above example is incorrect. If you want to copy the contents of one array into another, you
must copy each individual element from the first array into the second array as:

for (I = 0; I <20; I ++)


b [I] = a[I];

6-Multidimensional Arrays:
C is not limited to one-dimensional arrays. You can create two or more dimensions. For example,
to create an integer array called number with 5x5 elements, you would use:
int number [5][5]; uses 25 ram location
Additional dimensions can be added simply by attaching another set of brackets.

9
7-Initializing Arrays:
C provides a method in which you can assign an initial value to an array just like you would for
a variable. The general form for one-dimensional arrays is shown here:
type array_name[size] = {value_list};
The value_list is a comma separated list of constants that are compatible with the type of array. The
following example shows a 5-element integer array initialization.
int i[5] = {1,2,3,4,5};
A string (character array) can be initialized in two ways. First, you may make a list of each
individual character as such:
char str[3] = {‘a’, ‘b’, ‘c’};
The second method is to use a quoted string, as shown here
char name [5] = “John”;
You may have noticed that no curly braces enclosed the string; they are not used in this type of
initialization because strings in C must end with a null. The compiler automatically appends a null
at the end of “John”.
Multidimensional arrays are initialized in the same way as one-dimensional arrays. It is probably
easier to simulate a row/column format when using two-dimensional arrays. The following example
shows a 3x3 array initialization.
int num[3][3]={ 1,2,3,4,5,6,7,8,9};

--------------------------------------------
---------------------------------
-------------------

10

You might also like