1

我正在尝试做这样的事情。问题是我无法建立这样做的循环。 电路:

这是我的代码:

import parallel 
import time
p=parallel.Parallel() #object to use the parallel port
print ("Enter a string of numbers: ")
numStr = raw_input() #read line
numList=list(numSTr) #converts string to list
numlen=len(numList) #the length of the list
numBin=[['1','0001'], ['2','0010'],
 ['4','0100'], ['5','0101'],
 ['6','0110'], ['7','0111'],
 ['8','1000'], ['9','1001'],
 ['3','0011'], ['0','0000']] #Less significant bits of the numbers from 0 to 9 in a bidimesional array
p.setData(0) #clear the displays
pos=['0001','0010','0100','1000'] #Unique possible positions for the number from 0 to 9. 
c=(str(pos[])+str((numBin[][1]))) #here if the number in the list numList exist and also is in numBin. It joins the position and the number in binary, creating an number that will be send in decimal to the parallel port.
p.setData(int(c,2)) #send the binary number in decimal

如果有人可以帮助我,那将是令人欣慰的

numBin 中的最高有效位定义要打开的显示。而不太重要的定义数字。例如:

字符串是 {'7', '1', '5', '4', '8'}。所以最后一个显示中显示的第一个数字是“7”。因此,我们采用二进制 7 即“0111”并将该二进制字符串与第一个显示位置“0001”连接起来。所以我们创建一个二进制数:'00010111'。我们将该数字转换为十进制并将其发送到并行端口。并口打开las显示并显示数字7。第二次,它必须在第二个和第一个位置显示一个'7'和一个'1'等等。

X X X X
X X X 7
X X 7 1
X 7 1 5
7 1 5 4
1 5 4 8
5 4 8 X
4 8 X X
8 X X X
X X X X

“X”代表显示屏关闭,数字代表其自身处于显示位置,如您在电路中所见。

4

2 回答 2

1

查看您的电路,您实际上不能同时显示不同的数字。我在演示 FPGA 板上有一个这样的电路,并且必须创建一个软件驱动程序,以比肉眼能察觉的速度更快地在正确位置闪烁显示器上的数字。

下面是一个粗略的算法,使用一个 Mock 对象来模拟并行端口和显示以供我测试。它必须在支持回车而不换行的终端上运行。

您应该能够改为放入并行库,但可能必须调整控制位以匹配您的硬件:

import sys

class ParallelMock(object):

    def __init__(self):
        '''Init and blank the "display".'''
        self.display = [' '] * 4
        self._update()

    def setData(self,data):
        '''Bits 0-3 are the "value".
           Bits 4-7 are positions 0-3 (first-to-last).
        '''
        self.display = [' '] * 4
        value = data & 0xF
        if data & 0x10:
            self.display[0] = str(value)
        if data & 0x20:
            self.display[1] = str(value)
        if data & 0x40:
            self.display[2] = str(value)
        if data & 0x80:
            self.display[3] = str(value)
        self._update()

    def _update(self):
        '''Write over the same four terminal positions each time.'''
        sys.stdout.write(''.join(self.display) + '\r')

if __name__ == '__main__':
    p = ParallelMock()

    nums = raw_input("Enter a string of numbers: ")

    # Shift over the steam four-at-a-time.
    stream = 'XXXX' + nums + 'XXXX'
    data = [0] * 4
    for i in range(len(stream)-3):
        # Precompute data
        for pos in range(4):
            value = stream[i+pos]
            data[pos] = 0 if value == 'X' else (1<<(pos+4)) + int(value)
        # "Flicker" the display...
        for delay in xrange(1000):
            # Display each position briefly.
            for d in data:
                p.setData(d)
        # Clear the display when done
        p.setData(0)
于 2012-03-29T07:33:38.717 回答
1
import parallel 
import time
p=parallel.Parallel()                        # object to use the parallel port
print ("Enter a string of numbers: ")
numStr = bytearray(raw_input())
p.setData(0)                                 # clear the displays
while True:                                  # refresh as fast as you need to
    for i,n in enumerate(numStr,4):
        p.setData(1<<i | n&0xf)

在 for 循环中,i取值 4,5,6,7 所以1<<i我们得到:

4 => 0b00010000
5 => 0b00100000
6 => 0b01000000
7 => 0b10000000

这是按位或数字的 ascii 代码的最后 4 位,以给出您需要写入并行端口的值

于 2012-03-29T06:11:47.753 回答