|
|
View previous topic :: View next topic |
Author |
Message |
darkmagic
Joined: 14 Jun 2011 Posts: 5
|
If I use I2C my PIC sometimes stops work |
Posted: Tue Jun 14, 2011 4:40 pm |
|
|
Hi, I'm using PIC 18F67J50 (datasheet) and CCS compiler version 4.057. PIC is on the I2C bus master; two PWM drivers NXP PCA9635 (datasheet) and one serial EEPROM (24FC256) are slave devices. I was using this board for three months and I have no problems with I2C (I'm new in MCU programing), but now I'm having serious problem with I2C communication.
I compile my project (no errors, MPLAB + CCS, PICKIT2 - debugger) and program my PIC. Than I start my program (F8 Step Over) and void main(void) runs.
main.c:
Code: | #include <18F67J50.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT, PLL1, NOSTVREN, NOFCMEN
#use delay(clock=24000000)
#USE I2C (MASTER, SDA=PIN_C4, SCL=PIN_C3, FAST=100000, FORCE_HW) //100kHz
... |
Code: | void main(void) {
output_b(0xFF); //blink LED
delay_ms(100);
output_b(0x00);
lcd_init();
//setup ADC
setup_adc(ADC_CLOCK_INTERNAL); //enables the a/d module and sets the clock to internal adc clock
setup_adc_ports(ALL_ANALOG ); //sets all the adc pins to analog
ADCON0 = 0b00000001; //manually settings
set_adc_channel(0);
//init PCA drivers
i2c_start(); //NO PROBLEM
i2c_write(actuall_adress); //PROGRAM STOPS WORK (actuall_adress = 0xAE)
i2c_write(0b10000000);
i2c_write(0b00000001);
...
i2c_stop();
...
while(1){}
} |
On the line i2c_write(actuall_adress) is debugger stepping and stepping (10s, 60s, 5 minutes...) but nothing is happen. If I stop stepping (F5 Halt), the green cursor in C code in MPLAB point to line #USE I2C (MASTER, SDA=PIN_C4, SCL=PIN_C3, FAST=100000, FORCE_HW) //100kHz.
Bad is that it happens only sometimes. Sometimes it works without problem and sometimes it stops.
If I use F9 Run program (without stepping) it stops sometimes too.
I don't know where I should search mistake. Can anyone help me?
edit:
Disassembly Listing for the line with #USE I2C(Master,... Code: |
17: #USE I2C (MASTER, SDA=PIN_C4, SCL=PIN_C3, FAST=100000, FORCE_HW) //100kHz
00BA0 9EC6 BCF SSP1CON1, 0x7, ACCESS
00BA2 969E BCF PIR1, 0x3, ACCESS
00BA4 C0CE MOVFF 0xce, SSP1BUF
00BA8 0E02 MOVLW 0x2
00BAA BEC6 BTFSC SSP1CON1, 0x7, ACCESS
00BAC D005 BRA 0xbb8
00BAE A69E BTFSS PIR1, 0x3, ACCESS //stepping (F8) in Disassembly shows that the PIC works
00BB0 D7FE BRA 0xbae //on this two lines (cycle, loop)
00BB2 0E00 MOVLW 0
00BB4 BCC5 BTFSC SSP1CON2, 0x6, ACCESS
00BB6 0E01 MOVLW 0x1
00BB8 6E01 MOVWF 0x1, ACCESS
00BBA 0C00 RETLW 0
00D30 86C5 BSF SSP1CON2, 0x3, ACCESS
00D32 B6C5 BTFSC SSP1CON2, 0x3, ACCESS
00D34 D7FE BRA 0xd32
00D36 B000 BTFSC 0, 0, ACCESS
00D38 9AC5 BCF SSP1CON2, 0x5, ACCESS
00D3A A000 BTFSS 0, 0, ACCESS
00D3C 8AC5 BSF SSP1CON2, 0x5, ACCESS
00D3E 88C5 BSF SSP1CON2, 0x4, ACCESS
00D40 B8C5 BTFSC SSP1CON2, 0x4, ACCESS
00D42 D7FE BRA 0xd40
00D44 CFC9 MOVFF SSP1BUF, 0x1
00D48 0C00 RETLW 0
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 14, 2011 4:58 pm |
|
|
1. Try it with software i2c. Remove the FORCE_HW parameter.
2. What value of pull-up resistors do you have on SCL and SDA ?
3. Run the program in the link below, and see what i2c slave chips
it finds on the i2c bus:
http://www.ccsinfo.com/forum/viewtopic.php?t=42368&start=4
Modify the #include, #fuses, #use delay(), #use i2c(), etc., to fit your
board. Use software i2c. |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Tue Jun 14, 2011 11:44 pm |
|
|
PCM programmer wrote: | 2. What value of pull-up resistors do you have on SCL and SDA ? |
My guess is you are missing the pullup resistor on SDA. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
darkmagic
Joined: 14 Jun 2011 Posts: 5
|
|
Posted: Wed Jun 15, 2011 2:32 am |
|
|
Both pull-up resistors are 2k2 to 3V3 and to GND there are 100p capacitors (img).
I'll remove the force_hw parameter and test it. I'll try the program in the link too.
Can you explain me why are in my ASM code this two lines?
00BAE A69E BTFSS PIR1, 0x3, ACCESS //stepping (F8) in Disassembly shows that the PIC works
00BB0 D7FE BRA 0xbae //on this two lines (cycle, loop)
In PIR1 register in bits b1 and b0 (0x) are only interrupt flags for TMR2 and TMR1.
In disassembly listing for software I2C is nothing with PIR1 register.
edit1:
1. I compile my project without force_hw and it runs good.
2. I put back force_hw parametr and it doesn't work.
Last edited by darkmagic on Wed Jun 15, 2011 3:00 am; edited 1 time in total |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Wed Jun 15, 2011 2:54 am |
|
|
darkmagic wrote: | Both pull-up resistors are 2k2 to 3V3 and to GND there are 100p capacitors (img) |
Get rid of the capacitors.
Did you actually measure the resistors from the PIC pins or are you assuming they are intact because they are on your schematic? I had a hardware fault on one of my systems where one of the surface mount resistors on the I2C bus appears to be perfectly soldered to the PCB however the SMD end cap on the resistor was electrically isolated from the resistor. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Wed Jun 15, 2011 3:00 am |
|
|
1) Why are you adding 100pF capacitance to the bus?.
Normally bus capacitance is your _enemy_. It is what limits the bus speed. How long are the tracks to the chips?. The bus specifies a _total_ bus capacitance not in excess of 200pF, for resistive pull-up, at 100KHz. Given the capacitances of the chip inputs, and lines you may well be pushing over this, with your added capacitors.....
2) Is the PIC running off 3.3v?. For 3.3v I2C, 2.2K, is _high_. In fact it is right on the end of the line for legal values if your bus capacitance is up near to 200pF. The Rp (min) value for standard mode, with a bus capacitance approaching 200pF, is only about 900R. For bus loads above 200pF, switches pull up, or active pull up is _required_....
using software I2C, will help, and might get it working, since the high level input voltage is lower, and the bus may well run below 100KHz, but you really need to look again at your circuit.
PIR1.3 is the SSP interrupt flag. The ,3, is the bit number for a BTFSS test, not a mask value (referring to bits 0 and 1).....
Best Wishes |
|
|
darkmagic
Joined: 14 Jun 2011 Posts: 5
|
|
Posted: Wed Jun 15, 2011 3:21 am |
|
|
asmallri: If I measure resistors between PIC (SDA, SCL) and 3V3 net, I'll get 2170R.
Ttelmah:
1. I use capacitors because I see it in some circuit and it works perfect. I know that capacitance is my enemy but I try it with capacitors and it works.
The tracks are long about 10 cm.
2. Yes, my PIC is running off 3.3V.
I'll change my circuit. I'll get rid capacitors.
Thank you for the explanation ASM code. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Wed Jun 15, 2011 4:09 am |
|
|
The point about the voltage questions, is that on I2C, the _slave_ device, can hold the clock line 'low' while it does things. The PIC, when operating as a master, cannot then proceed to the next I2C operation. This will happen, if the bus doesn't get up to 2.64v (for a PIC on 3.3v), when released by the master. So, you need to look at everything affecting this. Obviously capacitance will slow the rise time down, but should not stop it from (eventually) reaching the required level. Depending on the leakage currents of your devices (slave and master), inadequate pull up could cause this, or if the 3.3v line you are connecting to for the pull-ups, is at a lower voltage than the line running the PIC. You could also add the keyword 'SMBUS' to the I2C declaration, which reduces the voltage needed for a 'high', to the SMBUS specification, rather than I2C. If it then starts working, it'll be telling you 'where' the problem is - you still need to fix it for reliability....
Best Wishes |
|
|
darkmagic
Joined: 14 Jun 2011 Posts: 5
|
|
Posted: Wed Jun 15, 2011 11:27 am |
|
|
I got rid capacitors.
I changed value of pull-up resistors - now 1320 ohm.
PIC, PCA drivers, EEPROM and pull-up resistors are connect to the same 3V3 net. Before each device is switch and I can turn the supply off. Pull-up resistors are connect to +3V3_PIC net. (image)
I did small test:
I compiled my program with force_hw parameter and ran the program. It stopped. I measured SDA net and there was only 0.04 V. On SCL net was 3.21 V. After that I turned off supply for the EEPROM -> on SDA was 3.20 V, too.
I ran the same program without the EEPROM (supply for EEPROM is off) and the program was running right.
test2:
I compiled my program without force_hw and with SMBUS parameter. It ran well. Without EEPROM and with too. It ran at 100kHz and 1MHz was no problem, too.
test3:
Combination SMBUS and FORCE_HW doesn't work at all (with and without EEPROM). If I remove EEPROM from the circuit, it works well.
In test3 is behaviour quite irregularly. Sometimes it works with EEPROM, sometimes not.
edit:
I changed my EEPROM and put a new one it the circuit. It works (#USE I2C (MASTER, SDA=PIN_C4, SCL=PIN_C3, FAST=100000, FORCE_HW)
It works at 100kHz and 1MHz.
edit2:
some tests (for each test I turned supply for a whole board off and than with delay on):
#USE I2C (MASTER, SCL=PIN_C3, SDA=PIN_C4, FAST=100000,FORCE_HW)
last version of my program: new EEPROM - runs ok; old EEPROM - 2x doesn't work, 1x works 14 days old version of the programm: new EEPROM - ok, old EEPROM - 1 test it stoped, 4 tests ok
I compere old and actual asm code: img. ASM code for actual program is much larger. (old asm code, actual asm code) |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Thu Jun 16, 2011 1:59 am |
|
|
SCL is being intermittently held low by the old EEPROM.
Sounds as if the chip is faulty, and cannot always complete an operation....
Best Wishes |
|
|
darkmagic
Joined: 14 Jun 2011 Posts: 5
|
|
Posted: Thu Jun 16, 2011 2:02 am |
|
|
Ttelmah wrote: | SCL is being intermittently held low by the old EEPROM.
Sounds as if the chip is faulty, and cannot always complete an operation....
Best Wishes |
If I use PicKit to read or to write to this EEPROM, is no problem. It's a mystery for my. |
|
|
|
|
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
|