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 issue after a long absence

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



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

i2c issue after a long absence
PostPosted: Thu Apr 19, 2018 3:40 pm     Reply with quote

I last used my CCS compiler in Oct of 2012. Program has been running great for 5 1/2 years. A customer needs a special modification so I renewed my license, download the latest version of CCS PCWH, upgraded to MPLAB X, converted the project, and finally recompiled my old files with the new compiler. The program will not run.

I reflashed with original hex file, runs great.

I reflash again with new hex file, will not run.

Rolled back to MPLAB 8, recompiled with new compiler, will not run.

Created a project in the CCS IDE, recompiled with new compiler, will not run.

Compared all three hex files created with new compiler using three different IDEs. With the exception of the creation time stamp at the end of the file, they are identical.

Loaded the new hex file into the Microchip IPE and verified that the config registers are the same between my old file and new file, identical.

Ran debug in MPLAB X and the program starts but hangs on:

#use i2c(master, sda=TEMP_SDA, scl=TEMP_SCL)

Commented out my initialization of the I2C and program continues as I can now read data I placed in the EEPROM.

I read the manual, saw no changes in '#use i2c' usage. Looked at the assembly listing and there have been significant changes in the code created upon a call to '#use i2c'. Code created for calls to '#use rs232' and '#use delay' have not changed.

Has there been a significant change in the I2C module over the past 5 1/2 years that I'm not aware of that might affect older code?
_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 19, 2018 3:54 pm     Reply with quote

1. Tell us the full part number of the PIC that you're currently using and
also the one that you used in the past.

2. Tell us the Vdd voltage of the PIC (then and now), and the i2c pull-up
voltage, and the Vdd of the i2c slave.

3. Post the pull-up resistors value.

4. Tell us the actual pin numbers used for i2c (then and now).
For example, PIN_C3 and PIN_C4 for SCL and SDA.

5. Tell us the oscillator frequency of the PIC, i.e., the value used in
#use delay(clock= ).

6. Tell us what the i2c slave chip is, and how many of them are on the
same i2c bus.
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Apr 19, 2018 4:31 pm     Reply with quote

hmmm.
A customer needs a special modification so I renewed my license,

Since the old code worked fine, what is the 'special modification' ? Is it hardware or software ?
Now assuming ALL the hardware is as it was 6 years ago, that points to a software problem.
Have to ask, why the upgrade?, assuming a software 'special modifcation', why couldn't you use the old compiler version as well as the programmer or IDE program?

If you've made a hardware change, run PCM Ps 'I2C Scanner' program and report if it sees the I2C devices at their correct addreses.
Does this fail on only one PCB and/or one PIC ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19497

View user's profile Send private message

PostPosted: Fri Apr 20, 2018 12:20 am     Reply with quote

There is one critical difference in #USE I2C.

Back at 4.121, software I2C was the default even if the hardware pins were used. With V5 compilers this was changed, and hardware I2C will be used if the hardware pins are selected.

It looks like your old code was using software I2C, and probably therefore a slower I2C rate. The new code is using the hardware peripheral.

#use i2c(master, sda=TEMP_SDA, scl=TEMP_SCL, FORCE_SW)

Should make it function like the old code.

You may well find it works OK, if you simply limit the clock rate. So:

#use i2c(master, sda=TEMP_SDA, scl=TEMP_SCL, SLOW)

The manual does give the clue for this. The old V4 manual:
Quote:

Software functions are generated unless the FORCE_HW is specified.


The V5 manual:
Quote:

If hardware pins are specified for SDA and SCL, then hardware functions are generated unless the force_sw is specified;
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Fri Apr 20, 2018 9:04 am     Reply with quote

PCM programmer wrote:
1. Tell us the full part number of the PIC that you're currently using and
also the one that you used in the past.

2. Tell us the Vdd voltage of the PIC (then and now), and the i2c pull-up
voltage, and the Vdd of the i2c slave.

3. Post the pull-up resistors value.

4. Tell us the actual pin numbers used for i2c (then and now).
For example, PIN_C3 and PIN_C4 for SCL and SDA.

5. Tell us the oscillator frequency of the PIC, i.e., the value used in
#use delay(clock= ).

6. Tell us what the i2c slave chip is, and how many of them are on the
same i2c bus.


I apologize if I was not clear in my first post.

My hardware has not changed between then and now. I have not reached that stage yet. My firmware I'm testing with has not changed between then and now. I have not reached that stage yet. As a result, everything is the same. I simply recompiled the exact same code for the exact same instrument with the new compiler (and even tried three different IDEs).

So to answer your questions:

