I currently using the following code to detect the size of a new EEProm prior to formatting.

Address VAR WORD
CounterA var byte
EpromType VAR byte
Control CON %10100000

Address=65535
For CounterA=5 to 1 step -1
I2CWRITE PORTC.4,PORTC.3,Control,Address,[CounterA]
PAUSE 10
Address=(Address>>1)
Next CounterA
I2CREAD PORTC.4,PORTC.3,Control,65535,[EpromType]

Since you are reading just the LAST byte in the SEEPROM, the answer in
EpromType will be a value between 1 and 5, where 1=24LC32 and
5=24LC515.

However I now am looking to use a 16K EEProm. The lowest this loop will go is 32K.

I would like two bits of info. 1) How can I modify this code to calculate all the way down to 1K? 2) Could someone give me a description of how and why this works?