unsigned char tx_byte, NumH, NumL; // send a character down the serial port, takes 104uSec void putch(unsigned char c) { tx_byte = c; #asm #include bcf INTCON, 7 ; bit-banged routines are time-sensitive ; so stop interrupts messing them up call bitout_1 ; start bit nop ; nop ; nop movlw 8 movwf _NumL ser_loop call bitout rrf _tx_byte, f decfsz _NumL, f goto ser_loop call bitout_0 bsf INTCON, 7 ; re-enable interrupts after ; time sensitive code is finished return ; routine to output a bit with the correct timing for 9600Bd @ 1Mhz ; this bit-sense works with a MAX-232 (i.e. inverted) bitout btfsc _tx_byte, 0 goto bitout_0 bitout_1 ; nop nop bcf RB6 goto bit_done bitout_0 bsf RB6 bit_done movlw 4 movwf _NumH bit_delay decfsz _NumH, f goto bit_delay return #endasm } // output a byte to SPI, return reply byte // From the datasheet, the first 1->0 of SCLK // outputs the first data bit from the ADC. // Then set the bit to send and then raise SCLK // to clock it into the AD7858 unsigned char spi(unsigned char c) { tx_byte = c; #asm movlw 8 movwf _NumL spi_loop: nop bcf RB1 ; SCLK goes low, read input bcf CARRY rlf _tx_byte ; bit to send is now in Carry btfsc RB3 ; data in bsf _tx_byte, 0 ; set the LSbit if RB3 high bcf RB2 ; bit to send btfsc CARRY bsf RB2 ; should be a "1" nop nop bsf RB1 ; SCLK goes high, clocks data out decfsz _NumL, f goto spi_loop #endasm return(tx_byte); } unsigned char TenK, Thou, Hund, Tens, Ones, NumH, NUmL; // Convert 16 bit binary to 5 BCD characters // from an application note by Maxim Semiconductors // http://pdfserv.maxim-ic.com/arpdf/software/DI_1298.asm void put_int4(unsigned int r) { NumH = r >> 8; NumL = r & 0xff; #asm ;********************** ; 16bit binary 2 BCD ;********************** bin2bcd ; Takes number in NumH:NumL ; Returns decimal in ; TenK:Thou:Hund:Tens:Ones swapf _NumH,w andlw 0x0f addlw 0xf0 movwf _Thou addwf _Thou,f addlw 0xe2 movwf _Hund addlw 0x32 movwf _Ones movf _NumH,w andlw 0x0f addwf _Hund,f addwf _Hund,f addwf _Ones,f addlw 0xe9 movwf _Tens addwf _Tens,f addwf _Tens,f swapf _NumL,w andlw 0x0f addwf _Tens,f addwf _Ones,f rlf _Tens,f rlf _Ones,f comf _Ones,f rlf _Ones,f movf _NumL,w andlw 0x0f addwf _Ones,f rlf _Thou,f movlw 0x07 movwf _TenK movlw 0x0a Lb1: addwf _Ones,f decf _Tens,f btfss 3,0 goto Lb1 Lb2: addwf _Tens,f decf _Hund,f btfss 3,0 goto Lb2 Lb3: addwf _Hund,f decf _Thou,f btfss 3,0 goto Lb3 Lb4: addwf _Thou,f decf _TenK,f btfss 3,0 goto Lb4 #endasm putch('0'+Thou); putch('0'+Hund); putch('0'+Tens); putch('0'+Ones); }