Программа корректировки значений емкости контроллера аккумулятора кпк ipaq 4700

Код:
 'Alex Wolfenstein, freem4@mail.ru
'IDE BASCOM-AVR 2.0.7.8
'
'Корректировка контроллера DS2760 в части емкости аккумулятора Ipaq 4700
'Разъем аккумулятора: |=|=|=|=|=|=|=|
'                      ? - - | ? + +
'                          1wire
'Ipaq 214
'Разъем аккумулятора: |=|=|=|=|=|=|=|
'                      ? - - ? ? + +
'
'DS2760 High Precision Li-Ion Battery Monitor
'
'64-BIT NET ADDRESS (8 byte):
'The first 8 bits are the 1-Wire family code (30h for DS2760).
'The next 48 bits are a unique serial number.
'The last 8 bits are a CRC of the first 56 bits.
'MSb 8-bit+CRC 48-bit Serial Number+8-Bit Family Code 30h LSb
'
'The protocol for accessing the DS2760 via the 1-Wire port is as follows:
'Initialization - Net Address Command - Function Command - Transaction/Data
'
'Read Net Address [33h or 39h]
'Match Net Address [55h]
'Skip Net Address [CCh]
'Search Net Address [F0h]
'SWAP [AAh]
'
'Read Data [69h, XX] - Reads data from memory starting at address XX
'Write Data [6Ch, XX] - Writes data to memory starting at address XX
'Copy Data [48h, XX] - Copies shadow RAM data to EEPROM block containing address XX
'Recall Data [B8h, XX] - Recalls EEPROM block containing address XX to shadow RAM
'Lock [6Ah, XX] - Permanently locks the block of EEPROM containing address XX
'

$regfile = "m328pdef.dat"
$crystal=3686400
$hwstack = 250
$swstack = 250
$framesize = 300

'Dim Con As String * 10

Dim Byt01 As Byte
Dim Byt02 As Byte
Dim Byt03 As Byte
Dim Byt04 As Byte
Dim Byt05 As Byte
Dim Byt06 As Byte
Dim Byt07 As Byte
Dim Byt08 As Byte
Dim Byt09 As Byte

Dim CapHi As Word
Dim CapLo As Word

'*** конфигурируем пины

Config 1wire = Portc.1 'на порт повесим DS2760 с подтяжкой резистором на 4,7К к +5 вольт

Open "comc.3:9600,8,n,1" For Input As #1

Open "comc.5:9600,8,n,1" For Output As #2

1wreset
If Err = 1 Then
Print #2 , "DS2760 not found";  '; 13
Stop
Else

'*** читаем сетевой адрес

1wwrite &H33 'Read Net Address [33h or 39h]
Byt01 = 1wread() '1
Byt02 = 1wread() '2
Byt03 = 1wread() '3
Byt04 = 1wread() '4
Byt05 = 1wread() '5
Byt06 = 1wread() '6
Byt07 = 1wread() '7
Byt08 = 1wread() '8

Print #2 , "DS2760 found, 1-Wire family code 30 = " ; Hex(Byt01)

End If

Gosub ProtReg

Gosub StaReg

'*** читаем верхний уровень емкости

Gosub EmkHi

'*** читаем нижний уровень емкости

Gosub EmkLow


Do
Cikl:

Print #2 , "Please input 1 - Read or 2 - Write"
Input #1, Byt01

If Byt01 = 1 Then
Goto ReadB

End If

If Byt01 = 2 Then
Goto WriteB

Stop

End If

ReadB:

'*** читаем верхний уровень емкости

Gosub EmkHi

'*** читаем нижний уровень емкости

Gosub EmkLow

Goto Cikl


WriteB:
Print #2 , "Please input Up emkost, DEC: "
Input #1, CapHi
Byt01 = high (CapHi)
Byt02 = low (CapHi)

Print #2 , "Write to DS2760 ..."

1wreset
1wwrite &HCC 'Skip Net Address [CCh]
1wwrite &H6C 'Write Data [6Ch, XX] - Writes data to memory starting at address XX
1wwrite &H20 'EEPROM, block 0
1wwrite Byt01
1wwrite Byt02

Gosub EmkHi

Print #2 , "Please input Low emkost, DEC: "
Input #1, CapHi
Byt01 = high (CapHi)
Byt02 = low (CapHi)

Print #2 , "Write to DS2760 ..."

1wreset
1wwrite &HCC 'Skip Net Address [CCh]
1wwrite &H6C 'Write Data [6Ch, XX] - Writes data to memory starting at address XX
1wwrite &H2E 'EEPROM, block 0
1wwrite Byt02

Gosub EmkLow

Goto Cikl

Loop

End

EmkHi:
'*** читаем верхний уровень емкости

1wreset
1wwrite &HCC 'Skip Net Address [CCh]
1wwrite &H69 'Read Data [69h, XX] - Reads data from memory starting at address XX
1wwrite &H20 'EEPROM, block 0
Byt01 = 1wread() '1 20H (MSB)
Byt02 = 1wread() '2 21H (LSB)
CapHi = MAKEINT (Byt02, Byt01) 'varn = MAKEINT(LSB , MSB)

Print #2 , "Emkost battery, 20H (MSB) и 21H(LSB) = " ; Hex(Byt01) ; " " ; Hex(Byt02) ; " " ; CapHi

Return


'*** читаем нижний уровень емкости

EmkLow:

1wreset
1wwrite &HCC 'Skip Net Address [CCh]
1wwrite &H69 'Read Data [69h, XX] - Reads data from memory starting at address XX
1wwrite &H2E 'EEPROM, block 0
Byt01 = 1wread() '1 2EH

Print #2 , "Emkost battery, 2EH = "; Hex(Byt01); " " ; Byt01

Return

ProtReg:

1wreset
1wwrite &HCC 'Skip Net Address [CCh]
1wwrite &H69 'Read Data [69h, XX] - Reads data from memory starting at address XX
1wwrite &H00 'Protection Register
Byt01 = 1wread() '1 00H

Print #2 , "OV UV COC DOC CC DC CE DE"
Print #2 , "Protection Register, 00H = "; Bin(Byt01)

Return

StaReg:

1wreset
1wwrite &HCC 'Skip Net Address [CCh]
1wwrite &H69 'Read Data [69h, XX] - Reads data from memory starting at address XX
1wwrite &H01 'Status Register
Byt01 = 1wread() '1 01H

Print #2 , "X X PMOD RNAOP SWEN X X X"
Print #2 , "Status Register, 01H = "; Bin(Byt01)

Return