{"id":866,"date":"2020-09-08T23:43:18","date_gmt":"2020-09-08T15:43:18","guid":{"rendered":"https:\/\/www.iotch.xyz\/?p=866"},"modified":"2020-09-08T23:43:18","modified_gmt":"2020-09-08T15:43:18","slug":"%e6%a0%91%e8%8e%93%e6%b4%be%e9%80%9a%e8%bf%87-i2c-%e9%a9%b1%e5%8a%a8-lcd1602-%e6%b6%b2%e6%99%b6%e5%b1%8f","status":"publish","type":"post","link":"https:\/\/www.iotch.xyz\/?p=866","title":{"rendered":"\u6811\u8393\u6d3e\u901a\u8fc7 I2C \u9a71\u52a8 LCD1602 \u6db2\u6676\u5c4f"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2020\/06\/20200617000032890-0.jpg\" alt=\"\" class=\"wp-image-6900\"\/><\/figure><\/div>\n\n\n\n<p>LCD 1602 \u6db2\u6676\u5c4f\u6a21\u5757\u7684\u4f7f\u7528\uff0c\u4e4b\u524d\u5728\u300c<a href=\"https:\/\/shumeipai.nxez.com\/2017\/03\/14\/server-monitoring-assistant-based-on-php-probe-and-python-crawler.html\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >\u57fa\u4e8ePHP\u63a2\u9488\u548cPython\u722c\u866b\u7684\u670d\u52a1\u5668\u76d1\u63a7<\/a>\u300d\u4e00\u6587\u4e2d\u6709\u8be6\u7ec6\u4ecb\u7ecd\u3002\u8fd9\u4e2a\u5c4f\u5e55\u8fd8\u6709\u4e00\u79cd I2C \u65b9\u5f0f\u7684\u9a71\u52a8\u6a21\u5757\uff0c\u53ef\u4ee5\u6781\u5927\u7684\u7b80\u5316\u63a5\u7ebf\u548c\u9a71\u52a8\u7a0b\u5e8f\u3002\u4e0b\u9762\u6811\u8393\u6d3e\u5b9e\u9a8c\u5ba4\u5c06\u5bf9\u8fd9\u4e2a\u6a21\u5757\u7684\u4f7f\u7528\u505a\u4e00\u4e2a\u4ecb\u7ecd\u3002<br><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u786c\u4ef6\u8bf4\u660e<\/h4>\n\n\n\n<p>\u5e26 I2C \u6a21\u5757\u7684 1602 \u5c4f\u5e55\u80cc\u9762\u5982\u56fe\u6240\u793a\u3002\u6a21\u5757\u4e0a\u6709\u4e00\u9897\u53ef\u8c03\u7535\u963b\uff0c\u7528\u4e8e\u8c03\u8282\u663e\u793a\u7684\u5bf9\u6bd4\u5ea6\u3002\uff08<a href=\"http:\/\/link.nxez.com\/buy\/lcd1602a\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >\u8d2d\u4e70\u94fe\u63a5<\/a>\uff0c\u5982\u679c\u4f60\u65b0\u62ff\u5230\u4e00\u5757\u5c4f\u5e55\u65e0\u8bba\u600e\u4e48\u8c03\u8bd5\u90fd\u4e0d\u89c1\u663e\u793a\uff0c\u8bb0\u5f97\u8c03\u8282\u4e00\u4e0b\u8fd9\u91cc\uff09<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2020\/06\/20200617000057183-0.jpg\" alt=\"\" class=\"wp-image-6902\"\/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">\u63a5\u7ebf<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1234<\/td><td><code>GND --- GND<\/code><code>VCC --- \u63a5\u6811\u8393\u6d3e 5V<\/code><code>SDA --- I2C \u6570\u636e<\/code><code>SCL --- I2C \u65f6\u949f<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2015\/03\/rpi-pins-40-0.png\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2020\/06\/20200617000045646-0.jpg\" alt=\"\" class=\"wp-image-6901\"\/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">\u542f\u7528\u6811\u8393\u6d3e\u7684I2C\u529f\u80fd<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>123<\/td><td><code>sudo<\/code> <code>apt-get <\/code><code>install<\/code> <code>-y python-smbus<\/code><code>sudo<\/code> <code>apt-get <\/code><code>install<\/code> <code>-y i2c-tools<\/code><code>sudo<\/code> <code>raspi-config<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2019\/04\/20190429203718829-0.png\" alt=\"\" class=\"wp-image-5783\"\/><\/figure><\/div>\n\n\n\n<p>\u6309\u7167\u4e0b\u9762\u7684\u6b65\u9aa4\u8bbe\u7f6e\u5f00\u542f I2C \u529f\u80fd\u3002<br><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2019\/04\/20190429203719366-0.png\" alt=\"\" class=\"wp-image-5784\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2019\/04\/20190429203720724-0.png\" alt=\"\" class=\"wp-image-5785\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2019\/04\/20190429203722164-0.png\" alt=\"\" class=\"wp-image-5780\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2019\/04\/20190429203723525-0.png\" alt=\"\" class=\"wp-image-5787\"\/><\/figure><\/div>\n\n\n\n<p>\u91cd\u542f\u6811\u8393\u6d3e\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td><code>sudo<\/code> <code>reboot<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7136\u540e\u8fd0\u884c\u4e0b\u9762\u7684\u547d\u4ee4\u770b\u4e00\u4e0b\u662f\u5426\u8bc6\u522b\u5230\u5c4f\u5e55\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td><code>sudo<\/code> <code>i2cdetect -y 1<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2020\/06\/20200617001940512-0.jpg\" alt=\"\" class=\"wp-image-6903\"\/><\/figure><\/div>\n\n\n\n<p>\u770b\u5230\u5730\u5740\u4e3a 0x27\uff0c\u8fd9\u8bf4\u660e\u5df2\u7ecf\u6210\u529f\u8fde\u63a5\u4e86 LCD1602 \u5c4f\u5e55\u3002\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u7528 Python \u63a7\u5236 LCD1602 \u663e\u793a\u4fe1\u606f\u4e86\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f16\u7a0b<\/h4>\n\n\n\n<p>\u8fd9\u91cc\u7528\u5230\u4e00\u4e2a\u9a71\u52a8\u7a0b\u5e8f\uff0c\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u4fdd\u5b58\u4e3a LCD1602.py\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import time\nimport smbus\nBUS = smbus.SMBus(1)\nLCD_ADDR = 0x27\nBLEN = 1 #turn on\/off background light\n \ndef turn_light(key):\n    global BLEN\n    BLEN = key\n    if key ==1 :\n        BUS.write_byte(LCD_ADDR ,0x08)\n    else:\n        BUS.write_byte(LCD_ADDR ,0x00)\n \ndef write_word(addr, data):\n    global BLEN\n    temp = data\n    if BLEN == 1:\n        temp |= 0x08\n    else:\n        temp &amp;= 0xF7\n    BUS.write_byte(addr ,temp)\n \ndef send_command(comm):\n    # Send bit7-4 firstly\n    buf = comm &amp; 0xF0\n    buf |= 0x04               # RS = 0, RW = 0, EN = 1\n    write_word(LCD_ADDR ,buf)\n    time.sleep(0.002)\n    buf &amp;= 0xFB               # Make EN = 0\n    write_word(LCD_ADDR ,buf)\n     \n    # Send bit3-0 secondly\n    buf = (comm &amp; 0x0F) &lt;&lt; 4\n    buf |= 0x04               # RS = 0, RW = 0, EN = 1\n    write_word(LCD_ADDR ,buf)\n    time.sleep(0.002)\n    buf &amp;= 0xFB               # Make EN = 0\n    write_word(LCD_ADDR ,buf)\n \ndef send_data(data):\n    # Send bit7-4 firstly\n    buf = data &amp; 0xF0\n    buf |= 0x05               # RS = 1, RW = 0, EN = 1\n    write_word(LCD_ADDR ,buf)\n    time.sleep(0.002)\n    buf &amp;= 0xFB               # Make EN = 0\n    write_word(LCD_ADDR ,buf)\n     \n    # Send bit3-0 secondly\n    buf = (data &amp; 0x0F) &lt;&lt; 4\n    buf |= 0x05               # RS = 1, RW = 0, EN = 1\n    write_word(LCD_ADDR ,buf)\n    time.sleep(0.002)\n    buf &amp;= 0xFB               # Make EN = 0\n    write_word(LCD_ADDR ,buf)\n \ndef init_lcd():\n    try:\n        send_command(0x33) # Must initialize to 8-line mode at first\n        time.sleep(0.005)\n        send_command(0x32) # Then initialize to 4-line mode\n        time.sleep(0.005)\n        send_command(0x28) # 2 Lines &amp; 5*7 dots\n        time.sleep(0.005)\n        send_command(0x0C) # Enable display without cursor\n        time.sleep(0.005)\n        send_command(0x01) # Clear Screen\n        BUS.write_byte(LCD_ADDR ,0x08)\n    except:\n        return False\n    else:\n        return True\n \ndef clear_lcd():\n    send_command(0x01) # Clear Screen\n \ndef print_lcd(x, y, str):\n    if x &lt; 0:\n        x = 0\n    if x > 15:\n        x = 15\n    if y &lt;0:\n        y = 0\n    if y > 1:\n        y = 1\n \n    # Move cursor\n    addr = 0x80 + 0x40 * y + x\n    send_command(addr)\n     \n    for chr in str:\n        send_data(ord(chr))\n \nif __name__ == '__main__':\n    init_lcd()\n    print_lcd(0, 0, 'Hello, world!')<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u7f16\u5199\u4e00\u4e2a\u663e\u793a\u5f53\u524d\u65f6\u95f4\u7684\u793a\u4f8b\u7a0b\u5e8f\uff0c\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u4fdd\u5b58\u4e3a time.py\u3002\u6587\u4ef6\u9700\u8981\u4fdd\u5b58\u4e0e LCD1602.py \u540c\u4e00\u4e2a\u76ee\u5f55\u4e0b\u9762\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/user\/bin\/env python \nimport smbus\nimport time\nimport sys\nimport LCD1602 as LCD\n \nif __name__ == '__main__':  \n    LCD.init_lcd()\n    time.sleep(1)\n    LCD.print_lcd(2, 0, 'WWW.QUWJ.COM')\n    for x in xrange(1, 4):\n        LCD.turn_light(0)\n        LCD.print_lcd(4, 1, 'LIGHT OFF')\n        time.sleep(0.5)\n        LCD.turn_light(1)\n        LCD.print_lcd(4, 1, 'LIGHT ON ')\n        time.sleep(0.5)\n \n    LCD.turn_light(0)\n     \n    while True:\n        now = time.strftime('%m\/%d %H:%M:%S', time.localtime(time.time()))\n        LCD.print_lcd(1, 1, now)\n        time.sleep(0.2)<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d LCD.print_lcd() \u7528\u6765\u663e\u793a\u5b57\u7b26\uff0c\u524d\u4e24\u4e2a\u53c2\u6570\u5206\u522b\u8868\u793a X\u3001Y \u5750\u6807\uff08\u4ece 0 \u5f00\u59cb\uff09\uff0c\u540e\u9762\u7684\u5185\u5bb9\u5c06\u4ece\u8fd9\u4e2a\u5750\u6807\u7684\u4f4d\u7f6e\u5f00\u59cb\u663e\u793a\u3002\u7b2c\u4e09\u4e2a\u53c2\u6570\u5c31\u662f\u8981\u663e\u793a\u7684\u5185\u5bb9\u4e86\u3002<br>LCD.turn_light(0) \u8868\u793a\u5173\u95ed\u80cc\u5149\uff0cLCD.turn_light(1) \u8868\u793a\u6253\u5f00\u80cc\u5149\u3002<\/p>\n\n\n\n<p>\u5728\u7a0b\u5e8f\u76ee\u5f55\u4e0b\u6267\u884c\u4e0b\u9762\u7684\u547d\u4ee4\u8fd0\u884c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td><code>sudo<\/code> <code>python <\/code><code>time<\/code><code>.py<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2020\/06\/20200616235923858-0.jpg\" alt=\"\" class=\"wp-image-6897\"\/><\/figure><\/div>\n\n\n\n<p>\u4f60\u8fd8\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\uff0c\u7f16\u5199\u663e\u793a IP \u5730\u5740\uff08<a href=\"https:\/\/shumeipai.nxez.com\/2019\/05\/29\/make-the-raspberry-pi-speak-its-own-ip-address.html\" rel=\"noreferrer noopener\" target=\"_blank\" rel=\"nofollow\" >\u53c2\u8003\u8fd9\u91cc<\/a>\uff09\u3001\u7cfb\u7edf\u72b6\u6001\u4fe1\u606f\u7684\u7a0b\u5e8f\uff0c\u5e76\u8bbe\u7f6e\u6210\u5f00\u673a\u542f\u52a8\uff08<a href=\"https:\/\/shumeipai.nxez.com\/2019\/04\/29\/use-the-ssd1306-oled-display-on-the-raspberry-pi.html\" rel=\"noreferrer noopener\" target=\"_blank\" rel=\"nofollow\" >\u53c2\u8003\u8fd9\u91cc<\/a>\uff09\u6765\u4f7f\u7528\u8fd9\u5757\u5c4f\u5e55\u3002<strong>\u6587\u7ae0\u6807\u9898\uff1a<\/strong><a href=\"https:\/\/shumeipai.nxez.com\/2020\/06\/17\/raspberry-pi-drives-lcd1602-screen-through-i2c.html\" target=\"_blank\"  rel=\"nofollow\" >\u6811\u8393\u6d3e\u901a\u8fc7 I2C \u9a71\u52a8 LCD1602 \u6db2\u6676\u5c4f<\/a> - <a href=\"https:\/\/shumeipai.nxez.com\" target=\"_blank\"  rel=\"nofollow\" >\u6811\u8393\u6d3e\u5b9e\u9a8c\u5ba4<\/a><strong>\u56fa\u5b9a\u94fe\u63a5\uff1a<\/strong><a href=\"https:\/\/shumeipai.nxez.com\/2020\/06\/17\/raspberry-pi-drives-lcd1602-screen-through-i2c.html\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/shumeipai.nxez.com\/2020\/06\/17\/raspberry-pi-drives-lcd1602-screen-through-i2c.html<\/a><\/p>\n<button class=\"simplefavorite-button\" data-postid=\"866\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>LCD 1602 \u6db2\u6676\u5c4f\u6a21\u5757\u7684\u4f7f\u7528\uff0c\u4e4b\u524d\u5728\u300c\u57fa\u4e8ePHP\u63a2\u9488\u548cPython\u722c\u866b\u7684\u670d\u52a1\u5668\u76d1\u63a7\u300d\u4e00\u6587\u4e2d\u6709\u8be6\u7ec6\u4ecb\u7ecd\u3002\u8fd9\u4e2a\u5c4f\u5e55\u8fd8\u6709\u4e00\u79cd I2 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-866","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=866"}],"version-history":[{"count":0,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/866\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}