def getval(self): tl = [] tb = [] wiringpi.wiringPiSetup() wiringpi.pinMode(self.owpin, 1) wiringpi.digitalWrite(self.owpin, 1) wiringpi.delay(1) wiringpi.digitalWrite(self.owpin, 0) wiringpi.delay(25) wiringpi.digitalWrite(self.owpin, 1) wiringpi.delayMicroseconds(20) wiringpi.pinMode(self.owpin, 0) while (wiringpi.digitalRead(self.owpin) == 1): pass for i in range(45): tc = wiringpi.micros() while (wiringpi.digitalRead(self.owpin) == 0): pass while (wiringpi.digitalRead(self.owpin) == 1): if wiringpi.micros() - tc > 500: break if wiringpi.micros() - tc > 500: break tl.append(wiringpi.micros() - tc) tl = tl[1:] for i in tl: if i > 100: tb.append(1) else: tb.append(0) return tb
def readEcho(self): time_out = wpi.micros() + 3E6 # timeout at current time plus 3 seconds while time_out > wpi.micros() and wpi.digitalRead(ECHO_PIN) == 0: pass to = wpi.micros() while wpi.digitalRead(ECHO_PIN) == 1: pass tf = wpi.micros() return to, tf
def pulseIn(pin): wp.pinMode(pin, wp.OUTPUT) wp.digitalWrite(pin, wp.HIGH) startTime = wp.micros() lastTime = startTime wp.pinMode(pin, wp.INPUT) while wp.digitalRead(pin) == 1: lastTime = wp.micros() return lastTime - startTime
def getval(owpin): tl = [] #存放每个数据位的时间 tb = [] #存放数据位 gpio.wiringPiSetup() #初始化wiringpi库 gpio.pinMode(owpin, 1) #设置针脚为输出状态 gpio.digitalWrite(owpin, 1) #输出高电平 gpio.delay(1) gpio.digitalWrite(owpin, 0) #拉低20ms开始指令 gpio.delay(25) gpio.digitalWrite(owpin, 1) #抬高20-40us gpio.delayMicroseconds(20) gpio.pinMode(owpin, 0) #设针脚为输入状态 #print("1owpin=",gpio.digitalRead(owpin)) m = 0 while (gpio.digitalRead(owpin) == 1): #print("while->owpin= %s" % gpio.digitalRead(owpin)) #time.sleep(1) #等待DHT11拉低管脚 #m+=1 #print(m) pass #print("2owpin=",gpio.digitalRead(owpin)) for j in range(45): #测试每个数据周期的时间(包括40bit数据加一个发送开始标志 #print("j= %s" % j) tc = gpio.micros() #记下当前us数(从初始化开始算起,必要时重新初始化) #print("tc=",tc) ''' 一个数据周期,包括一个低电平,一个高电平,从DHT11第一次拉低信号线开始 到DHT11发送最后一个50us的低电平结束(然后被拉高,一直维持高电平,所以 最后的完成标志是一直为高,超过500ms) ''' #print("3owpin=",gpio.digitalRead(owpin)) while (gpio.digitalRead(owpin) == 0): pass #print("4owpin=",gpio.digitalRead(owpin)) while (gpio.digitalRead(owpin) == 1): if gpio.micros() - tc > 500: #如果超过500ms就结束了 break if gpio.micros() - tc > 500: #跳出整个循环 break tl.append(gpio.micros() - tc) #记录每个周期时间的us数,存到tl这个列表 print(tl) #反注释后可打印时间列表 tl = tl[1:] #去掉第一项,剩下40个数据位 print(tl) for i in tl: if i > 100: #若数据位为1,时间为50us低电平+70us高电平=120us tb.append(1) else: tb.append(0) #若数据位为0,时间为50us低电平+25us高电平=75us #这里取大于100us就为1 #print("tb=",tb) #反注释可查看每一位状态 return tb
def arrayList1(): List1 = [] count = 0 while count < 300: n1 = GPIO.micros() #get the time before readbyte y = bus.read_byte(0x48) n2 = GPIO.micros() #get the time after readbyte List1.append(y) n3 = n2 - n1 print(n3) count = count + 1 return List1
def call_back(channel): print("channel %d" % channel) t1_callback = 0 t2_callback = 0 t1_callback = w_timer.micros() t1_callback = w_timer.micros() t2_callback = w_timer.micros() while GPIO.input(37) == GPIO.HIGH: t2_callback = w_timer.micros() # print("test %d" %t2_callback) print("In callback function t2-t1=%d us" % (t2_callback - t1_callback))
def run(self): while self.working == True: r_ft = wpi.micros() for i in range(self.num): while GPIO.input(self.r_pin): pass while GPIO.input(self.r_pin) == 0: pass print(i) t = wpi.micros() - r_ft v = 1000000 * (self.num - 1) / t * 60 # 每分钟的转速 v = round(v, 2) self.sinOut.emit(v) time.sleep(0.2)
def encoder_detect(channel): global pulse_a,t1,t2 for i in range(10): if GPIO.input(16) != 1: return; pulse_a += 1 t_temp= w_timer.micros() print("a= %d,time=%f"%(pulse_a,(t_temp-t1)/1000)) if pulse_a==1: t1=w_timer.micros() t2=0 if pulse_a==330+1: t1_temp=t2=w_timer.micros() print(((t2-t1)/1000)) pulse_a=1 t1=t1_temp
def main(): receiver = 27; sender = 1; state = 1; i = 0 global last_high, ones, zeros, bits_to_output, code while True: wiringpi.delayMicroseconds(25) if wiringpi.digitalRead(receiver) == False: if (bits_to_output > 0): sys.stdout.write("0") zeros += 1 if state < 80: state += 1 else: if state >= 2 and bits_to_output > 0: bits_to_output -= 1 #sys.stdout.write("\n") #sys.stdout.write(str(format(ones / (ones + zeros), '.2f')) + " ") #sys.stdout.write(str( (ones + zeros)) + " ") if ones / (ones + zeros) < 0.5: #sys.stdout.write("0") code += "0" else: #sys.stdout.write("1") code += "1" if bits_to_output == 0: sys.stdout.write(" " + code) sys.stdout.write(" " + compress_string(code)) #sys.stdout.write(" --\n") ones = 0 zeros = 0 if state == 80: calc_time() if (bits_to_output > 0): sys.stdout.write("1") ones += 1 if state >= 2: last_high = wiringpi.micros() state = 0 i += 1 if i == 40: sys.stdout.flush() i = 0
def getval(self): tl = [] #存放每个数据位的时间 tb = [] #存放数据位 gpio.wiringPiSetup() #初始化wiringpi库 gpio.pinMode(self.dhtChannel, 1) #设置针脚为输出状态 gpio.digitalWrite(self.dhtChannel, 1) #输出高电平 gpio.delay(1) gpio.digitalWrite(self.dhtChannel, 0) #拉低20ms开始指令 gpio.delay(25) gpio.digitalWrite(self.dhtChannel, 1) #抬高20-40us gpio.delayMicroseconds(20) gpio.pinMode(self.dhtChannel, 0) #设针脚为输入状态 while (gpio.digitalRead(self.dhtChannel) == 1): pass #等待DHT11拉低管脚 for i in range(45): #测试每个数据周期的时间(包括40bit数据加一个发送开始标志 tc = gpio.micros() #记下当前us数(从初始化开始算起,必要时重新初始化) ''' 一个数据周期,包括一个低电平,一个高电平,从DHT11第一次拉低信号线开始 到DHT11发送最后一个50us的低电平结束(然后被拉高,一直维持高电平,所以 最后的完成标志是一直为高,超过500ms) ''' while (gpio.digitalRead(self.dhtChannel) == 0): pass while (gpio.digitalRead(self.dhtChannel) == 1): if gpio.micros() - tc > 500: #如果超过500ms就结束了 break if gpio.micros() - tc > 500: #跳出整个循环 break tl.append(gpio.micros() - tc) #记录每个周期时间的us数,存到tl这个列表 # print(tl) #反注释后可打印时间列表 tl = tl[1:] #去掉第一项,剩下40个数据位 for i in tl: if i > 100: #若数据位为1,时间为50us低电平+70us高电平=120us tb.append(1) else: tb.append(0) #若数据位为0,时间为50us低电平+25us高电平=75us #这里取大于100us就为1 # print(tb) #反注释可查看每一位状态 return tb
def _distance(self): wp.digitalWrite(TRIG, 0) wp.delayMicroseconds(2) wp.digitalWrite(TRIG, 1) wp.delayMicroseconds(10) wp.digitalWrite(TRIG, 0) start = wp.micros() timeout = start + 1000000 while wp.digitalRead(ECHO) == 0 and start < timeout: start = wp.micros() if start > timeout: return 0 end = wp.micros() while wp.digitalRead(ECHO) == 1 and end < timeout: end = wp.micros() if end > timeout: return 0 duration = end - start distance = duration * 0.034 / 2 return distance
def _run(self): with serial.Serial(self.port_name, self.baud_rate) as ser: while not self.stop_thread and ser: line = ser.readline()[:-1].strip() time_received = wiringpi.micros() bytes_received = len(line) + 1 bits_received = bytes_received * BITS_PER_BYTE # This is at least how much time it took to reveive the string. # We know that the message must have been sent at the latest at this time. us_to_receive = (bits_received * USEC_PER_SEC) / self.baud_rate self.q.put('%s-%s: %s' % (str(time_received - us_to_receive), str(time_received), line)) self.print_debug('Exiting serial port listener thread')
def encoder_detect(channel): global pulse_a,t1,t2 for i in range(10): if GPIO.input(16) != 1: return; pulse_a += 1 # t_temp= w_timer.micros() # print("a= %d,time=%f"%(pulse_a,(t_temp-t1)/1000)) # t_temp2=w_timer.micros() # print("time in printf=%f"%((t_temp2-t_temp)/1000)) if pulse_a==1: t1=w_timer.micros() t2=0 if pulse_a==330+1: p.ChangeDutyCycle(0) for i in range(100000): pass p.ChangeDutyCycle(90) t1_temp=t2=w_timer.micros() # print(((t2-t1)/1000)) pulse_a=1 t1=t1_temp
def getval(pin): tl = [] tb = [] wiringpi.wiringPiSetup() wiringpi.pinMode(pin, GPIO.OUTPUT) wiringpi.digitalWrite(pin, GPIO.HIGH) wiringpi.delay(1) wiringpi.digitalWrite(pin, GPIO.LOW) wiringpi.delay(25) wiringpi.digitalWrite(pin, GPIO.HIGH) wiringpi.delayMicroseconds(20) wiringpi.pinMode(pin, GPIO.INPUT) while (wiringpi.digitalRead(pin) == 1): pass for i in range(45): tc = wiringpi.micros() ''' ''' while (wiringpi.digitalRead(pin) == 0): pass while (wiringpi.digitalRead(pin) == 1): if wiringpi.micros() - tc > 500: break if wiringpi.micros() - tc > 500: break tl.append(wiringpi.micros() - tc) tl = tl[1:] for i in tl: if i > 100: tb.append(1) else: tb.append(0) return tb
def calc_time(): global last, last_high, bits_to_output, ones, zeros, code now = wiringpi.micros() diff = now - last if (diff > 40000 and diff < 42000): sys.stdout.write("\n") sys.stdout.write("\n\nwhole signal duration: " + str(now - last) + "") sys.stdout.write(" sync duration:" + str(now - last_high) + " ") if ones + zeros != 0: sys.stdout.write(" sync ones-ration:" + str(ones / (ones + zeros)) + " ") sys.stdout.write(" code duration:" + str((last_high - last) / 24) + "\n") bits_to_output = 24 ones = 0 zeros = 0 code = "" last = now
def _pps_callback(self, channel): self.event_queue.put('%s: PPS' % str(wiringpi.micros()))
t2_callback = 0 t1_callback = w_timer.micros() t1_callback = w_timer.micros() t2_callback = w_timer.micros() while GPIO.input(37) == GPIO.HIGH: t2_callback = w_timer.micros() # print("test %d" %t2_callback) print("In callback function t2-t1=%d us" % (t2_callback - t1_callback)) GPIO.setmode(GPIO.BOARD) GPIO.setup(35, GPIO.OUT) #Trigger Pulse Input GPIO.setup(37, GPIO.IN) #Echo Pulse Output GPIO.add_event_detect(37, GPIO.RISING, callback=call_back) while True: t1 = 0 t2 = 0 GPIO.output(35, 1) #Enable Trigger Pulse t1 = w_timer.micros() t1 = w_timer.micros() t2 = w_timer.micros() while t2 - t1 < 10: # print("t2-t1=%d"% (t2-t1)) t2 = w_timer.micros() GPIO.output(35, 0) #Enable Trigger Pulse t2 = w_timer.micros() # print(t2-t1)
level=logging.DEBUG) GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) TRIG = 17 ECHO = 18 GPIO.setup(TRIG, GPIO.OUT) GPIO.setup(ECHO, GPIO.IN) while True: GPIO.output(TRIG, False) time.sleep(1.5) print("Waiting for sensor to settle") GPIO.output(TRIG, True) wiringpi.delayMicroseconds(50) GPIO.output(TRIG, False) pulse_start = wiringpi.micros() while GPIO.input(ECHO) == 0: pulse_start = wiringpi.micros() while GPIO.input(ECHO) == 1: pulse_end = wiringpi.micros() pulse_duration = (pulse_end - pulse_start) print("Pulse duration =%1f " % pulse_duration) distance = (pulse_duration / 1000 * 34.3 / 2) if distance > 2 and distance < 450: print("Distance = %1f cm" % distance) else: print("Out of Range")
def delayTime(self, micros): while (gpio.micros() - self.start_time) < micros: pass