Compass Click board basic I2C issues

General discussion on mikroC PRO for PIC.
Author
Message
djm2
Posts: 37
Joined: 23 Dec 2011 11:37

Compass Click board basic I2C issues

#1 Post by djm2 » 05 Oct 2013 12:47

Hi there

I have a PIC18F45K22 running at 8MHz and I am programming in MikroC Pro with the easy pic 7 development board.

I am trying to write some code to work with the compass click. However I am stuck already!

I have the following code -

Code: Select all

  
  ANSELB = 0;                // Configure PORTB pins as digital
  ANSELC = 0;                // Configure PORTC pins as digital

  TRISB = 0;                 // Configure PORTB as output
  LATB = 0;                  // Clear PORTB
  
  
    I2C1_Init(100000);
    I2C1_Start();        //Sets up the accelerometer registers to set to "normal mode"
    I2C1_Wr(0x32);
    I2C1_Wr(0x20);
    I2C1_Wr(0b00100111);
    I2C1_Stop();
I have a logic analyser and I can see it is sending the 0x32 however, there is no acknowledge back from the compass module. When my code runs all I see is the transmission of 0x32 and then that is it. I am getting the address of 0x32 from this datasheet on page 20 - http://www.st.com/st-web-ui/static/acti ... 027543.pdf. I would expect the compass module to send an acknowledge and then the rest of the data would flow.

I have tried both mikro bus sockets not that I can see it would make any difference and it made no difference :-). I have connected the logic analyser straight to the pins on the compass module to make sure the signal is getting there.

I do not have pull up resistors enabled on the main baord as the compass module has them installed.

djm2
Posts: 37
Joined: 23 Dec 2011 11:37

Re: Compass Click board basic I2C issues

#2 Post by djm2 » 06 Oct 2013 17:57

Hi there

Been looking at this most of the weekend and found something else which seems to be caused by the same issue.

Quite some time ago I made this post here - http://www.mikroe.com/forum/viewtopic.p ... t=pressure and just gave up on the board.

I have gone back to the example code because it runs on I2C and tried again. And again I get x2 transmissions like I previously reported. This is where the two transmissions occur -

Code: Select all

  Pressure_Write(_RES_CONF, 0x78);           // Initiating the pressure resolution on Pressure Click with writing HEX value in his register ( in this case HEX 0x78)
  temp = Pressure_Read(_RES_CONF);           // Begining of checking of entered value in the register _RES_CONF of the Pressure Click
The first transmission is from the write and the second from the read as far as I can tell. If I leave just one of the two lines in I get only one transmission and I played about with the read and write routines to make sure as well ie sending different numbers. This seems to be the same issue as I am having with the compass board ie only 1 byte is transmitted per write routine and the byte is not acknowledge by the receiving device ie click board.

I tried the debugger and found the code seems to get looped up here -

Code: Select all

L_I2C1_Start8:
0x0378        0xA0C5              BTFSS       SSP1CON2, 0 
0x037A        0xD001              BRA         L_I2C1_Start9
;__Lib_I2C_c34d01.c,54 ::                 
0x037C        0xD7FD              BRA         L_I2C1_Start8
It would go from the BTFSS to the last line and then start over again.

The main issue I seem to be having is that the devices are not returning acknowledges to the PIC and the transmissions seems to end at this point.
  • I assume the I2C address are correct because it does not even work with the example code.
    The signal is definitely getting to the click board I have the logic analyser connected to the pins on the click board.
    I tried the example I2C code for the EEPROM and it worked!
    I compared the EEPROM code to my code and the only differences was the data values being sent ie address/registers etc.
I am sure this is some stupid oversight/mistake on my part don't know if anyone can suggest anything else?

David

Pugnator
Posts: 70
Joined: 03 May 2011 17:20
Location: Moscow, Russia

Re: Compass Click board basic I2C issues

#3 Post by Pugnator » 06 Oct 2013 22:17

Look, I met exactly the same problem here
http://www.mikroe.com/forum/viewtopic.p ... 63#p222963

djm2
Posts: 37
Joined: 23 Dec 2011 11:37

Re: Compass Click board basic I2C issues

#4 Post by djm2 » 08 Oct 2013 08:52

Hi there

Thanks for the reply.

I guess you are no further forward either?

I tried the EEPROM example code and it worked okay for me but it doesn't make any sense. All I am doing is changing the values sent on the I2C to the correct values for the pressure and compass sensors and nothing happens.

Another strange thing I noticed is that if I use the code below it creates a start condition on the I2C! If I include a start line after the init and nothing else I get 2 start conditions with the second start condition remaining permanently low.

Code: Select all

 ANSELB = 0;                // Configure PORTB pins as digital
  ANSELC = 0;                // Configure PORTC pins as digital

  TRISB = 0;                 // Configure PORTB as output
  LATB = 0;                  // Clear PORTB
  
    Delay_ms(10);
    I2C1_Init(100000);
I am running out of ideas of things to check or try.

David

Pugnator
Posts: 70
Joined: 03 May 2011 17:20
Location: Moscow, Russia

Re: Compass Click board basic I2C issues

#5 Post by Pugnator » 08 Oct 2013 13:53

djm2 wrote:Hi there
Thanks for the reply.
I guess you are no further forward either?
I am running out of ideas of things to check or try.
I will try to take an trace with my dual oscilloscope. It will be clear what is going on.

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: Compass Click board basic I2C issues

#6 Post by marina.petrovic » 09 Oct 2013 13:07

Hi,

Did you try to pull-up SCL and SDA lines?
Also, check whether you turn-off LEDs on your EasyPIC v7 board.

Can you try to load pre-compiled HEX from Pressure click example on your hardware,
just to see whether you have the same behavior in that situation?

