$regfile = "attiny88.dat"
$crystal = 16000000
$hwstack = 32
$swstack = 32
$framesize = 64
Config Submode = New
'======================================== Константы ==============================================
'значения нужных нам фьюзов
Const High_fuse_write = &B1101_1111
Const Low_fuse_write = &B1110_1110
Const Extended_fuse_write = &B1111_1111
'команды
Const Cmd_write_fuse = &B0100_0000
Const Cmd_erase_chip = &B1000_0000
Const Cmd_read_signature = &B0000_1000
Const Cmd_read_fuse = &B0000_0100
'======================================= Переменные ==============================================
Dim Signature As Dword 'сигнатура. по ней.можно определять что за МК
Dim Low_fuse As Byte
Dim High_fuse As Byte
Dim Extended_fuse As Byte
Dim Lock_bits As Byte
Dim Buffer_byte As Byte
'======================================= Настройка портов =======================================
Clki Alias Porta.0 : Config Clki = Output : Reset Clki
'порты точно совпадают с заводскими портами Attiny88 для HVPP
Bsy Alias Pind.1 : Config Bsy = Input : Set Bsy
Oe Alias Portd.2 : Config Oe = Output : Set Oe
Wr Alias Portd.3 : Config Wr = Output : Set Wr
Bs1 Alias Portd.4 : Config Bs1 = Output : Reset Bs1
Bs2 Alias Portc.2 : Config Bs2 = Output : Reset Bs2
Xa0 Alias Portd.5 : Config Xa0 = Output : Reset Xa0
Xa1 Alias Portd.6 : Config Xa1 = Output : Reset Xa1
Pagel Alias Portd.7 : Config Pagel = Output : Reset Pagel
5v Alias Portc.5 : Config 5v = Output : 5v = 0
12v Alias Portc.7 : Config 12v = Output : 12v = 0
Led Alias Portd.0 : Config Led = Output
'выход в терминал
Open "Comc.4:115200,8,n,1" For Output As #1
'======================================== Ф У Н К Ц И И ================================================
Declare Sub Writebyte(byval Cmd As Byte)
Declare Sub Readbyte()
Declare Sub Writefuse()
Declare Sub Read_fuse()
Declare Sub Read_signature()
Declare Sub Erase_chip()
Declare Sub Start_prog()
Declare Sub Waitfornotbusy()
Sub Start_prog()
Waitms 100
Set 5v 'тут запитываем программируемый МК от порта МК-программатора
Waitms 1
Set Led : Wait 5 : Reset Led 'задержка на переключение ресет с 0В на 12В вручную.
Set 12v 'ВАЖНО чтобы до подачи питания ресет был прижат к земле
Waitms 5
End Sub
Sub Writebyte(byval Cmd As Byte)
Oe = 1 'сообщаем, что собираемся писать
Waitms 5
Config Portc.1 = Output 'порты на выход
Config Portc.0 = Output
Config Portb.5 = Output
Config Portb.4 = Output
Config Portb.3 = Output
Config Portb.2 = Output
Config Portb.1 = Output
Config Portb.0 = Output
Portc.1 = Cmd.7 'побитно присваиваем портам значение
Portc.0 = Cmd.6
Portb.5 = Cmd.5
Portb.4 = Cmd.4
Portb.3 = Cmd.3
Portb.2 = Cmd.2
Portb.1 = Cmd.1
Portb.0 = Cmd.0
Waitms 5
Set Clki 'передаем значение портов в МК положительным импульсом CLKI
Waitms 5
Reset Clki
Waitms 5
End Sub()
Sub Readbyte()
Buffer_byte = 0
Waitms 5
Oe = 0 'сообщаем, что собираемся читать
Waitms 5
Config Pinc.1 = Input : Set Pinc.1 'порты на вход с подтяжкой к питанию
Config Pinc.0 = Input : Set Pinc.0
Config Pinb.5 = Input : Set Pinb.5
Config Pinb.4 = Input : Set Pinb.4
Config Pinb.3 = Input : Set Pinb.3
Config Pinb.2 = Input : Set Pinb.2
Config Pinb.1 = Input : Set Pinb.1
Config Pinb.0 = Input : Set Pinb.0
Buffer_byte.7 = Pinc.1 'побитно присваиваем значение переменной
Buffer_byte.6 = Pinc.0
Buffer_byte.5 = Pinb.5
Buffer_byte.4 = Pinb.4
Buffer_byte.3 = Pinb.3
Buffer_byte.2 = Pinb.2
Buffer_byte.1 = Pinb.1
Buffer_byte.0 = Pinb.0
Waitms 5
Oe = 1
Waitms 5
End Sub
Sub Waitfornotbusy()
Waitms 5
Wr = 0 'отрицательным импульсом WR записываем
Waitms 5
Wr = 1
Waitms 5
While Bsy = 0 : Wend 'ждем пока МК запишет
End Sub
Sub Erase_chip() 'очистка МК
Xa0 = 0 : Xa1 = 1 : Bs1 = 0 : Bs2 = 0
Writebyte Cmd_erase_chip
Xa1 = 0
Waitfornotbusy
End Sub
Sub Writefuse() 'запись нужных нам фьюзов
Xa0 = 0 : Xa1 = 1 : Bs1 = 0 : Bs2 = 0
Writebyte Cmd_write_fuse
Xa1 = 0 : Xa0 = 1
Writebyte Low_fuse_write
Waitfornotbusy
Xa0 = 0 : Xa1 = 1 : Bs1 = 0 : Bs2 = 0
Writebyte Cmd_write_fuse
Xa1 = 0 : Xa0 = 1
Writebyte High_fuse_write
Bs1 = 1
Waitfornotbusy
Bs1 = 0
Xa0 = 0 : Xa1 = 1 : Bs1 = 0 : Bs2 = 0
Writebyte Cmd_write_fuse
Xa1 = 0 : Xa0 = 1
Writebyte Extended_fuse_write
Bs2 = 1
Waitfornotbusy
Bs2 = 0
Xa0 = 0 : Xa1 = 0 : Bs1 = 0 : Bs2 = 0
' Writebyte 0
' Xa1 = 0
End Sub
Sub Read_signature() 'чтение сигнатуры
Xa0 = 0 : Xa1 = 1 : Bs1 = 0 : Bs2 = 0
Writebyte Cmd_read_signature
Xa1 = 0
Writebyte 0
Readbyte
Signature = Buffer_byte
Shift Signature , Left , 8
Xa1 = 1
Writebyte Cmd_read_signature
Xa1 = 0
Writebyte 1
Readbyte
Signature = Signature + Buffer_byte
Shift Signature , Left , 8
Xa1 = 1
Writebyte Cmd_read_signature
Xa1 = 0
Writebyte 2
Readbyte
Signature = Signature + Buffer_byte
End Sub
Sub Read_fuse() 'чтение фьюзов
Xa0 = 0 : Xa1 = 1 : Bs1 = 0 : Bs2 = 0
Writebyte Cmd_read_fuse
Xa1 = 0
Readbyte
Low_fuse = Buffer_byte
Bs2 = 1 : Bs1 = 1
Readbyte
High_fuse = Buffer_byte
Bs2 = 1 : Bs1 = 0
Readbyte
Extended_fuse = Buffer_byte
Bs2 = 0 : Bs1 = 1
Readbyte
Lock_bits = Buffer_byte
End Sub
'=================================================================================================
'Г Л А В Н Ы Й Ц И К Л
'=================================================================================================
Start_prog
Waitms 100
Read_signature
Read_fuse
Print#1 , "signature " ; Hex(signature) ; " " ; "Low_fuse " ; Hex(low_fuse) ; " " ; "High_fuse " ; Hex(high_fuse) ; " " ; "Extended_fuse " ; Hex(extended_fuse) ; " " ; "Lock_bits " ; Hex(lock_bits)
Print#1 , "Erase chip..."
Erase_chip
Print#1 , "Write fuse..."
Writefuse
Read_signature
Read_fuse
Print#1 , "signature " ; Hex(signature) ; " " ; "Low_fuse " ; Hex(low_fuse) ; " " ; "High_fuse " ; Hex(high_fuse) ; " " ; "Extended_fuse " ; Hex(extended_fuse) ; " " ; "Lock_bits " ; Hex(lock_bits)
Do
Toggle Led
Waitms 500
Loop
End