|
|
View previous topic :: View next topic |
Author |
Message |
Cenatronics
Joined: 27 Nov 2006 Posts: 13
|
array struct list |
Posted: Tue Jun 18, 2019 8:49 pm |
|
|
Hello to everyone,
I'm having trouble reading an array of structures. I hope there's someone who can help.
Here's a very simplified version:
CCS version 5.081
Code: |
#include <18F4620.h>
#device ADC=10
#CASE
// ***********************************************************************
// * Types definitions
// ***********************************************************************
typedef unsigned int1 bit_t;
typedef unsigned int1 bool;
typedef unsigned int8 uint8_t;
typedef signed int8 int8_t;
typedef unsigned int16 uint16_t;
typedef signed int16 int16_t;
typedef unsigned int32 uint32_t;
typedef signed int32 int32_t;
#FUSES NOWDT //No Watch Dog Timer
#FUSES PUT //Power Up Timer
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES MCLR //Master Clear pin enabled
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(clock = 40MHz, crystal = 10MHz)
struct ButtonObj
{
uint8_t _but_id; // ButtonID
uint8_t _scn_cid; // Screen ID */
uint8_t _name[10]; // Button name */
void *_press_ptr; // Button Press Callback Func
};
#define CreateButtonObj(obj, butid, scnid, name) \
obj._but_id = butid; \
obj._scn_cid = scnid; \
strcpy(obj._name, name)
struct ButtonObj but_Down, but_Up, but_Enter; // Define Buttons
struct ButtonObj *button_list[] = {
&but_Down,
&but_Up,
&but_Enter
};
#define but_upScnID 0
#define but_upID 0
#define but_downScnID 0
#define but_downID 1
#define but_enterScnID 0
#define but_enterID 2
void Button_Checking(struct ButtonObj **list, uint8_t scnid, uint8_t butid, uint8_t ePress)
{
struct ButtonObj *ob = NULL;
uint8_t i = 0;
if (list == 0) return;
for (i = 0; list[i] != 0; i++)
{
ob = list[i];
//
//
Lcd_GotoXY(1, 2+i);
printf(Lcd_Putc, "%u: bid:%u scid:%u ", i, ob->_but_id, ob->_scn_cid);
//
//
}
}
void main()
{
CreateButtonObj(but_Down, but_downScnID, but_downID, "Down");
CreateButtonObj(but_Up, but_upScnID, but_upID, "Up");
CreateButtonObj(but_Enter, but_enterScnID, but_enterID, "Enter");
while(1)
{
Button_Checking(nex_listen_list, but_downScnID, but_downID, 1); // Print to LCD
delay_ms(1000); // for testing
Button_Checking(nex_listen_list, but_enterScnID, but_enterID, 1); // Not Print
}
}
|
If I try a wide variety of methods, this is the way I can get data output even if there is only one line in the for loop. I couldn't figure out why the list didn't accept only one object and the other two. I think there is a problem, especially in the following lines:
Code: | struct ButtonObj *button_list[] = {
&but_Down,
&but_Up,
&but_Enter
};
|
Thanks in advance for your help.
Namik |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19497
|
|
Posted: Wed Jun 19, 2019 2:38 am |
|
|
Use pointer offsets, not array indexes.
So:
ob = *(list+i);
If you use array accesses for a pointer to a pointer, CCS does't 'know' to
apply the size of a pointer to the arithmetic. So it'll be accessing the wrong
location when it accesses anything other than i=0.
With that change, you should find it'll work. |
|
|
Cenatronics
Joined: 27 Nov 2006 Posts: 13
|
|
Posted: Wed Jun 19, 2019 5:34 am |
|
|
thank you very much. |
|
|
Cenatronics
Joined: 27 Nov 2006 Posts: 13
|
|
Posted: Wed Jun 19, 2019 6:08 am |
|
|
How do we understand what CCS does and what it does. How do you obtain this information? These are not specified in the CCS manual.
I didn't read any more books on the markers (which I missed). And I'm not an expert on C.
Namik |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19497
|
|
Posted: Wed Jun 19, 2019 6:28 am |
|
|
Just by trying.
I tried using an array of pointers a while ago, and preferred to use 'array'
notation as you are doing, and found it did not work. Was testing in a
debugger, and could see the offset was being incorrectly calculated, so
tried using the pointer notation instead, and much to my surprise found
this ran OK.
That is the key to this forum. The dozens of 'old hands', who have at one time
or another tried almost everything possible on PIC's and the compiler, and
in doing so, have found solutions for this type of problem. |
|
|
Cenatronics
Joined: 27 Nov 2006 Posts: 13
|
|
Posted: Wed Jun 19, 2019 6:55 am |
|
|
Thanks for this nice explanation. It was very useful.
Namik |
|
|
|
|
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
|