CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Write to 18F45K50 flash memory

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
vtrx



Joined: 11 Oct 2017
Posts: 148

View user's profile Send private message

Write to 18F45K50 flash memory
PostPosted: Mon Sep 28, 2026 9:11 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Sep 28, 2026 10:38 am     Reply with quote

What compiler version???
vtrx



Joined: 11 Oct 2017
Posts: 148

View user's profile Send private message

PostPosted: Mon Sep 28, 2026 10:39 am     Reply with quote

5.115
From address 0x4000 onwards, the data becomes 00.
vtrx



Joined: 11 Oct 2017
Posts: 148

View user's profile Send private message

PostPosted: Mon Sep 28, 2026 7:21 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 29, 2026 12:04 am     Reply with quote

Ah.
Thanks for coming back, I was starting to work my way through the
assembler generated, to see if there was anything obvious wrong. Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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