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 : multiple read

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



Joined: 06 Jul 2010
Posts: 3

View user's profile Send private message

I2C : multiple read
PostPosted: Tue Jul 06, 2010 9:31 am     Reply with quote

Hi,

I have a problem with my project. The master do some loops and freeze at the second i2c_read (after PIN_A2 turned on).
I tried delay, i2c settings but randomly (from 1 to 27 loops), the master stops.

Here the slave complete .h code :
Code:

#include <18F252.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOOSCSEN                 //Oscillator switching is disabled, main oscillator is source
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOCPD                    //No EE protection
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads

#use delay(clock=10000000)
#use i2c(Slave,Fast,sda=PIN_C4,scl=PIN_C3,address=0x20)

int state;

int data;
int data1r, data2r;
int data1w = 15;
int data2w = 240;


The slave complete .c code :
Code:

#include "main.h"

#ZERO_RAM

#int_SSP
void SSP_isr(void) {
   state = i2c_isr_state();
   if(state < 0x80) {
      data = i2c_read();
      switch(state) {
         case 1: data1r = data; break;
         case 2: data2r = data; break;
      }
   }
   else {
      state -= 0x80;
      switch(state) {
         case 0: i2c_write(data1w); break;
         case 1: i2c_write(data2w); break;
      }
   }
}

void main() {
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   
   while(true) {
      if(data1r == 'A' && data2r == 'B') output_high(PIN_B0);
      else output_low(PIN_B0);
   }
}


The master complete .h code :
Code:

#include <18F2550.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES MCLR                     //Master Clear pin enabled
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL1
#FUSES CPUDIV1
#FUSES NOUSBDIV
#FUSES VREGEN                   //USB voltage regulator enabled

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Fast,sda=PIN_B0,scl=PIN_B1)

int data1, data2;


The master complete .c code :
Code:

#include "main.h"

#ZERO_RAM

#int_RDA
void RDA_isr(void) {
   
}

#int_SSP
void SSP_isr(void) {
   
}

void main() {
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_RDA);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   
   while(true) {
      i2c_start();
      i2c_write(0x20);
      i2c_write('A');
      i2c_write('B');
      i2c_stop();
     
      output_high(PIN_A0);
      delay_ms(500);
     
      i2c_start();
      i2c_write(0x21);
      data1 = i2c_read(1);
      output_high(PIN_A2);
      data2 = i2c_read(0); // RANDOMLY FREEZE HERE !!!
      output_low(PIN_A2);
      i2c_stop();
     
      if(data1 == 15 && data2 == 240) output_high(PIN_A1);
      else output_low(PIN_A1);
     
      output_low(PIN_A0);
      delay_ms(500);
   }
}


Thanks a lot in advance.

TiX
tixmcprods



Joined: 06 Jul 2010
Posts: 3

View user's profile Send private message

PostPosted: Tue Jul 06, 2010 10:29 am     Reply with quote

Re,

With "SLOW" settings in master and slave, it doesn't freeze anymore.
Both signal and data are OK.

Have you an idea why in "FAST" speed I have that problem ?

Thanks in advance.

TiX
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Jul 06, 2010 12:07 pm     Reply with quote

Just at a quick glance, I noticed that your master is running at 20MHZ while your slave is running at 10MHZ. I would have them swapped. In my opinion, your slave should be running faster than the master. Odds are, your master, in Fast mode, is sending commands faster than the slave can process them and is probably just barely keeping up until it misses one command and then freezes.

Ronald
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