Difference between revisions of "Serial Communications Library -- BoostC and 16F877A"

From OpenCircuits
Jump to navigation Jump to search
 
(35 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
=== Summary  ===
 
=== Summary  ===
 +
 +
This project has been replaced by [[Serial Communications (RS232) in BoostC]] which is designed to work with more chips, but this one should still work fine and may be simpler for the beginner.
  
 
*Name: Serial Communications Library -- BoostC and 16F877A
 
*Name: Serial Communications Library -- BoostC and 16F877A
*Purpose: A C library ( in source code form ) for serial communications together with a program demonstrating its use.
+
*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 page in draft. Code works but still in refinement, code available, email [[russ_hensel]]
+
*Status: This is near "final". Code works but still in refinement, see '''Download''' section below.
*Technology: BoostC and PIC 16F877A
+
*Technology: BoostC and PIC 16F877A -- should be fairly easily adapted to other chips
 
*Author: [[russ_hensel]] ( where you can find an email address to reach me )
 
*Author: [[russ_hensel]] ( where you can find an email address to reach me )
 
*License: not fully determined, but open source and object code.
 
*License: not fully determined, but open source and object code.
Line 11: Line 13:
 
This is one of a series of articles on Microcontroller Serial Communications, rooted at this site here:  [[Microcontroller Serial Communications Articles]]
 
This is one of a series of articles on Microcontroller Serial Communications, rooted at this site here:  [[Microcontroller Serial Communications Articles]]
  
 +
=== News  ===
 +
 +
* May '09 Now have a version for 16F877A and 18F2550, but beta, not quite ready for publication, email me if you need it.
 +
* New version now at: [[Serial Communications (RS232) in BoostC]]
 +
* August '09 Discovered error in code, at 20 meg hz fuse setting is still xt and some crystals may not run.  Should be hs for this clock rate.  The version on this page will no longer be updated however.
  
 
=== Features  ===
 
=== 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.
  
 
{| class="wikitable"
 
{| class="wikitable"
Line 18: Line 29:
 
|Feature
 
|Feature
 
|Details
 
|Details
 
 
  
 
|-valign="top"
 
|-valign="top"
Line 36: Line 45:
 
|Transmit is blocking.
 
|Transmit is blocking.
 
|During a transmit no activities that are not interrupt driven occur.
 
|During a transmit no activities that are not interrupt driven occur.
 
 
<!-------------------------------->
 
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
Line 45: Line 53:
 
|Command interface includes a stop command to halt long running operations.
 
|Command interface includes a stop command to halt long running operations.
 
|Character is !
 
|Character is !
 
 
 
<!-------------------------------->
 
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
Line 54: Line 60:
 
|-valign="top"
 
|-valign="top"
 
|Option to make single letter commands case insensitive.
 
|Option to make single letter commands case insensitive.
 
 
 
 
|
 
|
 
<!-------------------------------->
 
<!-------------------------------->
Line 65: Line 68:
 
|-valign="top"
 
|-valign="top"
 
|Beware of negative numbers which may not be handled as you expect.
 
|Beware of negative numbers which may not be handled as you expect.
|
+
|The demo program deliberately causes overflow which may push signed number negative and make unsigned numbers roll over.
<!-------------------------------->
+
<!--------------------------------
 
|-valign="top"
 
|-valign="top"
 
|
 
|
Line 106: Line 109:
 
=== Hardware for the Program ===
 
=== 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 loevel shifter is [[PIC based Stepper Motor Dancing Analog Clock]]
  
 
=== Compiling the Program ===
 
=== 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.....
 +
 +
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.
 +
 +
Before compiling make sure you change the target PIC to 16F877A.
 +
 +
Before you actually try it with real hardware review your clock frequency and baud rate settings in the code.
  
 
=== Running the Program ===
 
=== Running the Program ===
Line 113: Line 126:
 
Running the DemoSerial demo program  ( demo is set up to be caps insensitive, if you comment out this feature use lower case commands )
 
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 [http://www.rs485.com/psoftware.html 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.  ( <cr> is a carriage return )
  
 
{| class="wikitable"
 
{| class="wikitable"
 
|-valign="top"
 
|-valign="top"
|Feature
+
|Command
 
|Details
 
|Details
 
 
 
|-valign="top"
 
|Code is for USART hardware on PIC 16F877A.
 
|Should be fairly easy to port to similiar processors
 
 
<!-------------------------------->
 
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
|Receive is interrupt driven.
+
|r<cr>
|Receive should be active all the time although overrunning the buffer is possible.
+
|Report the version of the software ( and possibly other infomation ).
 
<!-------------------------------->
 
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
|[[Microcontroller Serial Communications Articles]]
+
|!
|
+
|Stop whatever software is doing, give stopped message. Note that no <cr> is required.
 +
 
 
<!-------------------------------->
 
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
|Transmit is blocking.
+
|d<cr>
|During a transmit no activities that are not interrupt driven occur.
+
|Print out a series of numbers using serial_print_dec
 
+
(The library has different version of serial_print_dec for different argument types )
 
<!-------------------------------->
 
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
|Command interface is included.
+
|h<cr>
|PIC is setup to receive serial commands and respond to them.
+
|Print out a series of numbers using print_hex.
 
<!-------------------------------->
 
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
|Command interface includes a stop command to halt long running operations.
+
|b<cr>
|Character is !
+
|Print out a series of numbers using print_bin.
 
 
  
 
<!-------------------------------->
 
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
|Input routines for parsing and converting a numerical parameter.
+
|e<cr>
|This makes commands like r22 able to pass the argument ( converted to binary ) to a subroutine.
+
|Echo a series of keystrokes as hex values.
<!-------------------------------->
 
|-valign="top"
 
|Option to make single letter commands case insensitive.
 
  
|
 
 
<!-------------------------------->
 
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
|Realizes a protocal similar to the one found in the link on the right.
+
|nxx<cr>
|[[MC_RS232_Comm]]
+
|Parse out a numeric parameter (xx) and send back to the PC as a decimal:  n23<cr> returns a 23.
<!-------------------------------->
 
|-valign="top"
 
|Beware of negative numbers which may not be handled as you expect.
 
|
 
<!--------------------------------
 
|-valign="top"
 
|
 
|
 
 
<!--------------------------------
 
<!--------------------------------
 
|-valign="top"
 
|-valign="top"
Line 204: Line 202:
 
=== Using the Library in Your Own Program ===
 
=== 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 ===
 
=== 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 it thru its serial connection.
+
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 [http://home.comcast.net/~russ_hensel/OC/DemoSerial.zip DemoSerial.zip]  I may have more recent versions, email me if you want to check [[russ_hensel]]
 +
 
 +
=== Projects using this Library ===
 +
 
 +
*[[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 ).
 +
 
 +
[[category:Serial Communications]][[category:Microcontroller]][[category:PIC]][[category:BoostC]]

Latest revision as of 19:31, 22 August 2009

Summary[edit]

This project has been replaced by Serial Communications (RS232) in BoostC which is designed to work with more chips, but this one should still work fine and may be simpler for the beginner.

  • Name: Serial Communications Library -- BoostC and 16F877A
  • 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". Code works but still in refinement, see Download section below.
  • Technology: BoostC and PIC 16F877A -- should be fairly easily adapted to other chips
  • Author: russ_hensel ( where you can find an email address to reach me )
  • License: not fully determined, but 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[edit]

  • May '09 Now have a version for 16F877A and 18F2550, but beta, not quite ready for publication, email me if you need it.
  • New version now at: Serial Communications (RS232) in BoostC
  • August '09 Discovered error in code, at 20 meg hz fuse setting is still xt and some crystals may not run. Should be hs for this clock rate. The version on this page will no longer be updated however.

Features[edit]

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.

Feature Details
Code is for USART hardware on PIC 16F877A. Should be fairly easy to port to similiar processors
Receive is interrupt driven. Receive should be active all the time although overrunning the buffer is possible.
Microcontroller Serial Communications Articles
Transmit is blocking. During a transmit no activities that are not interrupt driven occur.
Command interface is included. PIC is setup to receive serial commands and respond to them.
Command interface includes a stop command to halt long running operations. Character is !
Input routines for parsing and converting a numerical parameter. This makes commands like r22 able to pass the argument ( converted to binary ) to a subroutine.
Option to make single letter commands case insensitive.
Realizes a protocal similar to the one found in the link on the right. MC_RS232_Comm
Beware of negative numbers which may not be handled as you expect. The demo program deliberately causes overflow which may push signed number negative and make unsigned numbers roll over.

Hardware for the Program[edit]

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 loevel shifter is PIC based Stepper Motor Dancing Analog Clock

Compiling the Program[edit]

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.....

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.

Before compiling make sure you change the target PIC to 16F877A.

Before you actually try it with real hardware review your clock frequency and baud rate settings in the code.

Running the Program[edit]

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. ( <cr> is a carriage return )

Command Details
r<cr> Report the version of the software ( and possibly other infomation ).
! Stop whatever software is doing, give stopped message. Note that no <cr> is required.
d<cr> Print out a series of numbers using serial_print_dec

(The library has different version of serial_print_dec for different argument types )

h<cr> Print out a series of numbers using print_hex.
b<cr> Print out a series of numbers using print_bin.
e<cr> Echo a series of keystrokes as hex values.
nxx<cr> Parse out a numeric parameter (xx) and send back to the PC as a decimal: n23<cr> returns a 23.

Using the Library in Your Own Program[edit]

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[edit]

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[edit]

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

Projects using this Library[edit]

Comment, Questions, Contributions?[edit]

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 ).