#!/usr/bin/python # # For gstein's 16F688 I2C library, the MCU's I2C address is stored in # the first byte of EEPROM space. # # This script sets that address. # import tempfile import subprocess import sys import os def create_hex(addr): fd, path = tempfile.mkstemp(dir='.') #'.hex') # Addressing is 32 bits, and this provides the upper 16 bits. The following # lines will provide the lower 16 bits. os.write(fd, ':020000040000FA\n') # Write the I2C address into the first byte of EEPROM space checksum = 256 - ((66 + addr) & 0xff) os.write(fd, ':02420000%02x00%02x\n' % (addr, checksum)) # EOF os.write(fd, ':00000001FF\n') # Done creating the file. We don't really worry about somebody sneaking in, # and writing something else. Just return the path. os.close(fd) return os.path.basename(path) def set_address(addr): path = create_hex(addr) subprocess.check_call(['pk2cmd', '-P', 'PIC16F688', '-ME', '-F', path]) os.remove(path) if __name__ == '__main__': if len(sys.argv) != 2: print 'ERROR: %s ADDRESS' % (sys.argv[0],) print ' $ %s 20' % (sys.argv[0],) print ' $ %s 0x14' % (sys.argv[0],) sys.exit(1) if sys.argv[1].startswith('0x'): addr = int(sys.argv[1], 16) else: addr = int(sys.argv[1]) print 'Setting address = 0x%02x' % (addr,) set_address(addr) print 'Done.'