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 SET MULTI_MASTER

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



Joined: 09 Apr 2011
Posts: 1
Location: Taiwan

View user's profile Send private message

I2C SET MULTI_MASTER
PostPosted: Sun Apr 10, 2011 2:39 am     Reply with quote

I have to do is to use four dsPIC33 the value to an EEPROM using I2C transmission problems, I now hit me that the signal will be searched on the site also tried to sample a lot of information occur collision would like to ask the network signal Of you help me
Code:

#include <33FJ64MC802.h>
#fuses HS,NOWDT,NODEBUG,NOWRTSS,NOPROTECT ,PR_PLL,ALTI2C

#use delay (clock=80M, crystal=10M)

#pin_select U1TX = PIN_B4
#pin_select U1RX = PIN_B5

#use rs232(UART1,baud=115200,parity=N,bits=8)
#bit U1STA_UTXEN = 0x223.2

#use i2c(multi_master, scl=PIN_B8, sda=PIN_B9)

char cmd;
int  flag=0;
int16 data_a0=0,data_a1=0,data_read=0,x=0,POS1CNT=0,y=0;

/*void init_ext_eeprom()
{
   output_float(PIN_B8);
   output_float(PIN_B9);
}*/

void write()

     int status;
     int state;
     
     do
     {
         output_high(pin_a0);
         i2c_start();
         state=i2c_write(0xa0);
         if(state==0)
         {       
             if(y!=POS1CNT)
             {
                i2c_start();
                i2c_write(0xa0);
                i2c_write(0x01>>8);
                i2c_write(0x01);
                i2c_write(POS1CNT>>8);
                i2c_stop();
                i2c_start();
                status=i2c_write(0xa0);
                while(status==1)
                {
                   i2c_start();
                   status=i2c_write(0xa0);
                }
                i2c_stop();
             
                i2c_start();
                i2c_write(0xa0);
                i2c_write(0x02>>8);
                i2c_write(0x02);
                i2c_write(POS1CNT);
                i2c_stop();
                i2c_start();
                status=i2c_write(0xa0);
                while(status==1)
                {
                   i2c_start();
                   status=i2c_write(0xa0);
                }
                i2c_stop();
                y=POS1CNT;
            }
         }
         if(state==1)   i2c_stop();
         output_low(pin_a0);
     }while(state!=0);
}

int read1()
{
   int state;
   do
   {
      i2c_start();
      state=i2c_write(0xa0);
      if(state==0)
      {
         i2c_start();
         i2c_write(0xa0);
         i2c_write(0x01>>8);
         i2c_write(0x01);
         i2c_start();
         i2c_write(0xa1);
         data_a0=i2c_read(0);
         i2c_stop();
                       
         i2c_start();
         i2c_write(0xa0);
         i2c_write(0x02>>8);
         i2c_write(0x02);
         i2c_start();
         i2c_write(0xa1);
         data_a1=i2c_read(0);
         i2c_stop();
                 
         data_read=(data_a0<<8)+data_a1;
      }
      if(state==1)   i2c_stop();
   }while(state!=0);
   return data_read;
}

#int_RDA
void  RDA_isr()
{
    cmd = getchar();
    switch(cmd)
    {
        case 'S':                                                                       
                 U1STA_UTXEN = 1;
                 break;

        case 'E':                                                                       
                 U1STA_UTXEN = 0;
                 break;
                 
        case 'Q':
                 flag=1;
                 break;
                 
        case 'W':
                 flag=0;
                 break;
                 
     }
}

#INT_TIMER1
void  TIMER1_isr( )
{
   set_timer1(65535-125);
   x++;
   
   if(x == 100)
   {
      x=0;
      write();
   } 
}

void main (void)
{   
    enable_interrupts(INT_RDA);
    enable_interrupts(INT_TIMER1);
    enable_interrupts (INTR_GLOBAL);     
 
    setup_timer1(TMR_INTERNAL|TMR_DIV_BY_8);
   
    U1STA_UTXEN = 0;

    //init_ext_eeprom();
   
    POS1CNT=read1();

    while (1)
    {
         output_high(pin_b15);
         if(flag==1)
         { 
            printf("\r\nPOS1CNT:%d",POS1CNT++);
         }
    }   
}
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