Устанавливаем кнопку на Portb.3 для перехода в режим загрузчика.
Код
$regfile "m32def.dat"
$crystal = 8000000
$hwstack = 64
$swstack = 64
$framesize = 64
' для входа в режим bootloadera прижимаем к минусу PORTB.3
'устанавливаем pinb.3 как вход
Config Portb.3 = Input
Portb.3 = 1
Waitms 300
If Pinb.3 = 0 Then
'установка порта bootloadera
Open "COMB.7:38400,8,n,1" For Output As #1 'do not change #1
Open "COMB.6:38400,8,n,1" For Input As #2 'do not change #2
Goto &H3C00
End If
'*******************************************************************************
'далее ваш код
UART загрузчик Bascom
Код
'-------------------------------------------------------------------------------
'set the Lock and Fuse bits as you like, do not use this values
'-------------------------------------------------------------------------------
'$prog &HFF, &HFF, &HFF, &HFF
'-------------------------------------------------------------------------------
'uncomment the lines that correspond to your type of AVR and adjust the values
'-------------------------------------------------------------------------------
'$regfile = "m8def.dat"
'$loader = $C00
'$crystal = 8000000
'const MaxWordBit = 5
'$regfile = "m16def.dat"
'$loader = $1C00
'$crystal = 8000000
'const MaxWordBit = 6
$regfile = "m32def.dat"
$loader = $3c00
$crystal = 8000000
Const Maxwordbit = 6
'$regfile = "m64def.dat"
'$loader = $7c00
'$crystal = 8000000
'Const Maxwordbit = 7
'$regfile = "m88def.dat"
'$loader = $C00
'$crystal = 8000000
'const MaxWordBit = 5
'$regfile = "m128def.dat"
'$loader = $FC00
'$crystal = 8000000
'const MaxWordBit = 7
'$regfile = "m161def.dat"
'$loader = $1E00
'$crystal = 8000000
'const MaxWordBit = 6
'$regfile = "m162def.dat"
'$loader = $1C00
'$crystal = 8000000
'const MaxWordBit = 6
'$regfile = "m168def.dat"
'$loader = $1C00
'$crystal = 8000000
'const MaxWordBit = 6
'$regfile = "m2560def.dat"
'$loader = $1FC00
'$crystal = 8000000
'const MaxWordBit = 7
'$regfile = "m2561def.dat"
'$loader = $1FC00
'$crystal = 8000000
'const MaxWordBit = 7
'$regfile = "m329def.dat"
'$loader = $3C00
'$crystal = 8000000
'const MaxWordBit = 6
'-------------------------------------------------------------------------------
'select the pins / baud for Software Uart
'you can change COMB.7:38400 / COMB.6:38400
Open "COMB.7:38400,8,n,1" For Output As #1 'do not change #1
Open "COMB.6:38400,8,n,1" For Input As #2 'do not change #2
'-------------------------------------------------------------------------------
Const Maxword =(2 ^ Maxwordbit) * 2
Const Maxwordshift = Maxwordbit + 1
Dim Retries As Byte
Dim Receivedbyte As Byte
Dim Kindofdata As Byte
Dim Spmcsrvalue As Byte
Dim Calculatedchecksum As Byte
Dim Receivedblock As Byte
Dim Countedblock As Byte
Dim Invertedblock As Byte
Dim Receivedchecksum As Byte
Dim Receivedbytes(128) As Byte
Dim J As Byte
Dim Vl As Byte
Dim Vh As Byte
Dim Wrd As Word
Dim Page As Word
Dim Z As Long
Disable Interrupts
Const Soh = &H01
Const Stx = &H02
Const Eot = &H04
Const Ack = &H06
Const Nak = &H15
Const Can = &H18
Retries = 20
Testformagicbyte:
Receivedbyte = Waitkey(#2)
Print #1 , Chr(receivedbyte);
If Receivedbyte = 123 Then
Kindofdata = 0
Goto Loader
Elseif Receivedbyte = 124 Then
Kindofdata = 1
Goto Loader
Elseif Receivedbyte <> 0 Then
Decr Retries
If Retries <> 0 Then Goto Testformagicbyte
End If
Goto _reset
Loader:
If Kindofdata = 0 Then
Spmcsrvalue = 3 : Gosub Do_spm
Spmcsrvalue = 17 : Gosub Do_spm
End If
Retries = 10
Do
Calculatedchecksum = 0
Print #1 , Chr(nak);
Do
Receivedbyte = Waitkey(#2)
Select Case Receivedbyte
Case &H01: '<SOH>
Incr Countedblock
Calculatedchecksum = 1
Receivedblock = Waitkey(#2)
Calculatedchecksum = Calculatedchecksum + Receivedblock
Invertedblock = Waitkey(#2)
Calculatedchecksum = Calculatedchecksum + Invertedblock
For J = 1 To 128
Receivedbytes(j) = Waitkey(#2)
Calculatedchecksum = Calculatedchecksum + Receivedbytes(j)
Next
Receivedchecksum = Waitkey(#2)
If Countedblock = Receivedblock Then
If Receivedchecksum = Calculatedchecksum Then
Gosub Writepage
Print #1 , Chr(ack);
Else
Print Chr(nak);
End If
Else
Print #1 , Chr(nak);
End If
Case &H04: '<EOT>
If Wrd > 0 And Kindofdata = 0 Then
Wrd = 0
Spmcsrvalue = 5 : Gosub Do_spm
Spmcsrvalue = 17 : Gosub Do_spm
End If
Print #1 , Chr(ack);
Waitms 20
Goto _reset
Case &H18: '<CAN>
Goto _reset
Case Else
Exit Do
End Select
Loop
If Retries > 0 Then
Waitms 1000
Decr Retries
Else
Goto _reset
End If
Loop
Writepage:
If Kindofdata = 0 Then
For J = 1 To 128 Step 2
Vl = Receivedbytes(j)
Vh = Receivedbytes(j + 1)
lds r0, {vl}
lds r1, {vh}
Spmcsrvalue = 1 : Gosub Do_spm
Wrd = Wrd + 2
If Wrd = Maxword Then
Wrd = 0
Spmcsrvalue = 5 : Gosub Do_spm
Spmcsrvalue = 17 : Gosub Do_spm
Page = Page + 1
Spmcsrvalue = 3 : Gosub Do_spm
Spmcsrvalue = 17 : Gosub Do_spm
End If
Next
Else
For J = 1 To 128
Writeeeprom Receivedbytes(j) , Wrd
Wrd = Wrd + 1
Next
End If
Return
Do_spm:
Bitwait Spmcsr.0 , Reset
Bitwait Eecr.1 , Reset
Z = Page
Shift Z , Left , Maxwordshift
Z = Z + Wrd
lds r30, {Z}
lds r31, {Z + 1}
#if _romsize > 65536
lds r24, {Z + 2}
sts rampz, r24
#endif
Spmcsr = Spmcsrvalue
spm
nop
nop
Return
'How to use this program:
'1- compile this program
'2- program into chip
'3- select MCS Bootloader from programmers
'4- compile a new program
'5- press F4 and reset your micro
'the program will be uploaded into the chip
'How to call the bootloader from your program without a reset ???
' Goto &H1C00
'The GOTO will do the work, you need to specify the correct bootloader address
'this is the same as the $LOADER statement.