How can I concatenate 4 bytes ?
Posted: 13 Sep 2010 09:34
How can I concatenate 4 bytes ?
I know strcat.
But that is only doing 2 bytes at a time.
Thanks
I know strcat.
But that is only doing 2 bytes at a time.
Thanks
Development Tools, Compilers, Books
https://forum.mikroe.com/
Code: Select all
unsigned long Long_data =0 absolute 0x10;
unsigned short low_byte =0 absolute 0x10;
unsigned short middle_low =0 absolute 0x11;
unsigned short middle_high =0 absolute 0x12;
unsigned short high_byte =0 absolute 0x13;
Code: Select all
my_long_value=Make_Long(var1,var2,var3,var4);
Code: Select all
unsigned long my_var=0;
my_var=high_byte;
my_var=(my_var << 8) + middle_high;
my_var=(my_var << 8) +middle_low;
my_var=(my_var << 8) + low_byte;
I do not know about C, but in mikroPascal one can do the following:jumper wrote:Code: Select all
unsigned long my_var=0; my_var=high_byte; my_var=(my_var << 8) + middle_high; my_var=(my_var << 8) +middle_low; my_var=(my_var << 8) + low_byte;
Code: Select all
Highest(my_var) := high_byte;
Higher(my_var) := middle_high;
Hi(my_var) := middle_low;
Lo(my_var) := low_byte.
Code: Select all
etc = (aad<<32)+(aac<<16)+(aab<<8)+aaa;
LongToStr(etc/4, txt); //need to divide by 4, as the chip is incrementing in quarter-second
Glcd_Write_Text(txt, 1,7,1) ;
Code: Select all
etc = (aad>>32)+(aac>>16)+(aab>>8)+aaa;
Code: Select all
unsigned char sec; // sec number variables
unsigned char minutes; // min
unsigned char hours; // hours
char TIME[10]=" 00:00:00" absolute 0x20;
char hours_s[3] absolute 0x20; // string variables
char minutes_s[3] absolute 0x23; //
char sec_s[3]absolute 0x26; //
unsigned char day; // day
unsigned char month; //month
unsigned char year;
char DATE[10]=" 09/05/07" absolute 0x30;
char year_s[3] absolute 0x30;
char month_s[3] absolute 0x33;
char day_s[3]absolute 0x36;
void Update_time(){
// split your long seconds variable into years, months ,days, hours, min and secs and put them into the hours, minutes and sec bytes
ByteToStr(hours,hours_s);
if (hours_s[1]==' ') hours_s[1]='0';
ByteToStr(minutes,minutes_s);
minuter_s[0]=':';
if (minutes_s[1]==' ') minutes_s[1]='0';
ByteToStr(sec,sec_s);
sec_s[0]=':';
if (sec_s[1]==' ') sec_s[1]='0';
ByteToStr(year,year_s);
if (year_s[1]==' ') year_s[1]='0';
ByteToStr(month,month_s);
month_s[0]='/';
if (month_s[1]==' ') month_s[1]='0';
ByteToStr(day,day_s);
day_s[0]='/';
if (day_s[1]==' ') day_s[1]='0';
GLCD_write_text(DATE, 0, 1, 1) ;
GLCD_write_text(TIME,70, 1, 1) ;
}
1 sec per min is a little bit better than 2% and probably not much you can do about that unless you get a better time base i.e AC power line. And this 1 sec will probably drift with temperature so you can probably not remove it with a calculated formula.Calibrated, Temperature-Compensated RC Time
Base Accurate to 2% Typical
Code: Select all
union U32_ // place this above "main" somewhere
{
unsigned long dblword; // For accessing the whole 32-bit unsigned long
unsigned char byte[4]; // For accessing all 32-bits as individual bytes
};
...
...
void dispMyLong (unsigned char aad, unsigned char aac, unsigned char aab, unsigned char aaa)
{
union U32_ Bytes2long; // create an instance of the declared union called 'Bytes2long' in this function
unsigned char txt [12];
// combine bytes
Bytes2long.byte[3] = aad; // high byte
Bytes2long.byte[2] = aac; // mid-high byte
Bytes2long.byte[1] = aab; // mid-low byte
Bytes2long.byte[0] = aaa; // low byte
LongToStr ((Bytes2long.dblword / 4), txt); //need to divide by 4, as the chip is incrementing in quarter-second
Glcd_Write_Text(txt, 1,7,1) ;
}