PDA

View Full Version : I2C RTC Problem with Pic18f46k22



JimAvanti
- 21st April 2015, 14:31
I have been using DS1307 Real Time Clocks for some time now without problems. I am attempting to port a program I wrote a while back from a 18f4620 to a 18f46k22 and I can't seem to get the I2C RTC to communicate. The only thing I can see different should be the speed of the micro running at 16MHz instead of 8MHz.
I did try adding "DEFINE I2C_SLOW 1" without any luck. Any suggestions?

Thanks,

Jim Black

Heckler
- 21st April 2015, 18:00
I have played with the ds1307 clock modules a few times.
Are you using one of the modules you can get from ebay or one of the hobby houses that include the battery on the pcb??

I found that it did not seem to start up reliably every time. Have you tried to remove the battery and reset it??

I am now using the DS2417 one wire RTC with pretty good success.

good luck

JimAvanti
- 22nd April 2015, 00:36
I am using just the DS1307 (No ready-made module). I am using the surface mount SOIC package. I have used these plenty of times in the past. I don't recall if I ever used them on a pic running faster that 8MHz before though. Since I am using a PCB that I laid out I don't have much choice of selecting another RTC, but I can try changing the Micro to a 18f4620 (Same pinout as the 18f46k22). I just don't have any to try right now.

aratti
- 22nd April 2015, 11:22
Did you define OSC 16 in your code?

Al.

Dave
- 22nd April 2015, 12:35
I would start by checking the comfigurations of the pins being used to connect the one wire devices. There are quite a few new configuration registers compared to the 4620. I have converted a lot of code from the 4620 to 46K22. Look at the data sheet. Your code should be compatable 100%. Just look at the config's.

Scampy
- 22nd April 2015, 16:43
I too use the Ds1307 in a few of my projects, most of which are based on an 18F4580. These are running at 40Mhz (10 Mhz xtal with OSC set to 40 and HS_PLL in the config), and on occasion these would either display 10:10 for time and date regardless of being either breadboarded or on a plug in module for the Easypic development board. I often found that changing the 37 khz crystal resolved the issue, or connecting a LED and suitable resistor between the SQW pin and GND so it pulsed at 1 sec intervals would somehow cause the chip to communicate to the PIC

JimAvanti
- 23rd April 2015, 02:20
Al,

I did:
DIFINE OSC 16
OSCCON = %11111111


I also tried:
DEFINE OSC 8
OSCCON = %11101111




Did you define OSC 16 in your code?

Al.

JimAvanti
- 23rd April 2015, 02:24
I haven't tried changing the crystal. I will give that a try tomorrow.


I too use the Ds1307 in a few of my projects, most of which are based on an 18F4580. These are running at 40Mhz (10 Mhz xtal with OSC set to 40 and HS_PLL in the config), and on occasion these would either display 10:10 for time and date regardless of being either breadboarded or on a plug in module for the Easypic development board. I often found that changing the 37 khz crystal resolved the issue, or connecting a LED and suitable resistor between the SQW pin and GND so it pulsed at 1 sec intervals would somehow cause the chip to communicate to the PIC

JimAvanti
- 23rd April 2015, 02:47
I believe I got all the registers I needed to set. I have the RTC on pins D6 and D7 (Both set to output):
SDA VAR Portd.6 ' RTC I2C serial clock line
SCL VAR Portd.7 ' RTC I2C serial data line
TRISD=%00000010

'MPASM CONFIG
@ __config _CONFIG1H, _FOSC_INTIO67_1H & _FCMEN_OFF_1H & _PRICLKEN_ON_1H & _IESO_OFF_1H
@ __config _CONFIG2L, _PWRTEN_OFF_2L & _BOREN_OFF_2L & _BORV_285_2L
@ __config _CONFIG2H, _WDTEN_OFF_2H & _WDTPS_32768_2H
@ __config _CONFIG3H, _CCP2MX_PORTB3_3H & _PBADEN_OFF_3H & _MCLRE_INTMCLR_3H
@ __config _CONFIG4L, _STVREN_OFF_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
@ __config _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L
@ __config _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
@ __config _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L
@ __config _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
@ __config _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L
@ __config _CONFIG7H, _EBTRB_OFF_7H

All interrupts and ADC's are disabled also.



I would start by checking the comfigurations of the pins being used to connect the one wire devices. There are quite a few new configuration registers compared to the 4620. I have converted a lot of code from the 4620 to 46K22. Look at the data sheet. Your code should be compatable 100%. Just look at the config's.

JimAvanti
- 25th April 2015, 00:49
Dave,

You were correct, and it was a register I shouldn't have missed... ANSELx registers. I guess the 4620 default was digital I/O since it ran fine in the past on that micro. I would have guessed the 46k22 would have defaulted to digital as well since everything else that required digital I/O worked. I had one other small issue where the speed difference in processors was going too fast for voltage to drain on a pin that I am using for both input and output, but a very small delay was all that was needed there. Everything is working now.

Thanks to all who offered suggestions! I tried them all.

Jim


I would start by checking the comfigurations of the pins being used to connect the one wire devices. There are quite a few new configuration registers compared to the 4620. I have converted a lot of code from the 4620 to 46K22. Look at the data sheet. Your code should be compatable 100%. Just look at the config's.

Dave
- 26th April 2015, 13:04
Yes, I have found there are quite a few new functions added to the "K22" series of processors. You have to keep in mind the programmable alternate pins as well as the power control modules. Disabling unused functions saves a lot of power.