快捷搜索:  汽车  科技

esp-01s要和单片机分开烧录吗(ESP-mp-01开发板I2C驱动OLED显示)

esp-01s要和单片机分开烧录吗(ESP-mp-01开发板I2C驱动OLED显示)import mathimport ssd1306# main.py -- put your code here!import machinefrom machine import Pin I2C

ESP-mp-01开发板是基于ESP8266 Soc的可以运行MicroPython的开发板,不像STM32版本的Pyboard,ESP8266本身没有USB,只能通过TTL串口和Wifi访问ESP8266。文件传输需要借助uPyLoader或ESPyHarp等工具来进行文件传输。此次使用开发板的I2C功能来驱动OLED显示。

【1】硬件连接:

esp-01s要和单片机分开烧录吗(ESP-mp-01开发板I2C驱动OLED显示)(1)

esp-01s要和单片机分开烧录吗(ESP-mp-01开发板I2C驱动OLED显示)(2)

ESP8266 I2C对应的引脚为Pin(14)--SCL,Pin(2)--SDA:

【2】程序源码:

  1. # main.py -- put your code here!

  2. import machine

  3. from machine import Pin I2C

  4. import ssd1306

  5. import math

  6. import time

  7. # construct an I2C bus

  8. i2c = I2C(scl=Pin(14) sda=Pin(2) freq=100000)

  9. display = ssd1306.SSD1306_I2C(128 64 i2c)

  10. led_blue = machine.Pin(2 Pin.OUT) # 设置 GPIO2 为输出

  11. led_blue.high()

  12. try:

  13. display.poweron()

  14. display.init_display()

  15. display.text('PYBNano I2C OLED' 1 1)

  16. display.text('Hi MicroPython!' 1 16)

  17. display.text('By: hbzjt2012' 1 31)

  18. # Write display buffer

  19. display.show()

  20. time.sleep(10)

  21. display.fill(0)

  22. for x in range(0 128):

  23. display.pixel(x 32 int(math.sin(x/64*math.pi)*7 8) 1)

  24. display.show()

  25. time.sleep(10)

  26. display.fill(0)

  27. x = 0

  28. y = 0

  29. direction_x = True

  30. direction_y = True

  31. while True:

  32. # Clear the previous lines

  33. prev_x = x

  34. prev_y = y

  35. # Move bars

  36. x = (1 if direction_x else -1)

  37. y = (1 if direction_y else -1)

  38. # Bounce back if required

  39. if x == 128:

  40. direction_x = False

  41. x = 126

  42. elif x == -1:

  43. direction_x = True

  44. x = 1

  45. if y == 64:

  46. direction_y = False

  47. y = 63

  48. elif y == -1:

  49. direction_y = True

  50. y = 1

  51. # Draw new lines

  52. for i in range(64):

  53. display.pixel(prev_x i False)

  54. display.pixel(x i True)

  55. for i in range(128):

  56. display.pixel(i prev_y False)

  57. display.pixel(i y True)

  58. # Make sure the corners are active

  59. display.pixel(0 0 True)

  60. display.pixel(127 0 True)

  61. display.pixel(0 63 True)

  62. display.pixel(127 63 True)

  63. # Write display buffer

  64. display.show()

  65. except Exception as ex:

  66. led_blue.low()

  67. print('Unexpected error: {0}'.format(ex))

  68. display.poweroff()

复制代码

esp-01s要和单片机分开烧录吗(ESP-mp-01开发板I2C驱动OLED显示)(3)

esp-01s要和单片机分开烧录吗(ESP-mp-01开发板I2C驱动OLED显示)(4)

以上图文内容均是EEWORLD论坛网友:hanyeguxingwo原创,在此感谢。

欢迎微博@EEWORLD

如果你也写过此类原创干货请关注EEWORLD(电子工程世界)回复“投稿”,也可将你的原创发至:bbs_service@eeworld.com.cn,一经入选,我们将帮你登上头条!

与更多行业内网友进行交流请登陆EEWORLD论坛。

猜您喜欢: