Professional Documents
Culture Documents
For example to make PORTD count forever incrementing once per second:
#include <16f877.h> Tells compiler that it is a 16f877 PIC.
#fuses NOWDT,NOPROTECT,PUT,NOLVP No WD Timer, NO code PROTECT, Power-Up Timer, NO Low-Voltage Programming
#use delay(clock = 2000000) Tells the compiler that a 2MHz clock is in use (needed for delay_ms() below)
#byte PORTD = 8 Tells the compiler that File 8 is to be called PORTD
main() The main function
{ Begin
int count = 0; Define an integer variable of size 8 bits, and initialize it to zero
while(TRUE) TRUE is defined as 1 in the header file: Do everything below forever
{ Begin the while loop
PORTD = count++; Send out the 8-bit value of count to PORTD & increment count
delay_ms(1000); delay_ms(n) is a CCS built-in function giving a fixed delay of n milliseconds
} End of while loop
} End of main() function
main()
{
int i; One unsigned local 8-bit variable called i
signed int number2; One signed local 8-bit variable called number2
const int Weeks = 52; One 8-bit variable called Weeks that is not to be changed from 52 later on
const int Months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
An array of 12 8-bit variables called Months[] that are not be changed
1. The break keyword allows the programmer to break out of a loop prematurely; for example
the light emitting diode connected to pin RA0 (#bit LED = 5.0) will pulse continually until
SWITCH1 (say #bit SWITCH 5.3; i.e. pin RA3) goes low, and then the loop breaks:
while(1)
{
LED = 0; delay_ms(500);
LED = 1; delay_ms(500);
if(SWITCH1) {break;} Exit the loop if SWITCH1 is low (false)
}
2. The continue keyword allows the execution to go back to the top of the loop; for example
the LED will pulse only as long as SWITCH2 is high:
while(1)
{ Short-circuit the loop if SWITCH2 is low
if(SWITCH2==0) {continue;}
LED = 0; delay_ms(500);
LED = 1; delay_ms(500);
}
If you do not want to wait forever for a charac- If there is more than one serial port (see previous
ter use the function kbhit() to first test for a page); for instance, a stream called pc, then
character available. substuitute for getc()above:
character = fgetc(pc);