I guess the only disadvantage to everyone helping me, is that I'm going to break something new and ask more questions... :-) I have cobbled the following together from datasheets and borrowed piclist code. I am sure this is too much code to attach, but I dunno what the rules are, so I apologize in advance. I thought about just sending the pertinent routines, but Murphy assures me that I will leave out the code with the bug. Please don't attach the whole thing to your replies. Basically I am sampling an AD and then writing it to the UART. All that works great. But I am also trying to write it to an EEPROM and then dump it all back to the UART when I reach the end of the 24LC256. The first loop works fine and I get 16,767 samples (2 bytes each) to the screen. But the second loop gives me 16,767 of the same value read from memory. I'll give the examples in binary, since there's an unusual pattern. Keep in mind that just one of these values will be returned from all memory locations on any given run. These are the 4 basic variations. 1111100011111000 1111000111110001 1111000011110000 1110000111100001 As noted in my comments below, I do not understand the value chosen for SPPCON2 by the original author and I am making an educated guess on the value for the clock and what to do with it in SSPADD. Thanks in advance. (74HC595 to an LCD is next, in case anyone is wondering what chapter 4 will be) ; AD -> BCD -> USART -> I2C -> BCD -> USART test code ; Greg Hartung ; 1999-Nov-25 list p=16F877, R=DEC #include ; Constants CONSTANT SP = d'32' ; Space CONSTANT LF = d'10' ; Line Feed CONSTANT CR = d'13' ; Carriage Return CONSTANT REGISTERS = 0x20 ; Start of general registers ; Registers Recvd equ REGISTERS+0 NumH equ REGISTERS+1 NumL equ REGISTERS+2 TenK equ REGISTERS+3 Thou equ REGISTERS+4 Hund equ REGISTERS+5 Tens equ REGISTERS+6 Ones equ REGISTERS+7 Count equ REGISTERS+8 InsideCount equ REGISTERS+9 OutsideCount equ REGISTERS+10 I2C_MSB equ REGISTERS+11 I2C_LSB equ REGISTERS+12 I2C_Data equ REGISTERS+13 __CONFIG _CP_OFF & _WDT_OFF & _HS_OSC & _PWRTE_ON org 0 goto Main sendat bsf STATUS,RP0 btfss TXSTA,1 goto sendat bcf STATUS,RP0 movwf TXREG return waitforchar btfss PIR1, RCIF goto waitforchar movfw RCREG movwf Recvd return BCD16 swapf NumH,w iorlw 11110000B 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 STATUS,C goto Lb1 Lb2 addwf Tens,f decf Hund,f btfss STATUS,C goto Lb2 Lb3 addwf Hund,f decf Thou,f btfss STATUS,C goto Lb3 Lb4 addwf Thou,f decf TenK,f btfss STATUS,C goto Lb4 movlw 0x30 addwf TenK addwf Thou addwf Hund addwf Tens addwf Ones return WaitForAcquisition ; 20u second delay (100 instructions @ 20Mhz) movlw 0x18 ; 1 cycle movwf Count ; 1 cycle AcquisitionLoop decfsz Count ; 1 cycles goto AcquisitionLoop ; 3 cycles return ; 2 cycles WaitForConversion btfsc ADCON0, GO goto WaitForConversion return Beep bcf PORTD,7 ; Turn piezo on call BeepDelay bsf PORTD,7 ; Turn piezo off return BeepDelay ; 1/10th of a Second Delay movlw 0xFF movwf OutsideCount DelayOutside movlw 0xFF movwf InsideCount DelayInside decfsz InsideCount goto DelayInside decfsz OutsideCount goto DelayOutside return I2C_Read bcf STATUS,RP0 ;Bank 0 bcf PIR1,SSPIF bsf STATUS,RP0 ;Bank 1 bsf SSPCON2,SEN ;send Start bit bcf STATUS,RP0 ;Bank 0 ir1 btfss PIR1,SSPIF goto iw1 movlw b'10100001' ;send control byte (device+address+read) movwf SSPBUF bcf PIR1,SSPIF ir2 btfss PIR1,SSPIF ;wait for ACK goto ir2 movfw I2C_MSB ; send address movwf SSPBUF bcf PIR1,SSPIF ir3 btfss PIR1,SSPIF ;wait for ACK goto ir3 movfw I2C_LSB ;send the rest of the address movwf SSPBUF bcf PIR1,SSPIF ir4 btfss PIR1,SSPIF ;wait for ACK goto ir4 bsf SSPCON2,SEN ;send Start bit again bcf STATUS,RP0 ;Bank 0 ir5 btfss PIR1,SSPIF goto iw5 movlw b'10100001' ;send control byte (device+address+read) movwf SSPBUF bcf PIR1,SSPIF ir6 btfss PIR1,SSPIF ;wait for ACK goto ir6 movfw SSPBUF ;get the data movwf I2C_Data return I2C_Write bcf STATUS,RP0 ;Bank 0 bcf PIR1,SSPIF bsf STATUS,RP0 ;Bank 1 bsf SSPCON2,SEN ;send Start bit bcf STATUS,RP0 ;Bank 0 iw1 btfss PIR1,SSPIF goto iw1 movlw b'10100000' ;send control byte (device+address+write) movwf SSPBUF bcf PIR1,SSPIF iw2 btfss PIR1,SSPIF ;wait for ACK goto iw2 movfw I2C_MSB ; send address movwf SSPBUF bcf PIR1,SSPIF iw3 btfss PIR1,SSPIF ;wait for ACK goto iw3 movfw I2C_LSB ;send the rest of the address movwf SSPBUF bcf PIR1,SSPIF iw4 btfss PIR1,SSPIF ;wait for ACK goto iw4 movfw I2C_Data ;send the Data movwf SSPBUF bcf PIR1,SSPIF iw5 btfss PIR1,SSPIF ;wait for ACK goto iw5 bcf PIR1,SSPIF bsf STATUS,RP0 ;Bank 1 bsf SSPCON2,PEN ;Send Stop bit bcf STATUS,RP0 ;Bank 0 iw6 btfss PIR1,SSPIF goto iw6 return Main ; Initialize ports clrf PORTA ; Set PORTA output latch to 0 (optional) clrf PORTB ; Set PORTB output latch to 0 clrf PORTC ; Set PORTC output latch to 0 clrf PORTD ; Set PORTC output latch to 0 clrf PORTE ; Set PORTC output latch to 0 bsf STATUS,RP0 ; Switch to Bank1 movlw B'11111111' ; Set PORTA to all inputs for AD movwf TRISA movlw B'10011000' ; Set PORTC 7 to input for USART RX, 4:3 for I2C movwf TRISC movlw B'00000000' ; Set PORTD to all outputs movwf TRISD bcf STATUS,RP0 ; Switch back to Bank0 ; Initialize USART Transmitter movlw B'10000000' ; Select: Port C serial ports (not I/O), RX disabled movwf RCSTA bsf STATUS, RP0 ; Switch to Bank1 movlw d'10' ; 115kbps @ 20Mhz movwf SPBRG movlw B'00100100' ; Select Asynchronous, 8bit, high baud rate, TX enabled serial port movwf TXSTA ; bcf STATUS, RP0 ; Switch to Bank0 bsf RCSTA,SPEN ; enable serial port bsf RCSTA,CREN ; enable serial port ; Initialize AD bsf STATUS, RP0 ; Select Bank 1 movlw B'10000000' ; Right justify ADRESH movwf ADCON1 bcf STATUS, RP0 ; Select Bank 0 movlw B'10000001' ; 32Tosc (Tad will be 1.6us, 7:6), A/D is on (1:1), Channel 0 is selected (5:3) movwf ADCON0 ;set up I2C Port bsf STATUS,RP0 ; Bank 1 movlw b'01100000' ; What's all this about?!?! movwf SSPCON2 movlw b'00001100' ; 400KHz @ 20MHz Clock (385k,actually) movwf SSPADD ; 20,000,000/4*(SSPADD+1) bcf STATUS,RP0 ; Bank 0 clrf SSPSTAT movlw b'00101000' ; enabled, master mode movwf SSPCON bsf SSPCON,SSPEN ; initialize variables clrf I2C_MSB clrf I2C_LSB ; LED is already on call Beep Sample movlw LF call sendat movlw CR call sendat call WaitForAcquisition BSF ADCON0, GO ; Start A/D Conversion call WaitForConversion movfw ADRESH movwf NumH bsf STATUS, RP0 ; Switch to Bank1 movfw ADRESL bcf STATUS, RP0 ; Switch to Bank0 movwf NumL movfw NumH ; Write NumH to EEPROM movwf I2C_Data call I2C_Write incf I2C_LSB ; Increment the Lower Byte btfsc STATUS, Z ; If the Zero Flag is Set incf I2C_MSB ; Increment the Upper Byte movfw NumL ; Write NumL to EEPROM movwf I2C_Data call I2C_Write call BCD16 ; Write to serial port movfw TenK call sendat movfw Thou call sendat movfw Hund call sendat movfw Tens call sendat movfw Ones call sendat incf I2C_LSB ; Increment the Lower Byte btfsc STATUS, Z ; If the Zero Flag is Set incf I2C_MSB ; Increment the Upper Byte btfsc I2C_MSB, 7 ; If the Zero Flag is Set goto DumpMemory ; Memory is full (32KB) goto Sample DumpMemory clrf I2C_MSB clrf I2C_LSB dm1 movlw LF call sendat movlw CR call sendat call I2C_Read movfw I2C_Data movwf NumH incf I2C_LSB ; Increment the Lower Byte btfsc STATUS, Z ; If the Zero Flag is Set incf I2C_MSB ; Increment the Upper Byte call I2C_Read movfw I2C_Data movwf NumL call BCD16 ; Write to serial port movfw TenK call sendat movfw Thou call sendat movfw Hund call sendat movfw Tens call sendat movfw Ones call sendat incf I2C_LSB ; Increment the Lower Byte btfsc STATUS, Z ; If the Zero Flag is Set incf I2C_MSB ; Increment the Upper Byte btfsc I2C_MSB, 7 ; If the Zero Flag is Set goto $ ; End of memory (32KB) goto dm1 end