I2CCode

SwordfishUser.I2CCode History

Hide minor edits - Show changes to output

March 02, 2007, at 01:25 PM by David Barker -
Deleted lines 0-8:
The following sample code was used to evaluate different compilers.

*[[#C1 | Swordfish BASIC]] - %newwin%[[http://www.sfcompiler.co.uk |  Mecanique]]
*[[#C2 | CCS C]] - %newwin%[[http://www.ccsinfo.com |  Custom Computer Services, Inc]]
*[[#C3 | MikroElectronika BASIC]] - %newwin%[[http://www.mikroe.com |  MikroElectronika]]
*[[#C4 | PROTON BASIC]]- %newwin%[[http://www.picbasic.org |  Crownhill]]

The code itself is not intended to be optimised in any way. However, each implementation is intended to match as closel as possible the original Swordfish version.

March 02, 2007, at 09:37 AM by David Barker -
Added lines 1-242:
The following sample code was used to evaluate different compilers.

*[[#C1 | Swordfish BASIC]] - %newwin%[[http://www.sfcompiler.co.uk |  Mecanique]]
*[[#C2 | CCS C]] - %newwin%[[http://www.ccsinfo.com |  Custom Computer Services, Inc]]
*[[#C3 | MikroElectronika BASIC]] - %newwin%[[http://www.mikroe.com |  MikroElectronika]]
*[[#C4 | PROTON BASIC]]- %newwin%[[http://www.picbasic.org |  Crownhill]]

The code itself is not intended to be optimised in any way. However, each implementation is intended to match as closel as possible the original Swordfish version.

!!! [[#C1]]Swordfish
=code [=
dim RW as SSPSTAT.2     
dim NAck as SSPCON2.6 
dim Timer as TMR1L.AsWord
dim Value as byte
 
// wait for idle...
sub WaitForIdle()
  repeat
  until (SSPCON2 and $1F) = 0 and (RW = 0)
end sub

// wait for write completion...
sub WaitForWrite(pControl as byte)
  I2C.Start
  I2C.WriteByte(pControl)
  WaitForIdle
  while NAck = 1
      I2C.Restart
      I2C.WriteByte(pControl)
      WaitForIdle
  wend
  I2C.Stop
end sub
 
// program start...
I2C.Initialize
Timer = 0 
PIE1.0 = 1
T1CON.0 = 1

// write some data...
I2C.Start                         
I2C.WriteByte($A0)             
I2C.WriteByte(0)     
I2C.WriteByte(0)       
I2C.WriteByte("S")               
I2C.Stop
WaitForWrite($A0)

// read the data back...
I2C.Start
I2C.WriteByte($A0) 
I2C.WriteByte(0)       
I2C.WriteByte(0)       
I2C.Restart                       
I2C.WriteByte($A1)
Value = I2C.ReadByte
I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)
I2C.Stop
T1CON.0 = 0

// output the result
end
=]
!!! [[#C2]]CCS C
=cpp [=

#use i2c(Master,sda=PIN_C4,scl=PIN_C3,FORCE_HW,slow=100000)
#byte SSPSTAT = 0x0FC7
#byte SSPCON2 = 0x0FC5
#bit RW = SSPSTAT.2   
#bit NAck = SSPCON2.6
#byte PIE1 = 0x0F9D
#byte T1CON = 0x0FCD
#bit TMR1IE = PIE1.0
#bit TMR1ON = T1CON.0

// wait for idle...
void WaitForIdle() {
  do {}   
  while ((SSPCON2 & 0x1F) != 0 && (RW != 0));
}

// wait for write completion...
void WaitForWrite(int8 pControl) {
  i2c_start();
  i2c_write(pControl);
  WaitForIdle();
  while (NAck == 1) { 
      i2c_start();
      i2c_write(pControl);
      WaitForIdle();
  }
  i2c_stop();
}

// program start...
void main() {
  char Value;
 
  set_timer1(0);
  TMR1IE = 1;
  TMR1ON = 1;
 
  // write some data...
  i2c_start();
  i2c_write(0xA0);
  i2c_write(0);
  i2c_write(0);
  i2c_write('C');//("C");
  i2c_stop();
  WaitForWrite(0xA0);

  // read the data back...
  i2c_start();
  i2c_write(0xA0);
  i2c_write(0);
  i2c_write(0);
  i2c_start();
  i2c_write(0xA1);
  Value = i2c_read(0);
  i2c_stop();
 
  TMR1ON = 0;

  // display result...
}
=]
!!! [[#C3]]MikroElectronika BASIC
=code [=
dim TMR1 as word absolute 0x0FCE
dim Value as byte

' wait for idle...
sub procedure WaitForIdle
  do
  loop until (SSPCON2 and $1F) = 0 and (SSPSTAT.2 = 0)
end sub

' wait for write completion...
sub procedure WaitForWrite(dim pControl as byte)
  I2C_Start
  I2C_Wr(pControl)
  WaitForIdle
  while SSPCON2.6 = 1
      I2C_Repeated_Start
      I2C_Wr(pControl)
      WaitForIdle
  wend
  I2C_Stop
end sub

' program start...
main:
  I2C_Init(100000)
  TMR1 = 0
  SetBit(PIE1,0)
  SetBit(T1CON,0)

  ' write some data...
  I2C_Start
  I2C_Wr($A0)
  I2C_Wr(0)
  I2C_Wr(0)
  I2C_Wr("M")
  I2C_Stop
  WaitForWrite($A0)

  ' read the data back...
  I2C_Start
  I2C_Wr($A0)
  I2C_Wr(0)
  I2C_Wr(0)
  I2C_Repeated_Start
  I2C_Wr($A1)
  Value = I2C_Rd(0)
  I2C_Stop

  ClearBit(T1CON,0)

  ' output the result
end.
=]
!!! [[#C4]]PROTON BASIC
=code [=
HBUS_BITRATE 100

Dim RW As SSPSTAT.2   
Dim NAck As SSPCON2.6
Dim Value As Byte
Dim Control As Byte
Dim Timer As TMR1L.Word

Timer = 0    ' timer to zero
PIE1.0 = 1  ' init the timer
T1CON.0 = 1  ' start

' write some data...
HBStart
HBusOut $A0
HBusOut 0
HBusOut 0
HBusOut "D"
HBStop
Control = $A0
GoSub WaitForWrite

' read the data back...
HBStart
HBusOut $A0
HBusOut 0
HBusOut 0
HBREStart
HBusOut $A1
HBusIn Value
HBStop

T1CON.0 = 0  ' stop

' display result...
End

' wait for idle...
WaitForIdle:
  Repeat
  Until SSPCON2 & $1F = 0 And RW = 0 
Return

' wait for write completion...
WaitForWrite:
  HBStart
  HBusOut Control
  GoSub WaitForIdle
  While NAck = 1
      HBREStart
      HBusOut Control
      GoSub WaitForIdle
  Wend
  HBStop
Return
=]