Shop OBEX P1 Docs P2 Docs Learn Events
Revisting old project (Converting from spin to C) — Parallax Forums

Revisting old project (Converting from spin to C)

Greg LaPollaGreg LaPolla Posts: 320
edited 2014-12-22 17:47 in Propeller 1
I have an old project that I did several years ago. I am in the process of converting from spin to C and a new PCB layout..

I am having a bit on an issue getting my thermistor to work. When I hold the thermistor in my hand, I can see the ADCVal go down and the ohms go up. however the temperature reading also goes down as I heat up the thermistor.

I think this is backwards.

I used a tlv2543C previously, but am swapping it out for a MCP3204 to save a little space on the pcb as I only use 3 channels.

The thermistor is Negative Temperature Coefficient. I have a 10K Resister and a .01uf capacitor between the two wires from the thermistor ( See attached image). There is also a 1k resistor between the thermistor and the ADC.

I am using some code I found on the forum to drive the 3204 it is here: http://forums.parallax.com/showthread.php/157659-SimpleIDE-C-sample-code-for-ADC-MCP3204

Here is the code and formulas I converted from spin.
#include "simpletools.h"                      // Include simple tools
#include "mcp3204.h"                          // Include ADC Driver
#include <math.h>

#define ADC_cs 24                             // ADC CS pin
#define ADC_clk 27                            // ADC CLK pin
#define ADC_do 26                             // ADC Data out
#define ADC_di 25                             // ADC Data in

#define Coeff1 0.0009461261                 // Steinhart Constant                                        
#define Coeff2 0.00022226704                // Steinhart Constant                                 
#define Coeff3 0.000000105745286            // Steinhart Constant
#define R1 10000.0                                  



int main()                                    // Main function
{
  int ADCVal;
  ADCVal = 0;
  double temp;
  double ohms;
  double volts;
  volts = 0;
  ohms = 0;
  temp = 0;
  
  while(1)
  {
     pause(1000);                              
     ADCVal = readADCAverage(0, ADC_di, ADC_do, ADC_clk, ADC_cs, 10);
     
      
      print("adcval = %d%c\n",ADCVal, CLREOL); 
      
      ohms = ((4096.0 * R1) / ADCVal) - R1;
      print("ohms = %.1f\n", ohms, CLREOL);
    
      volts = ADCVal * 3.3 / 4096.0;
      print("volts = %f\n", volts, CLREOL);
      
      temp = 1 / ( Coeff1 + ( Coeff2 * log(ohms) ) + ( Coeff3 * pow(log(ohms),3)) );
      temp = temp - 273.15;
      temp = temp * 1.8 + 32.0;
      print("Temp = %.1f\n", temp, CLREOL);
                                                    
      print("%c", HOME);                       
      pause(100);                              
    
  }  
}

Screen Shot 2014-12-21 at 9.27.33 PM.png

Comments

  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-12-21 22:26
    It could be quite simple. Try spin2cpp

    https://code.google.com/p/spin2cpp/
  • DavidZemonDavidZemon Posts: 2,973
    edited 2014-12-22 06:58
    Spin2cpp is certainly a valid option.

    Another option - one that I'd love to help you with - is using PropWare. I use a MCP3008 for my own tinkering and recently modified my object to be compatible with the entire MCP3xxx range of ADCs. I don't have a built-in "readADCAverage" function. But a rolling-average buffer is something I've been very keen on creating so I'd be happy to get that knocked out for you.

    If this piques your interest, feel free to poke at the links in my signature and send me any questions.
  • jazzedjazzed Posts: 11,803
    edited 2014-12-22 07:37
    0.02 ....

    It's better just to convert code by hand.

    If people really need to use spin2cpp, the current github repository should be used. Spin2cpp forum thread.
    Another option is to use the spin code "as is" in propeller-gcc with spinwrap. Spinwrap forum thread.

    Propeller libraries propware and libpropeller are viable options that don't get enough attention.
  • Greg LaPollaGreg LaPolla Posts: 320
    edited 2014-12-22 14:53
    I would rather just convert my code by hand. I seem to have somehow made it work backwards. I was hoping it was something simple and easily spotted by another pair of eyes.
  • TorTor Posts: 2,010
    edited 2014-12-22 15:12
    Well.. it works exactly as coded. But what does the spin version look like?

    -Tor
  • DavidZemonDavidZemon Posts: 2,973
    edited 2014-12-22 17:47
    I'm not noticing any big errors. Let's definitely have a look at the original Spin.
Sign In or Register to comment.