Author Topic: Embedded C language question  (Read 1002 times)

0 Members and 1 Guest are viewing this topic.

Offline lawrence11Topic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 322
  • Country: ca
Embedded C language question
« on: March 05, 2020, 06:30:40 pm »
How do I access, and give a value to, precise individual byte of a 16bit (word) array ??

Quickest possible code, on ARM cortex-M. ?

Like this? Well, I guess it dont compile, so this must not be it.

For example.

int16_t bufferk[6];

const uint8_t mask1=1;

.........

while(1){

.....
(uint8_t*)&bufferk[0]=mask1;

}
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11811
  • Country: us
    • Personal site
Re: Embedded C language question
« Reply #1 on: March 05, 2020, 06:43:39 pm »
Code: [Select]
((uint8_t *)bufferk)[0]=mask1;

Also, when you say that some thing does not compile, always include the exact error message.
Alex
 
The following users thanked this post: lawrence11

Offline lawrence11Topic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 322
  • Country: ca
Re: Embedded C language question
« Reply #2 on: March 05, 2020, 06:47:29 pm »
A shit, thanks man, it compiles...

Its all about details, should have expected this.

Yeah I knew it was small and error code was not necessary in this case.
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 4653
  • Country: nz
Re: Embedded C language question
« Reply #3 on: March 05, 2020, 08:39:51 pm »
How do I access, and give a value to, precise individual byte of a 16bit (word) array ??

Quickest possible code, on ARM cortex-M. ?

Like this? Well, I guess it dont compile, so this must not be it.

For example.

int16_t bufferk[6];

const uint8_t mask1=1;

.........

while(1){

.....
(uint8_t*)&bufferk[0]=mask1;

}

Code: [Select]
#include <stdint.h>

const uint8_t mask1=1;

void foo(int16_t bufferk[6]){
  *(uint8_t*)&bufferk[0]=mask1;
}

Produces...

Code: [Select]
foo.c: In function ‘foo’:
foo.c:6:24: error: lvalue required as left operand of assignment
   (uint8_t*)&bufferk[0]=mask1;
                        ^

The message is helpful. You're trying to assign a uint8_t to a uint8_t*.

Code: [Select]
*(uint8_t*)&bufferk[0]=mask1;

Now you get...

Code: [Select]
00000000 <foo>:
   0: 2301      movs r3, #1
   2: 7003      strb r3, [r0, #0]
   4: 4770      bx lr

Note that the above code and ataradov's do different things if you change the 0 to another value.

When it is 0, both versions change the first half of bufferk[0], which on ARM is the least significant 8 bits.

If you change the 0 to a 1 then my code will change the least significant bits of bufferk[1], while ataradov's code will change the most significant bits of bufferk[0].
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 15607
  • Country: fr
Re: Embedded C language question
« Reply #4 on: March 06, 2020, 03:51:42 pm »
So yeah - this is not specifically an "embedded" C question, more like general C (and would probably be better asked in the Programming section, but anyway.)

You need to brush up on: C pointers, data types, casting and operator precedence. Then you'll be alright.
You got answers and fixes above - but take it as an opportunity to master the above rather than just apply a recipe blindly. Just a thought.
 ;D
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf