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 CCS Technical Support

UART PIC to PIC communication

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



Joined: 28 Sep 2008
Posts: 7

View user's profile Send private message Yahoo Messenger

UART PIC to PIC communication
PostPosted: Thu Mar 05, 2009 4:29 am     Reply with quote

Hi, i want to communicate multiple PIC16F877a. One is sort of a master and the rest are slaves. I am using CCS version 4.084 with MPLAB ICD2 debugger. Sorry for the long code.

Here is the code of the master:
Code:
#if defined(__PCM__)
#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#DEVICE *=16 //ICD=TRUE ADC=8
#use Delay (Clock=8000000)
#use i2c (SLAVE,sda=PIN_C4,scl=PIN_C3,address=0xa0,force_hw)
#use rs232(Baud=9600,Xmit=PIN_C6,Rcv=PIN_C7,parity=o)
#priority ssp, rda

enum {POLL,CTRL_ON,CTRL_OFF};
//int mode;
short int SOT_i2c=0,SOT=0,isPoll=0,isUpdate=0,isSendToAll=0,isBlink=0;
int maxOn[2], modemConf[5], priority[2][5];
int i2cBuff[17], packetType;
int ctr1,ctr2;
//byte msg;
unsigned char TXBuff[6], RCBuff[5];
unsigned int slaveAdd;
int ch;

int setCheckSum()
{
   long Tsum = 0;
   int TcheckSum = 0;
   for (ctr1=1;ctr1<4;ctr1++)
   {
      Tsum = Tsum + TXBuff[ctr1];
   }
   TcheckSum = make8(Tsum,0);
   return TcheckSum;
}

int getCheckSum()
{
   long Rsum = 0;
   int RcheckSum = 0;
   for (ctr1=0;ctr1<3;ctr1++)
   {
      Rsum = Rsum + RCBuff[ctr1];
   }
   RcheckSum = make8(Rsum,0);
   return RcheckSum;
}

void TXBuff_Init()
{
  for (ctr1=0;ctr1<5;ctr1++)
  {
   TXBuff[ctr1] = 0;
  }
  TXBuff[0] = 0x7E;
//Addressing 
  if (slaveAdd == 2)
  {
   TXBuff[1] = 0x22;
  }
  else if (slaveAdd == 1)
  {
   TXBuff[1] = 0x11;
  }
  else if (slaveAdd == 3)
  {
   TXBuff[1] = 0x33;
  }
  else if (slaveAdd == 4)
  {
   TXBuff[1] = 0x44;
  }
  else if (slaveAdd == 5)
  {
   TXBuff[1] = 0x55;
  }

  TXBuff[2] = 0xA5; // Source address
 //PAcket Type for Master2
  if (packetType == POLL)
  {
  TXBuff[3] = 0xFF;
  }
  if (packetType == CTRL_ON)
  {
  TXBuff[3] = 0xCA;
  }
  if (packetType == CTRL_OFF)
  {
  TXBuff[3] = 0xC3;
  }
  TXBuff[4] = setCheckSum();
  TXBuff[5] = 0x04;
}

#INT_SSP
void ssp_interrupt()
{
if(i2c_poll())
{
ch = i2c_read(0); // read from i2c
      if (ch == 0xF1)
      {
         isPoll = 1;
         packetType = POLL;
         slaveAdd = 1;
      }
      else if (ch == 0xF2)
      {
         isPoll = 1;
         packetType = POLL;
         slaveAdd = 2;
      }
      else if (ch == 0xF3)
      {
         isPoll = 1;
         packetType = POLL;
         slaveAdd = 3;
      }
      else if (ch == 0xF4)
      {
         isPoll = 1;
         packetType = POLL;
         slaveAdd = 4;
      }
      else if (ch == 0xF5)
      {
         isPoll = 1;
         packetType = POLL;
         slaveAdd = 5;
      }
      else if (ch == 0x91)  //I2C Activate Control Setting
      {
         isPoll = 1;
         
      }
      else if (ch == 0x92)  //I2C ShutOff All Device
      {
         isSendToAll= 1;
         packetType = CTRL_ON;
      }
      else if (ch == 0x94)  //I2C Disable All Control
      {
         isSendToAll= 1;
         packetType = CTRL_OFF;
      }
      else if ((ch==0x41)&&(SOT_i2c==0))
      {
         SOT_i2c = 1;
         ctr1=0;
      }
      else if ((ch==0x04)&&(SOT_i2c==1))
      {
      isUpdate = 1;
      SOT_i2c = 0;
      }
      else
      {
         if (SOT_i2c==1)
         {
         i2cBuff[ctr1]=ch;
         ctr1++;
         }
      }
}      
}

#INT_RDA
void rcv_interrupt()
{
   int rcv;
    rcv = getc(); // read from i2c
   if ((rcv == 0x7E) && (SOT == 0))
   {
      SOT = 1;
      isBlink = 1;
      ctr1 = 0;
      for(ctr2=0;ctr2<4;ctr2++)
      {
      RCBuff[ctr2] = 0;
      }
   }
   else if ((rcv == 0x04) && (SOT == 1))
   {
      SOT = 0;
      //if (RCBuff[3] == getCheckSum())
      //{
       if (RCBuff[0] == 0xA5) // Check if packet is for Slave 1
          {
      
            if (RCBuff[2] == 0x5A) // Check if packet is POLL
            {
            
         }
          if (RCBuff[2] == 0x53) // Check if packet is CTRL ON
            {
            }
          }
      //} // Checksum      
   }
   else
   {
      if (SOT == 1)
      {
         RCBuff[ctr1] = rcv;
         ctr1++;
      }
   }
}

