Hello everybody,
I am very new to C language. In my project I get data serially from a port pin. I want to save the first 8 bits to one variable, another to second & so on. In all I want to save 32 bits in 4 bytes. Can you suggest C code.
I know it in assembly by using RRC or RLC, but how to achieve it in C?
Thanks
It shouldn't be too complicated. Something is the lines of:
BOOL data_full; BYTE bits_received; insigned int data_received; // initialise global variables; data_full = __FALSE; bits_received = 0; data_received = 0; interrupt_handler() { if(DATA) { data_received =| ( 1 << bits_received ); } bits_received++; if(bits_received == 32) { bits_received = 0; data_full = __TRUE; // do something with data_received. } }
Thanks, that is exactly what I needed. The BOOL, BYTE, __FALSE & __TRUE keywords gave errors in compiling. So I replaced them with bit, unsigned char, 0 & 1 respectively. Now it compiles without errors.
Though I have not tested my project, I will let you know as soon as I test it.
And you manage to fit 32 bits in the following variable?
unsigned int data_received;
Good lord, why use this indentation
if(DATA) { data_received =| ( 1 << bits_received ); }
when you can do this
if(DATA) { data_received =| ( 1 << bits_received ) ; // I always add a trailing space }
The horror, the horror...
I always add a trailing space
Ohh. Very Noël Coward.
Actually, I prefer Jackson Pollock.
View all questions in Keil forum