A to D to A help


Results 1 to 8 of 8

Threaded View

  1. #1
    Join Date
    Jun 2011
    Posts
    19

    Default A to D to A help

    Im new at this so please be gentle. I'm using a pic185252 and a ltc1661 d to a.

    The circuit needs to take a voltage reading from a flow sensor sample that

    voltage by a set number of samples (16,32,64 etc.. this number will be chosen in

    a varible with the inital flash of the mcu) take that voltage go to a 256 point

    lookup table in the flash to convert this voltage reading to a flow rate number

    this flow rate number will then be multiplied by a scalar (variable in inital flash)

    this flow number will then be used with a 64point lookup table in the eeprom (64

    ponts set with inital flash) to convert it back to a different voltage..

    the first table will be 256 points from 0-6 volts

    the second table will be 64 points with flow values from 0 to 388 kg/hr, 0 to 777

    kg/hr, 0 to 1555 kg/hr, 0 to 2332 kg/hr, 0 to 3110, and 0 to 3888. the range will

    be selected with a variable in the inital flash. the table will convert these flows

    back to voltages.

    all the tables will need interpolation.

    My friend helped me write a simple code to just do a direct voltage conversion but i think I need to rework the whole thing for what i'm trying to do now..

    here is the code for the first one
    Code:
    ' Uses 18F252 and 20MHz external clock.
    ' The lookup table contains 65 values, from 0 to 6V input.
    ' communicates with PC through serial port. 
    ' Has an added 32 sample average on the input.
    
    
    
    Define OSC 20   ' use 20 Mhz clock
    
    Define  ADC_BITS     10
    Define  ADC_CLOCK    3
    Define  ADC_SAMPLEUS 24
    
    ' initial EEPROM values (output = input) 
    EEPROM [216,0,217,0,219,0,223,0,229,0,237,0,246,0,0,1,12,1,_
              22,1,30,1,42,1,55,1,69,1,86,1,104,1,_
              124,1,143,1,163,1,184,1,202,1,219,1,237,1,254,1,15,2,_
              32,2,49,2,66,2,82,2,98,2,114,2,130,2]         
    EEPROM 128,[146,2,161,2,177,2,192,2,207,2,222,2,237,2,252,2,11,3,_
              26,3,40,3,55,3,70,3,84,3,99,3,113,3,_
              128,3,142,3,156,3,170,3,183,3,197,3,210,3,223,3,236,3,_
              249,3,255,3,255,3,255,3,255,3,255,3,255,3,0,4]
              
    adval VAR WORD      ' analog input value
    outputval VAR WORD  ' output value
    tempval VAR WORD    ' temporary
    tablehigh VAR WORD  ' high table value
    tablelow VAR WORD   ' low table value
    tempval2 VAR WORD   ' temporary #2
    avgarray VAR WORD[32]' array for averaging input samples
    countnum VAR byte   ' integer for counting sample array
    arraysum VAR WORD   ' summing variable for average
    
    TRISA = 255         ' portA => input
    ADCON1 = %10000010  ' Set PORTA to analog and PORTE to digital
    
    TRISB = %00000000 ' Port B set for output
    HIGH PORTB.2      ' Serial D-A chip select off
    countnum = 0
    clearloop:          ' zero out array
    if countnum != 32 then
        avgarray[countnum]=0
        countnum = countnum + 1
        goto clearloop
    endif
    countnum = 0
    arraysum = 0
    
    loop:  ' start of main loop
        
        Adcin 0, adval
        arraysum = arraysum - avgarray[countnum]  'subtract value at countnum
        avgarray[countnum] = adval                'store new value in array 
        arraysum = arraysum + adval               'add to sum value
        adval = arraysum >> 5                     'divide by 32
        countnum = countnum + 1
        if countnum = 32 then
        countnum = 0
        endif
        
        tempval = adval >> 3    ' shift input for table lookup
        tempval2 = tempval & %1111111111111110 ' clear last bit for WORD lookup
        
        'get table high and low values
        READ tempval2+2,tablehigh.BYTE0
        READ tempval2+3,tablehigh.BYTE1
        READ tempval2,tablelow.BYTE0
        READ tempval2+1,tablelow.BYTE1
        
        'Table Interpolation
        tempval = adval & %0000000000001111 ' get interpolation value
        IF tablehigh >= tablelow THEN       ' get space between table values
        tempval2 = tablehigh - tablelow     '
        ELSE                                '
        tempval2 = tablelow - tablehigh     '
        ENDIF                               '
        tempval = tempval2 * tempval        'multiply high/low diff by interp value
        outputval = tempval >> 4            'divide by table spacing
        
        IF tablehigh >= tablelow THEN       ' offset table value by calculated
        outputval = outputval + tablelow    ' interpolation value
        ELSE                                '
        outputval = tablelow - outputval    '
        ENDIF                               '
        
        tempval = outputval << 2   ' shift output left 2 bits(LTC1661 DtoA)
        tempval = tempval + %1001000000000000   ' add control code(load DAC A)
        LOW PortB.2     ' Enable DtoA chip
        Shiftout PORTB.0,PORTB.1,1,[tempval\16] ' Serial output to DtoA conv
        High PORTB.2    ' Deselect DtoA chip
        
        goto loop       ' repeat main loop "forever"
    
    End
    Last edited by mackrackit; - 22nd June 2011 at 21:42. Reason: added code tags

Members who have read this thread : 0

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

Posting Permissions

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