Go Down

Topic: Displaying temperature on a MAX6951 (Read 588 times) previous topic - next topic

0miker0

Dec 17, 2012, 07:49 pm Last Edit: Dec 18, 2012, 01:19 pm by 0miker0 Reason: 1
I found code for driving the MAX6951 on this site that displays digits from the serial terminal. I've modified the code and attached a 1-wire temperature ic but the digits are pushed to the far left of the display with what was zeros to the right.  0x03 in the SCANLIMIT_ADDRESS will knock off the trailing zeros but I was wondering if the temperature can easily be pushed to the right. I believe that having SCANLIMIT_ADDRESS to 0x0F and putting blank digits in the first two spots will work but I have no idea how to do this.


Code: [Select]


/*
Using SPI for interfacing to MAX6951

- pin 13      SCK      SPI clock  ,DISPLAY CLK, PIN#2    
- pin 12      MISO     SPI master in, slave out  ,NOT USED
- pin 11      MOSI     SPI master out, slave in ,DISPLAY DIN, PIN#4
- pin 10      SS       SPI slave select, DISPLAY CS, PIN#3

The default SPI configuation is as follows:

- SPI Master enabled
- MSB of the data byte transmitted first
- SPI mode 0 (CPOL = 0, CPHA = 0)
- SPI clock frequency = system clock / 4

*/
#include <studio.h>
#include <OneWire.h>  //for DS18S20 temp sensor
#include <SPI.h>  // bring in SPI Library
// SPI uses hardware Chip Select vs having device address
#define DECODE_ADDRESS 0x01// 0x07, all 8 on
#define INTENSITY_ADDRESS 0x02 // 0x07 to start, half intensity 02
#define SCANLIMIT_ADDRESS 0x03 // 0x07, all 8 on  
#define CONFIG_ADDRESS 0x04
// 0000 0001 => 0 shutdown, 1 = normal mode, no fooling with blink modes & stuff
// 0000 0010 = not used
// 0000 0100 => 0  slow blink, 1 = fast blink
// 0000 1000 => 0 = global blink disabled (Plane 1 data ignored), 1  global blink enabled Plane 1 data used)
// 0001 0000 => 0 = blink unaffected during I2C acknowledge
// 0010 0000 => 0 = data unaffected during I2C ack, 1 = data cleared
// 0100 0000 => not used
// 1000 0000 => blank phase - blink status is read back

// for this use: 0000 0101 => normal operation, fast blink
#define DISPLAYTEST_ADDRESS 0x07 // 0x01, on, 0x00 off 07
#define DIGIT0_ADDRESS 0x20 // write Plane 0 20
#define DIGIT1_ADDRESS 0x21 // write Plane 0 21
#define DIGIT2_ADDRESS 0x22 // write Plane 0
#define DIGIT3_ADDRESS 0x23 // write Plane 0
#define DIGIT4_ADDRESS 0x24 // write Plane 0
#define DIGIT5_ADDRESS 0x25 // write Plane 0
#define DIGIT6_ADDRESS 0x26 // write Plane 0
#define DIGIT7_ADDRESS 0x27 // write Plane 0

int DS18S20_Pin = 0; //DS18S20 Signal pin on digital 0
int ColonLED=3;
const int slaveSelectPin = 10;

int led = 3;
//Temperature chip i/o
OneWire ds(DS18S20_Pin);  // on digital pin 2

void setup()
{
Serial.begin(9600); //monitor Temp
// define pins used - 13,12,11,10 defined by SPI
pinMode(ColonLED, OUTPUT);
//pinMode(SS, OUTPUT);
pinMode (slaveSelectPin, OUTPUT);

pinMode(led, OUTPUT);

// start up SPI
SPI.begin(); // nothing in () because we are the master

//  write config register  
digitalWrite(slaveSelectPin,LOW);
SPI.transfer(CONFIG_ADDRESS);  // select the Address,
SPI.transfer(0x05);      // select the data 5
digitalWrite(slaveSelectPin,HIGH); // take the SS pin high to de-select the chip:


//  write intensity register
digitalWrite(slaveSelectPin,LOW);
SPI.transfer(INTENSITY_ADDRESS);  // select the Address,
SPI.transfer(0x00);  //DIM Display  ,0x0E is full brightness


digitalWrite(slaveSelectPin,HIGH); // take the SS pin high to de-select the chip:
//Serial.println("intensity register");

// write scanlimit register
digitalWrite(slaveSelectPin,LOW); // take the SS pin low to select the chip:
SPI.transfer(SCANLIMIT_ADDRESS);  // select the Address,
SPI.transfer(0x0F);      // select the data //Was 0F
digitalWrite(slaveSelectPin,HIGH); // take the SS pin high to de-select the chip:

// write decode register
digitalWrite(slaveSelectPin,LOW); // take the SS pin low to select the chip:
SPI.transfer(DECODE_ADDRESS);  // select the Address,
SPI.transfer(0xFF);      // select the data, FF
//digitalWrite(SS,HIGH);   // take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin,HIGH);
//Serial.println("decode register");

//display test  
digitalWrite(slaveSelectPin,LOW); // take the SS pin low to select the chip:
SPI.transfer(DISPLAYTEST_ADDRESS);  // select the Address,
SPI.transfer(0x00);      // select the data,01
digitalWrite(slaveSelectPin,HIGH); // take the SS pin high to de-select the chip:
delay (1000);
}


