@@ -68,27 +68,35 @@ def readadc(adcnum, clockpin, mosipin, misopin, cspin):
6868 adcout /= 2 # first bit is 'null' so drop it
6969 return adcout
7070
71- # change these as desired
72- SPICLK = 18
73- SPIMOSI = 17
74- SPIMISO = 21
75- SPICS = 22
76-
77- # set up the SPI interface pins
78- GPIO .setup (SPIMOSI , GPIO .OUT )
79- GPIO .setup (SPIMISO , GPIO .IN )
80- GPIO .setup (SPICLK , GPIO .OUT )
81- GPIO .setup (SPICS , GPIO .OUT )
82-
83- # Note that bitbanging SPI is incredibly slow on the Pi as its not
84- # a RTOS - reading the ADC takes about 30 ms (~30 samples per second)
85- # which is awful for a microcontroller but better-than-nothing for Linux
86-
87- print "| #0 \t #1 \t #2 \t #3 \t #4 \t #5 \t #6 \t #7\t |"
88- print "-----------------------------------------------------------------"
89- while True :
90- print "|" ,
91- for adcnum in range (8 ):
92- ret = readadc (adcnum , SPICLK , SPIMOSI , SPIMISO , SPICS )
93- print ret ,"\t " ,
94- print "|"
71+ if __name__ == '__main__' :
72+
73+ try :
74+ # change these as desired
75+ SPICLK = 18
76+ SPIMISO = 21
77+ SPIMOSI = 17
78+ SPICS = 22
79+
80+ # set up the SPI interface pins
81+ GPIO .setup (SPICLK , GPIO .OUT )
82+ GPIO .setup (SPIMISO , GPIO .IN )
83+ GPIO .setup (SPIMOSI , GPIO .OUT )
84+ GPIO .setup (SPICS , GPIO .OUT )
85+
86+ # Note that bitbanging SPI is incredibly slow on the Pi as its not
87+ # a RTOS - reading the ADC takes about 30 ms (~30 samples per second)
88+ # which is awful for a microcontroller but better-than-nothing for Linux
89+
90+ print "| #0 \t #1 \t #2 \t #3 \t #4 \t #5 \t #6 \t #7\t |"
91+ print "-----------------------------------------------------------------"
92+ while True :
93+ print "|" ,
94+ for adcnum in range (8 ):
95+ ret = readadc (adcnum , SPICLK , SPIMOSI , SPIMISO , SPICS )
96+ print ret ,"\t " ,
97+ print "|"
98+
99+ except KeyboardInterrupt :
100+ pass
101+
102+ GPIO .cleanup ()
0 commit comments