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