'''This module facilitates ADC interfacing in Python using a Parallel port'''
__author__ = 'Aivin V. Solatorio'
import parallel
import ctypes
_ppt = ctypes.windll.simpleio
_ppt.init()
class ParallelADC(object):
'''Definition of ADC interfacing class'''
def __init__(self):
self.dataAdr = 0x0378
self.statAdr = self.dataAdr + 1
self.ctrlAdr = self.dataAdr + 2
def getData(self):
'''Acquires signal from data register'''
return _ppt.inp(self.dataAdr)
def setCtrl(self, val):
'''Dictates the value of control register'''
_ppt.outp(self.ctrlAdr, val)
def getStat(self):
'''Acquires value of status register'''
return _ppt.inp(statAdr)
'''Start of data gathering'''
gather_time = 100 #100 number of sample points
ADC = ParallelADC()
for i in xrange(gather_time):
lc = 0
ADC.setCtrl(0x32) #initializes ADC to start
ADC.setCtrl(0x36) #converting signal
while ((ADC.getStat() & 8) == 0) and (lc != 256):
lc += 1 #wait for ADC to finish converting
if (lc == 256):
print "Timed Out, check device or connection!"
else:
ADC.setCtrl(0x37)
value = ADC.getData() & 0xFF
ADC.setCtrl(0x36)
print "The ADC reading is: ", value #prints out ADC value
No comments:
Post a Comment