Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I2C LCD + Keypad - Adafruit 715
15-01-2014, 03:23 PM,
#31
RE: I2C LCD + Keypad - Adafruit 715
Thanks for your input. My typedef is in a header file

typedef union
{
u8 val;
struct
{
unsigned P0 :1;
unsigned P1 :1;
unsigned P2 :1;
unsigned P3 :1;
unsigned P4 :1;
unsigned P5 :1;
unsigned P6 :1;
unsigned P7 :1;
} bits;
} MCP23017_data;

but when I want to change the data in it from a macro in a different .c file, I get this compilation error.

My complete macro is:-

void lcdi2c_send4bits(u8 value, u8 mode)
{


// ---------- LCD Enable Cycle


union MCP23017_data;

MCP23017_data.bits.p5 = 0x00;

h_address = MCP23017_hardware_address;
r_address = GPIOA;

I2C_send_MCP23017(I2C1, h_address, r_address, value);
Delayus(1); // E Pulse Width > 300ns

//MCP23017_data.bits.P5 = LOW;
h_address = MCP23017_hardware_address;
r_address = GPIOA;


I2C_send_MCP23017(I2C1, h_address, r_address, value);
Delayus(1); // E Enable Cycle > (300 + 200) = 500ns
}

I am struggling with changing the bit value of a union/structure (MCP23017_data.val) from within a macro in a .c file.
I appreciate your help - if I know how to do this, it is a key requirement which I can use for bit addressing in future.
Many thanks.
Reply
15-01-2014, 03:59 PM,
#32
RE: I2C LCD + Keypad - Adafruit 715
In C

union MCP23017_data;

says that MCP23017_data is a union. Not a data item, but a union. Its details (i.e. what it is made up from) are as yet unstated.

Have a look at the various examples here and the many on the net. Or post a longer sample but also say what you meant it to do as otherwise we'll be guessing!

John
Reply
15-01-2014, 04:15 PM,
#33
RE: I2C LCD + Keypad - Adafruit 715
I only put the "union MCP23017_data" in the macro because the error called for a type before the "MCP23017_data.bits.p5 = 0x00;" statement, but this does not work. The Typedef of the union (below) is in a header file.

typedef union
{
u8 val;
struct
{
unsigned P0 :1;
unsigned P1 :1;
unsigned P2 :1;
unsigned P3 :1;
unsigned P4 :1;
unsigned P5 :1;
unsigned P6 :1;
unsigned P7 :1;
} bits;
} MCP23017_data

I am trying to alter the bits in the structure from within a macro but the compiler/linker does not seem to like the "MCP23017_data.bits.p5 = 0x00;" statement I have embedded. Not sure why!
Reply
15-01-2014, 04:35 PM,
#34
RE: I2C LCD + Keypad - Adafruit 715
lcdi2c_send4bits is a C function, not a macro, that confused me.

Ah, I see!
You want MCP23017_data to be a global variable (rather than a typedef symbol).
In this case:

A) replace "typedef" (in the include file) by "extern" AND give the union a TYPE name so that it looks like this:

extern union m23017 { u8 val; ... } MCP23017_data; // declares union TYPE and VAR in one!

B) remove the "union MCP23017_data;" line from lcdi2c_send4bits()

C) insert the following line into EXACTLY ONE of the .C files that include your .h file:

union m23017 MCP23017_data; // actual var definition, reserving memory space!

(outside any C functions of course!)

D) Consult C tutorials until you fully understand what we did here (just kidding!)

This SHOULD work, not tested of course.
I am referring to C, as I have zero pinguino experience!

Another issue - I believe it should be:
MCP23017_data.bits.P5 = 0; // rather than .p5 --- case sensitive?
I did not care for the rest of your code btw.
I guess MCP23017_hardware_address ist the I2C slave address?

Good luck ...
Reply
15-01-2014, 04:40 PM,
#35
RE: I2C LCD + Keypad - Adafruit 715
What you have there (apart from a missing ; at the very end) is a definition of a typedef called MCP23017_data. (If you'd left off the word typedef then you'd have created an actual union with data locations.) You've not declared anything that uses it, which you might do as:

MCP23017_data mydata;

Only the above actually creates something with memory locations to hold data. (The definition just describes what such locations look like.)

Then you might do:

mydata.bits.p5 = 0x00;

(you don't need the 0x00 just 0 will do)

John
Reply
15-01-2014, 05:11 PM,
#36
RE: I2C LCD + Keypad - Adafruit 715
Bingo! I went with your suggestion pingotg and removed the "typedef" from the .h file, just leaving the "union" and the code compiled. BTW I copied the typedef from pcf8574.h code example.
Now I should be able to address the data byte bit by bit to write the code to initialise and write to the LCD. This is a powerful function!
Many thanks!
Reply
15-01-2014, 05:24 PM,
#37
RE: I2C LCD + Keypad - Adafruit 715
Please take care! Without the "extern" modifier and the _single_ instanciation ("union m23017 MCP23017_data;"), it might compile and maybe even link, but you'd propably end up with TWO variables (by the same name) and thus the two modules (.c files) would NOT be able to use it as a COMMON object!
Reply
15-01-2014, 05:32 PM, (This post was last modified: 15-01-2014, 05:45 PM by charlie_abb.)
#38
RE: I2C LCD + Keypad - Adafruit 715
OK thanks for the caution - I will now try some basic code to write to the pins of the MCP23017 so that I know it works OK before I write the code for (and connect) the LCD to the expander.

BTW - on another subject, I tried to compile the dataloggerOTG project to start to read/write to my SD card but it gives the following errors:-
C:\Pinguino\x4-easy-rev959\source\/user.c:151:38: error: 'rtccDate' has no member named 'wday'

C:\Pinguino\x4-easy-rev959\source\/user.c:153:44: error: 'rtccDate' has no member named 'mday'

C:\Pinguino\x4-easy-rev959\source\/user.c:153:64: error: 'rtccDate' has no member named 'mon'

C:\Pinguino\x4-easy-rev959\source\/user.c:156:45: error: 'rtccTime' has no member named 'hour'

C:\Pinguino\x4-easy-rev959\source\/user.c:156:59: error: 'rtccTime' has no member named 'min'

C:\Pinguino\x4-easy-rev959\source\/user.c:156:72: error: 'rtccTime' has no member named 'sec'

How should I mod it? It would be better if the examples on the IDE were already thoroughly tested and bombproof to get newbies started more quickly.
Reply
15-01-2014, 06:35 PM,
#39
RE: I2C LCD + Keypad - Adafruit 715
With extern it will probably fail. Study how #include is used with .c files here.

John
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)