Hi all. I'm not a beginner when it comes to PICmicro MCU's and am very proficient in Microchip as well as Intel assembly. But now I'm working on learning C language for embedded MCU's. I'm versed with the basics of it but I'm still learning how to implement certain structs. I'm using Microchip's XC8 compiler.
My question is how do you create a variable array? I've searched online and can't seem to find anything that illustrates how to do this.
Basically what I'm asking is how to define a buffer of 16 variable characters that can be written out as a 16 byte page to a piece of hardware such as an LCD or an external memory chip. In assembly I would do the same thing using indirect addressing with the FSR/INDF.
You declare a 16 byte array like this:
uint8_t nameOfarray[16];
"uint8_t" is a C99 standard style variable type. You need to #include <stdint.h> to use it.
You can access the array by indexing:
nameOfArray[0] = value1;
nameOfArray[1] = value2;
// etc...
nameOfArray[15] = value16;
The compiler decides where the array memory is allocated. If you want to access specific memory location the codes gets a little more cryptic, but is not difficult.
This code writes a value 0xAA to a memory location 0x0123:
(*(uint8_t *)0x0123) = 0xAA; // cast memory location 0x0123 to a uint8_t pointer and "dereference" it for access.
You can use a direct memory address as an array like this:
((uint8_t *)0x0123)[0] = value1; // use indexing to write to an offset location from memory address. Notice the missing "dereference asterix".. the indexing [] does that in this case (with memory offset).
((uint8_t *)0x0123)[1] = value2;
// ...
((uint8_t *)0x0123)[15] = value16; // value 16 is written to memory location (0x0123+15)
You can also declare a pointer variable and assign it any memory address you like:
uint8_t* bufferPointer;
bufferPointer = (uint8_t*)0x0123;
If this was not what you were looking for, or was too confusing, please give a specific example problem and I can write it in C.