PDA

View Full Version : EEPROM write with USB



awdgsx
- 4th April 2012, 19:15
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..


<TBODY>
<CODE class="vb comments">' Name : EE.pbp </CODE>

</TBODY>

<TBODY>
<CODE>02</CODE>
<CODE class="vb comments">' Compiler : PICBASIC PRO Compiler 2.6 </CODE>

</TBODY>


<TBODY>
<CODE>03</CODE>
<CODE class="vb comments">' Assembler : PM or MPASM </CODE>

</TBODY>


<TBODY>
<CODE>04</CODE>
<CODE class="vb comments">' Target PIC : 12F, 16F or 18F types </CODE>

</TBODY>


<TBODY>
<CODE>05</CODE>
<CODE class="vb comments">' Hardware : Non specific </CODE>

</TBODY>


<TBODY>
<CODE>06</CODE>
<CODE class="vb comments">' Oscillator : internal or external </CODE>

</TBODY>


<TBODY>
<CODE>07</CODE>
<CODE class="vb comments">' Keywords : READ, SEROUT, WRITE </CODE>

</TBODY>


<TBODY>
<CODE>08</CODE>
<CODE class="vb comments">' Description : PICBASIC PRO program to demonstate the EEPROM, READ and WRITE Commands. </CODE>

</TBODY>


<TBODY>
<CODE>09</CODE>
<CODE class="vb comments">' Works on targets with data EE only!!! Initialized address 0..3 and 8. Writes 10..63. </CODE>

</TBODY>


<TBODY>
<CODE>10</CODE>
<CODE class="vb comments">' </CODE>

</TBODY>


<TBODY>
<CODE>11</CODE>
<CODE class=spaces> </CODE>

</TBODY>


<TBODY>
<CODE>12</CODE>
<CODE class="vb keyword">Include</CODE> <CODE class="vb string">"modedefs.bas"</CODE> <CODE class="vb comments">' Include serial modes </CODE>

</TBODY>


<TBODY>
<CODE>13</CODE>
<CODE class=spaces> </CODE>

</TBODY>


<TBODY>
<CODE>14</CODE>
<CODE class="vb plain">SO </CODE><CODE class="vb keyword">Con</CODE> <CODE class="vb plain">0 </CODE><CODE class="vb comments">' Define serial output pin </CODE>

</TBODY>


<TBODY>
<CODE>15</CODE>
<CODE class="vb plain">B0 </CODE><CODE class="vb keyword">Var</CODE> <CODE class="vb keyword">byte</CODE>

</TBODY>


<TBODY>
<CODE>16</CODE>
<CODE class="vb plain">B1 </CODE><CODE class="vb keyword">Var</CODE> <CODE class="vb keyword">byte</CODE>

</TBODY>


<TBODY>
<CODE>17</CODE>
<CODE class="vb plain">B2 </CODE><CODE class="vb keyword">Var</CODE> <CODE class="vb keyword">byte</CODE>

</TBODY>


<TBODY>
<CODE>18</CODE>
<CODE class=spaces> </CODE>

</TBODY>


<TBODY>
<CODE>19</CODE>
<CODE class="vb keyword">EEPROM</CODE> <CODE class="vb plain">[</CODE><CODE class="vb string">"wxyz"</CODE><CODE class="vb plain">] </CODE><CODE class="vb comments">' EEPROM[0..3] = 119..122 </CODE>

</TBODY>


<TBODY>
<CODE>20</CODE>
<CODE class="vb keyword">EEPROM</CODE> <CODE class="vb plain">8,[100] </CODE><CODE class="vb comments">' EEPROM[8] = 100 </CODE>

</TBODY>


<TBODY>
<CODE>21</CODE>
<CODE class=spaces> </CODE>

</TBODY>


<TBODY>
<CODE>22</CODE>
<CODE class="vb plain">mainloop: </CODE>

</TBODY>


<TBODY>
<CODE>23</CODE>
<CODE class=spaces> </CODE><CODE class="vb plain">B0 = 63 </CODE><CODE class="vb comments">' Set Size of EEPROM </CODE>

</TBODY>


<TBODY>
<CODE>24</CODE>
<CODE class=spaces> </CODE>

</TBODY>


