Explanation of procedures and functions :
Variables :
Code: Select all
Public Dim data_8(8) As Byte
Public Dim data_64(64) As Byte
Public Dim data_1024(1024) As Byte
' For PIC18F452,Pic18F4520, ..., PIC18FxxJxx '
Code: Select all
sub Flash_Mem_Erase(address as longword)
' Only for PIC18Fxx2 series, see datasheet of MCU '
Code: Select all
sub Flash_Mem_Write_8(address as longword)
' Only for PIC18F4520, ..., PIC18FxxJxx, see datasheet of MCU '
Code: Select all
sub Flash_Mem_Write_64(address as longword)
' Only for PIC18Fxx2 series, see datasheet of MCU '
Code: Select all
sub Flash_Mem_Erase_Write_64(address as longword)
' Only for PIC18F4520 series, see datasheet of MCU '
Code: Select all
sub Flash_Mem_Erase_Write_64A(address as longword)
' Only for PIC18FxxJxx, see datasheet of MCU '
Code: Select all
sub Flash_Mem_Erase_Write_1024(address as longword)
' For all type of MCU '
Code: Select all
function Flash_Mem_Read(address as longword) as byte
' For all type of MCU '
Code: Select all
sub Flash_Mem_Read_N_Bytes(address as longword, N as byte)
Code: Select all
{
*****************************************************************************
* Name : Flash_Mem_Library.BAS *
* Author : Medrea Florin ANdrei *
* Notice : Copyright (c) 2007 V1.1 *
* : All Rights Reserved *
* Date : 9/25/2007 *
* Version : 1.1 *
* Notes : *
* : *
*****************************************************************************
}
Module Flash_Mem_Library
Public Dim data_8(8) As Byte
Public Dim data_64(64) As Byte
Public Dim data_1024(1024) As Byte
Dim GIE_ As INTCON.7
Dim FREE_ As EECON1.4
Dim WREN_ As EECON1.2
Dim WR_ As EECON1.1
Dim RD_ As EECON1.0
Public Sub Flash_Mem_Erase(address As LongWord)
Dim flag As Byte ' Erase ...( depends of MCU ) block at address ... (64 or 1024 bytes, see datasheet of MCU)
flag = INTCON And %10000000 ' save state of GIE bit
GIE_ = 0 ' disable interrupts
TBLPTRL = address.byte0
TBLPTRH = address.byte1
TBLPTRU = address.byte2
EECON1 = EECON1 Or %10000000 ' for compatibility J series with standard MCU
EECON1 = EECON1 And %10111111 ' for compatibility J series with standard MCU
WREN_ = 1
FREE_ = 1
EECON2 = $55
EECON2 = $AA
WR_ = 1
WREN_ = 0
INTCON = INTCON Or flag ' enable interrupts with first state of GIE bit
End Sub
Public Sub Flash_Mem_Write_8(address As LongWord) 'only for PIC18Fxx2 series, see datasheet of MCU
Dim i,flag As Byte ' write 8 bytes block at address ...
flag = INTCON And %10000000 ' save state of GIE bit
GIE_ = 0 ' disable interrupts
TBLPTRL = address.byte0
TBLPTRH = address.byte1
TBLPTRU = address.byte2
FSR2 = @data_8
i = 0
While i < 8
TABLAT = POSTINC2
ASM
nop
TBLWT+*
nop
End ASM
Inc(i)
Wend
EECON1 = EECON1 Or %10000000
EECON1 = EECON1 And %10111111
WREN_ = 1
EECON2 = $55
EECON2 = $AA
WR_ = 1
WREN_ = 0
INTCON = INTCON Or flag ' enable interrupts with first state of GIE bit
End Sub
Public Sub Flash_Mem_Write_64(address As LongWord) 'only for PIC18F4520 and J series, see datasheet of MCU
Dim i,flag As Byte ' write 64 bytes block at address ...
flag = INTCON And %10000000 ' save state of GIE bit
GIE_ = 0 ' disable interrupts
TBLPTRL = address.byte0
TBLPTRH = address.byte1
TBLPTRU = address.byte2
FSR2 = @data_64
i = 0
While i < 64
TABLAT = POSTINC2
ASM
nop
TBLWT+*
nop
End ASM
Inc(i)
Wend
EECON1 = EECON1 Or %10000000
EECON1 = EECON1 And %10111111
WREN_ = 1
EECON2 = $55
EECON2 = $AA
WR_ = 1
WREN_ = 0
INTCON = INTCON Or flag ' enable interrupts with first state of GIE bit
End Sub
Public Sub Flash_Mem_Erase_Write_64(address As LongWord) 'only for PIC18Fxx2 series, see datasheet of MCU
Dim i,j,flag As Byte ' erase and write 64 bytes 8x8 block at address ...
flag = INTCON And %10000000 ' save state of GIE bit
GIE_ = 0 ' disable interrupts
TBLPTRL = address.byte0
TBLPTRH = address.byte1
TBLPTRU = address.byte2
EECON1 = EECON1 Or %10000000 ' for compatibility J series with standard MCU
EECON1 = EECON1 And %10111111 ' for compatibility J series with standard MCU
WREN_ = 1
FREE_ = 1
EECON2 = $55
EECON2 = $AA
WR_ = 1
ASM
nop
TBLRD*-
nop
End ASM
j = 0
FSR2 = @data_64
While j < 8
i = 0
While i < 8
TABLAT = POSTINC2
ASM
nop
TBLWT+*
nop
End ASM
Inc(i)
Wend
EECON1 = EECON1 Or %10000000
EECON1 = EECON1 And %10111111
WREN_ = 1
EECON2 = $55
EECON2 = $AA
WR_ = 1
Inc(j)
Wend
WREN_ = 0
INTCON = INTCON Or flag ' enable interrupts with first state of GIE bit
End Sub
Public Sub Flash_Mem_Erase_Write_64A(address As LongWord) 'only for PIC18F4520 series, see datasheet of MCU
Dim i,flag As Byte ' erase and write 64 bytes block at address ...
flag = INTCON And %10000000 ' save state of GIE bit
GIE_ = 0 ' disable interrupts
TBLPTRL = address.byte0
TBLPTRH = address.byte1
TBLPTRU = address.byte2
EECON1 = EECON1 Or %10000000 ' for compatibility J series with standard MCU
EECON1 = EECON1 And %10111111 ' for compatibility J series with standard MCU
WREN_ = 1
FREE_ = 1
EECON2 = $55
EECON2 = $AA
WR_ = 1
ASM
nop
TBLRD*-
nop
End ASM
FSR2 = @data_64
i = 0
While i < 64
TABLAT = POSTINC2
ASM
nop
TBLWT+*
nop
End ASM
Inc(i)
Wend
EECON1 = EECON1 Or %10000000
EECON1 = EECON1 And %10111111
WREN_ = 1
EECON2 = $55
EECON2 = $AA
WR_ = 1
WREN_ = 0
INTCON = INTCON Or flag ' enable interrupts with first state of GIE bit
End Sub
Public Sub Flash_Mem_Erase_Write_1024(address As LongWord) ' only for J series
Dim i,j,flag As Byte ' erase and write 1024 bytes block at address ...
flag = INTCON And %10000000 ' save state of GIE bit
GIE_ = 0 ' disable interrupts
TBLPTRL = address.byte0
TBLPTRH = address.byte1
TBLPTRU = address.byte2
EECON1 = EECON1 Or %10000000 ' for compatibility J series with standard MCU
EECON1 = EECON1 And %10111111 ' for compatibility J series with standard MCU
WREN_ = 1
FREE_ = 1
EECON2 = $55
EECON2 = $AA
WR_ = 1
ASM
nop
TBLRD*-
nop
End ASM
j = 0
FSR2 = @data_1024
While j < 16
i = 0
While i < 64
TABLAT = POSTINC2
ASM
nop
TBLWT+*
nop
End ASM
Inc(i)
Wend
EECON1 = EECON1 Or %10000000
EECON1 = EECON1 And %10111111
WREN_ = 1
EECON2 = $55
EECON2 = $AA
WR_ = 1
Inc(j)
Wend
WREN_ = 0
INTCON = INTCON Or flag ' enable interrupts with first state of GIE bit
End Sub
Public Function Flash_Mem_Read(address As LongWord) As Byte
TBLPTRL = address.byte0
TBLPTRH = address.byte1
TBLPTRU = address.byte2
ASM
TBLRD*
End ASM
result = TABLAT
End Function
Public Sub Flash_Mem_Read_N_Bytes(address As LongWord, N As Byte)
Dim i As Byte ' read N bytes (user can define this) at address ... and put them in data dim.
GIE_ = 0 ' disable interrupts
TBLPTRL = address.byte0
TBLPTRH = address.byte1
TBLPTRU = address.byte2
i = 0
While i < N
ASM
TBLRD*+
End ASM
data_64(i) = TABLAT
Inc(i)
Wend
End Sub