News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Can't get Cwrite to work on 16F1704

Started by charliecoutas, Nov 26, 2024, 08:06 AM

Previous topic - Next topic

charliecoutas

I'm sure this is a simple problem but I can't get this to work:

Device = 16F1704                                                       

Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_ON, MCLRE_OFF, CP_OFF, BOREN_OFF, CLKOUTEN_OFF, IESO_OFF, FCMEN_OFF
Config2 WRT_OFF, PLLEN_ON, STVREN_OFF, LVP_OFF

    Declare Xtal = 32
    CWrite $0F78, [$12]     'write low byte of $0F78

    Stop

After running it, I examine $0F78 and it is still $03FFF.
I am using PicKit2+ to read the memory.

trastikata

#1
Hello Charlie,

try this code:

Device = 16F1704                                                      

Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_ON, MCLRE_OFF, CP_OFF, BOREN_OFF, CLKOUTEN_OFF, IESO_OFF, FCMEN_OFF
Config2 WRT_OFF, PLLEN_ON, STVREN_OFF, LVP_OFF

Symbol PageSize = 32

Declare Xtal = 32

Dim bCounter As Byte
Dim bBuffer[32] As Byte

'Read FLASH page to buffer
For bCounter = 0 To 31
    bBuffer[bCounter] = CRead (($0F78 / PageSize) * PageSize + bCounter)
Next

'Erase Flash page
CErase (($0F78 / PageSize) * PageSize)
DelayMS 12

'Modify Flash page
bBuffer[$0F78 // PageSize] = $12

'Write Flash page
For bCounter = 0 To 31
    'CWrite (($0F78 / PageSize) * PageSize + bCounter), [$12] - instead of [$12] should be bBuffer[bCounter]
    CWrite (($0F78 / PageSize) * PageSize + bCounter), [bBuffer[bCounter]]
Next
DelayMS 12

End 

Edit: sorry the last line instead of [$12] should be bBuffer[bCounter]

charliecoutas

Wow, thanks very much, works a treat (had to change the clock, my oversight).

The reason I am trying to write to flash is that I want to use the High Endurance Flash as EEPROM. Your code wrote $12 to byte zero of a whole page of 32 bytes, which included address $078. I changed it to write from $08F0 which is the start of the High Endurance area on this chip. Sure enough, it now writes $12 to the low byte of of the first 32 "words" of High Endurance Flash.

I've got my EEPROM back!

Thanks again, I owe you one. I think that others might be able to use this.

Charlie

trastikata

Quote from: charliecoutas on Nov 26, 2024, 09:55 AMThe reason I am trying to write to flash is that I want to use the High Endurance Flash as EEPROM. Your code wrote $12 to byte zero of a whole page of 32 bytes, which included address $078. I changed it to write from $08F0 which is the start of the High Endurance area on this chip. Sure enough, it now writes $12 to the low byte of of the first 32 "words" of High Endurance Flash.

Sorry for that, I edit the code - the last line instead of [$12] should be bBuffer[bCounter]  this way you preserve other information in the current page being written.

Glad it worked out.

charliecoutas

Cheers, I was just trying to sort that! Thanks again.

Charlie

trastikata

#5
Quote from: charliecoutas on Nov 26, 2024, 10:04 AMCheers, I was just trying to sort that! Thanks again.

No problem. Here's a more abstract code

Device = 16F1704    
Declare Xtal = 32                                                 

Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_ON, MCLRE_OFF, CP_OFF, BOREN_OFF, CLKOUTEN_OFF, IESO_OFF, FCMEN_OFF
Config2 WRT_OFF, PLLEN_ON, STVREN_OFF, LVP_OFF

Symbol PageSize = 32

Dim bCounter As Byte
Dim bBuffer[PageSize] As Byte
Dim wFlashWritePosition As Word

wFlashWritePosition = $0F78

'Read FLASH page to buffer
For bCounter = 0 To PageSize - 1
    bBuffer[bCounter] = CRead ((wFlashWritePosition / PageSize) * PageSize + bCounter)
Next

'Erase Flash page
CErase ((wFlashWritePosition / PageSize) * PageSize)
DelayMS 12

'Modify Flash page at wFlashWritePosition
bBuffer[wFlashWritePosition // PageSize] = $12

'Write Flash page
For bCounter = 0 To PageSize - 1
    CWrite ((wFlashWritePosition / PageSize) * PageSize + bCounter), [bBuffer[bCounter]]
Next
DelayMS 12

End 

charliecoutas


trastikata

I am doing many things at the same time and missed this 0-based counter. I've corrected the code, should be

For bCounter = 0 To PageSize - 1

charliecoutas

Got it, cheers. Appreciate your time.