void main ()
{
   delay_ms(500);
   enable_interrupts(INT_RDA);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   set_tris_d(0x00);
   set_tris_c(0xFF);
   output_D(0x01);
   delay_ms(1000);
   output_D(0x00);
   delay_ms(1000);
   isPoll = 0;
   isBlink = 0;
   SOT_i2c = 0;
   while(TRUE)
   {
      if(isPOLL==1)
      {
   //   output_high(PIN_D0);
      TXBuff_Init();
            for(ctr1=0;ctr1<=5;ctr1++)
            {
            putc(TXBuff[ctr1]);
            delay_us(100);
            }
   //   delay_ms(500);
   //   output_low(PIN_D0);
      isPoll=0;
      enable_interrupts(INT_RDA);
      enable_interrupts(INT_SSP);
      }

      if(isBlink==1)
      {
      delay_ms(100);
      output_high(PIN_D0);
      delay_ms(500);
      output_low(PIN_D0);
      isBlink=0;
      enable_interrupts(INT_RDA);
      enable_interrupts(INT_SSP);
      }

      
   }
}



and here is the slave:

Code:

#if defined(__PCM__)
#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#DEVICE *=16 //ICD=TRUE ADC=8
#use Delay (Clock=8000000)
#use rs232 (baud = 9600, Xmit=PIN_C6, RCV=PIN_C7, parity=o)
#priority rda,

#BYTE ADCON0 = 0x1F
#BYTE ADCON1 = 0x9D

//int slaveAddress = 0x11;
enum {POLL,CTRL_ON,CTRL_OFF,STATUS,ACK};
short int SOT=0,isPOLL,off_flag;
int RCBuff[4],TXBuff[6];
int ctr1, ctr2;
unsigned int ACMonitor, packetType;
 
int setCheckSum()
{
   long sum = 0;
   int checkSum=0;
   for (ctr1=1;ctr1<4;ctr1++)
   {
      sum = sum + TXBuff[ctr1];
   }
   checkSum = make8(sum,0);
   return checkSum;
}

int getCheckSum()
{
   long sum = 0;
   int checkSum;
   for (ctr1=0;ctr1<3;ctr1++)
   {
      sum = sum + RCBuff[ctr1];
   }
   checkSum = make8(sum,0);
   return checkSum;
}

void TXBuff_Init()
{
  TXBuff[0] = 0x7E;
//Addressing 
  TXBuff[1] = 0xA5;
  TXBuff[2] = 0x33;
//PAcket Type for SLAVE
  if (packetType == STATUS)
  {
      if (off_flag == 1)
    {
    TXBuff[3] = 0x53;
    }
    else
    {
    TXBuff[3] = 0x5A;
    }
  }
  else if (packetType == ACK)
  {      
     TXBuff[3] = 0x66;    
  }
   //TXBuff[4] = setCheckSum();
    TXBuff[4] = 0x04;
}

#INT_RDA
void rcv_interrupt()
{
   int ch;
//   disable_interrupts(int_ad);
   if (kbhit())
   {
    ch = getc(); // read from i2c
   if ((ch == 0x7E) && (SOT == 0))
   {
      SOT = 1;
      ctr1 = 0;
      for(ctr2=0;ctr2<4;ctr2++)
      {
      RCBuff[ctr2] = 0;
      }
   }
   else if ((ch == 0x04) && (SOT == 1))
   {
      SOT = 0;
      if (RCBuff[3] == getCheckSum())
      {
       if (RCBuff[0] == 0x33) // Check if packet is for Slave 3
          {
            if (RCBuff[2] == 0xFF) // Check if packet is POLL
            {
            isPOLL = 1;
         }
          if (RCBuff[2] == 0xCA) // Check if packet is CTRL ON
            {
            output_high(PIN_D1);
            }
         if (RCBuff[2] == 0xC3) // Check if packet is CTRL_OFF
            {
            output_low(PIN_D1);
         }
          }
      } // Checksum      
   }
   else
   {
      if (SOT == 1)
      {
         RCBuff[ctr1] = ch;
         ctr1++;
      }
   }
}
}

void ADC_check()
{
 ACMonitor=read_adc();
// disable_interrupts(int_rda);
 if (ACMonitor > 0x6C)
 {
  output_high(PIN_D7);
  off_flag = 0;
 }
 else
 {
   output_low(PIN_D7);
   off_flag=1;
 }
// enable_interrupts(int_rda);
}

void main ()
{
   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_DIV_32);
//   enable_interrupts(INT_AD);  //enable interrupt on adc
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   set_tris_d(0x00);
   output_d(0x81);
   delay_ms(1000);
   output_d(0x00);
   delay_ms(1000);

   while(TRUE)
   {
      ADC_check();
      if(isPOLL==1)
      {
      packetType = STATUS;
       TXBuff_Init();
         for(ctr1=0;ctr1<=5;ctr1++)
         {
         putchar(TXBuff[ctr1]);
         delay_us(100);
         }
      }
      output_high(PIN_D0);
      delay_ms(1000);
      output_low(PIN_D0);
//      ACMonitor = read_adc();
//      disable_interrupts(int_ad);
      isPOLL = 0;
      enable_interrupts(int_rda);
      }
   }
}


the problem is that Master PIC does not receive anything after Polling. The Slave responds to the POLL if the address is correct. Can you help me point out the problem. tnx for any help.
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