Rockwell Automation 1771-DMC_DMC1_DMC4_DXPS Control Coprocessor User Manual User Manual
Page 43
Chapter 3
Getting Started with the Control Coprocessor
3-14
Non-Volatile Memory Example User Program MY_MEM.C
#include
#define PM_PTR 0x10000200 /* this is where my nv memory ptr is stored */
extern time_t time(); /* function declarations */
struct tm *localtime();
char * asctime();
typedef struct /* define my structure in nv ram */
{
unsigned *ptr_check; /* storage for checking nv pointer */
char time_stamp[26]; /* xxx mmm dd hh:mm:ss yyy\n\0 */
unsigned count; /* boot count */
}MY_MEM;
main (argc, argv)
int argc;
char *argv[];
{
MY_MEM *mm_ptr; /* ptr to my memory */
char *tim_ptr; /* string ptr for time string */
time_t cal_time; /* calendar time storage */
struct tm *loc_time; /* local time storage */
mm_ptr = *(MY_MEM **)PM_PTR; /* get pointer to my nv data */
if (mm_ptr == 0) /* make sure its allocated */
{
printf (”Protected Memory not allocated\n”);
exit (0);
}
if (argc > 1)mm_ptr->ptr_check=(unsigned *)mm_ptr; /* store ptr on init */
else
{
if (mm_ptr != (MY_MEM *)mm_ptr->ptr_check) /* check if ptr changed */
{
printf (”Protected Memory Pointer changed\n”);
exit (0);
}
}
cal_time = time(0); /* get time */
loc_time = localtime (&cal_time); /* convert to local time */
tim_ptr = asctime (loc_time); /* convert to string */
if (argc > 1) /* if command line parameter then initialize data */
{
mm_ptr->count = 0; /* start count at 0 */
strncpy (mm_ptr->time_stamp,tim_ptr,26); /* store initial time */
printf (”\nCurrent time is ->%s\n”,mm_ptr->time_stamp);
}
else
{
printf (”\nTime of last boot ->%s\n”,mm_ptr->time_stamp); /* print old */
strncpy (mm_ptr->time_stamp,tim_ptr,26); /* copy new time to nv */
printf (”Time of this boot ->%s\n”,mm_ptr->time_stamp); /* print new */
mm_ptr->count +=1; /* increment boot count */
printf (”Boot count = %d\n”,mm_ptr->count); /* print boot count */
}
}