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

12F1572 write program memory issue
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Wed Dec 10, 2014 1:22 am     Reply with quote

Hi guys.

I use this chip for the 3 independent PWM outputs only. Also price..

I have a PCB and working application with this PIC, so I need to add the byte array feature on this PCB.. I don't have where to run, I'll make it real somehow.

Ttelmah, if you answer my questions in the previous post, it will be much appreciated. I'm going to try that..

EEPROM is a need for this chip... Anyway.
Ttelmah



Joined: 11 Mar 2010
Posts: 20063

View user's profile Send private message

PostPosted: Wed Dec 10, 2014 9:44 am     Reply with quote

No guarantees on this. Haven't a PIC of this type to try.
However 'comments'. Found that the CCS write function would erase the whole 32word page, not 16words in MPLAB. Not sure if this is a fault with the emulation of the code, so rewrote this. Remember interrupts should be disabled before calling:
Code:

//for a const int8 array declared as:
const int8 array[100][3] = {
{1,2,3},{1,2,3},{1,2,3},{1,2,3},{1,2,3},{1,2,3},{1,2,3},{1,2,3},.... //etc.
};

#define ROW_LEN 3

#WORD PMADDR=getenv("SFR:PMADRL")
#bit CFGS=getenv("BIT:CFGS")
#bit FREE=getenv("BIT:FREE")
#bit WREN=getenv("BIT:WREN")
#bit WR=getenv("BIT:WR")
#bit LWLO=getenv("BIT:LWLO")
#byte PMCON2=getenv("SFR:PMCON2")
#word PMDAT=getenv("SFR:PMDATL")

#inline
void unlock()
{
   PMCON2=0x55;
   PMCON2=0xAA;
   WR=TRUE;
   delay_cycles(1);
   delay_cycles(1);
}

#inline
void erase(int16 address)
{
   //directly from the data sheet
   PMADDR=address;
   CFGS=FALSE;
   FREE=WREN=TRUE;
   unlock();
}

void write_page(int16 address, int16 * data, int8 ctr)
{
   //directly from the data sheet
   ctr/=2; //writing in words
   --ctr;
   WREN=TRUE;
   CFGS=FALSE;
   LWLO=TRUE;
   PMADDR=address;
   do
   {
      PMDAT=*data;
      unlock();
      PMADDR++;
      data++;
   } while (--ctr);
   LWLO=FALSE;
   PMDAT=*data;
   unlock();
}
   
void write_byte(int8 x,int8 y,int8 val)
{
   //routine to change a byte in 'array' at x,y with rows 'row_len' long
   int16 address;
   int8 word_address;
   int16 temp_buff[16];
   address=label_address(array); //address of the array start
   //Now calculate the offset
   address=address+((int16)ROW_LEN*x)+y;
   word_address=(address&0xF);
   address&=0xFFF0;
   //Now we have the page address and instruction in the page
   read_program_memory(address,&temp_buff,32); //read page
   //0x34xx is the code for a RETLW instruction
   temp_buff[word_address]=(int16)val + 0x3400; //change the byte
   //write the page back and erase.
   erase(address);
   write_page(address,&temp_buff,32);
}

//So to change byte [80][0] to 43, use:
   write_byte(80,0,43);


Last edited by Ttelmah on Thu Dec 18, 2014 3:34 am; edited 1 time in total
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Thu Dec 11, 2014 3:40 am     Reply with quote

Hi Ttelmah,

I'm going to try your code.

Just a question; do you mean +9 with label_address(array); ?

So I'll locate the array using #org to 0x500 and label_address will give 0x509, correct?

BR
Ttelmah



Joined: 11 Mar 2010
Posts: 20063

View user's profile Send private message

PostPosted: Thu Dec 11, 2014 4:20 am     Reply with quote

There is no point in locating it anywhere. Doing so, will reduce your space available, since then things can't be moved.

As I said already:

"
Declare the variable as:

const int8 array[100][3] = {.........};

Then "label_address(array)" will be the address in ROM where the first instruction of the table is stored."

The 'label_address' function, automatically tells you where the actual data is stored.
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Thu Dec 11, 2014 2:04 pm     Reply with quote

Hello,

I've tried it as below, but the mcu freezes after write.

Code:
  disable_interrupts(GLOBAL);
  write_byte(0,0,50);
  write_byte(0,1,100);
  write_byte(0,2,150);
  enable_interrupts(GLOBAL);
Ttelmah



Joined: 11 Mar 2010
Posts: 20063

View user's profile Send private message

PostPosted: Fri Dec 12, 2014 2:20 am     Reply with quote

So run it up in an ICD, or simulator and work out what is hanging. This is called debugging.
As I said 'no guarantees'. It is based directly on what the data sheet says.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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