+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2006
    Posts
    36

    Default Noobie could use a second set of eyes pls

    Hi all,

    I still consider myself new to PicBasic Pro and in fact, to any programming, and I could really use a second set of eyes on this little piece of my project.

    I am using a 12f683, a 20 Mhz Oscillator, two separate Pots for ADC input on channel 0 (GP0) and channel 3 (GP4) and the HPWM output (GP2.)

    Below is the code, but here is the overall plan:

    1st it reads the AN3 pot to get a static variable which will ultimately be a number between 80 and 175. This will not change again while the program is "looping."

    Then it gets a varying voltage from AN0. That part works perfectly.

    I can't get AN3 to read anything other than 0 even though there is 2V there.

    Methinks my setup is wrong but to me, it seems like that is what the data sheet wants. Thanks for any input you have. I really appreciate it. Being a beginner is really HARD.

    DEFINE OSC 20 '20 Mhz clock
    DEFINE ADC_BITS 10 ' This requires a "Word" variable
    DEFINE ADC_CLOCK 3 'Uses internal RC clock
    DEFINE ADC_SAMPLEUS 20 'Microsecond sample time

    'SETUP
    '-------------------------
    TRISIO = %00011001 ' AN0 (GPIO.0) and AN3 (GPIO.4) and GPIO.3 inputs

    'Analog to Digital Converter Configuration
    ANSEL = %00001001 ' Select AN0,3 as analog input (0 Will not work on EasyPIC dev)
    ADCON0 = %10001101 ' Right justify result, AN3 selected, ADC On
    '-----------------------------------------------------

    Pauseus 10
    ' Delay to allow for VREF to settle

    '------------------------------------------------------------------

    '#### VARIABLES ######

    adval var Word ' Create adval to store result
    x var Byte ' "For" temp variable
    adavg VAR Word ' Average of the AD input
    Modifier VAR Byte 'Modifier value from above A/D conversion
    DutyCycle1 VAR WORD 'Duty Cycle 1-255 for HPWM for O2-1
    Vin1 Var word

    'Temp for DIV32 math
    dc1tmp VAR WORD '

    '-------------------------------------------------------------------
    'Begin the program
    x=0 'Re-Set the initial variables and get ADC for modifier value
    adavg = 0 '(80%-175%)
    adval= 0

    '1st get the modifier value
    Repeat
    ADCIN 3 , adval ' Read channel 3 to adval
    adavg = adavg + adval 'Keep adding readings together
    x = x +1 'Increment temporary "x" variable
    until x = 31 'Until we add 8 samples
    adavg = adavg/32 'Then Average the readings of 32 samples
    Modifier = adavg/4 'Modifier becomes the averaged sample as 8 bits

    if Modifier > 175 then
    Modifier = 175 'Don't go over 175%
    endif

    If Modifier < 80 then 'Don't go under 80%
    Modifier = 80
    endif

    If Modifier <128 then 'test to see what it thinks
    gpio.1 = 1
    endif

    'Test
    'Modifier = 150

    ADCON0 = %10000001
    'Get the O2 Voltage and repeat infinitely.
    GetADC:
    x=0 'Re-Set the initial variables and get ADC
    adavg = 0
    adval= 0
    dutycycle1=0


    'Read Vin1 Input variable
    Repeat
    ADCIN 0 , adval 'Read channel 0 to adval
    adavg = adavg + adval 'Keep adding readings together
    x = x +1 'Increment temporary "x" variable
    until x = 31 'Until we add 32 samples
    adavg = adavg/32 'Then Average the readings of 32 samples
    Vin1 = adavg 'Vin1 becomes the averaged sample (10 bits!)

    if Vin1 < 5 then
    Vin1=5
    endif 'Keeps the math minimums tolerable to division


    'CREATE THE DUTY CYCLE
    'This math takes the sampled voltage and modifies the sample
    'which takes a DIV32 command.
    dc1tmp = Vin1 * modifier 'Increase (modify) the "sample" of the V
    DutyCycle1 = DIV32 352 'Bring it back to a byte size 0-255
    ' The discrepancy between this and 400 is experimental
    ' Trial and error made this correct Voltaqes in reality


    'Output a corresponding PWM (modified) relative to the voltage input
    'Send Pulse
    HPWM 1,DutyCycle1, 20000 'Send PWM at 20K out GPIO.2 (Pin 5)

    pause 10 'For some reason, this keep things more stable. No speed issues.

    goto GetADC 'Do it forever
    Last edited by ChrisHelvey; - 18th June 2009 at 22:59.

  2. #2
    Join Date
    Jul 2006
    Posts
    36

    Default

    DUH. I am trying to use AN3 with an external crystal! They share the same pin.

    Nevermind........

Similar Threads

  1. Pin won't stay high during Repeat...Until loop??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th August 2009, 23:57
  2. Interruptus Frustratus
    By Byte_Butcher in forum General
    Replies: 16
    Last Post: - 17th April 2009, 20:36
  3. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  4. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  5. error on compiling
    By parker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th June 2005, 14:31

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts