PDA

View Full Version : A/D problems



Roddy Wayne
- 4th June 2009, 01:26
'************************************************* ***************
'* Name : New_PWM.bas *
'* Author : Roddy Wayne *
'* Notice : Copyright (c) 2009 *
'* : All Rights Reserved *
'* Date : 05/09/2009 *
'* Version : 1.0 *
'* Notes: *
'* *
'* *
'************************************************* ***************
'
' Hi All,
'
'The problem I'm having is: The input to A2D 0, is somehow effecting
'the input to A2D 1. A2D 0 is reading a Pot's Position. A2D 1 is
'reading an analog voltage set up by an LM34 Temperature unit.
'
'The Temp unit works fine (A2D 1)until I increase the Pot's Position
'going into A2D 0. Doing this is causing the the Temperature to
'rise up to 33 degrees.
'
'Don't know what I've done wrong but, I'm sure there's something
'I've missed!
'
'I'd be greatful for any help!
'
'Thank You!
'
'Roddy
'
'
'
'
'
'
'
'
'
'************************************************* ***************

CLEAR ;always start with clear
DEFINE OSC 4 ;define oscillator speed
DEFINE ADC_BITS 8 ; Sets number of bits in result
DEFINE ADC_CLOCK 3 ; Sets clock source (3=rc)
DEFINE ADC_SAMPLEUS 400 ; Sets sampling time in uS
define DEBUG_REG PORTC
DEFINE DEBUG_BIT 6
define DEBUG_BAUD 2400
DEFINE DEBUG_MODE 0
DEFINE DEBUG_PACING 8000
Low PORTE.2 ;makes low for write only
TRISA = %00000111 ;sets porta 0, 1, & 2, as an inputs (A to D's)
TRISB = %00111111 ;sets bits 6 & 7 portb pins as outputs, 0 thru 5 as inputs
TRISC = %10111111 ;sets port c.6 for serial output, rest to inputs
TRISD = %00000011 ;sets PORTD 0,1, lines to inputs, rest to outputs
TRISE = %00000000 ;sets all PORTE lines to output
potpos var BYTE ;VAR BYTE for Pot Position
celltemp VAR BYTE ;VAR BYTE for Cell Temperature
A2D_V VAR BYTE ; Create A2D_V Value to store potpos result
A2D_V2 Var BYTE ;Create A2D_V2 Value to store cellamps result
ADCON1=%00000000 ;Sets the Analog to Digital control register


Start:



If portc.0 = 0 then portb.6 = 0 'If pin 15 is low, make pin 39 low
If Portc.0 = 1 then portb.6 = 1 'If pin 15 is high, make pin 39 high
'When Pin 39 goes high, this turns the
'pump on! I'm using a Darlington pair
'to do this (Part # TIP130) It also
'turns on the "Pump On" LED.
If portc.3 = 0 then portb.7 = 0 'If pin 18 is low, make pin 40 low
If portc.3 = 1 then portb.7 = 1 'If pin 18 is high, make pin 40 high
'When Pin 40 goes high, the "Low Tank
'Water" LED is turned on!


debug 1, 14 'Send curser home & Turn Backlight on
Pause 100 'Pause .1 second

HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
ADCON0=%11001101 'Sets up the AtoD Module to read Channel 1
ADCIN 1, A2D_V2 'Read channel 1 to A2D_V2 Value
celltemp= 1+(((300-70)*A2D_V2)/255*2)
DEBUG "Cell Temp = ", DEC3 celltemp," ", 223, 70," "
DEBUG "Cell Temp = ", BIN8 celltemp
Pause 200
ADCON0=%11000101 'Sets Up the AtoD Module to read channel 0
ADCIN 0, A2D_V ' Read channel 0 to A2D_V Value
potpos= 1+(((255-1)*a2d_v)/255)
Debug "Pot Position = ", DEC3 potpos," "

Pause 200
If (potpos <=%00001111)and(potpos <=%00011011) then percent_20 '15 to 27
Pause 200


If (potpos <=%00011100)and(potpos <=%00111111) then percent_40 '28 to 63
Pause 200


If (potpos <=%01000000)and(potpos <=%01011001) then percent_60 '64 to 89

Pause 200


If (potpos <=%01011010)and(potpos <=%01110011) then percent_80 '90 to 115

Pause 200


If (potpos <=%01110100)and(potpos <=%01111110) then percent_100 '116 to 126
If (potpos >=%01011110) Then percent_100




percent_20: 'Asking for 20% high PWM
HPWM 1, 51, 17500 'Output is a 17.5 Khz, 20% duty cycle (pin 17)
HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
DEBUG "PWM = 20%High/80%Low"
Pause 100
GoTO start



percent_40: 'Asking for 40% high PWM
HPWM 1, 102, 17500 'Output is a 17.5 Khz, 40% duty cycle (pin 17)
HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
DEBUG "PWM = 40%High/60%Low"
Pause 100
GoTO start



percent_60: 'Asking for 60% high PWM
HPWM 1, 153, 17500 'Output is a 17.5 Khz, 60% duty cycle (pin 17)
HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
DEBUG "PWM = 60%High/40%Low"
Pause 100
GoTO start



percent_80: 'Asking for 80% high PWM
HPWM 1, 204, 17500 'Output is a 17.5 Khz, 80% duty cycle (pin 17)
HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
DEBUG "PWM = 80%High/20%Low"
Pause 100
GoTO start



percent_100: 'Now at 100% on PWM
HPWM 1, 254, 17500 'Output is a 17.5 Khz, 100% duty cycle (pin 17)
HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
DEBUG "PWM = 99%High/01%Low"
Pause 100
GoTO start

End

Darrel Taylor
- 4th June 2009, 02:57
You have an ADCON0 assignment just prior to each ADCIN statement.
ADCON0=%11001101 'Sets up the AtoD Module to read Channel 1
ADCIN 1, A2D_V2 'Read channel 1 to A2D_V2 Value

That will change the channel and start the conversion immediately without allowing for acquisition time.
This leaves the voltage from the previous channel in the sample&hold capacitor.

The ADCIN statement handles ADCON0 for you, so Removing the ADCON0 lines should help.

You can also reduce the ADC_SAMPLEUS 400 to around 50 or less.
<br>