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

16F1509 HEF read program memory
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

16F1509 HEF read program memory
PostPosted: Wed Apr 20, 2022 5:16 am     Reply with quote

Hi everyone
I want to save a 10 bit ADC value to memory.
when i say read memory i get 16383 value.
Where am I going wrong, can you help me please?


Code:
:
#include <16f1509.h>
#device ADC = 10  // 0 ile 1023

#include <math.h>

#use delay(internal=1000000)



#fuses INTRC_IO   
#fuses NOWDT       
#fuses NOPUT     
#fuses NOLVP       
#fuses NOPROTECT   
#fuses NODEBUG     
#fuses NOBROWNOUT
#fuses NOWRT       


#use fast_io(c)
#use fast_io(a)
#use fast_io(b)

#use rs232 (baud=9600, xmit=pin_B7,rcv=pin_B5, parity=N, stop=1)

//=================TEST_ARENA==================

#define HEF 0x1F80

#ROM int16 HEF={1,2,3,4} //ensures the compiler will not accidentally
//put something else here.

//===============================================================================

int8 count_NTC;

unsigned int8* avg_CO;
unsigned int8*  oku;   

unsigned long int sum_CO,onuf_co;

void main()
{

delay_ms(300);


  // setup_psp(PSP_DISABLED);
     setup_timer_1(T1_DISABLED);
   //setup_CCP1(CCP_OFF);
  // setup_CCP2(CCP_OFF);
   
   set_tris_c(0b00001111);// C0,C1,C2,C3 pini input yapıldı.// C4 output
   set_tris_a(0x00); // a2,a4,a5 pini output //red,green,yellow leds
   
   set_tris_b(0b01000000); // Alarm_Set_Button
   
    output_a(0x00); //red led
    output_b(0x00);
    output_c(0x00);
   
    setup_adc(adc_clock_div_2);
   
   //  setup_adc_ports(4);
   // setup_adc_ports(5);
   //setup_adc_ports(6);
     setup_adc_ports(7);
 
 
while (True){


//==========================test_alan==============
 
 if (input(pin_b6)==1){
      delay_ms(20);
       
    output_high(pin_a2);
    delay_ms(20);
   
   //write the values
  write_program_memory(HEF,&avg_CO,4);
   delay_ms(1000);
   
 }
 
 if (input(pin_b6)==0) {

 output_low(pin_a2);
    delay_ms(20);
   
   //and read them back - CCS read works fine               
   read_program_memory(HEF,&oku,4);
   delay_ms(1000);
 
 }

  set_adc_channel(7); 
  delay_us(20);

  sum_CO=0;
    for (count_NTC=0; count_NTC<8; count_NTC++)
   
   {
      onuf_co=read_adc();
      delay_us(100);
     sum_CO=onuf_co + sum_CO;
    }

    &avg_CO= sum_CO/=8;

//==================red===

if (avg_CO >= oku){

output_high(pin_a5);
delay_ms(20);

}

else if (avg_CO < oku){

output_low(pin_a5);
delay_ms(20);

}

  //==================green LED ==========================
   output_high(pin_a4);
  // delay_ms(1000);
   
  //==================yellow LED ==========================
 
   
  printf("AVG_CO-ADC=%lu\n\r",avg_CO);
  printf("%lu\n\r",oku);
  delay_ms(2000);
   
  }

}
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Wed Apr 20, 2022 5:27 am     Reply with quote

What compiler version?.

Also you need to lay the data out to write to HEF. The HEF is the low
byte of each instruction word. So it is like this:

0000 HEF byte, do not use this byte
0002 HEF byte, do not use this byte
0004 HEF byte, do not use this byte

etc....

You need to lay your data out as a 16bit array, with the data you want
to store in the low bytes of each 16bit word.

When reading back you have to do the opposite.
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

PostPosted: Wed Apr 20, 2022 7:15 am     Reply with quote

Compiler version v5.007
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Wed Apr 20, 2022 7:23 am     Reply with quote

Ouch.
That compiler is before V5 was really a working compiler. It may well not
be reading/writing the memory correctly. It was a 'beta' at best at that
point.
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Wed Apr 20, 2022 9:22 am     Reply with quote

