EEPROM write with USB


Results 1 to 14 of 14

Threaded View

  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.

Members who have read this thread : 1

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