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

communication between 18F452 (Master) & 16F877 (Slave)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
AdamkT1



Joined: 21 Apr 2007
Posts: 44

View user's profile Send private message

communication between 18F452 (Master) & 16F877 (Slave)
PostPosted: Sat Jul 21, 2007 12:22 am     Reply with quote

I am having problems in creating a communication link between 18F452 (the Master) & 16F877 (The Slave).

As mentioned before one of my constraints is that I can’t use any interrupt based routine as they are being used by other components.

A brief description of the project is that the master asks the slave to send in the GPS data by putting logic ‘1’on the Pin RTR. The slave after reading the data from GPS sees this flag and goes into the routine of sending the data to the master. Now to ensure that the data received by the master is correct or not, the master sends the data back to the slave, who compares the received data with the data sent. I am trying to use the same pins for data Rx & Tx, so I am changing the pins direction for Rx/Tx.

I have succeeded in developing the data sending mechanism from Slave to Master. The data received is then displayed on the port which always seems to be correct.

The problem is that the data sending back does not work properly.

The debugging has showed that the pins direction gets properly sets but after that the program does not function as it should. The pins do not get the logic levels of 1 & 0 and hence get stuck.

The code segment given below does not function:

Code:
PORTD=0;   
   SET_TRIS_D(SysTx); // set the direction for Tx
   delay_ms(5);   
   CHK=Hi;
delay_ms(5);
   DTR=Hi;
delay_ms(5);   
PORTB=0xFF;   
   while (Go1==Lo);   


Below is the complete listing, I am desperately needing help...

==========Code for master========================
Code:
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#define Hi 1
#define Lo 0
#define SysInitz 0x03
#define SysTx 0x1A
#define SysRx 0x27

#bit DTR = PORTD.0 // Data Terminal Ready (in)
#bit CLK = PORTD.1 // Clock (in)
//PIN_D2 is used for Rx/Tx // Data in/out (in/out)
#bit GO1 = PORTD.3 // Data Read (in/out)
#bit RTR = PORTD.4 // signal for 877 to get ready to receive(in/out)
#bit CHK = PORTD.5 // For Checking any error in data received.(In)

#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)

int8 LAT,LON;

void TxByte(int8 data)
{
   int8 ctr;
   PORTD=0;   
   SET_TRIS_D(SysTx); // set the direction for Tx
   delay_ms(5);   
   CHK=Hi;
delay_ms(5);
   DTR=Hi;
delay_ms(5);   
PORTB=0xFF;   
   while (Go1==Lo);   

   for(ctr=0; ctr<8; ctr++)
   {
      while(CLK==Lo);
      output_bit(PIN_D2, shift_left(&data,1,0));
      while(CLK==Hi);
   }// end for
   CHK=Lo;   
   DTR=Lo;   
   delay_ms(2);
}//end TxByte

int8 RxByte(void)
{
   int8 ctr,data;

   SET_TRIS_D(SysRx); // set the direction is set for Rx
   delay_ms(5);
   while(CHK==Lo);
   while(DTR==Lo);   
   GO1=Hi; // No data read yet
   while (CLK==Hi);
   
   for(ctr=0; ctr<8; ctr++)
   {
      while (CLK==Hi); //wait for low edge of CLK     
      shift_left(&data,1,input(PIN_D2));
      while (CLK==Lo); //wait for high edge of CLK
   }
   
   GO1=Lo; // data has been read.
   while (DTR==Hi);
   RTR=Lo;
   delay_ms(2);
   return(data);
}//end getByte

float get_LAT_LON(void)
{
   int8 n,data, Deg, Min,Sec,ErrCtr;
   float tmp;
   for (n=0; n<3;n++)
   {
      do
      {
         data= RxByte();
PORTC=data;
         TxByte(data);
         delay_ms(5);
         ErrCtr++;
         if (ErrCtr>=9) return(0);
      }while (CHK==Hi);
     
      switch (n)
      {
      case 0:
         Deg=data;
         break;
      case 1:
         Min=data;
         break;
      case 2:
         Sec=data;
         break;
      }//end switch
 
   }//end for
   
   tmp=Deg+(Min/60) + (Sec/3600);
   return(tmp);
}

void get_GPS_Data(void)
{
   LAT=get_LAT_LON();

   LON=get_LAT_LON();
}//end get_GPS_Data

void SysInit(void)
{
   PORTD=0;
   SET_TRIS_D(SysInitz); //0x25 set the direction is set for Rx
   delay_ms(5);
   RTR=Hi;
   delay_ms(5);   
}//end SysInit

