Pages: [1]   Go Down
Author Topic: thermometer with binary output (LM35) (fixed)  (Read 698 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sinces nobody understud what i meant the firs time, im going to rewrite the topic:
 
 The LM35, is a termic sensor that lets 10mV pass tru it for each 1ºC
if the sensor is put in a place where the temperature is 25ºC, hes analogic output will be set to 250mV (not sure if realy millivolts)

what i need is, using this sensor, i want to convert this analog imput into a digital output (A/D conversor)
lets say that i use the digital outputs from a arduino from 0 down to 7, and the temperature is 23ºC

23 to binary => 00010011,
so the output should be:
L, L, L, H, L, L, H and H
(L = low, H = High).
if 25ºC: 00010101
and so on


srry for bad inglish smiley-roll-blue
Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1330
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



 I guess you are needing help getting a code started to light the LEDs?

Here is on part of a thread about doing a bit shift to light LEDs. Here is the important part that was submitted by CrossRoads :


Quote
I was assuming there is some clever command that passes an 8 bit integer to 8 defined output pins, "

If you had all 8 output on the same Port, you could use
PORTD = newCanByte;
for example using PORTD pins.
With an Uno, only PortD has 8 pins and if you are using Serial than 0,1 are committed.

So put your pins in an array
Code:

pinsArray[] = {2,3,4,5,6,7,8,9;}  // could be whatever group you select


and loop thru them to write each one
Code:

maskBit = 1; // start with B00000001
for (x=0; x<8; x=x+1){
if ((CANbyte & maskBit) >0){           // makes all bits but 1 low: 0000000C, next pass 000000C0, etc
digitalWrite (pinsArray
  • ,  HIGH);
}
else {
digitalWrite (pinsArray
  • , LOW);
}
maskBit = maskBit<1;  // next bit - B00000010, B00000100, B00001000, etc up to B10000000
} // next x

Logged


Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Fairly simple:

Code:
#define Vref 5000ul //Vref = 5000mv
#define ADC2MV(adc) ((Vref * (adc)) >> 10)  //turn 10-bit adc to mv
#define readLM35adc(pin) analogRead(pin) //read lm35, in adc counts
#define readLM35mv(pin) ADC2MV(readLM35adc(pin)) //read lM35, in mv
#define readLM35C(pin) (readLM35mv(pin) / 10) //read lm35, in degrees C

  ..
  tmp = readLM35C(LM35_0);  //read LM35_0 into tmp, in degrees


Logged

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello and welcome,

Maybe something like that?

Code:
int8_t temperature = 23;
uint8_t pins[] = { 7, 6, 5, 4, 3, 2, 1, 0 };
...
for ( int8_t i = 7; i >= 0; i-- )
    digitalWrite( pins[i], (temperature & (1 << i)) >> i );

This will (should) write:
0 to pin 0
0 to pin 1
0 to pin 2
1 to pin 3
0 to pin 4
1 to pin 5
1 to pin 6
1 to pin 7
 
(Edit: fixed a little mistake!)
« Last Edit: November 25, 2012, 01:16:25 am by guix » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thx all of you
that solved the problem  smiley
Logged

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, you might be interested by this other solution:
Code:
uint8_t temperature = 23;

//invert bit order
temperature = ( (temperature * 0x0802UL & 0x22110UL) | (temperature * 0x8020UL & 0x88440UL) ) * 0x10101UL >> 16;

// here is a test for Arduino Mega:
// set pins 54 to 61 (analog inputs 0 to 7) as digital outputs
DDRF = B11111111;

// set value of pins 54 to 61
PORTF = temperature;

// verify
for (uint8_t i = 54; i <= 61; i++)
  Serial.println( digitalRead( i ) );
prints:
Code:
0
0
0
1
0
1
1
1

So if your digital pins 0 to 7 are on the same port (probably PORTD if you don't have a Arduino Mega), then this solution is for you!
« Last Edit: November 25, 2012, 01:19:00 am by guix » Logged

Pages: [1]   Go Up
Jump to: