PC so that the data displayed will make sense to humans.
Sample programs will illustrate how the printf() escape sequences work.
////escape sequence demo 1 Csl9a
/include <16F818.h>
#use delay (internal = 4mhz)
#use rs232 (baud = 4800, xmit = PINAl) main()
{
while (input(PIN A2)==l); //go low? wait for ready switch to close
putc ('s'); //start here on-screen
printf ("C what happens!"); //send ASCII character string out a // serial port
while (TRUE);
} Result:
sC wrhat happens!
Notice that the "s" and "C" are run together. In the next example, we will use the \n escape sequence to put "C what happens" on a new line.
Notice, also, that the #use delay() and #use rs232() built-in functions are needed.
As used here, putcQ outputs a single ASCII character and printf() outputs a string of ASCII characters. They are built-in functions. Single quotes are used to indicate a single character which is defined in the putc() function. Quotation marks arc used to indicate a string of charac
ters w'hich is defined in the printf() function.
////escape sequence demo 2 /include <16F818.h>
#use delay {internal = 4mhz)
#use rs232 (baud = 4800, xmit = PIN A1)
//go low? wait for ready switch to close //start here on-screen
//new line
//send ASCII character string out a // serial port
while (TRUE);
Result:
C what happens!
"C what happens!" is on a new line with a blank space one character wide. Adding a carriage return escape sequence will cure that problem.
////escape sequence demo 3 /include <16F818.h>
/use delay (internal = 4mhz) /use rs232 (baud = 4800, xmit
//go low? wait for ready switch to close //start here on-screen
//new line, carriage return
//send ASCII character string out a // serial port
Result:
s
C what happens!
That's better! Notice that the \n and \r are together (no comma).
123
Now let's try out the horizontal tab escape sequence.
////escape sequence demo 4 /include <16F818.h>
#use delay (internal = 4mhz)
#use rs232 (baud = 4800, xmit = PIN A1) main()
{
while (input(PIN_A2)==1);
putc (’s');
printf ("\n\r\t");
printf ("C what happens!");
Csl9d
//go low? wait for ready switch to close //start here on-screen
//new line, carriage return, horiz tab //send ASCII character string out a // serial port
while (TRUE);
Result:
s
C wrhat happens!
STRINGS
A "string" is a group of characters. C does not have a string data type (string of characters).
String data is stored as one character per element in an array called a "string array".
A string literal is enclosed in quotation marks.
A string terminator tells the C compiler where the endvof the string is. It is called the null char
acter or null zero and is added automatically by the compiler. You can't see it, but it is there.
String length is the number of characters in the string including spaces and the null character.
The null character must be included in the count even though it is not visible. Examples are:
char msg[6] = "error"
error has 5 characters
Adding room for the null character makes 6
char alarm[15] = "smoke detected";
smoke = 5 characters space = 1 character detected = 8 characters
Add 1 to make room for the null character Total = 15
The next example program sends an ASCII character to a PC via the serial port. This example is nearly the same as what was done in the previous chapter, but now the emphasis is on explaining strings.
RS-232
SEND CONVERTER RECEIVE
125
////character demo - rs232 Cs20a /include <16F818.h>
#use delay (internal = 4mhz)
#use rs232 (baud = 4800, xmit = PIN Al) main()
{
while (input(PIN_A2)==1); //go low? wait for ready switch to close
putc ('a'); //send 'a' out a serial port
while (TRUE);
}
Power-up with the switch open. When you are ready to send, close the switch. Sending data some time after power-up allows time for the serial connection to become stable.
Notice that the #use delay() and #use rs232() built-in functions are needed.
The next example program sends an ASCII character string to a PC via the serial port. The hardware and procedure are the same.
////string demo - rs232 Cs20b
/include <16F818.h>
/use delay (internal = 4mhz)
/use rs232 (baud = 4800, xmit = PIN_A1) main( )
{
while (input(PINA2)==1); //go low? wait for ready switch to close printf ("C what happens!"); //send ASCII character string out a
// serial port while (TRUE);
This is a short introduction to strings. Manipulation of strings can become quite involved. The CCS compiler has 22 built-in functions for working with strings. For now, it is sufficient to be aware that these possibilities exist.
ARRAYS
Arrays have "elements" and all of the elements are the same data type.
Define array:
data type const array name [size];
number of elements in array modifier or qualifier
(if needed)
"const" is the constant qualifier. Use of const here will cause the constants to be placed in pro
gram memory (ROM).
Again, the elements in an array are all of one data type. The data type is specified in the array definition. Data types have corresponding array types.
As an example, we can define an integer array, which we will call i a, and the initial values:
int const i_a[4] = {1, 2, 3, 4};
Note that the order is int const, not the reverse as in the CCS manual, i a[3] is the third element of the array which has a value of 4.
The array name is the index to the first clement in the array. We can also declare an index to the array which will be useful in pointing to all elements in the array:
int pi_a; //declare index
The compiler will know that pi_a is an index because when it is used, it is associated with the name of the array.
4 elements
127
With an array and an index, we can:
• Step through the array using the index.
• Point to the nth element in the array.
• Add an offset to the index.
Examples will serve to illustrate.
The index may be incremented by:
pi_a++ //increments index pi_a to next element of array
We can step through array elements using this technique.
////array demo
//// uses time delay via function call /include <16F818.h>
//clock oscillator internal, frequency //delay function
//delay 500 milliseconds
//declare integer array, initialize //index to array
//declare integer i, used for counting //initialize port B
//display 4 array values
//display data pointed to by index via // port B LEDs
Notice, also, that the compiler learns that pvals is an index by observing that it is enclosed in [ J when first encountered.
We can extract the nth element from an array.
arrayname[n]
////array demo - extract nth element /include <16F818.h>
/fuses INTRCIO main()
{
int const vals[4] = {0,1,2,3};
outputb (0x00);
outputb (vals[3]);
while (TRUE);
}
Cs2 lb
//declare integer array, initialize //initialize port B
//display data pointed to by index via // port B LEDs
//circle, always
129
'
An offset may be added to or subtracted from the index.
int offset; //define offset
Later in in the program:
offset = 2; //offset 2 from current index value
outputb (i_a[pi_a + offset]); //offset added to index
If your code requires retrieving the 4th element (remember that "0" counts) in the array (contains
"3" in this example), because the result of an operation is 3, make offset = 3 and proceed. This assumes that the pi_a = 0 at the time operation begins.
again:
outputb (i a[pi_a + offset]); //offset added to index
////array demo = add offset to index Cs21c
/include <16F818.h>
/fuses INTRC 10 main()
{
int const vals[4] = {0,1,2,3}; //declare integer array, initialize
int pvals = 0; //index to array
int offset; //declare offset
outputb (0x00); //initialize port B
offset = 2; //value in offset
outputb (vals[pvals + offset]); //display data pointed to by index via // port B LEDs
while (TRUE); //circle, always