After many hours trying to get theI2C working I have found an issue.
Using the example for the EEPROM supplied by Mikro as shown below when compiled with the default settings of
Primary Osc (XT+,HS,EC) and Osc Freq = 8.000000 Mhz
everything works fine, however if you compile with
Primary Osc w/pll (XT+,HS,EC+pll) and Osc Freq = 80.000000 Mhz
the I2C bus fails to function correctly with the RA2 clock line staying mostly low instead of high.
The code continues to run but the writing and reading from the EEPROM now fails!
I have a large project which needs to run at 80Mhz but now stuck as I am unable to get the I2C to function. I am using the LV32MXv6 dev board with the pic32mx460.
I have tried changing to the I2C init advanced and other bits but can not get this to function with the PLL.
Any help creatly appreciated as I need to complete the project ASAP.
Ian
Code: Select all
program I2C_Simple
dim i, b as char
atest as word
sub procedure EEPROM_24C02_Init()
I2C2_Init(100000)
'I2C2_Init_Advanced(80000, 100000)
end sub
'--------------- Writes data to 24C02 EEPROM - signle location
sub procedure EEPROM_24C02_WrSingle(dim wAddr as byte, dim wData as byte)
I2C2_Start() ' issue I2C start signal
I2C2_Write(0xA0) ' send byte via I2C (command to 24cO2)
I2C2_Write(wAddr) ' send byte (address of EEPROM location)
I2C2_Write(wData) ' send data (data to be written)
I2C2_Stop()
end sub
'--------------- Reads data from 24C02 EEPROM - single location (random)
sub function EEPROM_24C02_RdSingle(dim rAddr as byte) as byte
I2C2_Start() ' issue I2C start signal
I2C2_Write(0xA0) ' send byte via I2C (device address + W)
I2C2_Write(rAddr) ' send byte (data address)
I2C2_Restart() ' issue I2C signal repeated start
I2C2_Write(0xA1) ' send byte (device address + R)
result = I2C2_Read(1) ' Read the data (NO acknowledge)
I2C2_Stop()
end sub
main:
AD1PCFG = 0xFFFFFFFF ' Configure AN pins as digital I/O
LATB = 0 ' Set PORTB value to zero
TRISB = 0 ' Configure PORTB as output
TRISD = 0 ' Configure PORTB as output
LATD = 0 ' Set PORTB value to zero
EEPROM_24C02_Init() ' performs I2C initialization
b = 0x00
for i = 0x00 to 0x7F
EEPROM_24C02_WrSingle(i,b)
Inc(b)
Delay_ms(5)
next i
for i = 0x00 to 0x7F
LATD = i
LATB = EEPROM_24C02_RdSingle(i)
Delay_ms(100)
next i
end.