View previous topic :: View next topic |
Author |
Message |
GL82
Joined: 12 Jun 2019 Posts: 15
|
PIC18F46K42 I2C |
Posted: Fri May 27, 2022 8:11 am |
|
|
Hi all!
I have to change from PIC18F46Q10 to PIC18F46K42 and now I2C old functions do not work in this micro so I must use the i2c_transfer() function.
I had this code working on the PIC18F46Q10
Code: | void setVoltageE2PROMDAC(int16 valorDAC)
{
int8 dato=0;
i2c_start(DAC); // Issues a start command
i2c_write(DAC,0xC0); // byte over the I2C interface. Address byte
i2c_write(DAC, 0x60); // byte over the I2C interface. E2PROM write
dato=valorDAC>>4;
i2c_write(DAC, dato); // byte over the I2C interface. Data byte
dato=(valorDAC&0x00FF)<<4;
i2c_write(DAC, dato); // Sends a single byte over the I2C interface.
i2c_stop(DAC); // Issues a stop command
} |
I have changed this code to the following one but it is not working
Code: | void setVoltageE2PROMDAC(int16 valorDAC)
{
int8 dato[3];
dato [0] = 0x60;
dato [1] = valorDAC>>4;
dato [2] = (valorDAC&0x00FF)<<4;
i2c_transfer(DAC,0xC0,dato,3); //
} |
The use i2c is the same in both codes
Code: | #use i2c(STREAM=DAC, master, FAST=800000,SCL=PIN_C3,SDA = PIN_C4,FORCE_HW) |
What am I doing wrong? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19550
|
|
Posted: Fri May 27, 2022 8:27 am |
|
|
Are you PPS mapping the pins (#PIN_SELECT)?. You need to. Look at the
sticky at the top of the forum on how to do this. On _some_ chips (a very
few), some peripherals will map without using #PIN_SELECT, but it is
safer to always use this.
Seriously 800000 bps, is above the speed that I2C can safely run, unless
you have active pull-ups. Do you have these?. It may well be that on the
other chip the compiler was automatically limiting you to 400000Hz,
since this is the maximum actually supported by the peripheral.
Other thing you need to turn off slew rate limiting on the pins for this.
This defaults to enabled. |
|
|
GL82
Joined: 12 Jun 2019 Posts: 15
|
|
Posted: Fri May 27, 2022 8:36 am |
|
|
Thanks for the reply Ttelmah!
The #pin select is the same in both PICs
Code: | #pin_select SCL1OUT = PIN_C3
#pin_select SCL1IN = PIN_C3
#pin_select SDA1OUT = PIN_C4
#pin_select SDA1IN = PIN_C4 |
I have 2.2K pull-ups, the hw is the same, I have only change the PIC and I achieve this speed (even faster speeds) with this hw.
I have studied both signals with an oscilloscope and the clock signal is ok at 800kHz but SDA signal is only doing the start command and one bit more.
I have tried to do it in slow speed and the result is the same. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Fri May 27, 2022 9:15 am |
|
|
Had a look at the specs...
Mr T is right (as always), you CANNOT go faster than 400KHz UNLESS you use 'active pullups'.
Just because the scope 'sees' some signals does NOT mean the peripheral / PIC WILL function 100% at that speed. |
|
|
GL82
Joined: 12 Jun 2019 Posts: 15
|
|
Posted: Sat May 28, 2022 1:05 am |
|
|
Thanks for the advice temtronic.
However, as I said in my last post, if I try with lower speed (see the code below) the result is the same
Code: | #use i2c(STREAM=DAC, master, SLOW,SCL=PIN_C3,SDA = PIN_C4,FORCE_HW) |
Where could be the problem? the change to the new i2c functions looks easy but it is not doing the same |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat May 28, 2022 1:45 am |
|
|
GL82 wrote: |
If I try with lower speed (see the code below) the result is the same |
I hope you didn't put it back to 800K. Keep it at SLOW for now.
GL82 wrote: |
Where could be the problem? the change to the new i2c functions looks easy but it is not doing the same |
What's the part number of the i2c slave device ?
Also what's your compiler version ?
What's the Vdd voltage of the Master PIC ?
What's the voltage on the 2.2K pull-ups ? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Sat May 28, 2022 5:19 am |
|
|
Have to agree use 'SLOW' until you get the program to work, THEN if needed, you can increase the speed but NOT beyond the maximum in the spec sheet.
Since I don't use that PIC, is the 'I2C peripheral' the old style MSSP version or a newer 'stand alone I2C' style ? I've read here there are differences in how to setup the two. I'm assuming it's 'old' as no one else has asked though.
As others have asked ,VDD is important, I2C pullups and of course WHAT is the external device ? It may have 'special' power requirements, a 'rigid' setup routine, details we need to know to further help you. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19550
|
|
Posted: Sat May 28, 2022 10:10 am |
|
|
He should use the standard syntax for setting up the port. Don't use the
pin numbers in the setup. Use:
Code: |
#pin_select SCL1OUT = PIN_C3
#pin_select SCL1IN = PIN_C3
#pin_select SDA1OUT = PIN_C4
#pin_select SDA1IN = PIN_C4
#use i2c(STREAM=DAC, master, FAST=400000, I2C1)
|
Using this syntax makes the compiler automatically turn off slew rate
limiting on the port, and set the pins to open collector mode.
2K2 would only support a bus with just under 200pF capacitance even
at 400K (assuming 3.3v).
However I would explicitly set the slew rate. I've seen several chips
where this is not set by default by the compiler, and if this was wrong,
there would be signals, the edges just would be much too slow to work.
Remember also the compiler expects to see the chip NACK the address
byte. If this is not seen the transaction will abort. |
|
|
GL82
Joined: 12 Jun 2019 Posts: 15
|
|
Posted: Mon May 30, 2022 6:35 am |
|
|
Hi all,
I have solved the issue. It was about the i2c configuration module. It seems that using fast/slow (and not setting the clock source) is not correct with the new i2c specific module. I have deleted it and added the clock source and configured it to work at 41.5 KHz. Now everything works fine. If I configure it up to 400 KHz it keeps working fine.
Code: | #use i2c(STREAM=DAC,master,CLOCK_SOURCE = TMR4,CLOCK_DIVISOR=4,SCL =PIN_C3,SDA=PIN_C4,FORCE_HW) |
Thanks to everybody in this post! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19550
|
|
Posted: Mon May 30, 2022 9:20 am |
|
|
I'd have expected you to have seen clocks, but much too fast. The default
clock is Fosc/4. The I2C module does not have any BRG (unlike the SSP),
so should have defaulted to generating a clock at your Fosc/16 or Fosc/20. |
|
|
|