View previous topic :: View next topic |
Author |
Message |
Jerry I
Joined: 14 Sep 2003 Posts: 99 Location: Toronto, Ontario, Canada
|
Using SW I2C on RA7 and RA6 |
Posted: Sat Mar 22, 2025 9:37 pm |
|
|
Using a PIC18F27K40 28pin Dip
Compiler Version 5.112.
I am having no luck using the I/O signals RA6 and RA7. I configured the 2 pins for I2C software mode. I know my code works because I can switch the pins to RA0 and RA5 and everything works fine. The code locks up when using RA6 and RA7. A i2c_start() is ok but i2c_write(xx) locks up.
Is there any special code to configure these pins as I/O for i2c use?.
Code: |
#define I2C_SDA PIN_A5
#define I2C_SCL PIN_A0
#use i2c(Master, Slow=100000, sda=I2C_SDA, scl=I2C_SCL, force_sw)
The above code works
#define I2C_SDA PIN_A7
#define I2C_SCL PIN_A6
#use i2c(Master, Slow=100000, sda=I2C_SDA, scl=I2C_SCL, force_sw)
This code does not work. It locks up when executing i2c commands.
Thank you in advance for any suggestions or solution.
|
|
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19753
|
|
Posted: Sun Mar 23, 2025 3:26 am |
|
|
First, Hurrah. You give compiler version, and what is happening. Helps.
However missing bits. Your clock setup. So the #use delay, and any fuses
you set affecting the clock.
These are critical, since these pins are the oscillator input and output.
Also any interrupts you are using/enabling (IOC could also apply).
Then any analog setups.
If you look at the Pin allocation tables in the data sheet, and look at these
pins, you will see 'why' these are the things to look at. These pins are
ANA6/ANA7 IOCA6/IOCA7, OSC2/CLKOUT, and OSC1/CLKIN. So these are
the chip features that could affect their use.
My most probable suggestion is that the clock setup has the oscillator output
enabled,
Best Wishes |
|
 |
Jerry I
Joined: 14 Sep 2003 Posts: 99 Location: Toronto, Ontario, Canada
|
|
Posted: Sun Mar 23, 2025 6:56 am |
|
|
Sorry about that.
Code: |
#use delay(INTERNAL, CLOCK=32MHz)
// #fuses HS, RSTOSC_EXT_PLL, NOWDT, NOPPS1WAY, PROTECT *** #1
#fuses HS, NOWDT, NOPPS1WAY, PROTECT // *** #2
#fuses MCLR, PUT, BROWNOUT, BORV27, NOCKS, EBTR
#device *=16 //icd=TRUE
#device adc=10
#PIN_SELECT U1RX=PIN_C7
#PIN_SELECT U1TX=PIN_C6
#PIN_SELECT INT1=PIN_B1
TRISA = 0b00001111; //
TRISB = 0b01111011; //
TRISC = 0b11000010;
port_a_pullups(0b11001111);
port_c_pullups(0b11000010);
port_b_pullups(0b01000111);
#use FAST_IO(C)
#PRIORITY ioc, rda, timer2, timer0
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(sAN11 | sAN12 | sAN13, VSS_VDD);
clear_interrupt(INT_IOC_C1_L2H); // turn on interrupts
enable_interrupts(INT_IOC_C1_L2H); //rising Encoder Input
set_timer0(61);
setup_timer_0(T0_INTERNAL | T0_DIV_256 | T0_8_BIT); // 1 ms
disable_interrupts(INT_TIMER0);
setup_timer_2(T2_CLK_INTERNAL | T2_DIV_BY_4, 255, 10); // approx.
setup_timer_3(T3_EXTERNAL|T3_DIV_BY_1|T3_SYNC); // 1 ms refresh
clear_interrupt(INT_TIMER0); // Optional to use Timer0 & 1 with Timer0
clear_interrupt(INT_TIMER2); // Timer2
clear_interrupt(INT_RDA); // RDA
enable_interrupts(INT_TIMER0); // Optional to use Timer0 & 1 with
enable_interrupts(INT_TIMER2); // TIMER2 Int
enable_interrupts(INT_RDA); // Serial Int
enable_interrupts(GLOBAL);
|
In the fuse setup I initially had code line #1 and then I tried code line #2.
The analog defines work and read values properly
Serial works fine, and interrupts appear to also work fine.
The problem is that it appears that RA6 & RA7 are not in the correct mode. I cannot even toggle the pins.
Thanks again for any help |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9441 Location: Greensville,Ontario
|
|
Posted: Sun Mar 23, 2025 7:11 am |
|
|
don't use that PIC but....
usually it's
fuses first
then delay( clock....)
also
should be NOprotect until final program is 100% working.....
and
I don't see a 'clock_I/O' setting in fuses. It's needed to allow 'clock pin to be used as an I/O pin. Course I hate light green on white,might have missed it.
I'm sure someone else who uses that PIC will reply....oh Mr. Teeeee !! |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19753
|
|
Posted: Sun Mar 23, 2025 8:03 am |
|
|
Er. HS says to use the external HS crystal oscillator.......
Not what your #use delay says.
Since this is after the #use delay, this will override using the internal
oscillator.
Wrong.
Remove all oscillator fuses. The clock statement will set these for you and
not make mistakes like this. |
|
 |
Jerry I
Joined: 14 Sep 2003 Posts: 99 Location: Toronto, Ontario, Canada
|
|
Posted: Sun Mar 23, 2025 9:42 am |
|
|
Thank you everyone.
Removing the HS fuse did it. I thought the HS fuse was for any high frequency clocks. I am only using the internal clock on this project.  |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19753
|
|
Posted: Sun Mar 23, 2025 12:12 pm |
|
|
Good.
That makes sense, it probably resulted in the oscillator output being
enabled, which is on one of those pins..... |
|
 |
jeremiah
Joined: 20 Jul 2010 Posts: 1373
|
|
Posted: Mon Mar 24, 2025 6:46 am |
|
|
I can't speak to pic18 chips, but on many of the PIC24 chips, the HS fuse switches in the actual external oscillator circuit on the OS pins, which has the potential to affect the capacitance and timing of an I2C bus (even on the input side, not just the output side). |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19753
|
|
Posted: Tue Mar 25, 2025 1:37 am |
|
|
Yes.
I must admit I'm surprised it worked at all!....
At least we found it.  |
|
 |
|