PDA

View Full Version : I2C and Serout problems with 18F4525



Yuantu Huang
- 1st March 2005, 07:31
Dear All,

The following code works fine for 16F777, but not work for 18F4525. Hope to get helps from you. The Led is flashing OK, but I2C, Serout, and string ABC do not work at all. Note that the slave is 16F747.

================================================== ===
include "modedefs.bas"

'DEFINE I2C_HOLD 1

SCL VAR PORTC.3 ' Clock pin
SDA VAR PORTC.4 ' Data pin
LED var PORTB.7
ABC var byte[2]
Out var PORTD.2

Address CON 2 ' Slave PIC 7-bit address

OSCCON = %01101110 ' Use internal 4MHz clock
ADCON1 = $0F ' Disable A/D convertor
' SSPADD = $09
' SSPCON1 = %00101000
' SSPCON2 = $FF
TRISA = %00000000 ' PORTA all set to Output
TRISB = %00000000
TRISC = %00011000
TRISD = %00000000
TRISE = %00000000

PORTA = 0
PORTB = 0
PORTC = 0
PORTD = 0
PORTE = 0
' LATA = 0
' LATB = 0
' LATC = 0
' LATD = 0
' LATE = 0

SCL = 1
SDA = 1

ABC[0] = "A"
ABC[1] = "B"
goto loop

FailedR:
serout Out, n9600, ["Read fail"] ' I2C command timed out
return

FailedW:
serout Out, n9600, ["write fail"] ' I2C command timed out
return

Loop:
low led
pause 500
I2CWRITE SDA,SCL,Address,[127],FailedW
pause 100
I2CRead SDA,SCL,Address,[str ABC\2], FailedR ' Read two bytes from slave
high led
serout Out, n9600, [ABC[0], ABC[1], 13, 10] ' sent to PC
pause 500
Goto Loop

END
=============================================

I made the changes of 18F4525.INC
from
__CONFIG _CONFIG1H, _OSC_XT_1H
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
__CONFIG _CONFIG4L, _LVP_OFF_4L
to
__CONFIG _CONFIG1H,_OSC_INTIO67_1H
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H
__CONFIG _CONFIG4L, _LVP_OFF_4L

Best regards,

Yuantu

mister_e
- 1st March 2005, 17:42
weird, what about if you enable the Watch Dog Timer ?!?

And what about if you use internal 8MHZ instead of 4MHZ with a DEFINE OSC 8 For 4Mhz, i'm not sure 9600 baud will work 100%

Have you some filtering caps (0.1 ceramic + 10uF tantalum) close.. REALLY close to your PIC. 18F are a more noise sensitive than the 16F.

Yuantu Huang
- 2nd March 2005, 07:15
Dear Steve,

I made it working as your suggestion although sometimes it doesn't go (seems no clock, it may be the circuit board problem). The following is the change of the code and cofiguration:

code:
========================
DEFINE OSC 8
OSCCON = %01110111
=======================

cofiguration:
==============================================
__CONFIG _CONFIG1H,_OSC_INTIO67_1H
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _PBADEN_OFF_3H
__CONFIG _CONFIG4L, _LVP_OFF_4L & _ENHCPU_OFF_4L
==============================================

Once again thank you very much.

mister_e
- 2nd March 2005, 15:21
Great. Try reprogram your PIC with the lowest Brown-Out-voltage treshold too. Power-Up timer have to bet set too.

Of course noisy supply line and 'weird contact' on a board can do bad results. PIC18F series are really noise sensitive... use of external pull-ups instead of the internal seems to solve some problems. After some tests, Pull-ups lower than 4.7k give better result. i use 1K in all my apps. For sure sink a bit more current but... it works.