Vous êtes sur la page 1sur 2

A simple C program

A very simple C program is shown below.


/*simple.c -- sets a pin low, then high*/
#INCLUDE <16F873.h>
#USE DELAY (CLOCK=4000000)
void main() {
output_low(pin_C1);
output_high(pin_C1);
}

This program has many common features of C programs. The first line is a comment
that is ignored by the compiler. It is simply there to document what the progr
am code does. A comment is anything that occurs between a "/*" and the subseque
nt "*/". The next line is a "directive". All directives begin with a "#" and a
re used to convey information to the compiler. The directive in this program t
ells the compiler to include a header file ("16F873.h") which is necessary when
using the microcontroller s input and output capabilities. The next two directiv
es tell it how to configure the device, and how fast it goes. The next line tel
ls the compiler that this is the "main" program, and that everything between the
opening brace, {, and the matching closing brace, , constitutes the main progra
m. The main program itself is only two lines. The first line (not a comment)
is a call to the "output_low" function, which sets output pin pin_C1 low, and a
call to output_high, which sets it high. . Note that after every program stateme
nt there is a semi-colon. This is very important.

#INCLUDE <16F873.h>
#USE DELAY (CLOCK=4000000)
void main() {
char i, j, k; /* declare characters */
i=2;
j=3;
k=i+j;
}

Again we have a fairly simple program that shows many different features of C. N
ote the semicolon after every program statement. We declare 3 char s, "i", "j" and
"k". A char is simply an 8 bit variable. You should use chars whenever possi
ble because the PIC is designed to work on data 8 bits at a time.

Often a series of instruction must be repeated over and over again. Instead of r
epeating the same operations repetitively it is useful to use a function that pe
rforms the repetitive operations. For instance to set a value on RC1 and then re
ad from RB0 and set RC0 to that value, and returning the value of RB0 you might
use a function called "RB0toRC0". (Note: this program isn't meant to be particu
larly useful, but to introduce the syntax for function declaration, and use).

/*simpleFunc.c -- to demonstrate function calls*/


#INCLUDE <16F873.h>
#USE DELAY (CLOCK=4000000)
short int RB0toRC0(RC1val)
short int RC1val;
{
Output_bit(pin_C1, RC1val); /*Set RC1 to the specified value*/
if (input(pin_B0)) { /*Read RB0*/
Output_high(pin_C0); /*If RB0 is high, RC0 set high*/
}
else {
Output_low(pin_C0); /*else set RC0 low*/
}
return(input(pin_B0)); /*Return RB0*/
}
void main() {
short int b;
b=RB0toRC0(1);
b=RB0toRC0(0);
}

This program introduces some new constructs. The most obvious is the function "R
B0toRC0" which is at the top. The first line of the function declares that the f
unction returns a short int, and has one argument. The type of the argument is g
iven before the function's opening brace. The body of the function (between the
braces) outputs a value to RC1, and reads from RB0 and echos to RC1. The last l
ine tells that compiler to return the value of RB0 to the calling function.
The function is called in the main program with different arguments. The first
call would set RC1 high, and return the current value of RB0 to the variable "b
". The next line would set RC1 low, and return the value of RB0 to the variable
"b".

Vous aimerez peut-être aussi