從上一次超音波測距實驗,在電腦上顯示距離後,覺得想再完美一點,於是隔日動手把LCD裝上,

摸索了一個上午,終於讓我摸出來要如何讓超音波的測量數據顯示在液晶上。

如何安裝LCD,請參照之前的文章----https://blairan121122885.pixnet.net/blog/post/119657991-%E6%A8%B9%E8%8E%93%E6%B4%BEp3b+%E5%B0%8D%E6%87%89LCD2x16%E9%A1%AF%E7%A4%BA

 

如何安裝超音波感測器,請參照之前的文章----https://blairan121122885.pixnet.net/blog/post/119711907-%E7%94%A8%E6%A8%B9%E8%8E%93%E6%B4%BE%E5%81%9A%E8%B6%85%E9%9F%B3%E6%B3%A2%E6%B8%AC%E8%B7%9D

 

以下就是兩個原件結合後的程式碼

  1. import RPi.GPIO as GPIO  
  2. import time  
  3. import smbus  
  4.   
  5. TRIG=23  
  6. ECHO=24  
  7.   
  8. GPIO.setwarnings(False)  
  9. GPIO.setmode(GPIO.BCM)  
  10. GPIO.setup(TRIG,GPIO.OUT)  
  11. GPIO.setup(ECHO,GPIO.IN)  
  12. # Define some device parameters  
  13. I2C_ADDR  = 0x27 # I2C device address, if any error, change this address to 0x27  
  14. LCD_WIDTH = 16   # Maximum characters per line  
  15.   
  16. # Define some device constants  
  17. LCD_CHR = 1 # Mode - Sending data  
  18. LCD_CMD = 0 # Mode - Sending command  
  19.   
  20. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line  
  21. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line  
  22. LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line  
  23. LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line  
  24.   
  25. LCD_BACKLIGHT  = 0x08  # On  
  26. #LCD_BACKLIGHT = 0x00  # Off  
  27.   
  28. ENABLE = 0b00000100 # Enable bit  
  29.   
  30. # Timing constants  
  31. E_PULSE = 0.0005  
  32. E_DELAY = 0.0005  
  33.   
  34. #Open I2C interface  
  35. #bus = smbus.SMBus(0)  # Rev 1 Pi uses 0  
  36. bus = smbus.SMBus(1) # Rev 2 Pi uses 1  
  37.   
  38. def lcd_init():  
  39.   # Initialise display  
  40.   lcd_byte(0x33,LCD_CMD) # 110011 Initialise  
  41.   lcd_byte(0x32,LCD_CMD) # 110010 Initialise  
  42.   lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction  
  43.   lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off   
  44.   lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size  
  45.   lcd_byte(0x01,LCD_CMD) # 000001 Clear display  
  46.   time.sleep(E_DELAY)  
  47.   
  48. def lcd_byte(bits, mode):  
  49.   # Send byte to data pins  
  50.   # bits = the data  
  51.   # mode = 1 for data  
  52.   #        0 for command  
  53.   
  54.   bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT  
  55.   bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT  
  56.   
  57.   # High bits  
  58.   bus.write_byte(I2C_ADDR, bits_high)  
  59.   lcd_toggle_enable(bits_high)  
  60.   
  61.   # Low bits  
  62.   bus.write_byte(I2C_ADDR, bits_low)  
  63.   lcd_toggle_enable(bits_low)  
  64.   
  65. def lcd_toggle_enable(bits):  
  66.   # Toggle enable  
  67.   time.sleep(E_DELAY)  
  68.   bus.write_byte(I2C_ADDR, (bits | ENABLE))  
  69.   time.sleep(E_PULSE)  
  70.   bus.write_byte(I2C_ADDR,(bits & ~ENABLE))  
  71.   time.sleep(E_DELAY)  
  72.   
  73. def lcd_string(message,line):  
  74.   # Send string to display  
  75.   
  76.   message = message.ljust(LCD_WIDTH," ")  
  77.   
  78.   lcd_byte(line, LCD_CMD)  
  79.   
  80.   for i in range(LCD_WIDTH):  
  81.     lcd_byte(ord(message[i]),LCD_CHR)  
  82.   
  83. def main():  
  84.   # Main program block  
  85.   
  86.   # Initialise display  
  87.   lcd_init()  
  88. def get_distance():  
  89.     GPIO.output(TRIG,GPIO.HIGH)  
  90.     time.sleep(0.5)  
  91.     GPIO.output(TRIG,GPIO.LOW)  
  92.       
  93.     while GPIO.input(ECHO)==0:  
  94.         start=time.time()  
  95.           
  96.     while GPIO.input(ECHO)==1:  
  97.         end=time.time()  
  98.           
  99.     Dis=(end - start) * 17150  
  100.     return Dis  
  101. while True:  
  102.     print("{}{:.1f}{}".format("測量距離為",get_distance(),"公分"))  
  103.   
  104.   
  105.     # Send some test  
  106.     #lcd_string(str(int(get_distance()))+"cm",LCD_LINE_1)  
  107.     lcd_string("Distance is=**cm",LCD_LINE_1)  
  108.     lcd_string("{}{}".format(str(int(get_distance())),"cm"),LCD_LINE_2)  
  109.     #lcd_string("i love python ",LCD_LINE_2)  
  110.   
  111.     time.sleep(0.5)  
  112.     
  113.     #Send some more text  
  114.     lcd_string("Distance is=**cm",LCD_LINE_1)  
  115.     lcd_string("{}{}".format(str(int(get_distance())),"cm"),LCD_LINE_2)  
  116.     #lcd_string(str(get_distance),LCD_LINE_2)  
  117.   
  118.     time.sleep(0.5)  
  119.   
  120. if __name__ == '__main__':  
  121.   
  122.   try:  
  123.     main()  
  124.   except KeyboardInterrupt:  
  125.     pass  
  126.   finally:  
  127.     lcd_byte(0x01, LCD_CMD)  
  128.                  
  129. GPIO.cleanup()  

其實它就是超音波測距程式加上LCD程式碼,但坎入的位置要對,

目前還不會呼叫自訂的函式庫到另一個程式裡,所以只能用複製

坎入的方式。

arrow
arrow
    創作者介紹
    創作者 定凱 的頭像
    定凱

    大的人身,小的心靈…保持赤子之心。

    定凱 發表在 痞客邦 留言(0) 人氣()