As a comment, use the proper defines for set_adc_ports. '7' is not a
legitimate ADC pin selection. sAN7 is the correct selection to select C3.
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Wed Apr 20, 2022 10:45 am     Reply with quote

Now, given the age of your compiler I know the program memory functions
will not work.
So here is a basic program showing reading and writing a 32bit value
to the HEF, with handwritten routines:
Code:

#include <16F1509.h>
#device ADC=10

#fuses NOWDT
#fuses NOLVP       
#fuses NOPROTECT   
#fuses NODEBUG     
#fuses NOBROWNOUT
#fuses NOWRT 

#use delay(internal=4000000)

#use rs232 (baud=9600, xmit=pin_B7,rcv=pin_B5, parity=N, stop=1)

#include <math.h>

#byte PMCON1=getenv("SFR:PMCON1")
#byte PMCON2=getenv("SFR:PMCON2")
#byte PMDATL=getenv("SFR:PMDATL")
#byte PMDATH=getenv("SFR:PMDATH")
#byte PMADRL=getenv("SFR:PMADRL")
#byte PMADRH=getenv("SFR:PMADRH")
#bit CFGS=PMCON1.6
#bit RD=PMCON1.0
#bit WREN=PMCON1.2
#bit FREE=PMCON1.4
#bit WR=PMCON1.1
#bit LWLO=PMCON1.5
#define MAX_BYTES_TO_USE 4 //change this to adjust how many bytes can be transferred

int16 PmBuff[MAX_BYTES_TO_USE]; //one word allocated for every byte

#define HEF_ADDRESS 0x1F80
#ROM int16 HEF_ADDRESS={1,2,3,4} //ensures the compiler will not accidentally
//put something else here

//Now basic routines to write the program memory given compiler is too old to have these working
#inline //Make this inline
void unlock(void)
{
   //unlock the memory
   PMCON2=0x55; //unlock sequence
   PMCON2=0xAA;
   WR=TRUE; //trigger unlock
   delay_cycles(1);
   delay_cycles(1); //Two NOPs
}

void erase(void)
{
   //erase the HEF page
   PMADRL=HEF_ADDRESS;
   PMADRH=make8(HEF_ADDRESS,1);
   CFGS=FALSE;
   FREE=TRUE;
   WREN=TRUE; //specify erase
   unlock(); //This unlocks and performs the erase
   WREN=FALSE;   
}

//Physical read and write routines
void write_HEF(char * buffer)
{
   int ctr;
   disable_interrupts(GLOBAL); //interrupts must be off during write
   erase(); //erase the page
   PMADRL=HEF_ADDRESS;
   PMADRH=make8(HEF_ADDRESS,1);   
   CFGS=FALSE;
   WREN=TRUE;
   LWLO=TRUE; //write to latches
   //now load the bytes into the latches
   for (ctr=0;ctr<MAX_BYTES_TO_USE;ctr++)
   {
      PMDATL=*(buffer++);
      PMDATH=*(buffer++);
      unlock();
      PMADRL++;
   }
   LWLO=FALSE; //turn off latch access
   unlock(); //perform the physical write
   WREN=FALSE;
   enable_interrupts(GLOBAL);
}

void read_HEF(void)
{
   int ctr;
   PMADRL=HEF_ADDRESS;
   PMADRH=make8(HEF_ADDRESS,1);
   //now need to read the four 16bit values
   for (ctr=0;ctr<MAX_BYTES_TO_USE;ctr++)
   {
      CFGS=FALSE;
      RD=TRUE;
      delay_cycles(1);
      delay_cycles(1); //two NOPs
      PmBuff[ctr]=make16(PMDATH,PMDATL);
      PMADRL++;
   }
}

//Routines to transfer an int32 in/out of the HEF layout
void build_table(int32 val)
{
   //format an int32 value into the HEF layout
   PmBuff[0]=make8(val,0);
   PmBuff[1]=make8(val,1);
   PmBuff[2]=make8(val,2);
   PmBuff[3]=make8(val,3);
}

int32 retrieve_table(void)
{
   //read the bytes back out of the table into an int32
   int32 val;
   //Just the low byte from each value
   val=make32((int8)PmBuff[3],(int8)PmBuff[2],(int8)PmBuff[1],(int8)PmBuff[0]);
   return val;
}

