Last active
August 2, 2018 02:18
-
-
Save koyamalmsteen/702b417766c08bb9f6fd13f6b8756da5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
OUTPUT_FILE="rom.mif" | |
REF_VOLTS=2700 | |
RESOLUTION=1024 | |
TEMP_VALUE=19.5 | |
TEMP_OFFSET=400 | |
# ROM Information | |
ROM_WIDTH=16 | |
ROM_DEPTH=1024 | |
def create_header(): | |
string = "-- create_mif_for_digital_thermometer.py created a memory initialization file (.mif) --\n" | |
string = string+"WIDTH="+str(ROM_WIDTH)+";\n" | |
string = string+"DEPTH="+str(ROM_DEPTH)+";\n" | |
string = string+"\n"; | |
string = string+"ADDRESS_RADIX=UNS;\n" | |
string = string+"DATA_RADIX=HEX;\n" | |
string = string+"\n"; | |
return string | |
def create_content(): | |
string = "CONTENT BEGIN\n" | |
str_temp="" | |
for adin in range(RESOLUTION): | |
vi=adin*(REF_VOLTS/RESOLUTION) | |
temp=(vi-TEMP_OFFSET)/TEMP_VALUE | |
if(temp<0): | |
temp=-temp | |
isneg=True | |
else: | |
isneg=False | |
temp_int=int(temp) | |
temp_dec=int((temp-temp_int)*10) | |
if( temp_int==0 and temp_dec==0): | |
isneg=False | |
temp_int=temp_int+1000 | |
if( isneg==True ): | |
str_temp="F"+str(temp_int)[-2:]+str(temp_dec) | |
else: | |
str_temp=str(temp_int)[-3:]+str(temp_dec) | |
# string = string+"\t"+str(adin)+":"+str_temp | |
string = string+"\t"+str(adin)+" : "+str_temp+";\n" | |
string = string+"END\n" | |
return string | |
def create_mif_for_digital_thermometer(): | |
header = create_header() | |
content = create_content() | |
file = open(OUTPUT_FILE, 'w') | |
file.write(header) | |
file.write(content) | |
file.close() | |
if os.path.isfile(OUTPUT_FILE): | |
print(OUTPUT_FILE+' is created.') | |
else: | |
print(OUTPUT_FILE+' is not created.') | |
if __name__ == '__main__': | |
create_mif_for_digital_thermometer() | |
I revised it for Python 3.6.6.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is for Python 2.6.6.