void loop()
{  
 float temperature = getTemp();
 int t = temperature*100;  //get rid of decimel point

 int ones = t%10;
 int tens = (t/10)%10;
 int hundreds = (t/100)%10;
 int thousands = (t/1000)%10;
 write_digits(0x00,0x00,ones,tens,hundreds+0x80,thousands); //0x80 is the decimal
}

static void write_digits(int u, int v,int w,int x, int y, int z)
{
 
  digitalWrite(slaveSelectPin,LOW);
  SPI.transfer(DIGIT5_ADDRESS);
  SPI.transfer(u);      // select the data
  digitalWrite(slaveSelectPin,HIGH);
 
  digitalWrite(slaveSelectPin,LOW);
  SPI.transfer(DIGIT4_ADDRESS);
  SPI.transfer(v);      // select the data
  digitalWrite(slaveSelectPin,HIGH);
 
  digitalWrite(slaveSelectPin,LOW);
  SPI.transfer(DIGIT3_ADDRESS);
  SPI.transfer(w);      // select the data
  digitalWrite(slaveSelectPin,HIGH);
 
  digitalWrite(slaveSelectPin,LOW);
  SPI.transfer(DIGIT2_ADDRESS);  // select the Address,
  SPI.transfer(x);      // select the data
  digitalWrite(slaveSelectPin,HIGH);
 
  digitalWrite(slaveSelectPin,LOW);
  SPI.transfer(DIGIT1_ADDRESS);  // select the Address,
  SPI.transfer(y);      // select the data
  digitalWrite(slaveSelectPin,HIGH);
 
  digitalWrite(slaveSelectPin,LOW);
  SPI.transfer(DIGIT0_ADDRESS);  // select the Address,
  SPI.transfer(z);      // select the data
  digitalWrite(slaveSelectPin,HIGH);
}

float getTemp(){
 //returns the temperature from one DS18S20 in DEG Celsius

 byte data[12];
 byte addr[8];

 if ( !ds.search(addr)) {
     //no more sensors on chain, reset search
     ds.reset_search();
     return -1000;
 }

 if ( OneWire::crc8( addr, 7) != addr[7]) {
     Serial.println("CRC is not valid!");
     return -1000;
 }

 if ( addr[0] != 0x10 && addr[0] != 0x28) {
     Serial.print("Device is not recognized");
     return -1000;
 }

 ds.reset();
 ds.select(addr);
 ds.write(0x44,1); // start conversion, with parasite power on at the end

 byte present = ds.reset();
 ds.select(addr);    
 ds.write(0xBE); // Read Scratchpad

 
 for (int i = 0; i < 9; i++) { // we need 9 bytes
   data[i] = ds.read();
 }
 
 ds.reset_search();
 
 byte MSB = data[1];
 byte LSB = data[0];

 float tempRead = ((MSB << 8) | LSB); //using two's compliment
 float TemperatureSum = tempRead / 16;
 
 float test = ((MSB << 8) | LSB);
 return TemperatureSum;
}



PaulS

At 77 posts, you REALLY should know how to post code properly. Modify your post, and do it right. The # icon is there for a reason. Use it!

0miker0

I have corrected post by posting code the proper way. Thanks PaulS.


PaulS

Quote
with what was zeros to the right.

What are they now?

Quote
I believe that having SCANLIMIT_ADDRESS to 0x0F and putting blank digits in the first two spots will work but I have no idea how to do this.

So, experiment. Just set SCANLIMIT_ADDRESS to 0x0F and make no other changes. What happens?

A link to the device in question would be useful.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy