MicroPython中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 6123|回复: 0

WiPy 的快速参考

[复制链接]

13

主题

14

帖子

731

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
731
发表于 2022-1-20 09:13:14 | 显示全部楼层 |阅读模式
WiPy 的快速参考

以下是 CC3200/WiPy 的快速参考。如果这是您第一次使用该板,请考虑先阅读以下部分:


通用板控制(包括睡眠模式)

S查看machine模块:

  1. import machine

  2. help(machine) # display all members from the machine module
  3. machine.freq() # get the CPU frequency
  4. machine.unique_id() # return the 6-byte unique id of the board (the WiPy's MAC address)

  5. machine.idle()        # average current decreases to (~12mA), any interrupts wake it up
  6. machine.lightsleep()  # everything except for WLAN is powered down (~950uA avg. current)
  7.                       # wakes from Pin, RTC or WLAN
  8. machine.deepsleep()   # deepest sleep mode, MCU starts from reset. Wakes from Pin and RTC.
复制代码




引脚和 GPIO

参见 machine.Pin.

  1. from machine import Pin

  2. # initialize GP2 in gpio mode (alt=0) and make it an output
  3. p_out = Pin('GP2', mode=Pin.OUT)
  4. p_out.value(1)
  5. p_out.value(0)
  6. p_out.toggle()
  7. p_out(True)

  8. # make GP1 an input with the pull-up enabled
  9. p_in = Pin('GP1', mode=Pin.IN, pull=Pin.PULL_UP)
  10. p_in() # get value, 0 or 1
复制代码




计时器

请参阅 machine.TimerWiPymachine.Pin。Timerid的取值从 0 到 3。:

  1. from machine import Timer
  2. from machine import Pin

  3. tim = Timer(0, mode=Timer.PERIODIC)
  4. tim_a = tim.channel(Timer.A, freq=1000)
  5. tim_a.freq(5) # 5 Hz

  6. p_out = Pin('GP2', mode=Pin.OUT)
  7. tim_a.irq(trigger=Timer.TIMEOUT, handler=lambda t: p_out.toggle())
复制代码




PWM(脉宽调制)

请参阅machine.Pinmachine.Timer

  1. from machine import Timer

  2. # timer 1 in PWM mode and width must be 16 buts
  3. tim = Timer(1, mode=Timer.PWM, width=16)

  4. # enable channel A @1KHz with a 50.55% duty cycle
  5. tim_a = tim.channel(Timer.A, freq=1000, duty_cycle=5055)
复制代码




ADC(模数转换)

参见machine.ADCWiPy.

  1. from machine import ADC

  2. adc = ADC()
  3. apin = adc.channel(pin='GP3')
  4. apin() # read value, 0-4095
复制代码




UART(串行总线)

参见 machine.UART.

  1. from machine import UART
  2. uart = UART(0, baudrate=9600)
  3. uart.write('hello')
  4. uart.read(5) # read up to 5 bytes
复制代码




SPI总线

请参阅machine.SPI.

  1. from machine import SPI

  2. # configure the SPI master @ 2MHz
  3. spi = SPI(0, SPI.MASTER, baudrate=200000, polarity=0, phase=0)
  4. spi.write('hello')
  5. spi.read(5) # receive 5 bytes on the bus
  6. rbuf = bytearray(5)
  7. spi.write_readinto('hello', rbuf) # send and receive 5 bytes
复制代码




I2C总线

参见 machine.I2C.

  1. from machine import I2C
  2. # configure the I2C bus
  3. i2c = I2C(baudrate=100000)
  4. i2c.scan() # returns list of slave addresses
  5. i2c.writeto(0x42, 'hello') # send 5 bytes to slave with address 0x42
  6. i2c.readfrom(0x42, 5) # receive 5 bytes from slave
  7. i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x10
  8. i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
复制代码




看门狗定时器 (WDT)

参见 machine.WDT.

  1. from machine import WDT

  2. # enable the WDT with a timeout of 5s (1s is the minimum)
  3. wdt = WDT(timeout=5000)
  4. wdt.feed()
复制代码




实时时钟 (RTC)

见机器. machine.RTC

  1. from machine import RTC

  2. rtc = RTC() # init with default time and date
  3. rtc = RTC(datetime=(2015, 8, 29, 9, 0, 0, 0, None)) # init with a specific time and date
  4. print(rtc.now())

  5. def alarm_handler (rtc_o):
  6.     pass
  7.     # do some non blocking operations
  8.     # warning printing on an irq via telnet is not
  9.     # possible, only via UART

  10. # create a RTC alarm that expires after 5 seconds
  11. rtc.alarm(time=5000, repeat=False)

  12. # enable RTC interrupts
  13. rtc_i = rtc.irq(trigger=RTC.ALARM0, handler=alarm_handler, wake=machine.SLEEP)

  14. # go into suspended mode waiting for the RTC alarm to expire and wake us up
  15. machine.lightsleep()
复制代码




SD卡

参见 machine.SD.

  1. from machine import SD
  2. import os

  3. # clock pin, cmd pin, data0 pin
  4. sd = SD(pins=('GP10', 'GP11', 'GP15'))
  5. # or use default ones for the expansion board
  6. sd = SD()
  7. os.mount(sd, '/sd')
复制代码




无线局域网 (WiFi)

请参阅 network.WLANmachine.

  1. import machine
  2. from network import WLAN

  3. # configure the WLAN subsystem in station mode (the default is AP)
  4. wlan = WLAN(mode=WLAN.STA)
  5. # go for fixed IP settings
  6. wlan.ifconfig(config=('192.168.0.107', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
  7. wlan.scan()     # scan for available networks
  8. wlan.connect(ssid='mynetwork', auth=(WLAN.WPA2, 'mynetworkkey'))
  9. while not wlan.isconnected():
  10.     pass
  11. print(wlan.ifconfig())
  12. # enable wake on WLAN
  13. wlan.irq(trigger=WLAN.ANY_EVENT, wake=machine.SLEEP)
  14. # go to sleep
  15. machine.lightsleep()
  16. # now, connect to the FTP or the Telnet server and the WiPy will wake-up
复制代码




Telnet 和 FTP 服务器

network.Server

  1. from network import Server

  2. # init with new user, password and seconds timeout
  3. server = Server(login=('user', 'password'), timeout=60)
  4. server.timeout(300) # change the timeout
  5. server.timeout() # get the timeout
  6. server.isrunning() # check whether the server is running or not
复制代码




心跳指示

wipy.

  1. import wipy

  2. wipy.heartbeat(False)  # disable the heartbeat LED
  3. wipy.heartbeat(True)   # enable the heartbeat LED
  4. wipy.heartbeat()       # get the heartbeat state
复制代码





回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|MicroPython中文社区 ( 粤ICP备15040352号 )microPython技术交流

粤公网安备 44030702004355号

GMT+8, 2024-11-25 07:49 , Processed in 0.124800 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表