Best regards,
Marina

djm2
Posts: 37
Joined: 23 Dec 2011 11:37

Re: Compass Click board basic I2C issues

#7 Post by djm2 » 09 Oct 2013 13:16

Hi there

Yes I have tried the code with and without the pull up resistors on SW4. I have also tried the supplied .hex file with the same setup from the examples given. And the LED's are definitely turned off.

Unfortunately it still does not work.

David

Pugnator
Posts: 70
Joined: 03 May 2011 17:20
Location: Moscow, Russia

Re: Compass Click board basic I2C issues

#8 Post by Pugnator » 09 Oct 2013 14:39

here is my results.
Tested on mikroC 6.0.0
EasyPIC v7.0 with 18F4550 onboard, 5V, 8MHz
PullUp switches are on.
I2C device is connected to RB0/RB1

Here is my init part

Code: Select all

  ADCON1 = 0x8;
  GIE_bit = 0;
  PEIE_bit = 0;
  TRISA = 0xFF;
  TRISB = 0;
  CMCON = 0x7;
  TMR0L = 0;
  TMR0H = 0;
  T0CS_bit = 0;
  PSA_bit = 0;
  T0PS0_bit = 0;
  T0PS1_bit = 0;
  T0PS2_bit = 0;
  T08BIT_bit = 1;
  T0SE_bit = 0;
  TMR0IF_bit = 0;
  TMR0IE_bit = 1;
  TMR0ON_bit = 0;
It hangs SOMETIMES on write procedure.
Tested on different devices/chips

Code: Select all

I2C1_init(100000);
I2C1_Start();
I2C1_Wr(0x3C);
I2C1_Wr(0x0);
I2C1_Wr(0x71);
I2C1_Stop();
If I manually pull SDA down, for example by pushing the button - it works further untill next Write cycle
I also tested it in Proteus - it compliance about clock fluctuations

djm2
Posts: 37
Joined: 23 Dec 2011 11:37

Re: Compass Click board basic I2C issues

#9 Post by djm2 » 09 Oct 2013 15:17

Hi there

Tried out your code just now. I don't have a 18F4550 so I used a PIC18F45K22. Had to comment out the CMCON = 0x7.

Tried it with the switches on and off. Tried numerous times and it hangs on the Start every time. It generates a start signal on the SDA/SCK lines and then does nothing else. (No I2C devices connected to the bus during this time.)

Tried the code with a 18F4685 and it writes the first byte and then nothing else.

I then tried your template on the same 18F4685 and copied the code for the EEPROM and then it worked.

Regards

David

Pugnator
Posts: 70
Joined: 03 May 2011 17:20
Location: Moscow, Russia

Re: Compass Click board basic I2C issues

#10 Post by Pugnator » 09 Oct 2013 15:51

Let's try to write code in assembler for I2C just to test the board connections

djm2
Posts: 37
Joined: 23 Dec 2011 11:37

Re: Compass Click board basic I2C issues

#11 Post by djm2 » 09 Oct 2013 16:06

Hi there

Its an idea.

But I have exactly the same setup as is used in the pressure click example code. I have even tried running with the .hex file provided and setup and have the same issues. Mikro say that the code works at there end but it does not work here. I have connected to the legs of the click module and I can see the first byte being sent.

David

Pugnator
Posts: 70
Joined: 03 May 2011 17:20
Location: Moscow, Russia

Re: Compass Click board basic I2C issues

#12 Post by Pugnator » 10 Oct 2013 01:17

My problem solved by dirty adding delay between setting Start/Stop.

djm2
Posts: 37
Joined: 23 Dec 2011 11:37

Re: Compass Click board basic I2C issues

#13 Post by djm2 » 10 Oct 2013 08:49

Hi there

Can you post your code so I can see what you mean?

David

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: Compass Click board basic I2C issues

#14 Post by marina.petrovic » 10 Oct 2013 15:49

Hi,

Please, djm2, can you take a look at your Pressure click to see whether the jumpers (J1 - J4)
are set properly (I2C)?
Also, check I2C address (I2C ADD) jumper?

Can you send me the project that you wrote for Compass click on EasyPIC v7 board so I can try it on my hardware?

Best regards,
Marina

Pugnator
Posts: 70
Joined: 03 May 2011 17:20
Location: Moscow, Russia

Re: Compass Click board basic I2C issues

#15 Post by Pugnator » 11 Oct 2013 09:05

djm2 wrote:Hi there

Can you post your code so I can see what you mean?

David
Here what worked for me, used with digital compass hmc5843

Code: Select all

uint16_t read_hmc5843(uint8_t reg_adr)
{
  if ( I2C1_Start( ))
  {    
    goto i2cstop;
  }
  if ( I2C1_Wr(HMC5843_W))
  {  
    goto i2cstop;
  }
  if ( I2C1_Wr(reg_adr))
  {   
    goto i2cstop;
  }
  I2C1_Repeated_Start( );
  if ( I2C1_Wr(HMC5843_R))
  {   
    goto i2cstop;
  }
  i2c_result = I2C1_Rd(0);
i2cstop:
  I2C1_Stop( );
  return i2c_result;
}

uint16_t write_hmc5843(uint8_t reg_adr, uint8_t _data)
{
  if ( I2C1_Start( ))
  {    
    goto i2cstop;
  }
  if ( I2C1_Wr(HMC5843_W))
  {   
    goto i2cstop;
  }
  if ( I2C1_Wr(reg_adr))
  {  
    goto i2cstop;
  }
  if ( I2C1_Wr(_data))
  {    
    goto i2cstop;
  }
i2cstop:
  I2C1_Stop( );
}

Post Reply

Return to “mikroC PRO for PIC General”