
Originally Posted by
mackrackit
Maybe this will get you going with the ADC on this chip. Seems like MicroChip likes to keep folks guessing by changing some things around for different chips.
This will display the 8 bit ADC reading on a terminal.
Thanks I'll try it tomorrow. I just needed to get away from it for awhile.
I started over from scratch this morning and finally got it working. Only thing is that it reads exactly half the temperature (input voltage from the LM34). I multiplied it by 2 in the software to fix the problem but I don't know why it does that. Is there a calibration for the A/D?
Anyway here is my final working code for anyone interested. It could probably be optimized but it ain't broke so I ain't gonna fix it.
I built this to control a solar heater (see it on my web site). It basically reads the voltage from the LM34 on GPIO.0 10mv per deg F and
turns on a fan relay on GPIO.1 when the temperature goes above the preset temp (110F) and outputs a PWM signal on GPIO.2 to open a damper door.
Below the preset low temperature (90F) it turns off the fan and again sends out a PWM signal to close the damper. I still need to tweak pulse widths for the servo. I just set them up to make sure the PWM works.
It also sends out the current temperature and preset info on GPIO.4 to a PC terminal if needed using the debug command.
Hope someone else can use the info and thanks to Bruce for making the basics available.
Code:
'Modified from file found at Renolds Electronics
'http://www.rentron.com/PIC12C67XAD.htm
'LM34 F temperature sensor
'PIN NAME Purpose
' 1 VDD +5v
' 2 Output 10mv/degF, +5° to +300°F = 50mv-3000mv
' 3 VSS GND
'
'PIC12F675
' PIN NAME Purpose
' 1 VDD +5v
' 8 VSS GND
' 3 GPIO.4 Serial out for debug
' 5 GPIO.2 PWM out to servo
' 6 GPIO.1 Output to NPN transistor sinking relay.
' 7 GPIO.0 A/D input from center leg of LM34
'********************************* Fuses ******************************
@ device pic12F675, intrc_osc_noclkout, wdt_off, pwrt_on, mclr_off, bod_off, protect_off
'********************************* Setup *******************************
DEFINE debug_mode 1 '0=True, 1=Inverted serial data
DEFINE debug_bit 4 'sends serial data out GPIO.4 at N,8,1
DEFINE debug_baud 2400 'Default baud rate = 2400
DEFINE osc 4 'We're using a 4 MHz oscillator
Define OSCCAL_1K 1 'Set OSCCAL for 1K device
DEFINE ADC_BITS 8 'Set A/D for 8-bit operation
DEFINE ADC_SAMPLEUS 50 'Set A/D sampling time @ 50 uS
CMCON=7 '(%00000111) Turn off analog comparators
TRISIO=%001001 'Port direction register. GPIO.0 & GPIO.3 =inputs (.3 is always input)
ADCON0=%00000000 'Left justified,Vref = Vdd,GP.0=ANALOG INPUT
ANSEL=%01010001 'FOSC=16=101,GPIO.0=analog
Samples VAR WORD ' Multiple A/D sample accumulator
Sample VAR BYTE ' Holds number of Samples to take
Temp VAR BYTE ' Temperature storage
PWMPin var GPIO.2 'Variable for PWM output
Relay var GPIO.1 'Variable for relay output
'LowTemp var byte
'HighTemp var byte
LowTemp con 90 'Turn off relay
HighTemp con 110 'Turn on relay
Samples = 0 ' Clear Samples accumulator on power-up
PAUSE 500 ' Wait .5 second
'********************************* Main ********************************
loop:
FOR sample = 1 TO 20 ' Take 20 Samples
ADCIN 0, temp ' Read channel 0 into temp variable
Samples = Samples + temp ' Accumulate 20 Samples
PAUSE 250 ' Wait approximately 1/4 seconds per loop
NEXT sample
temp = Samples/20*2
if temp >lowtemp and temp>hightemp then gosub onrelay
if temp <=lowtemp then gosub offrelay
DEBUG "Current Temp: ",DEC temp," Low Turn Off: ",dec lowtemp," High Turn On: ",dec hightemp,10,13
Samples = 0 ' Clear old sample accumulator
GOTO loop ' Do it forever
END
'******************************* Output **********************************
OnRelay:
high relay 'Turns on GPIO.1
PWM PWMPin, 127, 500 '50% duty cycle for 500 cycles on GPIO.2
return
OffRelay:
low relay 'Turns off GPIO.1
PWM PWMPin, 64, 500 '25% duty cycle for 500 cycles on GPIO.2
return
Bookmarks