Swordfish temperature display

Status
Not open for further replies.

edeca

Active Member
Thanks for all the help yesterday, 2 hours work today has got me further than ever with a PIC. Once again, Swordfish is pretty awesome!

Below is a program that uses a Microchip MCP9700 sensor on RA2 with a 2.56V voltage reference on Vref+. Google doesn't think many people seem to use these sensors but they are pretty standard, 10mV/C. 0 degrees C is 500mV, commented in the code.

I can read the ADC and convert to a positive temperature fine. However I ran into a few problems, if anybody can answer I'd be really grateful.

1) Setting ADCON0 works for Vref+ but setting the individual bits doesn't, is there a reason for this? Purely out of curiosity!

Code:
// Works:
ADCON0 = $40

// Doesn't work:
VCFG1 = 0
VCFG0 = 1

2) What's the best way to incorporate negative temperatures? I guess it should be possible to calculate +ve or -ve in the same way but I can't figure it at the minute. My temperature calculation probably isn't the best.

3) Is it easy to display the decimal portion? I can't think how to calculate it, but 0.25 degrees resolution should be possible with the full 10 bits.

Any general pointers for improving my code would also be good.

Code:
Device = 18F1320
Clock = 8

Config OSC = INTIO2
Config MCLRE = ON

// some LCD options...
#option LCD_DATA = PORTB.4
#option LCD_RS = PORTA.0
#option LCD_EN = PORTA.1

Include "IntOSC8.bas"
Include "LCD.bas"
Include "ADC.bas"
Include "convert.bas" 
Include "utils.bas"

Dim ADVal As Word
Dim Temp As Byte
Dim LED As PORTB.1

Function ADInAsVolt() As Word
   result = (ADC.Read(2) + 1) * 500 / 1024
End Function

SetAllDigital

// External 2.56V voltage reference on Vref+
ADCON0 = $40

// Enable RA2 as analogue input
PCFG2 = 0
TRISA.2 = 1
ADCON1.2 = 1

// Write static text, this is never overwritten
LCD.Cls
LCD.WriteAt(1,1,"A/D Value:")
LCD.WriteAt(2,1,"     Temp:")

While True
    ADVal = ADC.Read(2)

    // Write the A/D value to the LCD
    LCD.MoveCursor(1,12)
    LCD.Write(DecToStr(ADVal, 4, "0"))
    
    // Write the temperature
    LCD.MoveCursor(2,12)

    // If it's <200, it's negative
    //  -> Sensor has 10mV/degree C resolution, ADC has 2.5mV per step
    //  -> 500mV is 0 degrees C
    //  -> 500 / 2.5 = ADC value 200
    if (adval < 200) then
        LCD.write("-ve! ")
    else
        LCD.write("+")    
        Temp = (ADVal - 200) / 4
        LCD.Write(DecToStr(Temp, 2, " "), 223, "C")
    endif

    
    Toggle(LED)
    DelayMS(500)
Wend
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…