Arduino

Arduino HighByte() Function

Arduino has made electronics and programming more accessible to a wider audience. Arduino has several different functions to manipulate byte data. One such function is highByte(). This function extracts the high bytes from the given input data. This article will cover the Arduino highByte() function in detail.

highByte() Function in Arduino

The highByte() function is a part of the Arduino Language Reference library that allows the user to extract the high byte from a 16-bit variable. A byte is a unit of data that can store a value from 0 to 255. A 16-bit variable, on the other hand, can store a value from 0 to 65,535.

Explanation of Arduino highByte() Function

The highByte() function extracts the most significant byte (the high byte) from a 16-bit variable. The high byte is the byte that contains the most significant bits of the variable, while the low byte contains the least significant bits. The highByte() function is useful when dealing with 16-bit variables, such as analog-to-digital converter (ADC) values, PWM values, and timer values.

Syntax of highByte() Function

The syntax of the highByte() function is as follows:

highByte(x)

Where x is the 16-bit variable whose high byte we want to extract.

Parameter

The function takes only one parameter, which is the 16-bit value to extract the high byte from.

Return

The return value of the highByte() function is an 8-bit value, which represents the high byte of the 16-bit input value. The return value is an unsigned integer between 0 and 255.

Examples of highByte() Function

Here’s a complete example Arduino code that demonstrates the use of the highByte() function:

void setup() {

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  // create a 16-bit integer value:
  unsigned int myValue = 0xABCD;

  // extract the high byte from the 16-bit value:
  unsigned char highByteValue = highByte(myValue);

  // print the original value and the high byte value to the serial monitor:
  Serial.print("Original value: ");
  Serial.print(myValue, HEX);
  Serial.print(", High byte value: ");
  Serial.println(highByteValue, HEX);

  // wait for 1 second before repeating the loop:
  delay(1000);


}

In this example, the setup() function initializes serial communication with a baud rate of 9600. The loop() function creates a 16-bit integer value myValue, extracts its high byte using the highByte() function, and then prints both the original value and the high byte value to the serial monitor using the Serial.print() function. The delay() function is used to pause the loop for 1 second before repeating.

When you upload this code to an Arduino board and open the serial monitor, you should see the original value 0xABCD and the high byte value 0xAB printed repeatedly, once every second.

Conclusion

The highByte() function is an essential function in Arduino that allows the user to extract the most significant byte from a 16-bit variable. It is useful when dealing with 16-bit variables, such as ADC values, PWM values, and timer values. For more detail on highByte() function parameter, return value and syntax read the article.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.