void main()
{
   SET_TRIS_A(0);
   SET_TRIS_B(0);   
   SET_TRIS_C(0);
   //Direction of PORTD is done in Send/Get byte routines
   SET_TRIS_E(0);   

   PORTA=0;
   PORTB=0;
   PORTC=0;
   PORTD=0;
   PORTE=0;

   delay_ms(100); //let the system stabilize

   while (TRUE)
   {
/*============Dummy Work Here============
Here actual tasking like reading switches,
vector Calculations and giving servo commands
shall be done here. The delay is to mimic the
above mentioned functions. This is to remove
when actual code is developed for all the
above activities.*/
delay_ms(500);
//=======================================
      SysInit();
      if (DTR==Hi)
      {
         get_GPS_Data();       
      }//end if
      RTR=Lo;
      delay_ms(5);
PORTE=~PORTE;     
   }//end while
   
}


================Code for Slave=====================
Code:
#include "16F877.h"
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#bit DTR = PORTD.0 // Data Terminal Ready (out)
#bit CLK = PORTD.1 // Clock (out)
//PIN_D2 is used for Rx/Tx // Data in/out (in/out)
#bit Go1 = PORTD.3 // Go (in/out)
#bit RTR = PORTD.4 // signal for 877 to get ready to receive(in/out)
#bit CHK = PORTD.5 // For Checking any error in data received.(out)

#define Hi 1
#define Lo 0

#define SysInitz 0x10
#define SysTx 0x18
#define SysRx 0x05

#use fast_io(A)
#use fast_io(B)   
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)

int8 LAT_Deg,LAT_Min,LAT_Sec;
int8 LON_Deg,LON_Min,LON_Sec;

int8 RxByte(void)
{
   int8 ctr,data;

   SET_TRIS_D(SysRx); // set the direction is set for Rx
   delay_ms(5);   

PORTB=0xFF;
   while(CHK==Lo);   
   while(DTR==Lo);   
   GO1=Hi; // No data read yet
   CLK=Hi;   
   for(ctr=0; ctr<8; ctr++)
   {
      CLK=Lo;
      delay_ms(2);     
      shift_left(&data,1,input(PIN_D2));
      CLK=Hi;
      delay_ms(2);     
   }
   GO1=Lo; // data has been read. 
   while (DTR==Hi);
   RTR=Lo;
   delay_ms(2);
   return(data);
}//end getByte

void TxByte(int8 data)
{
   int8 ctr;
   PORTD=0;
   SET_TRIS_D(SysTx); // set the direction for Tx
   delay_ms(5);   
   CLK=Hi;   
   CHK=Hi;
   DTR=Hi;
   
   while (Go1==Lo);
   for(ctr=0; ctr<8; ctr++)
   {
      output_bit(PIN_D2, shift_left(&data,1,0));
      CLK=Lo;
      delay_ms(2); // let the pin stabilize
      CLK=Hi;
   }// end for
   CLK=Lo;
   CHK=Lo;   
   DTR=Lo;
   delay_ms(2);
}//end sendByte

void SendByte_Confirm(int8 TxData)
{
   int8 RxData;//,ErrCtr;
      CHK=Lo;
     
      TxByte(TxData);
     
      RxData=RxByte();
PORTB=RxData;
PORTC=TxData;
      if (TxData!=RxData)
         CHK=Hi;
        else
         CHK=Lo;
}//end sendByte_Confirm

void SysInit(void)
{
   PORTD=0;
   SET_TRIS_D(SysInitz); // direction is set for Tx initially
   DTR=Hi;
   delay_ms(5);
}//end SysInit

void get_LAT_LON()
{
   //=======Assumed Values, GPS Data Read Code here===========
   LAT_Deg=78;
   LAT_Min=43;
   LAT_Sec=51;

   LON_Deg=34;
   LON_Min=28;
   LON_Sec=12;
}

void main(void)
{
   SET_TRIS_A(0);
   SET_TRIS_B(0);   
   SET_TRIS_C(0);
   //Direction of PORTD is done in Send/Get byte routines
   SET_TRIS_E(0);   
   
   PORTA=0;
   PORTB=0;
   PORTC=0;
   PORTD=0;
   PORTE=0;   
     
   delay_ms(100); //let the system to stabilize

   while (TRUE) 
   {
      get_LAT_LON();
      if (RTR==Hi)
      {
PORTE=~PORTE;     
         sysInit();     
         SendByte_Confirm(LAT_Deg);
//!         SendByte_Confirm(LAT_Min);
//!         SendByte_Confirm(LAT_Sec);
DTR=Lo;
      }

      delay_ms(100);
   }//end while
}
[/code]
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion 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