| View previous topic :: View next topic |
| Author |
Message |
vtrx
Joined: 11 Oct 2017 Posts: 151
|
| Write to 18F45K50 flash memory (SOLVED) |
Posted: Mon Sep 28, 2026 9:11 am |
|
|
I am trying to write to the flash memory of the 18F45K50 starting at address 0x4000, but the write operation isn't working.
I searched online but couldn't find the reason.
I ran a test using:
| Code: | #include <18F45K50.h>
...
#FUSES INTRC_IO // Usa o oscilador interno
#FUSES PLLEN // Ativa o módulo PLL
#FUSES PLL3X // Multiplicador do PLL em 3x (16MHz interno x 3 = 48MHz)
#FUSES NOCPUDIV // CPU roda direto na frequência máxima (sem divisão)
#FUSES NOXINST, NOFCMEN, NOIESO
#FUSES PUT, NOBROWNOUT, NOWDT
#FUSES MCLR, NOLVP, SDOB3
#FUSES NOSTVREN, NODEBUG
#FUSES NOPROTECT, NOCPB, NOCPD, NOPBADEN
#FUSES NOWRT, NOWRTC, NOWRTB, NOWRTD,
#FUSES NOEBTR, NOEBTRB
...
#use delay(int, clock=48MHz, USB_FULL, act=USB)
...
#define FLASH_BLOCK_SIZE 64 // Tamanho exato do bloco para o PIC18F45K50
unsigned int8 data_aa[FLASH_BLOCK
...
void start_4000() {
unsigned int32 addr;
// Preenche o buffer RAM inteiramente com o valor 0xAA
memset(data_aa, 0xAA, FLASH_BLOCK_SIZE);
// Desativar interrupções é OBRIGATÓRIO. O ciclo de clock da Flash precisa ser exato.
disable_interrupts(GLOBAL);
// Varre de 0x4000 até o limite máximo da Flash (0x7FFF)
for(addr = 0x4000; addr < 0x8000; addr += FLASH_BLOCK_SIZE) {
// 1. Apaga o bloco de 64 bytes (Fisicamente transforma em 0xFF)
// erase_program_memory(addr);
// 2. Grava os 64 bytes com o valor 0xAA por cima do bloco apagado
write_program_memory(addr, data_aa, FLASH_BLOCK_SIZE);
}
// Reativa as interrupções após concluir todo o processo na Flash
enable_interrupts(GLOBAL);
}
...
void main(void)
{
set_tris_a(0b00000001); // RA0 como entrada
setup_adc_ports(NO_ANALOGS);
start_4000();
...
...
|
It didn't work.
Last edited by vtrx on Sat Oct 10, 2026 5:41 am; edited 1 time in total |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20145
|
|
Posted: Mon Sep 28, 2026 10:38 am |
|
|
| What compiler version??? |
|
 |
vtrx
Joined: 11 Oct 2017 Posts: 151
|
|
Posted: Mon Sep 28, 2026 10:39 am |
|
|
5.115
From address 0x4000 onwards, the data becomes 00. |
|
 |
vtrx
Joined: 11 Oct 2017 Posts: 151
|
|
Posted: Mon Sep 28, 2026 7:21 pm |
|
|
Upon analysis, the problem is likely the use of the PICkit 2 programmer with a `devicedat` file modified for the 18F45K50.
There must be an error in the fuse settings, as programming works perfectly if I use the 18F4550.
I’m going to buy another programmer. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20145
|
|
Posted: Tue Sep 29, 2026 12:04 am |
|
|
Ah.
Thanks for coming back, I was starting to work my way through the
assembler generated, to see if there was anything obvious wrong.  |
|
 |
Eddy71ua
Joined: 23 Sep 2009 Posts: 66 Location: Ukraine
|
|
 |
vtrx
Joined: 11 Oct 2017 Posts: 151
|
|
Posted: Wed Oct 07, 2026 4:52 pm |
|
|
Returning with the solution.
The problem was indeed the Pickit 2 with the modified DevFileDat.
I programmed it using the Pickit 3 and MPLAB, and the programming beyond address 0x4000 proceeded as expected. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20145
|
|
Posted: Thu Oct 08, 2026 5:05 am |
|
|
Phew...
Please edit the thread title, to include (Solved). Only you can do this,
and it helps to keep things nicely sorted.
Best Wishes |
|
 |
|