Hallo,
ich nutze das PIC32 Starterkit und versuche eine I2C Kommunikation
aufzubauen.
Der Code sieht wie folgt aus:
1 | #define _SUPPRESS_PLIB_WARNING 1
|
2 |
|
3 | #include <p32xxxx.h>
|
4 | #include <stdio.h>
|
5 | #include <stdlib.h>
|
6 | #include <plib.h>
|
7 |
|
8 |
|
9 | #include <stdio.h>
|
10 | #include <stdlib.h>
|
11 |
|
12 | #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
|
13 | #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
|
14 | #define SYS_FREQ (80000000L)
|
15 |
|
16 | #define GetSystemClock() (SYS_FREQ)
|
17 | #define GetPeripheralClock() (SYS_FREQ/1)
|
18 | #define GetInstructionClock() (SYS_FREQ)
|
19 |
|
20 | #define I2C_CLOCK_FREQ 100000
|
21 |
|
22 |
|
23 |
|
24 | #define EEPROM_I2C_BUS I2C1
|
25 | #define EEPROM_ADDRESS 32 // 0b0100000 = 32 = 0x20
|
26 |
|
27 | int main()
|
28 | {
|
29 |
|
30 | UINT8 i2cData[10];
|
31 | // erstes Bit -> read/write; 7-Bit Slave Adresse
|
32 | I2C_7_BIT_ADDRESS SlaveAddress;
|
33 | int Index;
|
34 | int DataSz;
|
35 | UINT32 actualClock;
|
36 | BOOL Acknowledged;
|
37 | BOOL Success = TRUE;
|
38 | UINT8 i2cbyte;
|
39 |
|
40 | // Initialize debug messages (when supported)
|
41 | DBINIT();
|
42 |
|
43 | // Set the I2C baudrate
|
44 | actualClock = I2CSetFrequency(EEPROM_I2C_BUS, GetPeripheralClock(), I2C_CLOCK_FREQ);
|
45 | if ( abs(actualClock-I2C_CLOCK_FREQ) > I2C_CLOCK_FREQ/10 )
|
46 | {
|
47 | DBPRINTF("Error: I2C1 clock frequency (%u) error exceeds 10%%.\n", (unsigned)actualClock);
|
48 | }
|
49 |
|
50 |
|
51 | // Enable the I2C bus
|
52 | I2CEnable(EEPROM_I2C_BUS, TRUE);
|
53 |
|
54 | // Initialize the data buffer
|
55 | I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, EEPROM_ADDRESS, I2C_WRITE);
|
56 | i2cData[0] = SlaveAddress.byte;
|
57 | i2cData[1] = 0xFF; // EEPROM location to program (high address byte)
|
58 | i2cData[2] = 0xFF; // EEPROM location to program (low address byte)
|
59 |
|
60 | DataSz = 3;
|
61 |
|
62 |
|
63 | // Start the transfer to write data to the EEPROM
|
64 | if( !StartTransfer(FALSE) )
|
65 | {
|
66 | while(1);
|
67 | }
|
68 |
|
69 | ...
|
70 | }
|
Beim Debuggen bleibt das Programm immer in der Schleife
1 | if( !StartTransfer(FALSE) )
|
2 | {
|
3 | while(1);
|
4 | }
|
hängen.
Hier nochmal die bereitgestellte StartTransfer Funktion:
[c]
BOOL StartTransfer( BOOL restart )
{
I2C_STATUS status;
Nop();
Nop();
Nop();
// Send the Start (or Restart) signal
if(restart)
{
I2CRepeatStart(EEPROM_I2C_BUS);
}
else
{
// Wait for the bus to be idle, then start the transfer
while( !I2CBusIsIdle(EEPROM_I2C_BUS) );
if(I2CStart(EEPROM_I2C_BUS) != I2C_SUCCESS)
{
DBPRINTF("Error: Bus collision during transfer Start\n");
return FALSE; // <- Hier wird FALSE zurückgegeben
}
}
// Wait for the signal to complete
do
{
status = I2CGetStatus(EEPROM_I2C_BUS);
} while ( !(status & I2C_START) );
return TRUE;
}
[\c]
Wo False zurückgegeben wird habe ich oben markiert. Scheinbar wird das
Modul nicht richtig konfiguriert. Ich nutze ja das I2C1-Modul. Diese
befinden sich beim PIC32MX795L an den Pins 84 und 86. Bei vorherigen
Pics musste ich noch mittels Peripheral Pin select die Funktionen auf
die jeweiligen Pins Mappen. Dies entfällt offenbar bei dem PIC32. Also
einfach I2C Komponente an Pins 84 u. 86 und fertig!
Irgendwelche Verbesserungsvorschläge oder Ideen woran es liegen kann,
dass ich immer in der Schleife hängen bleibe?
Danke und Gruß