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 support@ccsinfo.com

Internal serial number
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 19264

View user's profile Send private message

PostPosted: Fri May 28, 2021 8:16 am     Reply with quote

What you post, cannot work, since 'buffer; is not declared anywhere.
It won't actually compile as shown.
Also the declaration of 'main' is simply wrong. Again won't compile.

So this is not what you are testing. Not a good start... Sad

rand will always give the same sequence, unless you seed it with srand.
No magic involved, look at the source code. It simply takes the current
seed value, multiplies by 1103515245, adds 12345, then returns the upper
16bits mod RAND_MAX.
This will always give the same sequence unless the seed is changed. It
cannot do anything else.

Actually there is a way you could get different sequences. If you reset
the chip and did not power down, the sequence would continue from the
last place it was at before the reset. The RAM does not get cleared on a
reset. However power down, and the sequence will start again at the
beginning.
temtronic



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

View user's profile Send private message

PostPosted: Fri May 28, 2021 8:32 am     Reply with quote

hmm... since he's now using the 10f322,...

using this...
from the datasheet...

3.5 User ID
Four memory locations (2000h-2003h) are designated
as ID locations where the user can store checksum or
other code identification numbers. These locations are
readable and writable during normal execution.

would be EASY to get unique 'serial numbers' into the PICs....

or am I missing the something?

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19264

View user's profile Send private message

PostPosted: Fri May 28, 2021 9:21 am     Reply with quote

No. That is why the 3 22 is so much better. Smile
rodrigo_cirilo



Joined: 31 Mar 2019
Posts: 43
Location: Sao paulo/Brazil

View user's profile Send private message Send e-mail

PostPosted: Fri May 28, 2021 11:38 am     Reply with quote

Ttelmah wrote:
No. That is why the 3 22 is so much better. Smile



Sorry for not posting the whole code, I only posted what I thought was necessary.

This is true, in the protheus simulator, if I reset the mclr, it would change the code, even in the simulation, if I switched it off and on again, then the number would be the same.

But during the pic recording with pickit3, I record and observe it in the memory, and it changes the values after I erase the chip and record again, maybe that's it, it doesn't turn off completely when being recorded several times by pickit3, so the values change .


Code:

#include <10F322.h>
//#include <12f675.h>
#FUSES NOWDT                      //Watch Dog Timer
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOPROTECT                //Code not protected from reading
#FUSES INTRC
#FUSES NOWRT
#use delay(clock=4000000)//,RESTART_WDT)
#ORG 0x1E0, 0x1E3 {}
#define RAND_MAX 255
#INCLUDE <STDLIB.H>


#define HEF 0x1E0
#define saida PIN_A0
//Register and bit definitions
#byte PMADRH=getenv("SFR:PMADRH")
#byte PMADRL=getenv("SFR:PMADRL")
#byte PMDATH=getenv("SFR:PMDATH")
#byte PMDATL=getenv("SFR:PMDATL")
#bit CFGS=getenv("BIT:CFGS")
#bit WREN=getenv("BIT:WREN")
#bit FREE=getenv("BIT:FREE")
#bit LWLO=getenv("BIT:LWLO")
#BIT WR=getenv("BIT:WR")
#byte PMCON2=getenv("SFR:PMCON2")

int8 buffer[8];
int8 buffer2[3];
int8 buffer1[8] = {0,0,0,0,0,0,0,0};
 const int16 te = 500;  //valor de TE
int counter = 0;
 int8 a;
void my_erase_program_eeprom(int16 address)
{
  //erases the page at the selected address
  disable_interrupts(GLOBAL);
  CFGS=FALSE;
  PMADRL=make8(address,0);
  PMADRH=make8(address,1);
  CFGS=FALSE;
  FREE=TRUE;
  WREN=TRUE;
  PMCON2=0x55;
  PMCON2=0xAA;
  WR=TRUE;
  delay_cycles(1);
  delay_cycles(1);
  WREN=FALSE;
  enable_interrupts(GLOBAL);
}

void my_write_program_memory(int16 address, int16 *data, int8 ctr)
{
  //Beware - this does not erase a page for you, and will not handle
  //a write that goes beyond the end of a page. Only works to
  //write ctr bytes, to the program memory starting at address
  int8 count;
  disable_interrupts(GLOBAL);
  WREN=TRUE;
  CFGS=FALSE;
  LWLO=TRUE; //load latches only
  PMADRL=make8(address,0);
  PMADRH=make8(address,1);
  for (count=0;count<((ctr/2)-1);count++)
  {
    PMDATL=make8(data[count],0);
    PMDATH=make8(data[count],1);
    PMCON2=0x55;
    PMCON2=0xAA;
    WR=TRUE;
    delay_cycles(1);
    delay_cycles(1);
    PMADRL++;
  }
  LWLO=FALSE;
  PMDATL=make8(data[count],0);
  PMDATH=make8(data[count],1);  //last word
  PMCON2=0x55;
  PMCON2=0xAA;
  WR=TRUE;
  delay_cycles(1);
  delay_cycles(1);   
  WREN=FALSE;
  enable_interrupts(GLOBAL);
}

