Code: Select all
int range;
unsigned short highbyte, lowbyte;
highbyte = 1;
lowbyte = 234;
range = (highbyte << 8) | lowbyte;
Code: Select all
int range;
unsigned short highbyte, lowbyte;
highbyte = 1;
lowbyte = 234;
range = (highbyte << 8) | lowbyte;
Code: Select all
int range;
unsigned short highbyte, lowbyte;
highbyte = 1;
lowbyte = 234;
range = (highbyte << 8) || lowbyte;
This works as a sample example:ascomm wrote:As far as I know, this shoud do the trick but it doesn't...Code: Select all
int range; unsigned short highbyte, lowbyte; highbyte = 1; lowbyte = 234; range = (highbyte << 8) | lowbyte;
Code: Select all
void main(){
int range;
unsigned short highbyte, lowbyte;
highbyte = 1;
lowbyte = 234;
range = (highbyte << 8) | lowbyte;
}
Code: Select all
int read_sonar_range(unsigned short SRF08ADDRESS)
{
unsigned short highbyte, lowbyte;
char *txt[7];
int range;
I2C_Start(); // Generate start
while (!I2C_Is_Idle()) asm nop; // Wait until the I2C-bus is free
I2C_Wr(SRF08ADDRESS); // send sonar address
while (!I2C_Is_Idle()) asm nop; // Wait until the I2C-bus is free
I2C_Wr(0x03); // send sonar register address
while (!I2C_Is_Idle()) asm nop; // Wait until the I2C-bus is free
I2C_Repeated_Start(); // Generate repeated start
//while (!I2C_Is_Idle()) asm nop; // Wait until the I2C-bus is free
I2C_Wr(SRF08ADDRESS+1); // send address
//while (!I2C_Is_Idle()) asm nop; // Wait until the I2C-bus is free
lowbyte = I2C_Rd(1); // Read low-byte
while (!I2C_Is_Idle()) asm nop; // Wait until the I2C-bus is free
highbyte = I2C_Rd(0); // Read high-byte
while (!I2C_Is_Idle()) asm nop; // Wait until the I2C-bus is free
I2C_Stop(); // Generate stop
Usart_Write(highbyte);
Usart_Write(lowbyte);
Lcd_Cmd(Lcd_CLEAR);
ByteToStr(highbyte,txt);
LCD_Out(1,1,"H:");
LCD_Out(1,3,txt);
ByteToStr(lowbyte,txt);
LCD_Out(1,7,"L:");
LCD_Out(1,9,txt);
//highbyte = 1;
//lowbyte = 25;
range = (highbyte << 8) | lowbyte;
IntToStr(range,txt);
LCD_Out(2,1,"H+L:");
LCD_Out(2,5,txt);
return (0);
}
I'm not so familiar with pointers, so no I have not considered them.gambrose wrote:have you considered using a pointer to byte assigning it the value of range then loading in highbyte adding one (or subtracting one i can't remember which) to the pointer then assigning lowerbyte.
Code: Select all
int range; // declare you int
unsigned short * ptr = ⦥ // obtain a reference to it
* ptr = 234; // load lower byte first
ptr++; // increment the pointer
* ptr = 1; // load the higher byte
Yes, this is a standard C way of doing such things. mikroC has a useful extension to handle such cases, which is 'stolen' from mikroPascal, and these are the built-in functions for byte access at multi-byte variables: Lo(), Hi(), Higher() and Highest(). As you see, they cover variables of up to 4 bytes, and can be used for all the fundamental data types (except for the floating point). You can access structure elements as well, but it doesn't pay off since access to structure members is direct anyway.gambrose wrote:Code: Select all
int range; // declare you int unsigned short * ptr = ⦥ // obtain a reference to it * ptr = 234; // load lower byte first ptr++; // increment the pointer * ptr = 1; // load the higher byte
They are already implemented, and we'll document them in the next Help release. In the meantime, you can see how they work from mikroBasic or mikroPascal's Help. In short:gambrose wrote:these functions are not documented in the current mikroC are they due for the next release?
Code: Select all
long sl1;
unsigned short us;
...
us = Lo(sl1); // us <- sl1_1
us = Hi(sl1); // us <- sl1_2
us = Higher(sl1); // us <- sl1_3
us = Highest(sl1); // us <- sl1_4
Code: Select all
function Hi(number : word..longint) : byte;