Serial Communications (RS232) in BoostC

Summary

 * Name: Serial Communications Library (RS232) -- BoostC
 * Purpose: A C library ( in source code form ) for serial communications ( RS232 ) together with a program demonstrating its use. Code includes a simple command interperter and parsing for various number bases.
 * Status: This is near "final". But forever subject to refinement, see Download section below.
 * Technology: BoostC and PIC 16F877A or 18F2550 -- should be fairly easily adapted to other chips
 * Author: russ_hensel ( where you can find an email address to reach me )
 * License: Open source and object code.

This is one of a series of articles on Microcontroller Serial Communications, rooted at this site here: Microcontroller Serial Communications Articles

News

 * This page is still draft, but near final, will work on it if you have questions or I am otherwise motivated.
 * My most recent version seems to have a new problem with the 877, working on it. Supporting multiple chips seems to have caused me to introduce a bug.  New version should come out in the next few days, basi code struchture however will not change.

Features
Consider using this library any time you want serial control RS232 of a PIC or where you need serial output. Since serial boot loading is so nice, if you use it you can add serial features to your program at little cost. The serial interface can also be very useful for debugging an application.

The code include here is not the world's greatest code or particularly exemplary, but it does seem to work and is easy to incorporate into projects of your own.

Hardware for the Program
Stepper Motor Tester has a Eagle file that will work for this demo. You can drop the stepper driver chip. Even the level shifter can be dropped if you use a cable with a level shifter. The circuit is an easy project to do on a proto board. Another project, with very similiar hardware, but no level shifter is PIC based Stepper Motor Dancing Analog Clock

Compiling the Program
The code for the library and the demo program is in one zip file ( called DemoSerial.zip, see Download below ). Unzip into a directory of its own. Open the project file SerialDemo.__c and compile with the source boost C compiler. The source files are SerialDemo.c SerialDemo.h.....

Before compiling make sure you change the target PIC to whatever you are using and check the #defines to see that they are right for your code Project.h has some of the most important. See File Structure and Pre Processing below.

I also use a non standard location for my compiler and you may need to adjust for your setup. Check Options -> Tools -> and make sure the setting there corresponds to you set up.

You should do a full build, what is called a clean and build in some environments. You can erase the intermediary files befor you build or press the control key at the same time you click the build icon. Before you actually try it with real hardware review your clock frequency and baud rate settings etc. .... in the code.

I am not sure if the program is small enough to compile with the free compiler, my compiler reports a size of 2392 bytes of ROM, which is probably not small enough. There should enough demo code that can be deleted to get under the 2K limit.

File Structure and Pre Processing
I am working on a standard sturcture for my programs to make them adapatable to different configurations including processor, clock speed etc. For you to make sense of this you need to understand a bit about the C preprocessor esp. #include and #define. Note that I use #warning for messages about compile options, they are not warnings and normall begin with "Info >>".

mainfile.c
Note this is not necessiarly named mainfile, for this demo the mainfile is DemoSerial.c. This is whrere the main part of the program resides. I almost always have a .h file for each mainfile, because that seems to be how the pros do it. Not entirely sure why. I do included a lot of preprocess and #define stuff in the mainfile. Definitions for the port configuration etc. are currently in mainfile.c.

Project.h
All my code has an include for project.h this file is for specifing the main configuration options. Commonly some of these options are collected together in setups which are numbered. Setups with numbers below 100 should not be changed in your project, if one of the low numbered setups is not adaquate you can add a new one with a number above 100. Currently all my projects are defined for a baud rate of 19.2 K. In the future the baud rate may be set in project.h.  For now you would have to mess with SerialLib.h.

For details on what each #define means see comments in the file.

Fuse.h
Contains the fuse settings. These are determined from the stuff set up in Project.h. Fuse.h should be included only once, this should be in the mainfile right after the include for Project.h.  Fuse.h should not be modified for an individual project, you should be able to make additions so it will work with all projects.

Library Files
My libraries are not precompiled, they are just .c and .h files that you make a copy of for your project and compile with it. Thus when the files are updated they will not effect your project until you copy in new versions. This leaves you in control of adapting to the changes. The library here is SerialLib.c ( and its .h file ). Add the .c files and .h files to your project, #include the .h files to any other file using the library.

Running the Program
Running the DemoSerial demo program ( demo is set up to be caps insensitive, if you comment out this feature use lower case commands )

Once running the demo program supports the following commands. Use a terminal emulator set for 8N1 and 19.2 baud ( assuming you have not change the PIC program). One terminal emulator I like is R. E. Smith I/O Commander. Use hyperterminal only if you are desperate. For full details on what each command does review the code, I have tried to fully comment the code so that it should be fairly easy to read. I have generally avoided coding tricks. (  is a carriage return )

Using the Library in Your Own Program
The simplest way is probably to copy the whole BoostC project to a new directory and rename SerialDemo .c and .h. Adjust the include statements and the source code file list. Then add your own code. Otherwise ( if you already have a bunch of your own code ) you can add the serial library files to your project and cut and paste from SerialDemo.c into your project. Including functions from the library that you do not use in your code should not be an isssue as the linker seems to eliminate them from the load file.

Code History
Origninal source code found, I think, on code on the SourceBoost website ( see program header for more inforation ). I have used the code on a series of different projects, it has evolvled with each one. I have added the code for a command interface, this is based on the idea that if you have serial capability then you may want to control the program through its serial connection.

Download
Zip file here DemoSerial_v5.zip I may have more recent versions, email me if you want to check russ_hensel

Projects using this Library
but the posted version may use the earlier PIC16F877A version.


 * Stepper Motor Tester
 * PIC based Stepper Motor Dancing Analog Clock
 * Experimenting with IR Remotes using a PIC running BoostC Project
 * Experimenting with Stepper Motors as Rotary Encoders using a PIC running BoostC Project

Comment, Questions, Contributions?
Email me russ_hensel, or use the talk page for this topic. All feedback is welcome, even bug reports or additions ( interrupt driven transmit perhaps ).