News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

I cannot get I2C to work with the 18F27K42.

Started by JohnB, Jun 12, 2022, 01:53 PM

Previous topic - Next topic

JohnB

I cannot get I2C to work with the 18F27K42.
These are my setups:

$define PCF8523_SDA_Pin Portb.0    ' RTC Data
$define PCF8523_SCL_Pin Portb.1    ' RTC Clock

  LATB = 0
  T3CON =    0b00000000
  TRISA =    0b00000000  ; ALL OUTPUT
  TRISB =    0b00000111  ; Bits 0-1 I2C, bits 3-4 Onewire
  TRISC =    0b10100100  ; USART1 pins 7-6 USART 2 pins 5-4  RTC SquWave 2
  WPUA =     0b00000000  ; NO PULLUPS
  WPUB =     0b00000011  ; NO PULLUPS
  WPUC =     0b00100000  ; PULLUP ON C5

This is my Main program
do
    I2COut PORTB.0, PORTB.1, cRTC_DEV_ADDR, 0, [0, 0, 7]
    Pulse_LED()
loop
Scoping the SDA or SLC lines permanently high but I am looping as the leds are flashing.
JohnB

trastikata

Hello John,

does the standard BStart-BusOut-BStop work?

Yasin

Dear @JohnB , you may have skipped these.

QuoteANSELx: ANALOG SELECT REGISTER

JohnB

Not tried the individual Bus Start, Stop Out commands.
I'll give it a try
I am not using any of the analogue pins at present so why do I need to set ANSEL
JohnB

Yasin

#4
Yes, because the default analog is selected.

In order to use the pins digitally, the corresponding analog selections must be turned off. The default analog is selected.

top204

You cannot use the internal pull-up resistors for I2C John. They are too weak.

Your line of code:

WPUB = 0b00000011

Is enabling the internal pull-up resistors for PORTB.0 and PORTB.1. You must use, either 4.7K Ohm or 2,2K Ohm external resistors for the SDA and SCL lines.

Yasin... The compiler automatically sets the pins to digital before the user program starts. See near the end of the .def files for each device and you will see the SFRs manipulated. For example, the PIC18F27K42 device has:

'-----------------------------------------------------------------------------------
' Make the device's I/O pins digital (if required) before the main program starts
' This can be added too by the user and shared on the forum
'
#IfnSym __SYSCOM_LIBRARY_OFF_       ' Has the library been disabled in the compiler?
    ANSELA = $00
    ANSELB = $00
    ANSELC = $00
    SLRCONA = $00
    SLRCONB = $00
    SLRCONC = $00
    CM1CON0 = $00
    CM2CON0 = $00
#EndIfSym






Yasin

Quote from: top204 on Jun 12, 2022, 07:37 PMThe compiler automatically sets the pins to digital before the user program starts. See near the end of the .def files for each device and you will see the SFRs manipulated. For example, the PIC18F27K42 device has:

It used to be the default analog sourced from Microchip. That's how I remember it. I think you fixed it after the updates. This is out of my mind. Then if @JohnB is using the current version it shouldn't be a problem. Maybe if a bootloader is used it can cause this indirectly.

JohnB

After declaring SDA and SCL pins just tried BusOut command, the output remains high.
Tried toggling SDA line and that works so its not the hardware.

I have attached  a copy of my program with I2COut, BusOut and Toggle all included in a loop. Just comment out the appropriate lines.

JohnB

tumbleweed

Missing 'Context Restore' in the ISR_Handler?

JohnB

Corrected that, It was originally there but got removed in over zealous editing.
The SDA and SLC still doggedly high.
JohnB

JohnB

Its running, there was a dry joint on the board with the MPU on it.  Just hadn't properly soldered the pin.  Now got to find out why the RTC is only giving me 32Khz lock and not the 1 Hz I had set up.

Thank you very much for all the help, I am now gradually beginning to feel more confident with the setup.

Have noticed a number of bugs with Positron Studio over while going through this exercise so hopefully you will see an improved version in the not too distant future.
JohnB