|
|
View previous topic :: View next topic |
Author |
Message |
bold Guest
|
struct operations in ROM? |
Posted: Sat Aug 15, 2009 2:27 pm |
|
|
hi I have a problem in const structs
Code: |
struct info{
char name[20];
int8 age;
};
....
struct info const student1;
......in a function I try to write into struct student1
strcpy(student1.name, "Mihael Alessendro");
student.age=33;
.....
printf("name: %s",student1.name);
printf("name: %s",(char *)student1.name);
printf("name: %s",(char *)&student1.name);
printf("name: %s",(char *)&student1.name[0]);
printf("age: %d",student1.age);
|
program prints age correctly but none of the names are correct
what is wrong with this code? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Aug 15, 2009 10:58 pm |
|
|
Quote: | struct info{
char name[20];
int8 age;
};
struct info const student1;
// in a function I try to write into struct student1
strcpy(student1.name, "Mihael Alessendro");
|
I can't even get it to compile. You're trying to create a structure in ROM
and then write to it at run-time with strcpy(). That won't work.
The strcpy() function can only write to RAM.
See my post in this thread which shows how to put a structure in ROM,
and also how to initialize it.
http://www.ccsinfo.com/forum/viewtopic.php?t=32604
This post shows how to put an array of structures into ROM:
http://www.ccsinfo.com/forum/viewtopic.php?t=30610
More 'const' structure examples:
http://www.ccsinfo.com/forum/viewtopic.php?t=28849
http://www.ccsinfo.com/forum/viewtopic.php?t=22070
If this answer didn't help, then post a full, compilable test program.
In other words, post the #include statement, any #device statements,
#fuses, #use delay, main(), etc. |
|
|
Guest
|
|
Posted: Sun Aug 16, 2009 5:08 am |
|
|
Code: | #include <18F452.H>
#fuses XT, NOWDT, PROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
typedef struct
{
char menu_type;
char menu_function_index;
char line1[17]; // For a 16x2 LCD
char line2[17];
}menu_t;
#define MAX_INDEX 15
// The PCH compiler allows const arrays larger than 256
// ROM words, so it's much better than PCM for this purpose.
const menu_t menu_screens[MAX_INDEX] =
{
{0, 1, "Menu 1", "Menu 1, Line 2"},
{1, 2, " sub-Menu 1A", " 1A Line 2"},
{2, 3, " sub-Menu 1B", " 1B Line 2"},
{0, 1, "Menu 2", "Menu 2, Line 2"},
{1, 2, " sub-Menu 2A", " 2A Line 2"},
{2, 3, " sub-Menu 2B", " 2B Line 2"},
{3, 4, " sub-Menu 2C", " 2C Line 2"},
{0, 1, "Menu 3", "Menu 3, Line 2"},
{1, 2, " sub-Menu 3A", " 3A Line 2"},
{2, 3, " sub-Menu 3B", " 3B Line 2"},
{3, 4, " sub-Menu 3C", " 3C Line 2"},
{4, 5, " sub-Menu 3D", " 3D Line 2"},
{0, 1, "Menu 4", "Menu 4, Line 2"},
{1, 2, " sub-Menu 4A", " 4A Line 2"},
{2, 3, " sub-Menu 4B", " 4B Line 2"}
};
void display_menu(int8 index);
//====================================
void main()
{
int8 i;
for(i = 0; i < MAX_INDEX; i++)
{
display_menu(i);
printf("\r");
}
while(1);
}
//===============================
// FUNCTIONS
//-----------------------------------------------
void display_menu(int8 index)
{
printf("%s\r", menu_screens[index].line1);
printf("%s", menu_screens[index].line2);
} |
this is one of the samples you send, but this does not work with ccs 4.084 with 18F4620
what I see is
P„“HÖ(á4
ÀebkËÃ.ãÕğÁì|"‹d sth like this
not other printf functions work correctly, all baudrate and other stuffs are OK |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Aug 16, 2009 12:14 pm |
|
|
It worked for me. I installed vs. 4.084, and plugged an 18F4620 into
my PicDem2-Plus board and ran it. It gave the correct output.
I did change one thing. The original code was tested in MPLAB simulator
so I only used "\r" in the printf, because the simulator automatically puts
in a linefeed. For use in real hardware with TeraTerm, I changed it to
"\n\r". Also, the PROTECT fuse is set by mistake. I don't normally
use that fuse in a test program. But it doesn't make any difference to
this test. It works either way.
The most likely thing is your baud rate on your terminal program.
Check that it's the same as this program, 9600 baud. Check your
other serial settings, such as parity, number of bits, etc. |
|
|
|
|
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
|