<TBODY>
<CODE>25</CODE>
<CODE class=spaces> </CODE><CODE class="vb keyword">For</CODE> <CODE class="vb plain">B1 = 10 </CODE><CODE class="vb keyword">To</CODE> <CODE class="vb plain">B0 </CODE><CODE class="vb comments">' Check WRITE Command </CODE>

</TBODY>


<TBODY>
<CODE>26</CODE>
<CODE class=spaces> </CODE><CODE class="vb plain">B2 = B1 + 100 </CODE><CODE class="vb comments">' EEPROM[10..63] = 110..163 </CODE>

</TBODY>


<TBODY>
<CODE>27</CODE>
<CODE class=spaces> </CODE><CODE class="vb keyword">Write</CODE> <CODE class="vb plain">B1,B2 </CODE>

</TBODY>


<TBODY>
<CODE>28</CODE>
<CODE class=spaces> </CODE><CODE class="vb keyword">Next</CODE> <CODE class="vb plain">B1 </CODE>

</TBODY>


<TBODY>
<CODE>29</CODE>
<CODE class=spaces> </CODE>

</TBODY>


<TBODY>
<CODE>30</CODE>
<CODE class=spaces> </CODE><CODE class="vb keyword">For</CODE> <CODE class="vb plain">B1 = 0 </CODE><CODE class="vb keyword">To</CODE> <CODE class="vb plain">B0 </CODE><CODE class="vb comments">' Check READ Command </CODE>

</TBODY>


<TBODY>
<CODE>31</CODE>
<CODE class=spaces> </CODE><CODE class="vb keyword">Read</CODE> <CODE class="vb plain">B1,B2 </CODE><CODE class="vb comments">' Dump EEPROM Contents </CODE>

</TBODY>


<TBODY>
<CODE>32</CODE>
<CODE class=spaces> </CODE><CODE class="vb keyword">Serout</CODE> <CODE class="vb plain">SO,N2400,[#B2,</CODE><CODE class="vb string">" "</CODE><CODE class="vb plain">] </CODE>

</TBODY>


<TBODY>
<CODE>33</CODE>
<CODE class=spaces> </CODE><CODE class="vb keyword">Next</CODE> <CODE class="vb plain">B1 </CODE>

</TBODY>


<TBODY>
<CODE>34</CODE>
<CODE class=spaces> </CODE>

</TBODY>


<TBODY>
<CODE>35</CODE>
<CODE class=spaces> </CODE><CODE class="vb keyword">Serout</CODE> <CODE class="vb plain">SO,N2400,[10,10] </CODE><CODE class="vb comments">' Skip 2 Lines </CODE>

</TBODY>


<TBODY>
<CODE>36</CODE>
<CODE class=spaces> </CODE><CODE class="vb keyword">Goto</CODE> <CODE class="vb plain">mainloop </CODE><CODE class="vb comments">' Forever </CODE>

</TBODY>


<TBODY>
<CODE>37</CODE>
<CODE class=spaces> </CODE>

</TBODY>


<TBODY>
<CODE>38</CODE>
<CODE class=spaces> </CODE><CODE class="vb keyword">End</CODE>

</TBODY>



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:

awdgsx
- 4th April 2012, 20:19
I apologize.. some of my code comments need updating.

awdgsx
- 5th April 2012, 15:39
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

awdgsx
- 5th April 2012, 15:49
Sorry for not using "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,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

mackrackit
- 6th April 2012, 00:03
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...

awdgsx
- 7th April 2012, 15:35
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.

mackrackit
- 8th April 2012, 07:22
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/showthread.php?t=1078&highlight=write+array+to+eeprom
http://www.picbasic.co.uk/forum/showthread.php?t=8876&highlight=write+array+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.

awdgsx
- 11th April 2012, 03:03
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!

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

Demon
- 11th April 2012, 04:33
'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

mackrackit
- 11th April 2012, 13:23
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

Demon
- 11th April 2012, 23:21
Just make sure you don't have a big pause after USB started, probably going to lose connection.

Robert

mackrackit
- 12th April 2012, 00:20
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.

awdgsx
- 19th April 2012, 16:12
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?


' 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,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

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

awdgsx
- 25th April 2012, 16:58
Any ideas?