1. PIC18LF2520 and PIC18LF2520
2a. 5Vdc and 5Vdc
2b. 5Vdc and 5Vdc
2c: 5Vdc and 5Vdc
3. 47K (high, I know but engineer worked with Microchip app engineer to push limit) and 47K
4. SCL- PIN_C3, SDA - PIN_C4 and SCL - PIN_C3, SDA - PIN_C4
5. 4MHz (4000000) and 4MHz (4000000)
6. MCP9800AOT (only device on the bus) and MCP9800AOT (only device on the bus)

I have the hex file from code compiled with V4.121. Works.
I have the hex file from code compiled with V5.078. Does not work.

I'll locate and try different instruments.

Ttelmah wrote:
Back at 4.121, software I2C was the default even if the hardware pins were used. With V5 compilers this was changed, and hardware I2C will be used if the hardware pins are selected.


Exactly what I was looking for and I'll try your suggestions. Thanks.

Interesting enough, I have a hex file from code compiled with V5.036 in which an engineer was making a small firmware change during testing. It works on my instrument. :/
_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Fri Apr 20, 2018 10:23 am     Reply with quote

Ttelmah wrote:
There is one critical difference in #USE I2C.

Back at 4.121, software I2C was the default even if the hardware pins were used. With V5 compilers this was changed, and hardware I2C will be used if the hardware pins are selected.

It looks like your old code was using software I2C, and probably therefore a slower I2C rate. The new code is using the hardware peripheral.

#use i2c(master, sda=TEMP_SDA, scl=TEMP_SCL, FORCE_SW)

Should make it function like the old code.

You may well find it works OK, if you simply limit the clock rate. So:

#use i2c(master, sda=TEMP_SDA, scl=TEMP_SCL, SLOW)

The manual does give the clue for this. The old V4 manual:
Quote:

Software functions are generated unless the FORCE_HW is specified.


The V5 manual:
Quote:

If hardware pins are specified for SDA and SCL, then hardware functions are generated unless the force_sw is specified;


Changed to the following like you suggested:

#use i2c(master, sda=TEMP_SDA, scl=TEMP_SCL, FORCE_SW)

Problem improved with slightly different response. Added the following prior to initializing the device:

i2c_init(100000);

So far, the combination of the two changes has it working. I'll keep an eye on it.
_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 20, 2018 11:55 am     Reply with quote

Fusillade wrote:

#use i2c(master, sda=TEMP_SDA, scl=TEMP_SCL, FORCE_SW)

i2c_init(100000);

So far, the combination of the two changes has it working.

If you use software i2c, the i2c_init() won't do anything.
Look at the .LST file. This was compiled with vs. 5.078:
Code:

