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 support@ccsinfo.com

I2C don't send any data

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



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

I2C don't send any data
PostPosted: Sun Mar 24, 2019 1:33 pm     Reply with quote

The compiler version is 5.078
PIC18F4520 DIP

The routine get stuck on the start()
I tried with RC3 & RC4 as input but didn't work either.

Code:

#include <18F4520.h>
#device ADC=8
#use delay(clock=16M)

#use fast_io(ALL)

#fuses INTRC,NOWDT,NOLVP,BORV27
#use i2c(master, slow, I2C1,fast=400000)

void main()
{
    setup_oscillator(OSC_16MHZ);
   
    output_a(0);
    output_b(0);
    output_c(0);
    output_d(0);
    output_e(0);
   
    set_tris_a(0);
    set_tris_b(0);
    set_tris_c(0);
    set_tris_d(0);
    set_tris_e(0);
   
    i2c_init(100000);
 
   while(1)
   {
      delay_ms(100);
      i2c_start();
                i2c_write(0x60);//address
                i2c_write(PLL_W);
                i2c_write(byte3_w);
                i2c_write(byte4_W);
                i2c_write(byte5_W);
                i2c_stop();
   }   
}


I put two 1K resistances, one from RC3 to +V and from RC4 to +5V.

Any idea what can be happening?

I'm trying to control a TEA5767 Radio module.
_________________
Electric Blue
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Sun Mar 24, 2019 2:47 pm     Reply with quote

1K is a bit low. Many I2C devices only support 3mA drive (you don't say what
the device you are talking to is?). 3mA on 5v to meet the VOl requirement
is about 1500R.

Then you are specifying FAST_IO. This means that _you_ become responsible
for setting the TRIS correctly. You have the TRIS bits for your I2C bus
set to 0. Yet the data sheet says:

Quote:

Selection of any I2C mode, with the SSPEN bit set,
forces the SCL and SDA pins to be open-drain,
provided these pins are programmed to inputs by
setting the appropriate TRISC bits. To ensure proper
operation of the module, pull-up resistors must be
provided externally to the SCL and SDA pins.


So the I2C bits need to be set as _inputs_ with their TRIS bits set to
'1'.

Then given we don't know the device, there is no guarantee that the
address being used is right. Use PCM_Programmers I2C scanner
program from the code library, and find if the device responds and on
what address, before trying your own code.
temtronic



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

View user's profile Send private message

PostPosted: Sun Mar 24, 2019 3:08 pm     Reply with quote

I downloaded the datasheet for the device, first thinking it's a 5 volt PIC VS 3 volt device but...it should be OK...
However it's the device ADDRESS that caught my eye.

It's 1100 000n where n=0 for write, 1 for read. This means the actual address is 0xC0 NOT 0x60. Some think of I2C as 7 bit+r/w bit, so you need to shift left 1 bit position to get the 'real ' address. CCS typically uses the 8 bit address philosphy.

When you run PCMP's I2C scanner program it will confirm the device's address.

Whenever using I2C deices, ALWAYS run his program ! it's a quick, fast reliable test to be sure any I2C device is really there.

Also I'd get rid of the fast_io() and tris() functons and let the compiler do it all for you. Unless you are very,very careful, a simple misconfiguration of bits(fatfingeritis) will make you lose hours if not days wondering 'why doesn't it work?'

Jay
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

PostPosted: Sun Mar 24, 2019 6:12 pm     Reply with quote

Thanks; I changed the resistor and commented FAST_IO(ALL) and start to work; I mean, I get data out from the pins(oscilloscope).

Now I can work in the code to make it work with the TEA5767.

UPDATE:
SUCCESS IT'S WORKING!

The 0xC0 address it's ok; now I will try to read at 0xC1.
_________________
Electric Blue
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Sun Mar 24, 2019 11:57 pm     Reply with quote

OK.
Make sure you add the keyword 'SMBUS' to your I2C setup.
With a 3.3v device, the lines won't go high enough to be seen as '1'
for the PIC I2C buffers. The SMBUS option reprograms these to the
lower SMBUS levels which are compatible with a 3.3v device.
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

PostPosted: Tue Mar 26, 2019 12:44 am     Reply with quote

The device is running at 5V and as far I know the I2C uses open drains so the voltage swing is from 0 to resistor V+ that in this case is 5V.

The only thing that I note it is that the TEA5767 doesn't go to zero but instead 0.3V/0.4V; may be is the 20cm cable.

Anyway it's working now, the problem seems to have been the resistors.
_________________
Electric Blue
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Tue Mar 26, 2019 3:13 am     Reply with quote

No.

If you have your pull up resistors to 5v, the TEA5767, will be having it's inputs
'overvoltaged' by the pullups. It will probably clamp them to just over 3.3v
as it's internal protection kicks in, but this may well result in it's power
supply rail being raised by the I2C drive.
Dangerous.

The correct way to use a 3.3v device with a 5v PIC master, is to have the
pullups go to the 3.3v rail only, and switch the PIC to accept the lower
signalling voltage. Otherwise you risk chip damage.

No normal transistor actually pulls to 'zero'. The Vil spec for I2C, is 0.3*Vdd.
The Vol spec is 0.4v. So a device is meant to pull the signal down to 0.4v or
below, while the input will accept up to a volt (on 3.3v) as 'low'.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Tue Mar 26, 2019 5:23 am     Reply with quote

Why not use a level converter chip?
temtronic



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

View user's profile Send private message

PostPosted: Tue Mar 26, 2019 5:54 am     Reply with quote

I'm guessing he has a 'module' and not just the bare chip ? If a module, it might already have Logic Level converters onboard.
I downloaded the TEA datasheet and they don't explicitly say '3 volt only' operation, some operating specs are 5v. Kinda fuzzy, got this old guy confused, though these days I 'assume' all itty, bitty add-on black chips are 3 volt.
Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 26, 2019 6:52 am     Reply with quote

dluu13 wrote:
Why not use a level converter chip?

With a 5v PIC, all you need to do is enable SMbus, which is a parameter
in the #use i2c() statement, and then use 3.3v on 3.3K pullups.
It's cheaper to do it that way, and it's completely acceptable electronically,
compared to using level converters.
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Tue Mar 26, 2019 7:07 am     Reply with quote

Absolutely/exactly.

Exception is if you have one of the (rare) PIC's that doesn't have the SMBUS
option.
temtronic



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

View user's profile Send private message

PostPosted: Tue Mar 26, 2019 2:33 pm     Reply with quote

I actually ordered a small PIC and wondered WHY the Timer interrupt didn't work...didn't even compile...
DOH.
THAT PIC doesn't HAVE an interrupt !!
After 25 years of playing with PICs, it never occoured to me they'd make a PIC without a timer interrupt.
Man I gotta printout and READ the datasheets not just scan online....
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