 |
 |
| View previous topic :: View next topic |
| Author |
Message |
vtrx
Joined: 11 Oct 2017 Posts: 148
|
| Write to 18F45K50 flash memory |
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. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20141
|
|
Posted: Mon Sep 28, 2026 10:38 am |
|
|
| What compiler version??? |
|
 |
vtrx
Joined: 11 Oct 2017 Posts: 148
|
|
Posted: Mon Sep 28, 2026 10:39 am |
|
|
5.115
From address 0x4000 onwards, the data becomes 00. |
|
 |
vtrx
Joined: 11 Oct 2017 Posts: 148
|
|
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: 20141
|
|
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.  |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|