//write an int32 to HEF
void HEF_write(int32 val)
{
   build_table(val);
   write_HEF((char *)PmBuff); //write the 8 bytes to HEF
}

//read an int32 from HEF
int32 HEF_read(void)
{
   read_HEF(); //read the 8 bytes back
   return retrieve_table(); //and convert back to int32
}
   
   
void main()
{
   int1 dowrite=TRUE;
   int32 value = 0x112255AA; //test value
   while(TRUE)
   {
      //Now just do a single write to HEF to prove how this works.
      if (dowrite==TRUE)
      {
         HEF_write(value);
         dowrite=FALSE; //stop writing - remember writes use lives.
      }
      value=HEF_read();
      printf("Value is %LX\n",value);
      delay_ms(1000);     
   }
}
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

16F1509 HEF read program memory
PostPosted: Thu Apr 21, 2022 3:39 am     Reply with quote

Thank you very much for your effort But I get the value is FFFFFFFF
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Thu Apr 21, 2022 3:57 am     Reply with quote

Hmm. Two possibilities.
One your compiler is not setting up one of the registers correctly. I don't
have 5.007, the earliest V5 compiler I bothered to keep was 5.012. Before
this there were so many problems that it was pointless to keep them.
Two, you have killed the HEF by repeatedly writing.
Are you testing with a known test value as I show?.
The code works correctly on a later compiler.
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

PostPosted: Thu Apr 21, 2022 4:25 am     Reply with quote

Yes I am testing it with the test value you sent.
I will test with the updated version.
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

16F1509 HEF read program memory
PostPosted: Tue Apr 26, 2022 12:39 am     Reply with quote

Hello again

Even though I tried again with CCS C Version 5.049V, the result did not change.
I am doing the simulation in proteus. Is this why I am getting wrong results (proteus version 8.10)?
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Tue Apr 26, 2022 1:16 am     Reply with quote

AAARGH!.......

Seriously see the previous comments here about Proteus. IT DOES NOT WORK.
You can take perfectly working code, and Proteus will tell you it doesn't
work. You can design code that works in Proteus, put it into a real chip,
and it won't work.
You are wasting your time using Proteus.

I use it as a PCB package. For that it is good. However the simulator
is a complete waste of time on the PIC (actually quite good for analog
designs). On the PIC so many parts don't work, or work incorrectly, it
is pointless.
temtronic



Joined: 01 Jul 2010
Posts: 9253
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Apr 26, 2022 5:25 am     Reply with quote

re: You are wasting your time using Proteus.


AND OURS !!!!
Arrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrgh...

It should be a 'rule' that the first line posted is 'this is real code' or 'this is a 'simulation'....

NO 'simulator' actually runs like a PIC, None, nada, zilch, zero. while some emulate some portion/features of some PICs in 2+decades of 'playing with PICS', NONE run like a real PIC in the Real World using Real code.

If I had a penny for every hour wasted on programmers trying to use Proteus, I could retire. Better yet, buy a plant to manufacture PICs !!
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Tue Apr 26, 2022 5:47 am     Reply with quote

Also look at the sticky at the head of the Forum.

Questions here should be real hardware _only_.
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

16F1509 HEF read program memory
PostPosted: Wed Apr 27, 2022 5:36 am     Reply with quote

Sorry, I'm very new to this work. I will be more careful. I have a few shortcomings in the hardware, I will try it immediately when I complete it.
Rhinomx



Joined: 24 Aug 2022
Posts: 1

View user's profile Send private message

PostPosted: Wed Aug 24, 2022 1:32 pm     Reply with quote

temtronic wrote:
re: You are wasting your time using Proteus.


AND OURS !!!!
Arrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrgh...

It should be a 'rule' that the first line posted is 'this is real code' or 'this is a 'simulation'....

NO 'simulator' actually runs like a PIC, None, nada, zilch, zero. while some emulate some portion/features of some PICs in 2+decades of 'playing with PICS', NONE run like a real PIC in the Real World using Real code.

If I had a penny for every hour wasted on programmers trying to use Proteus, I could retire. Better yet, buy a plant to manufacture PICs !!

HI!
hope this helps!
BIG BIG thank you for your code you didn't waste your time, it just help me a lot to understand how to store in the HEF I just tested and works great. BIG thank you!
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 1, 2  Next
Page 1 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