//memory layout definitions
struct twobytes
{
     int8 l;
     int8 h;
};

union prog_mem
{
     int16 word;
     struct twobytes b;
};



void pilot_period(){
 output_low(saida);
 delay_us(23*te);
}

void start_bit(){
    output_high(saida);
    delay_us(te);
    output_low(saida);

}
void bit1(){
    output_low(saida);  //'1'
    delay_us(2*te);
    output_high(saida);
    delay_us(te);
    output_low(saida);

}
void bit0(){
    output_low(saida);  //'0'
    delay_us(te);
    output_high(saida);
    delay_us(2*te);
    output_low(saida);
}

void end_period(){
    bit0();
    bit1();
    bit0();
    bit1();
}



void envia(int valor){
  int b;
  for(b=0;b<8;b++){

      if(valor&1){
         bit1();
      }
      else{
         bit0();
      }
      valor>>=1;
  }
}
void main()
{

   read_program_memory(HEF,buffer1,8);
   if(buffer1[0] == 0xFF)
    {
  //    my_erase_program_eeprom(HEF);
  buffer[0] = 0x10;
  buffer[2]=rand();
   buffer[4]=rand();
    buffer[6]=rand();
    if(buffer[2] < 100)
      {
        buffer[2] = buffer[2] + 100;
      }
     if(buffer[4] < 100)
      {
        buffer[4] = buffer[4] + 100;
      }
      if(buffer[6] < 100)
      {
        buffer[6] = buffer[6] + 100;
      } 
      my_write_program_memory(HEF, buffer, 8);
     
    }
   else
   {
     read_program_memory(HEF,buffer,8);
     buffer2[2] = buffer[2];
     buffer2[1] = buffer[4];
     buffer2[0] = buffer[6];
     
   
   }


while(true)
{
    for(int x = 0; x < 200; x++)
     {
     counter++;
 
       
      pilot_period();
      start_bit();
           
      for(a=3;a>0;a--){
      envia(buffer2[a-1]);
      }
      end_period();
     }
   while(counter >= 200)
   {
   
   }
 
}
}
 
   

Ttelmah



Joined: 11 Mar 2010
Posts: 19264

View user's profile Send private message

PostPosted: Fri May 28, 2021 11:59 am     Reply with quote

Reprogramming a PIC, does not erase it's RAM. That is why you are seeing
different sequences. To see what happens on a real chip, you need to use a
real chip and cycle the power. You will then see the sequence starts again
with the same values. Sad
rodrigo_cirilo



Joined: 31 Mar 2019
Posts: 43
Location: Sao paulo/Brazil

View user's profile Send private message Send e-mail

PostPosted: Fri May 28, 2021 12:07 pm     Reply with quote

Ttelmah wrote:
Reprogramming a PIC, does not erase it's RAM. That is why you are seeing
different sequences. To see what happens on a real chip, you need to use a
real chip and cycle the power. You will then see the sequence starts again
with the same values. Sad


What I reported was with a real chip, I look at the pickit3 programming screen, I can see the memory addresses, then I record the pic, then start reading the RAM, and I see that the generated numbers were recorded in that indicated location 0X1E0, then erase the pic, and record again, start and read, and see that they are different numbers.
Ttelmah



Joined: 11 Mar 2010
Posts: 19264

View user's profile Send private message

PostPosted: Sat May 29, 2021 6:48 am     Reply with quote

No, you are 'missing the point'.

When you reprogram the chip, it does not erase the RAM.
When you carry on after this, the chip carries on as if it has been running
'all the time'. The RAM is retained. So though you have put in new code,
the rand function functions as if the chip has kept on working. So you see
apparently a 'new' sequence. However this is not what would happen if
you powered the chip down, and retried. You would then see the same
sequence repeated as Proteus shows.
temtronic



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

View user's profile Send private message

PostPosted: Sat May 29, 2021 8:39 am     Reply with quote

Should have
Code:
#zero_ram

Near the beginning of the program...
This would eliminate any 'ghosting'...
A term I learned in the 70s when a certain video card for a PC had GREAT caps, so the Video RAM kept data even though PC had been powered off for a hard reboot.

Zero it all out, preset with KNOWN values, then run program.

Always best to KNOW what's in RAM to begin with....
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, 3
Page 3 of 3

 
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