xMega und TWI / I2C

  • 1
  • 2
  • 3
  • Page 3 of 3
miparo
Administrator
Avatar
Gender:
Location: Germany
Age: 58
Posts: 959
Registered: 09 / 2007
Subject:

Re: xMega und TWI / I2C

 · 
Posted: 24.10.2013 - 19:01  ·  #17
Hallo Thomas,
wenn ich das mal noch wüsste aber es könnte sein, da man ja nur selten LESEN muss .
In der Regel wird ja nur geschrieben.

Gruß
miparo
Thomas.AC
Benutzer
Avatar
Gender: n/a
Age: 43
Posts: 308
Registered: 07 / 2013
Subject:

Re: xMega und TWI / I2C

 · 
Posted: 11.11.2013 - 12:21  ·  #18
zur Info: zwischenzeitlich konnte ich den Lesezugriff über folgende Funktion realisieren.
Kritik ist Willkommen!

var twitimeout : SysTimer8;

{-------------------------------------------------------------------------------
TWICombinedRead
input parameters:
DEVADR : 7-bit I2C Device Address
subaddress : subaddress
pdata : location for the read data
bytesToRead : bytes to read
-------------------------------------------------------------------------------}
function twiCombinedRead (
DEVADR : byte;
subaddress : Word;
pData : pointer;
bytesToRead : byte
) : boolean;
begin


SetSysTimer(twitimeout,2); //

// Reset TWIE
TWIEMASTERCTRLA := $00;
TWIEMASTERCTRLA := $08;
TWIEMASTERSTATUS := $FD; // clear all flags, force bus state = idle;

// write I2C address Byte +RW = 0
TWIEMASTERADDR := DEVADR shl 1;
repeat
until Bit(TWIEMASTERSTATUS, 6) or isSysTimerZero(twitimeout); // wif?
if TWIEMASTERSTATUS <> $62 then // return, if incorrect STATUS
Return (false);
endif;

// write subaddress
TWIEMASTERDATA := HI(subaddress); // write highbyte subaddress
repeat
until Bit(TWIEMASTERSTATUS, 6) or isSysTimerZero(twitimeout); // wif?
if TWIEMASTERSTATUS <> $62 then // return, if incorrect STATUS
Return (false);
endif;
TWIEMASTERDATA := LO(subaddress); // write lowbyte subaddress
repeat
until Bit(TWIEMASTERSTATUS, 6) or isSysTimerZero(twitimeout); // wif
if TWIEMASTERSTATUS <> $62 then // return, if incorrect STATUS
Return (false);
endif;

// bytesToRead?
if bytesToRead = 0 then
TWIEMASTERCTRLC := $07; // stop
Return(true);
endif;

// read first byte (read access with repeated start condition)
TWIEMASTERADDR := (DEVADR shl 1) or $01; // write I2C address + R/W = 1
repeat
until Bit(TWIEMASTERSTATUS, 7) or isSysTimerZero(twitimeout); // rif?
if TWIEMASTERSTATUS <> $A2 then // return, if incorrect STATUS
Return (false);
endif;
pData^++ := TWIEMASTERDATA;
dec(bytesToRead);

// read next bytes
while bytesToRead > 0 do
TWIEMASTERCTRLC := $02; // ack + read more byte
repeat
until Bit(TWIEMASTERSTATUS, 7) or isSysTimerZero(twitimeout); // rif?
if TWIEMASTERSTATUS <> $A2 then // return, if incorrect STATUS
Return (false);
endif;
pData^++ := TWIEMASTERDATA;
dec(bytesToRead);
endwhile;

// send stop condition
TWIEMASTERCTRLC := $07;
Return(true);
end;
  • 1
  • 2
  • 3
  • Page 3 of 3
Selected quotes for multi-quoting:   0

Registered users in this topic

Currently no registered users in this section

The statistic shows who was online during the last 5 minutes. Updated every 90 seconds.
MySQL Queries: 15 · Cache Hits: 14   50   64 · Page-Gen-Time: 0.028435s · Memory Usage: 2 MB · GZIP: on · Viewport: SMXL-HiDPI