hello
I have a simple question:
In this portion of an Arduino code.
{int analogRead(uint8_t pin)
uint8_t low, high;}
I can not understand this statement {uint8_t low, high;}
how we can give a value for a type configuration (uint8_t)?!
thanx
hello
I have a simple question:
In this portion of an Arduino code.
{int analogRead(uint8_t pin)
uint8_t low, high;}
I can not understand this statement {uint8_t low, high;}
how we can give a value for a type configuration (uint8_t)?!
thanx
the statement
uint8_t low, high;
defines two variables high and low of type uint8_t
have a look at the complete source code of wiring_analog.c
https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/wiring_analog.c
where the last statement of the function is
return (high << 8) | low;
which forms the int function result from the values of the high and low bytes
In this is a portion from an Arduino code.
{int analogRead(uint8_t pin)
uint8_t low, high;}
What a very strange portion of code.
oh yah
I thought that LOW and HIGH are values not variables, because I used them as constant values in arduino. programming.
horace and AWOL thanx for replying.
Fatima_H:
I thought that LOW and HIGH are values not variables, because I used them as constant values in arduino. programming.
You're absolutely right, HIGH and LOW are values (through a macro), high and low are not
But yeah, the code being confusing is just a part of why the code is weird...
septillion, thanx for your explanation
I would like to ask you a second related question:
in this code (http://garretlab.web.fc2.com/en/arduino/inside/arduino/wiring_analog.c/analogRead.html):
int analogRead(uint8_t pin)
{
uint8_t low, high;
if (pin >= 14) pin -= 14; // allow for channel or pin numbers
// set the analog reference (high two bits of ADMUX) and select the
// channel (low 4 bits). this also sets ADLAR (left-adjust result)
// to 0 (the default).
ADMUX = (analog_reference << 6) | (pin & 0x07);
// start the conversion
sbi(ADCSRA, ADSC);
// ADSC is cleared when the conversion finishes
while (bit_is_set(ADCSRA, ADSC));
// we have to read ADCL first; doing so locks both ADCL
// and ADCH until ADCH is read. reading ADCL second would
// cause the results of each conversion to be discarded,
// as ADCL and ADCH would be locked when it completed.
low = ADCL;
high = ADCH;
// combine the two bytes
return (high << 8) | low;
}
ADCH contains 8 bits, when we shift it by 8 bits it become 0000 0000.
I think that we should shift it by 6 bits to get 10 bit resolution.
can you explain me the issue, please?
thanx a lot
ADCH is shifted by 8 bits because ADCL is 8 bits. If you shifted ADCH by only 6 bits, then when it was combined with ADCL with the bitwise OR operation (|) then the two least significant bits of ADCH would combine with the two most significant bits of ADCL in an incorrect way.
It's always nice when code is smiling at me... But I hope you now see why we ask people to use code-tags in How to use the forum.
But why are you messing with the core parts of the IDE?
But no, ADCH contains 8 bits indeed but only 2 useful bits (bits 8 bit 9), the two MSB's. ADCH contains 8 bits of which all 8 bits are useful and the LSB part. So to make room for those 8 bits, we have to shift high 8 places. And I must say, I didn't expected it either but apparently it's promoted to a bigger variable.
And for others, what did surprise me even more, why does
Serial.println((0xFF << 8), BIN);
Print: 11111111111111111111111100000000 ???
Why does it become a uint32_t / (unsigned) long and more important, why are the two higher bytes set?
I don't know why it becomes a long rather than an int. I can think of a reason why the upper two bytes are filled with ones. It's so that a signed int is correctly represented as a signed long. I guess the compiler assumes the values are signed.
Mm, weird.
Serial.println(0xFFu << 8, BIN);
Indeed prints just
1111111100000000
But
uint8_t test = 0xFF;
Serial.println(test << 8, BIN);
Prints
11111111111111111111111100000000
I have no idea why the compiler thinks that should be signed...
PaulRB & septillion, thanx for replying
YOU answered my question and I hope to find someone who answer your question.
I need fast answer so I didn't read how to use this forum, I just post my question and reply your replays
may read it next time.
thanx
Fatima_H:
may read it next time.
MUST read it next time
OK, I will (Inshaallah)