# absolute beginer question -- converting input bits into integer

i’m trying to understand the arduino library for max6675 ic used in thermocouple measurement.

`````` for (int i=11; i>=0; i--) {
digitalWrite(_SCK_pin,HIGH);
digitalWrite(_SCK_pin,LOW);
}
``````

the value is declared as unit16_t

can anyone tell me what is the for loop doing ?

if i assume that input data from SO_pin is 000000000001 (Lsb is 0 and Msb is 1)

in the ending of for loop did value hold bit array or equivalent uint_t number?

is there anyway to do the above code in different way

and here’s the c source of library

``````/*
MAX6675.cpp - Library for reading temperature from a MAX6675.

*/

#include <MAX6675.h>

MAX6675::MAX6675(uint8_t CS_pin, uint8_t SO_pin, uint8_t SCK_pin, uint8_t units)
{
pinMode(CS_pin, OUTPUT);
pinMode(SO_pin, INPUT);
pinMode(SCK_pin, OUTPUT);

digitalWrite(CS_pin, HIGH);

_CS_pin = CS_pin;
_SO_pin = SO_pin;
_SCK_pin = SCK_pin;
_units = units;
}

{
uint16_t value = 0;
uint8_t error_tc = 0;
float temp = 0.0;

/*
Initiate a temperature conversion. According to MAX's tech notes FAQ's
for the chip, Line going high initiates a conversion, which means, we
need to clock the chip low to high to initiate the conversion, then wait
for the conversion to be complete before trying to read the data from
the chip.
*/
digitalWrite(_CS_pin,LOW);
delay(2);
digitalWrite(_CS_pin,HIGH);
delay(220);

/* Read the chip and return the raw temperature value */

/*
Bring CS pin low to allow us to read the data from
the conversion process
*/
digitalWrite(_CS_pin,LOW);

/* Cycle the clock for dummy bit 15 */
digitalWrite(_SCK_pin,HIGH);
delay(1);
digitalWrite(_SCK_pin,LOW);

/*
Read bits 14-3 from MAX6675 for the Temp. Loop for each bit reading
the value and storing the final value in 'temp'
*/
for (int i=11; i>=0; i--) {
digitalWrite(_SCK_pin,HIGH);
digitalWrite(_SCK_pin,LOW);
}

/* Read the TC Input inp to check for TC Errors */
digitalWrite(_SCK_pin,HIGH);
digitalWrite(_SCK_pin,LOW);

/*
Read the last two bits from the chip, faliure to do so will result
in erratic readings from the chip.
*/
for (int i=1; i>=0; i--) {
digitalWrite(_SCK_pin,HIGH);
delay(1);
digitalWrite(_SCK_pin,LOW);
}

// Disable Device
digitalWrite(_CS_pin, HIGH);

/*
Keep in mind that the temp that was just read is on the digital scale
from 0˚C to 1023.75˚C at a resolution of 2^12.  We now need to convert
to an actual readable temperature (this drove me nuts until I figured
this out!).  Now multiply by 0.25.  I tried to avoid float math but
it is tough to do a good conversion to ˚F.  THe final value is converted
to an int and returned at x10 power.

2 = temp in deg F
1 = temp in deg C
0 = raw chip value 0-4095
*/
if(_units == 2) {
temp = (value*0.25) * 9.0/5.0 + 32.0;
} else if(_units == 1) {
temp = (value*0.25);
} else {
temp = value;
}

/* Output negative of CS_pin if there is a TC error, otherwise return 'temp' */
if(error_tc != 0) {
return -_CS_pin;
} else {
return temp;
}
}
``````

Everytime you toggle the clock you are receiving a new bit. So as it says "Read bits 14-3 from MAX6675 for the Temp" and it is doing it by "Loop[ing] for each bit reading the value and storing the final value in 'temp' "
Its a 12 bit temperature sensor so you are receiving a 12 bit value of its reading stored in a 16 bit variable.

Why would you want to do it in a different way? And what different way were you thinking?

Ps991:
you are receiving a 12 bit value of its reading stored in a 16 bit variable.

receiving a 12 bit value like 10000000000

and assigning it to a 16 bit int variable will it save as 0000100000000000
or like 1024 int value?

assigning a binary value to int value?

If you know about data types, you know that there is 8 bit, 16 bit, 32 bit variables.
This guy chose the 16 bit data type because we dont need 32 bits, it would be a waste of valuable SRAM and speed. 8 bit is too small. So logically you will choose 16 bit, that is why you see uint16_t.

If the value received was 100000000000 then if you were to read "value" then you would get 0000100000000000.

All this guy is doing is reading 1 bit at a time and directly setting them inside "value".

Notice he starts his for loop at 11, this is the 12th bit of "value". Each time he reads a new bit from the device, he is decrementing "i" and sets "value"'s 11th bit, then the 10th bit, and so on. Bits 12-15 are not needed and ignored.

If you don't know this, then I recommend experimenting a little with bits. Run this little bit of code on any variable to get an idea of whats happening. It will show you the bits

``````//Remember to include "Serial.begin(9600);" in the setup() function
Serial.println(value, BIN);
``````

Ps991:
If you don't know this, then I recommend experimenting a little with bits. Run this little bit of code on any variable to get an idea of whats happening. It will show you the bits

Thanks for that.

i get it.. what i am confusing from the start is how can we perform << operation between Boolean and integer data type.. may be it is one of the stupid question of many i asked in this forum. thanks for ur patience..

this little code help me.

``````void setup(){
Serial.begin(9600);
int value = 0;
boolean b[12] = {1,0,0,0,0,0,0,0,0,0,0,1};
for (int i=11; i>=0; i--) {
value += b[i] << i;
Serial.print(i);
Serial.print("--->");
Serial.print(value);
Serial.println();
}
Serial.println("final --->");
Serial.println(value);
}
void loop(){
}
``````

You've declared it as "boolean" and then assigned ones and zeros to it. Which isn't really a good idea, although it works to maintain backwards compatibility with the 1970's.

So if your boolean is actually a 1, then shifting it left a bunch of times will give you 16,8,4,2 etc

A boolean should really be true, or false, or a logical condition evaluating to one of those. Assuming it is always 1 or 0 can get you into problems in some other programming languages or processor environments.

I think his question is how a boolean is being shifted.

A boolean isnt just true or false, it is represented as a 1 or 0. When you see this "value += b[ i ] << i;" we are not shifting b[ i ], we are using a temporary general purpose 16 bit register inside the processor, why 16 bit? because "value" is 16 bit.
Here is what is happening:
First, b[ i ] (0 or 1) is put into the register, which now equals, lets say, 1
Then, the register is shifted "i" times to the left, if i = 4 and the register = 1, then the result is 16.
After that, the register is added to "value"

That is what is happening, you are not in fact shifting a boolean variable...