; data is stored on a Microchip 24LC64 8kbyte serial eeprom;;;; ; eeprom i^2c protocol for use with Microchip 24LC64 8kbyte serial eeprom;;;; constants [[ee-port portc] [ee-port-ddr portc-ddr] [sck 7] [sdat 6]] global [ee-addr ee-data] to powerup setee-addr -1 clearbit sck ee-port-ddr clearbit sdat ee-port-ddr end to read-eeprom :address ; read the byte located at :address ; in the eeprom and output it ;if it's not a sequentialread we need to send the address if (not (ee-addr-match? :address)) [ee-start ; send a start bit ee-send $a0 ; control byte ee-send highbyte :address ee-send lowbyte :address] ee-start ; send a start condition ee-send $a1 ; send current addr read command setbit sdat ee-port-ddr ; data pin to input setee-data 0 repeat 8 [eein1] ; read a byte into ee-data clearbit sdat ee-port-ddr ; data pin back to output ;ack ; send an ack (this seems to be unnecessary....) ee-stop ; send a stop condition setee-addr :address + 1 ; increment address output ee-data end ; check if ee-addr is the same as the address to ee-addr-match? :address ifelse (:address = ee-addr) [output 1] [output 0] end ; read a data bit and shift it into ee-data to eein1 setee-data leftshift ee-data 1 clearbit sck ee-port repeat 4 [no-op] setbit sck ee-port if testbit sdat ee-port [setee-data ee-data + 1] end ; write a byte to the ee-prom to write-eeprom :address :value ee-start ; send a start bit ee-send $a0 ; send the control byte ;ee-send highbyte :address ee-send lowbyte :address ee-send :value ; then the data ee-stop ; send a stop bit setee-addr :address + 1 ; increment the address mwait 5 ; wait 5 ms for write to finish end to ee-send :value ; send the input byte out to the ee-port setee-data :value ; ee-data has the data repeat 8 [eeout1] ack end to eeout1 ; shift a bit out from ee-data clearbit sck ee-port ifelse ee-data and 128 [setbit sdat ee-port] [clearbit sdat ee-port] setbit sck ee-port setee-data leftshift ee-data 1 end to ee-start ; send a start condition (falling edge during sck high) clearbit sck ee-port setbit sdat ee-port setbit sck ee-port clearbit sdat ee-port end to ee-stop ; send a stop condition (rising edge during sck high) clearbit sck ee-port clearbit sdat ee-port setbit sck ee-port setbit sdat ee-port end to ack clearbit sck ee-port setbit sdat ee-port setbit sck ee-port end