If I use shiftin or a similar routine it works fine. Here is my shiftin clone. Why won't HARDWARE SPI work ?
Might it be because I connected the SPI's SDO (output) to the ADC CS line ? (see first post, first attachment)
Code:DEFINE OSC 20 DEFINE LCD_DREG PORTD' Set starting Data bit (0 or 4) if 4-bit bus DEFINE LCD_DBIT 4' Set LCD Register Select port DEFINE LCD_RSREG PORTD' Set LCD Register Select bit DEFINE LCD_RSBIT 0' Set LCD Enable port DEFINE LCD_EREG PORTD' Set LCD Enable bit DEFINE LCD_EBIT 2' Set LCD bus size (4 or 8 bits) DEFINE LCD_BITS 4' Set number of lines on LCD DEFINE LCD_LINES 2' Set command delay time in us DEFINE LCD_COMMANDUS 3000' Set data delay time in us DEFINE LCD_DATAUS 100 ' -----[ Variables ]------------------------------------------------------- one var byte two var byte three var byte result var word a_mux var porta.1 b_mux var porta.2 c_mux var porta.3 d_mux var porta.4 inh var porta.5 led var portd.3 'led connected to portd.3 sd_mps var porte.1 'shutdown pin on the main power supply dat var portc.4 sclk var portc.3 ad_cs var portc.5 ' ad chip select line i var byte ' -----[ Initialization ]-------------------------------------------------- adcon1=$0F 'all digital i/o CMCON=$07 'disable comparators trisa=%11000001 'porta HZ; except mux pins ' 7 6 5 4 3 2 1 0 <= TRISA ' | | | | | | | |___________ charger control ' | | | | | | | ' | | | | | | |_____________ MUX - A ' | | | | | | ' | | | | | |_______________ MUX - B ' | | | | | ' | | | | |_________________ MUX - C ' | | | | ' | | | |___________________ MUX - D ' | | | ' | | |_____________________ MUX - INH ' | | ' | |_______________________ oscilator ' | ' |_________________________ oscilator trisb=$FF 'portb HZ ' 7 6 5 4 3 2 1 0 <= TRISB ' | | | | | | | |___________ charger enable ' | | | | | | | ' | | | | | | |_____________ backup battery level ' | | | | | | ' | | | | | |_______________ CAN TXD ' | | | | | ' | | | | |_________________ CAN RXD ' | | | | ' | | | |___________________ keyswitch ' | | | ' | | |_____________________ DCD - C ' | | ' | |_______________________ DCD - B ' | ' |_________________________ DCD - A trisc=%11010111 'portc HZ; except ad lines ' 7 6 5 4 3 2 1 0 <= TRISC ' | | | | | | | |___________ capacitor enable ' | | | | | | | ' | | | | | | |_____________ matrix switch off ' | | | | | | ' | | | | | |_______________ DC/DC sync ' | | | | | ' | | | | |_________________ A/D converter SCLK, SPI SCK ' | | | | ' | | | |___________________ A/D converter DATA, SPI SDI ' | | | ' | | |_____________________ A/D converter CS, SPI SDO ' | | ' | |_______________________ DCD - D ' | ' |_________________________ standby trisd=%00000000 'set the lcd lines as digital output ' 7 6 5 4 3 2 1 0 <= TRISD ' | | | | | | | |___________ LCD RS ' | | | | | | | ' | | | | | | |_____________ LCD R/W ' | | | | | | ' | | | | | |_______________ LCD E ' | | | | | ' | | | | |_________________ running LED/main contactor ' | | | | ' | | | |___________________ LCD D4 ' | | | ' | | |_____________________ LCD D5 ' | | ' | |_______________________ LCD D6 ' | ' |_________________________ LCD D7 trise=%00000000 'porte output ' 7 6 5 4 3 2 1 0 <= TRISE ' | | | | | | | |___________ buzzer ' | | | | | | | ' | | | | | | |_____________ main power supply SD ' | | | | | | ' | | | | | |_______________ DCD - G1, G2 ' | | | | | ' | | | | |_________________ MCLR, programmer interface ' | | | | ' | | | |___________________ 0 ' | | | ' | | |_____________________ 0 ' | | ' | |_______________________ 0 ' | ' |_________________________ 0 pause 2000 'LCD power up delay main: high inh lcdout $FE, 1 'clear lcd high led 'turn on running led high sd_mps 'turn on the main power supply low a_mux:high b_mux:low c_mux:low d_mux:low inh 'select channel 0 pauseus 100 high sclk 'clock idles high low ad_cs 'turn on the a/d converter result=0 ' result = 00000000 00000000 for i=0 to 7 ' output 8 clocks one = one * 2 ' Shift each bit to the left one = one + dat ' Data in port pulsout sclk,5 ' Clock pulse port, 1 msec next i for i=0 to 7 ' output 8 clocks two = two * 2 ' Shift each bit to the left two = two + dat ' Data in port pulsout sclk,5 ' Clock pulse port, 1 msec next i for i=0 to 7 ' output 8 clocks three = three * 2 ' Shift each bit to the left three = three + dat ' Data in port pulsout sclk,5 ' Clock pulse port, 1 msec next i high ad_cs ' turn off the a/d converter high inh ' disable mux result=0 ' result = 00000000 00000000 result=two ' result = 00000000 xxxxxxxx result=result<<4 ' result = 0000xxxx xxxx0000 result.highbyte=result.highbyte|one<<4 ' result = xxxxxxxx xxxx0000 result.lowbyte=result.lowbyte|three>>4 ' result = xxxxxxxx xxxxxxxx ' voila, 'result' holds the 16 bit conversion result result=result*/16 'quantisation LCDOUT $FE, $80, BIN one 'print first byte of result, first line LCDOUT $FE, $C0, BIN two 'print second byte of result, first line LCDOUT $FE, $94, BIN three 'print third byte of result, first line LCDOUT $FE, $D4, DEC result 'must be around 2500 pause 1000 goto main




Bookmarks