how to use the ccs compiler with the serial port

5
How to use the CCS compiler with the serial port

Upload: marsha-mccarthy

Post on 24-Dec-2015

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: How to use the CCS compiler with the serial port

How to use the CCS compiler with the serial port

Page 2: How to use the CCS compiler with the serial port

Setting up and initialization

1.Tell the compiler what pins are to be used for the transmit and receive serial streams. For example, if you want a 4,800-baud serial connection using pins RC6 and RC7 to trasmit and receive then put the following line at the head of your program:#use rs232(baud=4800, xmit=PIN_C6, rcv=PIN_C7)Hint: Do not leave spaces around the = sign!Hint: If you choose these pins the compiler will automatically use the integral USART. Other pins can be used, in which case the compiler will generate/process signals using software only.

There are many other options for the serial port. For example, you can have more than one serial port by giving each stream its own #use rs232 incantation; each with unique pins and with a name. For instance, stream=GPS. Functions such as fputc() can then name the serial port as required.

Page 3: How to use the CCS compiler with the serial port

2. To transmit a single character from the serial port then use the putchar() (or putc()) function. For example, to send out the character Z:

putchar(‘Z’);

3. To transmit a single byte from the serial port then specify the byte as the function argument. For example, to send out the byte hex 0D (which happens to be the code for Carriage Return: putchar(0x0d);

4. To transmit a complete character string then use the puts() function. For example, to transmit the message “Hello world”:

puts(“Hello world”);

Hint: A new-line (Carriage-feed, Line-Feed or 0x0D, 0x0A) sequence is sent out automatically after the message.

Outputting a single character or string

Page 4: How to use the CCS compiler with the serial port

Outputting strings and data values

Say you want to print out the value of some variables, as well as an optional message e.g.: int i = 0; /* Loop counter */ while(TRUE) /* DO forever */ { printf(“Hello world for the %uth time\n\r”,i); }

Tags are put in the string which then pick up the list of comma separated variables at the end. These tags should match the type of variable. Some of these are (see manual): %u Unsigned int %lu Long Unsigned int %d signed Decimal %ld Long signed int %x/X Unsigned int in hex/Hex %lx/lX Long Unsigned int in hex/Hex

Hint: A number can appear after the %; e.g. %4lu means print only the first four digits or %04lu means pad out with leading zeros.

Tag for unsigned int

New line/Return

Page 5: How to use the CCS compiler with the serial port

To receive a single character from the serial port then use the getchar() (or getc()) function. For example:

char answer; /* Sometime later */ printf(“Continue? Input Y or N.”); while(TRUE) { value = getchar(); /* Get a character */

if((value == ‘Y’)| (value == ‘y’)){do this;}

if ((value == ‘N’) | (value == ‘n’)){do that;}

} This askes the operator to input a Y (for Yes) or N (for No) and waits (forever!)

for a response. Provided it is a Y (or y) or N (or n) then something happens. If it is anything else, then it waits for the next character.

Reading in from the serial port