CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Self Sync Soft Serial - 4S

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

Self Sync Soft Serial - 4S
PostPosted: Tue Nov 19, 2013 10:43 pm     Reply with quote

Hi All,

This was just an exercise/experiment I had always thought of doing.
It works, and I'm sharing.


*Take this with a grain of salt, it will not play well with interrupts, and there are several inherent flaws to this "protocol".


As the tittle describes "4S" is Regular software serial with a Sync prelude.

What this means is that you can communicate PIC-to-PIC using software serial without having to worry about missing data and lockups.*

Things are organized as Clients and Servers.

Servers do tasks that need to be reported to the client. Like reading a sensor, processing data and sending it to the Client.

Clients use the server data.

Clients are the "Masters" since they decide when to listen, which is important since this is all based on software serial which implies a lock up while waiting to receive something.
This keeps the waiting in GETC() to a minimum.
Servers however, they wait for the client for as long as it takes.

This is a one wire physical layer and unidirectional (+shared ground).
Must use a pullup on the line.
Server talks when Client wants to listen.
One wire per Server

Can be modified easily to handle several server with one client, but this version only does ONE server.

OPERATION:
Server keeps line low - No data to report
Server Releases line and waits for client - Data is ready
Client pulses line low for 100us - Client is ready to listen
Client starts software GETC()
Server proceeds Software PUTC()
Server keeps line low


Its not meant to be flawless, its just meant to make sending a byte form PIC-to-PIC easier or simpler with soft serial.
Remember this was only a late night experiment.


CLIENT:
Code:
//******************************************************************************
//                      Self Sync Soft Serial
//                           4S Protocol
//******************************************************************************
// AUTHOR: GABRIEL BARRIOS
// DATE: 19/11/2013
// Panama, Rep. Panama.
//______________________________________________________________________________
//
//----------------------------------CLIENT--------------------------------------
#use RS232(STREAM=S4, BAUD=9600, RCV=COM_PIN)

void Init_4S()
{
   input(COM_PIN);               // Make sure pin is set as an input
   output_float(COM_PIN);         // Release the line
}

int Get_4S_Data()
{
   if(input(COM_PIN))            // Check if line has been released
   {
      output_low(COM_PIN);      // Bring line low for 100us
      delay_us(100);            // 100us Delay
      output_float(COM_PIN);      // Release the line
      return(fgetc(S4));         // Wait for data
   }
   return(0);                  // Return 0 if line was not released
}



SERVER:
Code:
//******************************************************************************
//                      Self Sync Soft Serial
//                           4S Protocol
//******************************************************************************
// AUTHOR: GABRIEL BARRIOS
// DATE: 19/11/2013
// Panama, Rep. Panama.
//______________________________________________________________________________
//
//----------------------------------SERVER--------------------------------------
#use RS232(STREAM=S4, BAUD=9600, XMIT=COM_PIN)

void Init_4S()
{
   output_low(COM_PIN);      // Drive line low - No data to send
}

void Send_4S_Data(char data)
{
   output_float(COM_PIN);      // Release the line / Signal data is available
   while(input(COM_PIN)){}      // Wait till Client brings line low
   while(!input(COM_PIN)){}   // Wait Duration of low pulse
   delay_us(100);            // Allow the Client to set it self up
   fputc(data,S4);            // Send Data
   output_low(COM_PIN);      // Drive line low - No data to send
}



TEST PROGRAM:
Code:
//******************************************************************************
//                      Self Sync Soft Serial
//                           4S Protocol
//******************************************************************************
// AUTHOR: GABRIEL BARRIOS
// DATE: 19/11/2013
// Panama, Rep. Panama.
//______________________________________________________________________________
//
//--------------------------------TEST PROGRAM----------------------------------
#include <16f886.h>
#DEVICE adc=8
#DEVICE *=16
#DEVICE PASS_STRINGS=IN_RAM
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NODEBUG
#use delay(clock=20000000)
#use rs232(STREAM=STD, baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)


#define COM_PIN PIN_B4                  // Pin to XMIT/RCV
//#define CLIENT                     // Client or Server

#ifdef CLIENT
   #include <4S - Client.c>            // Include Client Code
#else                              // or
   #include <4S - Server.c>            // Include Server Code
#endif

int temp=0;                           // Just for Example purposes
//==========================
void main()
{
   Init_4S();               //Initialize Harware - Call once

   while(1)
   {
      #ifdef CLIENT
      {
         fprintf(STD,"Client says: %X \r",temp);
         //DO STUFF
         delay_ms(293);       // Its a prime number to show operaration in mismatched loop times
         delay_ms(211);       // Same  thing - Imagine these are functions
         //
         //
         //Right at the end of the main while:
         temp=Get_4S_Data();
      }
      #else
      {
         //DO STUFF
         delay_ms(223);       // Its a prime number to show operaration in mismatched loop times
         delay_ms(317);       // Same  thing - Imagine these are functions
         //
         //
         //Right at the end of the main while:
         Send_4S_Data(0xAA);   // Nice example value to send (10101010)
      }
      #endif
   }
}

_________________
CCS PCM 5.078 & CCS PCH 5.093
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group