00B2:  MOVLW  07
00B4:  MOVWF  CMCON
.................... { 
.................... int8 temp;
.................... 
.................... i2c_init(100000);   <=== *** No code is generated ***
.................... 
.................... i2c_write(0x55);
00B6:  MOVLW  55
00B8:  MOVWF  ??65535
00BA:  BRA    0004
.................... 
.................... temp = i2c_read(0);
00BC:  CLRF   @00
00BE:  BRA    0054
00C0:  MOVFF  01,temp
.................... 
.................... while(TRUE); 
00C4:  BRA    00C4

Test program:
Code:
#include <18LF2520.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)

#use i2c(master, sda=PIN_C4, scl=PIN_C3, FORCE_SW)

//=======================================
void main()
{
int8 temp;

i2c_init(100000);

i2c_write(0x55);

temp = i2c_read(0);

while(TRUE);
}
 
Ttelmah



Joined: 11 Mar 2010
Posts: 19497

View user's profile Send private message

PostPosted: Fri Apr 20, 2018 12:11 pm     Reply with quote

and with 47K, I'd be lowering the clock rate even further. So:
#use i2c(master, sda=TEMP_SDA, scl=TEMP_SCL, slow=50000, FORCE_SW)

Honestly, 47K, is so high that I'd doubt if the bus can meet the rise time requirements for even 100KHz.
Now the software libraries have been improved since version 4.121 quite a lot, so probably clock the bus faster than they did in V4.121.....
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Fri Apr 20, 2018 1:45 pm     Reply with quote

Ttelmah wrote:
and with 47K, I'd be lowering the clock rate even further. So:
#use i2c(master, sda=TEMP_SDA, scl=TEMP_SCL, slow=50000, FORCE_SW)

Honestly, 47K, is so high that I'd doubt if the bus can meet the rise time requirements for even 100KHz.
Now the software libraries have been improved since version 4.121 quite a lot, so probably clock the bus faster than they did in V4.121.....


I was told the original hardware engineer worked with an app engineer to determine a maximum resistor value in our design and 47K was determined to be right at the limit. It seemed high to me but 47k has worked for over 9 years. I've always used 10K pull-ups for 100KHz I2C lines.

I see your point about the code. The command adds no code to the project. Strange that I added it and it worked; I took it out and it didn't. I was hoping I just needed to specify the speed to fix my issue. I think with the clock speed, I can push it down to 40KHz. I'll try slow=40000 (I think that is BRG=63h). The device is only accessed once per minute anyway.

Thanks again for your help.
_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 20, 2018 2:02 pm     Reply with quote

Fusillade wrote:

I see your point about the code. The command adds no code to the
project. Strange that I added it and it worked; I took it out and it didn't.

Look at your own .LST file to verify that it's behaving the same way
as in my post.

Fusillade wrote:

I think with the clock speed, I can push it down to 40KHz. I'll try
slow=40000 (I think that is BRG=63h). The device is only accessed once
per minute anyway.

FORCE_SW mode doesn't use the BRG. Only hardware i2c uses it.
Software i2c uses NOP's or jumps or loops to do software delays.
Also, the hardware BRG value for 40 KHz scl with a 4 MHz oscillator is
not 63h. It's 18h. You can see this in the .LST file if you compile for
hardware i2c. In any case, you don't have to program the BRG when
in hardware mode. The compiler handles it for you.
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Fri Apr 20, 2018 3:34 pm     Reply with quote

PCM programmer wrote:

Fusillade wrote:

I think with the clock speed, I can push it down to 40KHz. I'll try
slow=40000 (I think that is BRG=63h). The device is only accessed once
per minute anyway.

FORCE_SW mode doesn't use the BRG. Only hardware i2c uses it.
Software i2c uses NOP's or jumps or loops to do software delays.
Also, the hardware BRG value for 40 KHz scl with a 4 MHz oscillator is
not 63h. It's 18h. You can see this in the .LST file if you compile for
hardware i2c. In any case, you don't have to program the BRG when
in hardware mode. The compiler handles it for you.


Fcy = Fosc/4 = 4MHz/4 = 1MHz
1MHz/40kHz = 25 = 19h
BRG = 19h (- 01h) = 18h (if it were used)

I forgot to divide my oscillator by 4. Sigh. Failing at simple math. I'll quit while I'm behind and tackle this issue fresh on Monday.
_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
Ttelmah



Joined: 11 Mar 2010
Posts: 19497

View user's profile Send private message

PostPosted: Sat Apr 21, 2018 2:19 am     Reply with quote

Just to make a comment about the resistor value.

Standard mode I2C, has a specified maximum rise time of 1uSec.
The rise time is specified as 0.847RC (where R is the pull up, and C the bus capacitance). Turning this round gives:

C=Trise/(0.847*R)

So solving for 1uSec and R=47K, gives:

C=25.1E-12

25pF.

So the absolute maximum bus capacitance that can be used with 47K, is 25pF total, to actually be 'in spec' for 100K I2C. Now when you consider that most chips will have something like 5pF pin capacitance, this doesn't give a lot of allowable track capacitance before the bus will go out of spec. Probably about 75mm maximum trace length.

Now software I2C, does not mind out of spec rise times generally. Your original code was using software I2C. Historically in 4.121, the maximum I2C clock rate generated, was Fosc/64. So with your 4MHz clock, 62500bps. This would have been the rate you were generating. The V5 I2C library has been slightly optimised and gives Fosc/52, so would be defaulting to 77000bps. Hence dropping the rate to (say) SLOW=60000 would be necessary to meet your timings.
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Mon Apr 23, 2018 7:11 am     Reply with quote

Ttelmah wrote:
So the absolute maximum bus capacitance that can be used with 47K, is 25pF total, to actually be 'in spec' for 100K I2C. Now when you consider that most chips will have something like 5pF pin capacitance, this doesn't give a lot of allowable track capacitance before the bus will go out of spec. Probably about 75mm maximum trace length.


Using estimates of:

3.3pF per 25.4mm trace
0.33pF per via
5pF per pin connection

= 3 pins + 3 vias + 50.8mm trace
= 15pF + 1pF + 6.6pF
= 22.6pF

Ttelmah wrote:
Now software I2C, does not mind out of spec rise times generally. Your original code was using software I2C. Historically in 4.121, the maximum I2C clock rate generated, was Fosc/64. So with your 4MHz clock, 62500bps. This would have been the rate you were generating. The V5 I2C library has been slightly optimised and gives Fosc/52, so would be defaulting to 77000bps. Hence dropping the rate to (say) SLOW=60000 would be necessary to meet your timings.


That is extremely useful information. Thanks. Before I left on Friday, and based on earlier conversations, I set the device to FORCE_SW and SLOW=50000 and the instrument ran all weekend.
_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
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