+ Reply to Thread
Results 1 to 14 of 14
  1. #1
    Join Date
    Jun 2011
    Posts
    19

    Default EEPROM write with USB

    I'm trying to figure out how to do a complete 256 byte eeprom write and dump with usb. any help would be appreciated..

    here is the code I found for doing it with serial..
    ' Name : EE.pbp
    02 ' Compiler : PICBASIC PRO Compiler 2.6

    03 ' Assembler : PM or MPASM

    04 ' Target PIC : 12F, 16F or 18F types

    05 ' Hardware : Non specific

    06 ' Oscillator : internal or external

    07 ' Keywords : READ, SEROUT, WRITE

    08 ' Description : PICBASIC PRO program to demonstate the EEPROM, READ and WRITE Commands.

    09 ' Works on targets with data EE only!!! Initialized address 0..3 and 8. Writes 10..63.

    10 '

    11

    12 Include "modedefs.bas" ' Include serial modes

    13

    14 SO Con 0 ' Define serial output pin

    15 B0 Var byte

    16 B1 Var byte

    17 B2 Var byte

    18

    19 EEPROM ["wxyz"] ' EEPROM[0..3] = 119..122

    20 EEPROM 8,[100] ' EEPROM[8] = 100

    21

    22 mainloop:

    23 B0 = 63 ' Set Size of EEPROM

    24

    25 For B1 = 10 To B0 ' Check WRITE Command

    26 B2 = B1 + 100 ' EEPROM[10..63] = 110..163

    27 Write B1,B2

    28 Next B1

    29

    30 For B1 = 0 To B0 ' Check READ Command

    31 Read B1,B2 ' Dump EEPROM Contents

    32 Serout SO,N2400,[#B2," "]

    33 Next B1

    34

    35 Serout SO,N2400,[10,10] ' Skip 2 Lines

    36 Goto mainloop ' Forever

    37

    38 End
    Here is my code so far. The program takes a 0-5V input, converts it to a flow with a program memory lookup table. will eventually do some math to the flow value, then takes the flow value and converts it back to AD counts in an eeprom lookup table for an external DAC.

    I would like to send letters to the pic from a VB program telling it to either write eeprom, read eeprom, send flow value, send v out value, etc.
    Include "cdc_desc.bas" ' Include the HID descriptors
    Define OSC 48
    define ADC_BITS 10
    Define ADC_clock 6
    Define ADC_sampleus 3
    ' initial EEPROM values (flow to dac counts out)
    DATA WORD 0,WORD 16,WORD 32,WORD 48,WORD 64,WORD 80,WORD 96,WORD 112,WORD 128
    DATA WORD 144,WORD 160,WORD 176,WORD 192,WORD 208,WORD 224,WORD 240,WORD 256
    DATA WORD 272,WORD 288,WORD 304,WORD 320,WORD 336,WORD 352,WORD 368,WORD 384
    DATA WORD 400,WORD 416,WORD 432,WORD 448,WORD 464,WORD 480,WORD 496
    DATA WORD 512,WORD 528,WORD 544,WORD 560,WORD 576,WORD 592,WORD 608,WORD 624,WORD 640
    DATA WORD 656,WORD 672,WORD 688,WORD 704,WORD 720,WORD 736,WORD 752,WORD 768
    DATA WORD 784,WORD 800,WORD 816,WORD 832,WORD 848,WORD 864,WORD 880,WORD 896
    DATA WORD 912,WORD 928,WORD 944,WORD 960,WORD 976,WORD 992,WORD 1008
    DATA WORD 1024,WORD 1024
    'eeprom 230, [254,0,210,0,190,0,100,0,60,0,40,0,30,0,0,0] 'temp out table word size ohm out values
    'eeprom 244, [147,147,130,130,125,125,125,120,120,115,115,115,11 5,115,115,115] 'desired afr table
    'eeprom 250, [122,0] 'displacement scalar
    'eeprom 252, [ 50] 'min load for auto tune
    eeprom 253, [ 100,0] 'tube size scalar (tube area used vs tube size used for orginal calculations)



    buffer Var Byte[16]
    cnt Var Byte[16]
    mafin var word
    mafout var byte[2]
    maf0 var byte
    maf1 var byte
    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
    flow var word
    inputtable con ext
    mafscalar var word
    hzout var word
    flowinval var word
    flowinval2 var word
    flowinval3 var word
    flowinval4 var word
    flowfinal var word
    ' OSCTUNE.6 = 1 ' Enable PLL for 18F87J50 family
    Pause 10
    TRISC = %11000000
    ADCON2 = %10000000

    ANSELH = %00000010

    USBInit ' Get USB going
    startloop:
    Adcin 9, mafin
    READ 253, mafscalar

    goto overdata
    '0-5v hpx-e transfer function in 2.9 tube
    asm
    inputtable
    DW 0, 0, 0, 0, 0, 7, 12, 16, 20, 24, 28, 33, 39, 45, 53, 62, 72, 83, 94, 107
    DW 121, 136, 152, 169, 188, 208, 229, 252, 277, 304, 332, 363, 395, 430, 468
    DW 507, 549, 594, 641, 691, 743, 798, 855, 915, 977, 1043, 1110, 1181, 1255
    DW 1331, 1410, 1493, 1579, 1668, 1761, 1858, 1960, 2065, 2175, 2289, 2408, 2532
    DW 2659, 2791, 2925, 2925
    endasm
    overdata:

    usbservice
    tempval = mafin >> 3 ' shift input for table lookup
    tempval2 = tempval & %1111111111111110 ' clear last bit for WORD lookup
    'get table high and low values
    READcode (inputtable+tempval2+2),tablehigh.BYTE0
    READcode (inputtable+tempval2+3),tablehigh.BYTE1
    READcode (inputtable+tempval2),tablelow.BYTE0
    READcode (inputtable+tempval2+1),tablelow.BYTE1
    'Table Interpolation
    tempval = mafin & %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
    flow = tempval >> 4 'divide by table spacing
    IF tablehigh >= tablelow THEN ' offset table value by calculated
    flow = flow + tablelow ' interpolation value
    ELSE '
    flow = tablelow - flow '
    ENDIF '
    flowsub:
    usbservice
    'mafscalar = mafscalar/100
    ' flow = flow*mafscalar 'multiply flow value by maf housing size scalar

    flowfinal = flow
    if flowfinal > 511 then goto mafouthighloop
    mafoutlowloop:
    flowinval = flowfinal /16 ' shift input for table lookup take 10 bit flow number change to 0-64 number of memory locations
    flowinval2 = tempval & %1111111111111110 ' clear last bit for WORD lookup
    'get table high and low values
    READ flowinval2+2,tablehigh.BYTE0
    READ flowinval2+3,tablehigh.BYTE1
    READ flowinval2,tablelow.BYTE0
    READ flowinval2+1,tablelow.BYTE1
    'Table Interpolation
    flowinval = flowfinal & %0000000000001111 ' get interpolation value add 15 to adval
    IF tablehigh >= tablelow THEN ' get space between table values
    flowinval2 = tablehigh - tablelow '
    ELSE '
    flowinval2 = tablelow - tablehigh '
    ENDIF '
    flowinval = flowinval2 * tempval 'multiply high/low diff by interp value
    outputval = flowinval /16 'divide by table spacing 8
    IF tablehigh >= tablelow THEN ' offset table value by calculated
    outputval = outputval + tablelow ' interpolation value
    ELSE '
    outputval = tablelow - outputval '
    goto outputsub
    endif
    mafouthighloop: '
    usbservice
    flowinval3 = flowfinal /128 ' shift input for table lookup take 14 bit mafin number change to 131-261 number of eeprom locations
    flowinval4 = flowinval & %1111111111111110 ' clear last bit for WORD lookup
    'get table high and low values
    READ flowinval4+61+2,tablehigh.BYTE0
    READ flowinval4+61+3,tablehigh.BYTE1
    READ flowinval4+61,tablelow.BYTE0
    READ flowinval4+61+1,tablelow.BYTE1
    'Table Interpolation
    flowinval3 = flowfinal & %0000000001111111 ' get interpolation value add 127 to adval
    IF tablehigh >= tablelow THEN ' get space between table values
    flowinval4 = tablehigh - tablelow '
    ELSE '
    flowinval4 = tablelow - tablehigh '
    ENDIF '
    outputval = flowinval4 * flowinval3 'multiply high/low diff by interp value
    outputval = tempval /128 'divide by table spacing
    IF tablehigh >= tablelow THEN ' offset table value by calculated
    outputval = outputval + tablelow ' interpolation value
    ELSE '
    outputval = tablelow - outputval '
    endif
    outputsub:
    usbservice
    mafout[0] = outputval.byte1
    mafout[1] = outputval.byte0
    hzout = 10
    HPWM 1,127,hzout
    ' Wait for USB input
    idleloop:
    high portc.3
    USBService ' Must service USB regularly
    cnt = 16 ' Specify input buffer size
    USBIn 3, buffer, cnt, startloop
    ' Message received

    outloop:
    Last edited by awdgsx; - 4th April 2012 at 18:18.

  2. #2
    Join Date
    Jun 2011
    Posts
    19

    Default Re: EEPROM write with USB

    I apologize.. some of my code comments need updating.

  3. #3
    Join Date
    Jun 2011
    Posts
    19

    Default Re: EEPROM write with USB

    Cleaned up the code and the comments a little..

    ' program for pic18f14k50
    Include "cdc_desc.bas" ' Include the HID descriptors
    Define OSC 48
    Define ADC_BITS 10
    Define ADC_clock 6
    Define ADC_sampleus 3
    ' initial EEPROM values (output = input)
    DATA WORD 0,WORD 16,WORD 32,WORD 48,WORD 64,WORD 80,WORD 96,WORD 112,WORD 128
    DATA WORD 144,WORD 160,WORD 176,WORD 192,WORD 208,WORD 224,WORD 240,WORD 256
    DATA WORD 272,WORD 288,WORD 304,WORD 320,WORD 336,WORD 352,WORD 368,WORD 384
    DATA WORD 400,WORD 416,WORD 432,WORD 448,WORD 464,WORD 480,WORD 496
    DATA WORD 512,WORD 539,WORD 566,WORD 593,WORD 620,WORD 647,WORD 674,WORD 701,WORD 727
    DATA WORD 754,WORD 781,WORD 808,WORD 835,WORD 862,WORD 889,WORD 916,WORD 943
    DATA WORD 970,WORD 997,WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024
    DATA WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024
    DATA WORD 1024
    EEPROM 230, [254,0,210,0,190,0,100,0,60,0,40,0,30,0,0,0] 'temp out table word size ohm out values
    'eeprom 244, [147,147,130,130,125,125,125,120,120,115,115,115,11 5,115,115,115] 'desired afr table
    'eeprom 250, [122,0] 'displacement scalar
    'eeprom 252, [ 50] 'min load for auto tune
    eeprom 253, [ 64] 'tube size scalar tube area
    'eeprom 255,[ 254] 'autotune check bit


    BUFFER Var Byte[16]
    CNT Var Byte[16]
    MAFIN var word ' 10 bit a/d input value
    MAFOUT var byte[6] ' string for usb debugging
    OUTPUTVAL VAR WORD ' output value to DAC
    TEMPVAL VAR WORD ' input temporary
    TABLEHIGH VAR WORD ' high table value
    TABLELOW VAR WORD ' low table value
    TEMPVAL2 VAR WORD ' input temporary #2
    FLOW var word ' flow value from input lookup table
    INPUTTABLE con ext
    MAFSCALAR var word ' scalar for input flow value correction
    FLOWINVAL var word ' output temporary
    FLOWINVAL2 var word ' output temporary #2
    FLOWFINAL var word ' flow value after mafscalar correction

    TRISC = %11000000 'make port c6,7 input make port c 012345 input
    ADCON2 = %10000000

    ANSELH = %00000010

    USBInit ' Get USB going
    HIGH PORTC.2 ' Serial D-A chip select off
    startloop:
    Adcin 9, mafin ' take value from an9 place it in mafin
    READ 253, mafscalar ' read eeprom value at location 253 place it in mafscalar

    goto overdata
    '0-5v input sensor transfer function in 2.9 tube adc counts to flow
    asm
    inputtable
    DW 0, 0, 0, 0, 0, 7, 12, 16, 20, 24, 28, 33, 39, 45, 53, 62, 72, 83, 94, 107
    DW 121, 136, 152, 169, 188, 208, 229, 252, 277, 304, 332, 363, 395, 430, 468
    DW 507, 549, 594, 641, 691, 743, 798, 855, 915, 977, 1043, 1110, 1181, 1255
    DW 1331, 1410, 1493, 1579, 1668, 1761, 1858, 1960, 2065, 2175, 2289, 2408, 2532
    DW 2659, 2791, 2925, 2925
    endasm
    overdata:

    usbservice
    tempval = mafin >> 3 ' shift input for table lookup devide by 8
    tempval2 = tempval & %1111111111111110 ' clear last bit for WORD lookup
    'get table high and low values
    READcode (inputtable+tempval2+2),tablehigh.BYTE0
    READcode (inputtable+tempval2+3),tablehigh.BYTE1
    READcode (inputtable+tempval2),tablelow.BYTE0
    READcode (inputtable+tempval2+1),tablelow.BYTE1
    'Table Interpolation
    tempval = mafin & %0000000000001111 ' get interpolation value add 1 less than table spacing to mafin 15
    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
    flow = tempval >> 4 'divide by table spacing 16
    IF tablehigh >= tablelow THEN ' offset table value by calculated
    flow = flow + tablelow ' interpolation value
    ELSE '
    flow = tablelow - flow '
    ENDIF '
    flowsub:
    usbservice
    mafscalar = mafscalar/64
    flow = flow * mafscalar 'multiply flow value by maf housing size scalar
    flowfinal = flow
    if flowfinal >= 511 then goto mafouthighloop ' splint flow to ad counts conversion between
    ' two tables with different spacing for resolution
    mafoutlowloop:
    flowinval = flowfinal /8 ' shift input for table lookup take 9 bit flow number change to 0-64 number of memory locations
    flowinval2 = flowinval & %1111111111111110 ' clear last bit for WORD lookup
    'get table high and low values
    READ flowinval2+2,tablehigh.BYTE0
    READ flowinval2+3,tablehigh.BYTE1
    READ flowinval2,tablelow.BYTE0
    READ flowinval2+1,tablelow.BYTE1
    'Table Interpolation
    flowinval = flowfinal & %0000000000000111 ' get interpolation value add 7 to adval
    IF tablehigh >= tablelow THEN ' get space between table values
    flowinval2 = tablehigh - tablelow '
    ELSE '
    flowinval2 = tablelow - tablehigh '
    ENDIF '
    flowinval = flowinval2 * flowinval 'multiply high/low diff by interp value
    outputval = flowinval /8 'divide by table spacing 8
    IF tablehigh >= tablelow THEN ' offset table value by calculated
    outputval = outputval + tablelow ' interpolation value
    ELSE '
    outputval = tablelow - outputval '
    endif
    goto outputsub
    mafouthighloop: '
    usbservice
    flowinval = flowfinal /64 ' shift input for table lookup take 14 bit mafin number change to 56-120 number of eeprom locations
    flowinval2 = flowinval & %1111111111111110 ' clear last bit for WORD lookup
    ' 'get table high and low values
    READ flowinval2+56+2,tablehigh.BYTE0 ' values start at 56 instead of 65 because 512/64 is 8
    READ flowinval2+56+3,tablehigh.BYTE1
    READ flowinval2+56,tablelow.BYTE0
    READ flowinval2+56+1,tablelow.BYTE1
    'Table Interpolation
    flowinval = flowfinal & %0000000000111111 ' get interpolation value add 63 to adval
    IF tablehigh >= tablelow THEN ' get space between table values
    flowinval2 = tablehigh - tablelow '
    ELSE '
    flowinval2 = tablelow - tablehigh '
    ENDIF '
    flowinval = flowinval2 * flowinval 'multiply high/low diff by interp value
    outputval = flowinval /64 'divide by table spacing 64
    IF tablehigh >= tablelow THEN ' offset table value by calculated
    outputval = outputval + tablelow ' interpolation value
    ELSE '
    outputval = tablelow - outputval '
    endif
    outputsub:
    usbservice
    mafout[0] = mafin.byte1 'place variables into usb buffer string
    mafout[1] = mafin.byte0
    mafout[4] = outputval.byte1
    mafout[5] = outputval.byte0
    mafout[2] = flowfinal.byte1
    mafout[3] = flowfinal.byte0

    HPWM 1,127,outputval ' can't figure out how to snd values lower than 2.9hz
    ' Wait for USB input
    tempval = outputval << 2 ' shift output left 2 bits(LTC1661 DtoA)
    tempval = tempval + %1001000000000000 ' add control code(load DAC A)
    LOW PortC.2 ' Enable DtoA chip
    Shiftout PORTC.0,PORTC.1,1,[tempval\16] ' Serial output to DtoA conv
    High PORTC.2 ' Deselect DtoA chip


    idleloop:
    USBService ' Must service USB regularly
    cnt = 16 ' Specify input buffer size
    USBIn 3, buffer, cnt, startloop
    ' Message received

    outloop:
    USBService ' Must service USB regularly
    USBOut 3, mafout, 6, outloop

    Goto startloop
    Last edited by awdgsx; - 5th April 2012 at 14:46.

  4. #4
    Join Date
    Jun 2011
    Posts
    19

    Default Re: EEPROM write with USB

    Sorry for not using "code"

    Code:
    ' program for pic18f14k50
     Include "cdc_desc.bas" ' Include the HID descriptors
     Define  OSC     48
     Define  ADC_BITS 10
     Define  ADC_clock 6
     Define  ADC_sampleus 3
    ' initial EEPROM values (output = input)
    DATA WORD 0,WORD 16,WORD 32,WORD 48,WORD 64,WORD 80,WORD 96,WORD 112,WORD 128
    DATA WORD 144,WORD 160,WORD 176,WORD 192,WORD 208,WORD 224,WORD 240,WORD 256
    DATA WORD 272,WORD 288,WORD 304,WORD 320,WORD 336,WORD 352,WORD 368,WORD 384
    DATA WORD 400,WORD 416,WORD 432,WORD 448,WORD 464,WORD 480,WORD 496
    DATA WORD 512,WORD 539,WORD 566,WORD 593,WORD 620,WORD 647,WORD 674,WORD 701,WORD 727
    DATA WORD 754,WORD 781,WORD 808,WORD 835,WORD 862,WORD 889,WORD 916,WORD 943
    DATA WORD 970,WORD 997,WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024
    DATA WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024
    DATA WORD 1024
    EEPROM 230, [254,0,210,0,190,0,100,0,60,0,40,0,30,0,0,0] 'temp out table word size ohm out values
    'eeprom 244, [147,147,130,130,125,125,125,120,120,115,115,115,115,115,115,115] 'desired afr table
    'eeprom 250, [122,0] 'displacement scalar
    'eeprom 252, [ 50] 'min load for auto tune
    eeprom 253, [ 64] 'tube size  scalar  tube area
    'eeprom 255,[ 254] 'autotune check bit
     
     
    BUFFER Var Byte[16]
    CNT Var Byte[16]
    MAFIN var word      ' 10 bit a/d input value
    MAFOUT var byte[6]  ' string for usb debugging
    OUTPUTVAL VAR WORD  ' output value to DAC
    TEMPVAL VAR WORD    ' input temporary
    TABLEHIGH VAR WORD  ' high table value
    TABLELOW VAR WORD   ' low table value
    TEMPVAL2 VAR WORD   ' input temporary #2
    FLOW var word       ' flow value from input lookup table
    INPUTTABLE con ext
    MAFSCALAR var word  ' scalar for input flow value correction
    FLOWINVAL var word  ' output temporary
    FLOWINVAL2 var word ' output temporary #2
    FLOWFINAL var word  ' flow value after mafscalar correction
     
    TRISC = %11000000 'make port c6,7 input make port c 012345 input
    ADCON2 = %10000000
    
    ANSELH = %00000010
    
     USBInit   ' Get USB going
     HIGH PORTC.2      ' Serial D-A chip select off
    startloop:
     Adcin 9, mafin  ' take value from an9 place it in mafin
     READ 253, mafscalar  ' read eeprom value at location 253 place it in mafscalar
    
     goto overdata
    '0-5v input sensor transfer function in 2.9 tube adc counts to flow
    asm
    inputtable
     DW 0, 0, 0, 0, 0, 7, 12, 16, 20, 24, 28, 33, 39, 45, 53, 62, 72, 83, 94, 107
     DW 121, 136, 152, 169, 188, 208, 229, 252, 277, 304, 332, 363, 395, 430, 468
     DW 507, 549, 594, 641, 691, 743, 798, 855, 915, 977, 1043, 1110, 1181, 1255
     DW 1331, 1410, 1493, 1579, 1668, 1761, 1858, 1960, 2065, 2175, 2289, 2408, 2532
     DW 2659, 2791, 2925, 2925
    endasm
    overdata:
     
     usbservice
     tempval = mafin >> 3    ' shift input for table lookup devide by 8
     tempval2 = tempval & %1111111111111110 ' clear last bit for WORD lookup
        'get table high and low values
     READcode (inputtable+tempval2+2),tablehigh.BYTE0
     READcode (inputtable+tempval2+3),tablehigh.BYTE1
     READcode (inputtable+tempval2),tablelow.BYTE0
     READcode (inputtable+tempval2+1),tablelow.BYTE1
        'Table Interpolation
     tempval = mafin & %0000000000001111 ' get interpolation value add 1 less than table spacing to mafin 15
     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
     flow = tempval >> 4            'divide by table spacing 16
     IF tablehigh >= tablelow THEN       ' offset table value by calculated
     flow = flow + tablelow    ' interpolation value
     ELSE                                '
     flow = tablelow - flow    '
     ENDIF                               '
    flowsub:
     usbservice
     mafscalar = mafscalar/64
     flow = flow * mafscalar 'multiply flow value by maf housing size scalar
     flowfinal = flow
     if flowfinal >= 511 then goto mafouthighloop ' splint flow to ad counts conversion between
                                                     ' two tables with different spacing for resolution
    mafoutlowloop:
     flowinval = flowfinal /8 ' shift input for table lookup take 9 bit flow number change to 0-64 number of memory locations
     flowinval2 = flowinval & %1111111111111110 ' clear last bit for WORD lookup
        'get table high and low values
     READ flowinval2+2,tablehigh.BYTE0
     READ flowinval2+3,tablehigh.BYTE1
     READ flowinval2,tablelow.BYTE0
     READ flowinval2+1,tablelow.BYTE1
        'Table Interpolation
     flowinval = flowfinal & %0000000000000111 ' get interpolation value add 7 to adval
     IF tablehigh >= tablelow THEN       ' get space between table values
     flowinval2 = tablehigh - tablelow     '
     ELSE                                '
     flowinval2 = tablelow - tablehigh     '
     ENDIF                               '
     flowinval = flowinval2 * flowinval        'multiply high/low diff by interp value
     outputval = flowinval /8            'divide by table spacing 8
     IF tablehigh >= tablelow THEN       ' offset table value by calculated
     outputval = outputval + tablelow    ' interpolation value
     ELSE                                '
     outputval = tablelow - outputval    '
     endif
     goto outputsub
    mafouthighloop:                              '
     usbservice
     flowinval = flowfinal /64 ' shift input for table lookup take 14 bit mafin number change to 56-120 number of eeprom locations
     flowinval2 = flowinval & %1111111111111110 ' clear last bit for WORD lookup
    '    'get table high and low values
     READ flowinval2+56+2,tablehigh.BYTE0  ' values start at 56 instead of 65 because 512/64 is 8
     READ flowinval2+56+3,tablehigh.BYTE1
     READ flowinval2+56,tablelow.BYTE0
     READ flowinval2+56+1,tablelow.BYTE1
        'Table Interpolation
     flowinval = flowfinal & %0000000000111111 ' get interpolation value add 63 to adval
     IF tablehigh >= tablelow THEN       ' get space between table values
     flowinval2 = tablehigh - tablelow     '
     ELSE                                '
     flowinval2 = tablelow - tablehigh     '
     ENDIF                               '
     flowinval = flowinval2 * flowinval        'multiply high/low diff by interp value
     outputval = flowinval /64           'divide by table spacing 64
     IF tablehigh >= tablelow THEN       ' offset table value by calculated
     outputval = outputval + tablelow    ' interpolation value
     ELSE                                '
     outputval = tablelow - outputval    '
     endif
    outputsub:
     usbservice
     mafout[0] = mafin.byte1     'place variables into usb buffer string
     mafout[1] = mafin.byte0
     mafout[4] = outputval.byte1
     mafout[5] = outputval.byte0
     mafout[2] = flowfinal.byte1
     mafout[3] = flowfinal.byte0
        
     HPWM  1,127,outputval  ' can't figure out how to snd values lower than 2.9hz
    ' Wait for USB input
     tempval = outputval << 2   ' shift output left 2 bits(LTC1661 DtoA)
     tempval = tempval + %1001000000000000   ' add control code(load DAC A)
     LOW PortC.2     ' Enable DtoA chip
     Shiftout PORTC.0,PORTC.1,1,[tempval\16] ' Serial output to DtoA conv
     High PORTC.2    ' Deselect DtoA chip
     
    
    idleloop:
     USBService  ' Must service USB regularly
     cnt = 16  ' Specify input buffer size
     USBIn 3, buffer, cnt, startloop
    ' Message received
    
    outloop:
     USBService  ' Must service USB regularly
     USBOut 3, mafout, 6, outloop
        
     Goto startloop

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,822

    Default Re: EEPROM write with USB

    I would make it easier (for me anyway) to help if you broke the problem down, approach it block by block.

    Are you able to send data to the MCU?

    Or is the problem with the EEPROM.

    I have not studied you code...
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Jun 2011
    Posts
    19

    Default Re: EEPROM write with USB

    I can send and receive data to the mcu. I'm using the usbout command for debugging right now. It sends 3 16 bi twords in one buffer to hyperterminal right now. I just can't figure out how to send a specific value to the mcu and have it wait for 256 byte of data and rewrite the contents of the eeprom with those values. I would also lite to dump the contents of the mcu eeprom back to my pc.

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,822

    Default Re: EEPROM write with USB

    To start with, the DATA and EEPROM commands only work at compile time. Use READ and WRITE during run time.

    Concept to receive data via USB then WRITE to eeprom.

    Send a small array to the MCU then check the array for a specific value.
    If the value is X the jump to a WHILE or DO loop.
    Wait for data to be recieved that you want to WRITE to the eeprom.
    Use a FOR/NEXT loop to loop through the array received via USB, (this will be a different array/buffer than before)
    WRITING each byte of the array to the next address of the eeprom.
    When finished change the variable X to another value that do not mean GOTO the DO loop.
    RETURN to main program.

    These may help
    http://www.picbasic.co.uk/forum/show...rray+to+eeprom
    http://www.picbasic.co.uk/forum/show...rray+to+eeprom
    http://www.picbasic.co.uk/forum/showthread.php?t=544

    I am not in a place to write code at the moment but you may get the idea.
    Dave
    Always wear safety glasses while programming.

  8. #8
    Join Date
    Jun 2011
    Posts
    19

    Default Re: EEPROM write with USB

    I think this is what you intended for me to do.. I'm not sure how to advance the array location in the loop. You will see my comment in the code. Thank you again for the help!
    Code:
     usbservice
    doloop:
           inbyte = 1
           USBin 3, action, inbyte, doloop2        ' Get a char from usb port
    verify:
           IF action = 102 THEN   ' Verify connection
           USBout 3, action, 1, verify
           ENDIF    ' end of verify action
         '  IF action = 117 THEN  ' Write EEPROM
         '  USBOUT 3, action, 1, doloop2
         '  tempval = 0
    tlabel2:
         '  USBin 3, char, inbyte, tlabel2
         '  WRITE tempval, char
         '  USBout 3, char, 1, startloop
          ' tempval = tempval + 1
          ' IF tempval < 255 THEN tlabel2
         '  ENDIF ' end of Write action
    valuesout:
           IF action = 99 THEN    ' Return last input and output
           USBout 3, mafout, 6, valuesout
           ENDIF ' end of IO action
           IF action = 107 THEN   ' Dump EEPROM
           tempval = 0
    Dumploop:
           Read tempval, epromdump[0]  'epromdump is a 256 byte array
            tempval = tempval + 1 'advance to the next eeprom location 
            'not sure how to advance places in the array for the new eeprom value
            USBout 3, epromdump, 256, dumploop

  9. #9
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    1,726

    Default Re: EEPROM write with USB

    'not sure how to advance places in the array for the new eeprom value
    Check at the front of the PBP manual on arrays. You use a FOR loop to scan through the array, using a variable as position pointer.

    Have you considered using Darrel's USB interrupt routine?
    http://darreltaylor.com/DT_INTS-18/home.html

    Robert

  10. #10
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,822

    Default Re: EEPROM write with USB

    Code:
    DATA @1,$31,$32,$33,$34,$35,$36,$37,$38,$39,$30
       X = 0
       PAUSE 5000 : Pause 5000   'TIME TO START TERMINAL
    idleloopq:
    FOR X = 1 TO 10
        READ X,BUFFER
        USBOut 3, buffer, 1, idleloopq
    	PAUSE 250
    	NEXT
    	END
    Dave
    Always wear safety glasses while programming.

  11. #11
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    1,726

    Default Re: EEPROM write with USB

    Just make sure you don't have a big pause after USB started, probably going to lose connection.

    Robert

  12. #12
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,822

    Default Re: EEPROM write with USB

    Quote Originally Posted by Demon View Post
    Just make sure you don't have a big pause after USB started, probably going to lose connection.

    Robert
    I should have mentioned that my USBSERVICE is interrupt driven so PAUSE does not matter.


    Hi, we are pleased to see that you are using our forum.

    We'd like you to become a member of our community. Membership is FREE, please REGISTER and view our forums without these annoying reminders!

    Once registered you may post on the forums, download from the file areas and use the WIKI without interruption.




    Dave
    Always wear safety glasses while programming.

  13. #13
    Join Date
    Jun 2011
    Posts
    19

    Default Re: EEPROM write with USB

    Ok, I finally have it somewhat working. I can now read my variables, I can read what's programmed into the eeprom. The only problem is when I send values to my eeprom in my write loop it only programs the value of "50" to the memory. What am I missing?

    Code:
    ' program for pic18f14k50
    '                     u = Program EEPROM
    '                    k = Dump EEPROM
    '                     f = verify connection
    '                     c = send last I/O values to PC
     Include "cdc_desc.bas" ' Include the HID descriptors
     Define  OSC     48
     Define  ADC_BITS 10
     Define  ADC_clock 6
     Define  ADC_sampleus 3
    ' initial EEPROM values (output = input)
    DATA WORD 0,WORD 16,WORD 32,WORD 48,WORD 64,WORD 80,WORD 96,WORD 112,WORD 128
    DATA WORD 144,WORD 160,WORD 176,WORD 192,WORD 208,WORD 224,WORD 240,WORD 256
    DATA WORD 272,WORD 288,WORD 304,WORD 320,WORD 336,WORD 352,WORD 368,WORD 384
    DATA WORD 400,WORD 416,WORD 432,WORD 448,WORD 464,WORD 480,WORD 496
    DATA WORD 512,WORD 539,WORD 566,WORD 593,WORD 620,WORD 647,WORD 674,WORD 701,WORD 727
    DATA WORD 754,WORD 781,WORD 808,WORD 835,WORD 862,WORD 889,WORD 916,WORD 943
    DATA WORD 970,WORD 997,WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024
    DATA WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024,WORD 1024
    DATA WORD 1024
    EEPROM 230, [254,0,210,0,190,0,100,0,60,0,40,0,30,0,0,0] 'temp out table word size ohm out values
    'eeprom 244, [147,147,130,130,125,125,125,120,120,115,115,115,115,115,115,115] 'desired afr table
    'eeprom 250, [122,0] 'displacement scalar
    'eeprom 252, [ 50] 'min load for auto tune
    eeprom 253, [ 64] 'tube size  scalar  tube area
    eeprom 255,[ 254] 'autotune check bit
     
    EWRITE var byte
    EPROMDUMP var byte
    BUFFER Var Byte[16]
    CNT Var Byte[16]
    MAFIN var word      ' 10 bit a/d input value
    MAFOUT var byte[6]  ' string for usb debugging
    OUTPUTVAL VAR WORD  ' output value to DAC
    TEMPVAL VAR WORD    ' input temporary
    TABLEHIGH VAR WORD  ' high table value
    TABLELOW VAR WORD   ' low table value
    TEMPVAL2 VAR WORD   ' input temporary #2
    FLOW var word       ' flow value from input lookup table
    inputtable con ext
    MAFSCALAR var word  ' scalar for input flow value correction
    FLOWINVAL var word  ' output temporary
    FLOWINVAL2 var word ' output temporary #2
    FLOWFINAL var word  ' flow value after mafscalar correction
    ACTION var byte
    CHAR var byte
    inbyte var byte
    X var byte
    TRISC = %11000000 'make port c6,7 input make port c 012345 input
    ADCON2 = %10000000
    
    ANSELH = %00000010
    
     USBInit   ' Get USB going
     HIGH PORTC.2      ' Serial D-A chip select off
     
    startloop:
     
    
     goto overdata
    '0-5v input sensor transfer function in 2.9 tube adc counts to flow
    asm
    inputtable
     DW 0, 0, 0, 0, 0, 7, 12, 16, 20, 24, 28, 33, 39, 45, 53, 62, 72, 83, 94, 107
     DW 121, 136, 152, 169, 188, 208, 229, 252, 277, 304, 332, 363, 395, 430, 468
     DW 507, 549, 594, 641, 691, 743, 798, 855, 915, 977, 1043, 1110, 1181, 1255
     DW 1331, 1410, 1493, 1579, 1668, 1761, 1858, 1960, 2065, 2175, 2289, 2408, 2532
     DW 2659, 2791, 2925, 2925
    endasm
    overdata:
     usbservice
    doloop:
           inbyte = 1
           USBin 3, action, inbyte, doloop2        ' Get a char from serial port
    verify:
           IF action = 102 THEN   ' Verify connection
           USBout 3, action, 1, verify
           ENDIF    ' end of verify action
           IF action = 117 THEN  ' Write EEPROM
           
           X = 0
    writeloop:
          USBSERVICE
          USBin 2, EWRITE, inbyte, writeloop
          
          WRITE X, EWRITE
          USBout 2, EWRITE, 1, writeloop
          X = X+1
          IF X < 10 THEN writeloop   ' this will get changed to 255 when it works.
    USBSERVICE
          
          
           ENDIF ' end of Write action
    valuesout:
           IF action = 99 THEN    ' Return last input and output
           USBout 3, mafout, 6, valuesout
           ENDIF ' end of IO action
           IF action = 107 THEN   ' Dump EEPROM
           X = 0
    dumploop:
           
      FOR X = 1 TO 20      'this will get changed to 255 to dump the whole eeprom when it all works
        READ X, epromdump
        USBOut 3, epromdump, 1, dumploop
     pause 100
     usbservice
            NEXT
     ENDIF
    
         
    tlabel6:
         
         
         '  ENDIF ' end of Dump action

  14. #14
    Join Date
    Jun 2011
    Posts
    19

    Default Re: EEPROM write with USB

    Any ideas?

Members who have read this thread : 7

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