1 / 5

How to use the CCS compiler with the serial port

How to use the CCS compiler with the serial port. Setting up and initialization.

binh
Download Presentation

How to use the CCS compiler with the serial port

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How to use the CCS compiler with the serial port

  2. Setting up and initialization • 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.

  3. Outputting a single character or string • 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’); • 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); • 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.

  4. Tag for unsigned int New line/Return 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.

  5. Reading in from 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.

More Related