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

I2c Slave receiving Problem on Proteus

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
mateeb4u@gmail.com



Joined: 07 Feb 2012
Posts: 19
Location: pakistan

View user's profile Send private message Send e-mail Yahoo Messenger

I2c Slave receiving Problem on Proteus
PostPosted: Tue Feb 07, 2012 11:50 pm     Reply with quote

I'm trying to communicate to pic 18f452. First I try on Proteus, at master side it sends the data correctly but at the receiver side it is not responding. I'm posting codes and images.

MASTER:
Code:
 
#include<18F452.h>
#use delay(clock = 4000000)
#use I2C(MASTER, SDA=PIN_B1, SCL=PIN_B0,FAST=400000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
int first_addr=0,last_addr=0;
#fuses NOWDT,NOPROTECT,NOLVP,XT

BYTE write_bit=0x0,read_bit=0x01;
BYTE address=0x60;

BYTE merge (BYTE address,BYTE flag)
{
  BYTE out_data;
  out_data=((address<<1)|(flag));// to read or write
  return out_data;
}

BYTE write(BYTE data )  ///by using default function //it returns ack//problem
{
   int ack;
   if(first_addr==0)
    {
      i2c_start();    // Start condition
      ack=i2c_write(merge(data,write_bit));// Device address///return ack error
      printf("data=%x\n",merge(data,write_bit));
      //delay_ms( 2000 );
      first_addr++;
    }
   else
    {
        ack=i2c_write(data);
   printf("data=%x\n",data);
   //delay_ms( 2000 );
        last_addr++;
        if(last_addr==5)
            {
              i2c_stop();     // Stop condition
              last_addr=0;
            }
       }
return ack;
}

BYTE next_data (int num)
   {
    BYTE temp;
     switch(num)
      {
       case 0:
       temp=address;
       return temp;

       case 1:
       temp=0xB;
       return temp;

       case 2:
       temp=0xF3;
       return temp;

       case 3:
       temp=0x0;
       return temp;

       case 4:
       temp= 0x10  ;
       return temp;

       case 5:
       temp=0x0;
       return temp;
     }
  }

void main (void)
{
   BYTE ack;
   int i;
   BYTE data;//problem of 1 byte
   port_b_pullups(TRUE);
     for(i=0;i<=5;i++)
      {
       data=next_data(i);
        ack=write(data);
        //if(ack==0x01)   //if there is no ack//to restart transmission
        // i=0;
      }
}


SLAVE:
Code:
 
#include <18F452.h>   
#fuses NOWDT,NOPROTECT,NOLVP,XT   
#use delay(clock=4000000)   
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)   
 
#use i2c(SLAVE, sda=PIN_B0, scl=PIN_B1, address=0x60,FORCE_HW)   
   
BYTE datar;
BYTE dataw=0x60; 
   
 #INT_SSP   
 void ssp_interupt ()   
 {   
    BYTE  state;   
 

      //printf("*/n");
      state = i2c_isr_state();    //0 - Address match received with R/W bit clear

                                  //1-0x7F - Master has written data; i2c_read() will immediately return the data
 
                                  //0x80 - Address match received with R/W bit set; respond with i2c_write()

                                  //0x81-0xFF - Transmission completed and acknowledged; respond with i2c_write()

      if(state == 0x00)
         {
          datar=i2c_read();
          printf("Adress match:%x\n",datar);
          datar=0x00;
        }

      else if(state > 0 && state < 0x80)
        {
         datar= i2c_read();
         printf("Bytes:%x\n",datar);
        datar=0x00;
        }
      else if(state == 0x80)
        {
         i2c_write(dataw);
          printf("Data write on Master:%x\n",dataw);
       }
      else if(state > 0x80 && state < 0x100)
        {
        printf("Data has been acknowledged\n");
       }
   

 }   
   
 void main ()   
 { 
    port_b_pullups(TRUE); 
    enable_interrupts(GLOBAL);   
    enable_interrupts(INT_SSP);   
   
    while (TRUE) {}   
 }


[img]https://mail-attachment.googleusercontent.com/attachment?ui=2&ik=d701f24f53&view=att&th=1355b82a18454c5f&attid=0.1&disp=inline&realattid=f_gydxutpe0&safe=1&zw&saduie=AG9B_P-t9JviJqK6ghtP39UDqSb_&sadet=1328680208998&sads=oTXFmSiCwv_KMYeFGo_65uQu5PY&sadssc=1[/img]
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Wed Feb 08, 2012 5:34 am     Reply with quote

1) Forget Proteus. It is massively buggy on the PIC peripherals. Individual ones may work on some PIC's, but exactly the same peripheral then doesn't on another.

2) Get rid of the printf's in the receive code. These _will_ stop it from having any hope of working. If you want to debug what is happening, set a number as you go through the receive, and have the loop in the main check when this changes and print it then reset it.
Fundamental problem is that with I2C, things happen too fast _and have to be reacted to quickly_, meaning there is not time to be spent printing in the ISR....

Best Wishes
mateeb4u@gmail.com



Joined: 07 Feb 2012
Posts: 19
Location: pakistan

View user's profile Send private message Send e-mail Yahoo Messenger

Thanks
PostPosted: Thu Feb 09, 2012 5:33 am     Reply with quote

Thanks
https://picasaweb.google.com/115553796405453974841/February82012?authuser=0&feat=directlink
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