Page 2 of 2
Re: Problem translating VB6 to PB (serial port communication
Posted: Mon Jun 07, 2010 8:21 am
by infratec
Hi,
in PureBASIC it should be
which means a test for the highest bit.
Because AND is a logical and and not abit wise and.
So this is definately a fault.
But you should also replace the command $51 with $EE and after this I2C packet wait 1s to give the
IC a chance to convert the temperature.
Are you sure that it is a DS1621?
Because $51 is not a valid command according to the datasheet.
Bernd
Re: Problem translating VB6 to PB (serial port communication
Posted: Mon Jun 07, 2010 8:37 am
by CrazyFrog112
It's DS1631, I changed the value, acording to DS1631 Datasheet, so $51 is good.
Re: Problem translating VB6 to PB (serial port communication
Posted: Mon Jun 07, 2010 8:42 am
by CrazyFrog112
Thanks, I got it, it works !
The "If B & $80" was the fault
Re: Problem translating VB6 to PB (serial port communication
Posted: Mon Jun 07, 2010 8:44 am
by infratec
Hi,
yes, that makes sense.
Please try this
Code: Select all
Procedure IIC_tx_byte(B.a)
For i=0 To 7
If B & $80
IIC_tx_bit_1()
Else
IIC_tx_bit_0()
EndIf
B << 1
Next i
IIC_tx_byte = IIC_rx_bit
ProcedureReturn IIC_tx_byte
EndProcedure
and
Code: Select all
Procedure.d temperature(adress.a)
address << 1
IIC_stop()
IIC_start()
IIC_tx_byte(adress)
IIC_tx_byte($22)
IIC_stop()
IIC_start()
IIC_tx_byte(adress)
IIC_tx_byte($AC)
IIC_tx_byte($C)
IIC_stop()
IIC_start()
IIC_tx_byte(adress)
IIC_tx_byte($51)
IIC_stop()
Delay(1000)
IIC_start()
IIC_tx_byte(adress)
IIC_tx_byte($AA)
IIC_start()
IIC_tx_byte(address | $01)
temperature_int.l = IIC_rx_byte(1)
temperature_frac.l = IIC_rx_byte(1)
IIC_stop()
Debug temperature_int
Debug temperature_frac
I think than it should work.
Please note also the .a at the parameters which means it is a unsigned byte.
Bernd
Ohhhh, I was to late

Re: Problem translating VB6 to PB (serial port communication
Posted: Mon Jun 07, 2010 8:47 am
by CrazyFrog112
Here is what the debug window "says" :
Code: Select all
Serial Port Opened!
The Integer part is :22
The Fraction part is :208
Temperature is :22.812500
Serial Port Opened!
The Integer part is :22
The Fraction part is :208
Temperature is :22.812500
Serial Port Opened!
The Integer part is :22
The Fraction part is :240
Temperature is :22.937500
Serial Port Opened!
The Integer part is :24
The Fraction part is :48
Temperature is :24.187500
Serial Port Opened!
The Integer part is :24
The Fraction part is :144
Temperature is :24.562500
And...Thanks Again

Re: [SOLVED] Problem translating VB6 to PB (serial port )
Posted: Mon Jun 07, 2010 9:47 am
by infratec
Hi,
could you do me a favour?
Please try this
Code: Select all
;
; DS1631 at a serial port
;
#StartConvertT = $51
#StopConvertT = $22
#ReadTemperature = $AA
#AccessTH = $A1
#AccessTL = $A2
#AccessConfig = $AC
#SoftwarePOR = $54
#IIC_Read = $01
#IIC_Write = $00
#IIC_Ack = #True
#IIC_Nack = #False
#Accuracy9Bit = $00
#Accuracy10Bit = $04
#Accuracy11Bit = $08
#Accuracy12Bit = $0C
Procedure.i open_iic_bus()
comport.s = "COM1"
OpenSerialPort(0, comport, 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_RtsCtsHandshake, 1, 1)
EndProcedure
Procedure close_iic_bus()
CloseSerialPort(0)
EndProcedure
Procedure iic_wait()
EndProcedure
Procedure SDA_value()
ProcedureReturn GetSerialPortStatus(0, #PB_SerialPort_CTS)
EndProcedure
Procedure SDA_low()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 0)
iic_wait()
EndProcedure
Procedure SDA_high()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 1)
iic_wait()
EndProcedure
Procedure SCL_low()
SetSerialPortStatus(0, #PB_SerialPort_RTS, 0)
iic_wait()
EndProcedure
Procedure SCL_high()
SetSerialPortStatus(0, #PB_SerialPort_RTS, 1)
iic_wait()
EndProcedure
Procedure SDA_input()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 1)
EndProcedure
Procedure SDA_output()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 1)
EndProcedure
Procedure IIC_rx_bit()
SDA_input()
SCL_low()
SCL_high()
retval = SDA_value()
SCL_low()
ProcedureReturn retval
EndProcedure
Procedure IIC_tx_bit_0()
SCL_low()
SDA_output()
SDA_low()
SCL_high()
SCL_low()
EndProcedure
Procedure IIC_tx_bit_1()
SCL_low()
SDA_output()
SDA_high()
SCL_high()
SCL_low()
EndProcedure
Procedure IIC_rx_byte(Acknowledge)
retval = 0
For i = 0 To 7
retval << 1
If IIC_rx_bit()
retval | $01
EndIf
Next i
If Acknowledge
IIC_tx_bit_0()
Else
IIC_tx_bit_1()
EndIf
ProcedureReturn retval
EndProcedure
Procedure IIC_tx_byte(Byte)
;Debug Hex(Byte)
For i = 0 To 7
If Byte & $80
IIC_tx_bit_1()
Else
IIC_tx_bit_0()
EndIf
Byte << 1
Next i
ProcedureReturn IIC_rx_bit()
EndProcedure
Procedure IIC_stop()
SDA_output()
SDA_low()
SCL_high()
SDA_high()
EndProcedure
Procedure IIC_start()
SDA_output()
SDA_high()
SCL_high()
SDA_low()
EndProcedure
Procedure.f DS1631ReadTemperature(Address, Accuracy)
Address << 1
open_iic_bus()
IIC_stop()
IIC_start()
IIC_tx_byte(Address | #IIC_Write)
IIC_tx_byte(#StopConvertT)
IIC_stop()
IIC_start()
IIC_tx_byte(Address | #IIC_Write)
IIC_tx_byte(#AccessConfig)
IIC_tx_byte(Accuracy)
IIC_stop()
IIC_start()
IIC_tx_byte(Address | #IIC_Write)
IIC_tx_byte(#StartConvertT)
IIC_stop()
Select Accuracy
Case #Accuracy9Bit : Delay(100)
Case #Accuracy10Bit : Delay(200)
Case #Accuracy11Bit : Delay(400)
Case #Accuracy12Bit : Delay(800)
EndSelect
IIC_start()
IIC_tx_byte(Address | #IIC_Write)
IIC_tx_byte(#ReadTemperature)
IIC_start()
IIC_tx_byte(Address | #IIC_Read)
Value.u = IIC_rx_byte(#IIC_Ack) << 8
Value | IIC_rx_byte(#IIC_Nack)
IIC_stop()
Debug Value
If Value & $8000 ; if sign bit is set, then temp is negative
temperature.f = (Value - 65536) / 256.0
Else
temperature.f = Value / 256.0
EndIf
;temperature =(temperature * 9/5) + 32 ; Fahrenheit
ProcedureReturn temperature
EndProcedure
For i = 1 To 1
Debug DS1631ReadTemperature($48, #Accuracy12Bit)
; Delay(1000)
Next i
Thanks,
Bernd
Re: [SOLVED] Problem translating VB6 to PB (serial port )
Posted: Mon Jun 07, 2010 9:51 am
by infratec
I modified the code above!
IIC_tx_byte() had not returned the acknowledge bit.
Bernd
Re: [SOLVED] Problem translating VB6 to PB (serial port )
Posted: Mon Jun 07, 2010 9:58 am
by CrazyFrog112
This is what it shows in both cases ( before and after the modification ) :
Code: Select all
65535
-0.00390625
65535
-0.00390625
65535
-0.00390625
65535
-0.00390625
65535
-0.00390625
Re: [SOLVED] Problem translating VB6 to PB (serial port )
Posted: Mon Jun 07, 2010 10:02 am
by CrazyFrog112
This is my working code :
Code: Select all
Procedure.i open_iic_bus()
comport.s = "COM1"
gg= OpenSerialPort(0, comport, 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1024, 512)
If gg
Debug "Serial Port Opened!"
Else
Debug "nooo"
EndIf
EndProcedure
Procedure close_iic_bus()
CloseSerialPort(0)
EndProcedure
Procedure iic_wait()
EndProcedure
Procedure SDA_value()
sda_value = GetSerialPortStatus(0, #PB_SerialPort_CTS)
ProcedureReturn sda_value
EndProcedure
Procedure SDA_low()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 0)
iic_wait()
EndProcedure
Procedure SDA_high()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 1)
iic_wait()
EndProcedure
Procedure SCL_low()
SetSerialPortStatus(0, #PB_SerialPort_RTS, 0)
iic_wait()
EndProcedure
Procedure SCL_high()
SetSerialPortStatus(0, #PB_SerialPort_RTS, 1)
iic_wait()
EndProcedure
Procedure SDA_input()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 1)
EndProcedure
Procedure SDA_output()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 1)
EndProcedure
Procedure IIC_rx_bit()
SDA_input()
SCL_low()
SCL_high()
retval = SDA_value()
SCL_low()
IIC_rx_bit = retval
ProcedureReturn IIC_rx_bit
EndProcedure
Procedure IIC_tx_bit_0()
SCL_low()
SDA_output()
SDA_low()
SCL_high()
SCL_low()
EndProcedure
Procedure IIC_tx_bit_1()
SCL_low()
SDA_output()
SDA_high()
SCL_high()
SCL_low()
EndProcedure
Procedure IIC_rx_byte(acknowledge.l)
For i=0 To 7
retval = retval * 2
If IIC_rx_bit()
retval = retval + 1
EndIf
Next i
If acknowledge
IIC_tx_bit_0()
Else
IIC_tx_bit_1()
EndIf
IIC_rx_byte = retval
ProcedureReturn IIC_rx_byte
EndProcedure
Procedure IIC_tx_byte(B.l)
For i=0 To 7
If (B & $80)
IIC_tx_bit_1()
Else
IIC_tx_bit_0()
EndIf
B = B * 2
Next i
IIC_tx_byte = IIC_rx_bit()
ProcedureReturn IIC_tx_byte
EndProcedure
Procedure IIC_stop()
SDA_output()
SDA_low()
SCL_high()
SDA_high()
EndProcedure
Procedure IIC_start()
SDA_output()
SDA_high()
SCL_high()
SDA_low()
EndProcedure
Procedure.d temperature(adress.l)
adress = adress * 2
open_iic_bus()
IIC_stop()
IIC_start()
IIC_tx_byte(adress)
IIC_tx_byte($22)
IIC_stop()
IIC_start()
IIC_tx_byte(adress)
IIC_tx_byte($AC)
IIC_tx_byte($C)
IIC_stop()
IIC_start()
IIC_tx_byte(adress)
IIC_tx_byte($51)
IIC_stop()
Delay(1000)
IIC_start()
IIC_tx_byte(adress)
IIC_tx_byte($AA)
IIC_stop()
IIC_start()
ass=adress+1
IIC_tx_byte(ass)
temperature_int = IIC_rx_byte(1)
temperature_frac = IIC_rx_byte(1)
IIC_stop()
Debug "The Integer part is :" +Str(temperature_int)
Debug "The Fraction part is :" +Str(temperature_frac)
temperature.f = (temperature_int * 256 + temperature_frac) / 128 * 5 / 10
If temperature_int >= 128
temperature.f = temperature - 256
EndIf
close_iic_bus()
ProcedureReturn temperature.f
EndProcedure
For i=1 To 5
Debug "Temperature is :" + StrF(temperature($48))
Next i
Re: [SOLVED] Problem translating VB6 to PB (serial port )
Posted: Mon Jun 07, 2010 10:28 am
by infratec
Hi, I modified the code above again.
I found that I made a mistake at the #Accuracy definitions.
Maybe that was the fault.
Bernd
Re: [SOLVED] Problem translating VB6 to PB (serial port )
Posted: Mon Jun 07, 2010 10:41 am
by CrazyFrog112
Yes, it works, but you have tu use #PB_SerialPort_Nohandshake.
With #PB_SerialPort_RtsCtsHandshake it gives me 0.0
With #PB_SerialPort_NoHandshake it gives me :
This is the modified code, with #PB_SerialPort_NoHandshake
Code: Select all
;
; DS1631 at a serial port
;
#StartConvertT = $51
#StopConvertT = $22
#ReadTemperature = $AA
#AccessTH = $A1
#AccessTL = $A2
#AccessConfig = $AC
#SoftwarePOR = $54
#IIC_Read = $01
#IIC_Write = $00
#IIC_Ack = #True
#IIC_Nack = #False
#Accuracy9Bit = $00
#Accuracy10Bit = $04
#Accuracy11Bit = $08
#Accuracy12Bit = $0C
Procedure.i open_iic_bus()
comport.s = "COM1"
OpenSerialPort(0, comport, 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1, 1)
EndProcedure
Procedure close_iic_bus()
CloseSerialPort(0)
EndProcedure
Procedure iic_wait()
EndProcedure
Procedure SDA_value()
ProcedureReturn GetSerialPortStatus(0, #PB_SerialPort_CTS)
EndProcedure
Procedure SDA_low()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 0)
iic_wait()
EndProcedure
Procedure SDA_high()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 1)
iic_wait()
EndProcedure
Procedure SCL_low()
SetSerialPortStatus(0, #PB_SerialPort_RTS, 0)
iic_wait()
EndProcedure
Procedure SCL_high()
SetSerialPortStatus(0, #PB_SerialPort_RTS, 1)
iic_wait()
EndProcedure
Procedure SDA_input()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 1)
EndProcedure
Procedure SDA_output()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 1)
EndProcedure
Procedure IIC_rx_bit()
SDA_input()
SCL_low()
SCL_high()
retval = SDA_value()
SCL_low()
ProcedureReturn retval
EndProcedure
Procedure IIC_tx_bit_0()
SCL_low()
SDA_output()
SDA_low()
SCL_high()
SCL_low()
EndProcedure
Procedure IIC_tx_bit_1()
SCL_low()
SDA_output()
SDA_high()
SCL_high()
SCL_low()
EndProcedure
Procedure IIC_rx_byte(Acknowledge)
retval = 0
For i = 0 To 7
retval << 1
If IIC_rx_bit()
retval | $01
EndIf
Next i
If Acknowledge
IIC_tx_bit_0()
Else
IIC_tx_bit_1()
EndIf
ProcedureReturn retval
EndProcedure
Procedure IIC_tx_byte(Byte)
;Debug Hex(Byte)
For i = 0 To 7
If Byte & $80
IIC_tx_bit_1()
Else
IIC_tx_bit_0()
EndIf
Byte << 1
Next i
ProcedureReturn IIC_rx_bit()
EndProcedure
Procedure IIC_stop()
SDA_output()
SDA_low()
SCL_high()
SDA_high()
EndProcedure
Procedure IIC_start()
SDA_output()
SDA_high()
SCL_high()
SDA_low()
EndProcedure
Procedure.f DS1631ReadTemperature(Address, Accuracy)
Address << 1
open_iic_bus()
IIC_stop()
IIC_start()
IIC_tx_byte(Address | #IIC_Write)
IIC_tx_byte(#StopConvertT)
IIC_stop()
IIC_start()
IIC_tx_byte(Address | #IIC_Write)
IIC_tx_byte(#AccessConfig)
IIC_tx_byte(Accuracy)
IIC_stop()
IIC_start()
IIC_tx_byte(Address | #IIC_Write)
IIC_tx_byte(#StartConvertT)
IIC_stop()
Select Accuracy
Case #Accuracy9Bit : Delay(100)
Case #Accuracy10Bit : Delay(200)
Case #Accuracy11Bit : Delay(400)
Case #Accuracy12Bit : Delay(800)
EndSelect
IIC_start()
IIC_tx_byte(Address | #IIC_Write)
IIC_tx_byte(#ReadTemperature)
IIC_start()
IIC_tx_byte(Address | #IIC_Read)
Value.u = IIC_rx_byte(#IIC_Ack) << 8
Value | IIC_rx_byte(#IIC_Nack)
IIC_stop()
Debug Value
If Value & $8000 ; if sign bit is set, then temp is negative
temperature.f = (Value - 65536) / 256.0
Else
temperature.f = Value / 256.0
EndIf
;temperature =(temperature * 9/5) + 32 ; Fahrenheit
ProcedureReturn temperature
EndProcedure
For i = 1 To 1
Debug DS1631ReadTemperature($48, #Accuracy12Bit)
; Delay(1000)
Next i
Re: [SOLVED] Problem translating VB6 to PB (serial port )
Posted: Mon Jun 07, 2010 10:44 am
by infratec
Hi,
thank you very much
But one thing makes me wonder:
You devide the received value by 64 (128 * 5 / 10) I divide it with 256, so
it should be impossible, that we have the same result.
Bernd
Re: [SOLVED] Problem translating VB6 to PB (serial port )
Posted: Mon Jun 07, 2010 11:30 am
by CrazyFrog112
Here is my code result :
Code: Select all
Serial Port Opened!
The Integer part is :23
The Fraction part is :96
Temperature is :23.375000
Here is your code result :
I use the formula :
Code: Select all
Debug "The Integer part is :" +Str(temperature_int)
Debug "The Fraction part is :" +Str(temperature_frac)
temperature.f = (temperature_int * 256 + temperature_frac) / 128 * 5 / 10
If temperature_int >= 128
temperature.f = temperature - 256
EndIf
Re: [SOLVED] Problem translating VB6 to PB (serial port )
Posted: Mon Jun 07, 2010 11:43 am
by CrazyFrog112
I got it . In the formula :
Code: Select all
temperature.f = (temperature_int * 256 + temperature_frac) / 128 * 5 / 10
, first time, the (temperature_int * 256 + temperature_frac) is divided by 128, then the result is multiplied by 0.5
Code: Select all
( x / 128 ) * 0.5 = x / 64, because x*0.5 means x/2
Re: [SOLVED] Problem translating VB6 to PB (serial port )
Posted: Mon Jun 07, 2010 12:03 pm
by infratec
Hi, hi,
now you made the same mistake like I did:
You wrote that you are dividing by 64. That's wrong.
You divide also by 256 like I did, because it is not
128 * 5 / 10 (which is 64)
the multiplicator is 1/128 * 5 / 10 = 0.0039 wich is 1/256.
Puh... what a heavy birth
Bernd