Dear board Im writing a driver for HX711 load cell pickup unit.
This has a clock pin which is input to hx711 ic and this clk will be driven by stm32f103 And a Dout pin where for every low to high transition hx711 will out a bit and this goes 24 bit long..
I need to read this serial input and store to a byte which is 24 bit data where i can use array of 3 bytes..
I can easily read input status low or high from input function.
But how can i assign that bit value to a byte at particular position??
Earlier in pic micro.. i have function called bit_set and bit_clear to set or clear particular bit in a byte...
I googled and found there are no code fullfilling my requirements.
Can anyone suggest me please?? I need to read pin and save 24 bit info and convert to decimal.
Thanks
Then your google must be broken!
www.microchip.com/.../m63305.aspx
No... Actually i need the code for keil mdk because im using stm32f104... Not for microchip...
Or shall i try to define same as mentioned in mplab code in keilmdk...? Is it possible?
you dont need bit test or set or clear to get your 24 bits collected. there is a more advanced technique. learn from my code.
int BLOCK_OF_24_BITS; int INDEX; BLOCK_OF_24_BITS = 0; for ( INDEX=0 ; INDEX < 24 ; INDEX += 1 ) { BLOCK_OF_24_BITS = BLOCK_OF_24_BITS << 1; if ( GET_SINGLE_BIT() == 1 ) { BLOCK_OF_24_BITS = BLOCK_OF_24_BITS + 1; } } /* Now VAR_OF_24_BITS has your collected data */
Did you actually read that thread at all?
It shows you a way to do it using standard 'C' operators - nothing specific to PIC nor Microchip.
As Walleed Zumbambi showed, the bitwise shift operator is key.
Rather than
BLOCK_OF_24_BITS = BLOCK_OF_24_BITS + 1;
you could also do
BLOCK_OF_24_BITS |= 1;
But note that, by convention, ALL-UPPERCASE identifiers are reserved for #defined symbols.
Also, as the specific size of BLOCK_OF_24_BITS is fundamental, you should use a fixed-width type; eg,
uint32_t BLOCK_OF_24_BITS;
en.cppreference.com/.../integer
en.wikibooks.org/.../stdint.h
my examples always use CAPITALS to highlight for beginners and my real code is very professional
BLOCK_OF_24_BITS += 1;
you could also do safely here
BLOCK_OF_24_BITS ^= 1;
BLOCK_OF_24_BITS = BLOCK_OF_24_BITS^1;
one answer and lots ways do it and most compilers make same size and speed code for all
important you learn from our excellent code examples and understand what is happening
... consider the case(s) where
this
and this
are not equivalent.
Hint: Consider what happens when BLOCK_OF_24_BITS is odd or even ...
Dears, thank you for your quick help.
i ve used the one which is easily understandable for me. after i created a function and i run into debug session to see how really it works.
below is the code
uint32_t hx711_read(void){ static uint32_t BLOCK_OF_24_BITS; static int i_nav = 0; static uint8_t INDEX; BLOCK_OF_24_BITS = 0; for(INDEX=0;INDEX<24;INDEX++) { BLOCK_OF_24_BITS = BLOCK_OF_24_BITS <<1; BLOCK_OF_24_BITS = BLOCK_OF_24_BITS + 1; i_nav++; } return(BLOCK_OF_24_BITS ); }
above code when i enter into debug, actually debug did not work at all. untill For loop it is coming and only INDEX value is updating upto 23 and exiting.. break points are not working at Block_of_24_bits... break point works at i_nav++ but value if i_nav is not getting updated. its because im not using it anywhere??
if i put some code like
lcd_update_val(25);
then all break points works and i can see values of block_of_24_bits update.
is there any way i can see the value update without adding any lcd or other function. i even used static for all variables to see the updates occur. why they are not allowing me to see??
And what do you think that code actually does?
Most likely, your problems with debugging are because it does not do anything useful at all - so the compiler just optimises it all away completely!
function called bit_set and bit_clear to set or clear particular bit in a byte
#include <stdio.h> #include <stdint.h> uint8_t bit_set( uint8_t data, int position ) { data |= (1u<<position); return data; } uint8_t bit_clear( uint8_t data, int position ) { data &= ~(1u<<position); return data; } void main(void) { printf( "%X\n", bit_set( 0x0, 0 ) ); printf( "%X\n", bit_set( 0x0, 1 ) ); printf( "%X\n", bit_set( 0x0, 2 ) ); printf( "%X\n", bit_set( 0x0, 3 ) ); printf( "%X\n", bit_clear( 0xFF, 0 ) ); printf( "%X\n", bit_clear( 0xFF, 1 ) ); printf( "%X\n", bit_clear( 0xFF, 2 ) ); printf( "%X\n", bit_clear( 0xFF, 3 ) ); }
#include <stdio.h> #include <stdint.h> uint8_t bit_set( uint8_t data, int position ) { data |= (1u<<position); return data; } uint8_t bit_clear( uint8_t data, int position ) { data &= ~(1u<<position); return data; } void main(void) { uint8_t Target = 0xC9; printf( "%X\n", Target ); Target = bit_set( Target, 1 ); Target = bit_clear( Target, 3 ); printf( "%X\n", Target ); }