circuitpython中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

WiPy 的快速参考

[复制链接]

12

主题

12

帖子

648

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
648
发表于 2021-11-19 09:07:25 | 显示全部楼层 |阅读模式
WiPy 的快速参考

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


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

S查看machine模块:

import machinehelp(machine) # display all members from the machine modulemachine.freq() # get the CPU frequencymachine.unique_id() # return the 6-byte unique id of the board (the WiPy's MAC address)machine.idle()        # average current decreases to (~12mA), any interrupts wake it upmachine.lightsleep()  # everything except for WLAN is powered down (~950uA avg. current)                      # wakes from Pin, RTC or WLANmachine.deepsleep()   # deepest sleep mode, MCU starts from reset. Wakes from Pin and RTC.


引脚和 GPIO

参见 machine.Pin.

from machine import Pin# initialize GP2 in gpio mode (alt=0) and make it an outputp_out = Pin('GP2', mode=Pin.OUT)p_out.value(1)p_out.value(0)p_out.toggle()p_out(True)# make GP1 an input with the pull-up enabledp_in = Pin('GP1', mode=Pin.IN, pull=Pin.PULL_UP)p_in() # get value, 0 or 1


计时器

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

from machine import Timerfrom machine import Pintim = Timer(0, mode=Timer.PERIODIC)tim_a = tim.channel(Timer.A, freq=1000)tim_a.freq(5) # 5 Hzp_out = Pin('GP2', mode=Pin.OUT)tim_a.irq(trigger=Timer.TIMEOUT, handler=lambda t: p_out.toggle())


PWM(脉宽调制)

请参阅machine.Pinmachine.Timer

from machine import Timer# timer 1 in PWM mode and width must be 16 butstim = Timer(1, mode=Timer.PWM, width=16)# enable channel A @1KHz with a 50.55% duty cycletim_a = tim.channel(Timer.A, freq=1000, duty_cycle=5055)


ADC(模数转换)

参见machine.ADCWiPy.

from machine import ADCadc = ADC()apin = adc.channel(pin='GP3')apin() # read value, 0-4095


UART(串行总线)

参见 machine.UART.

from machine import UARTuart = UART(0, baudrate=9600)uart.write('hello')uart.read(5) # read up to 5 bytes


SPI总线

请参阅machine.SPI.

from machine import SPI# configure the SPI master @ 2MHzspi = SPI(0, SPI.MASTER, baudrate=200000, polarity=0, phase=0)spi.write('hello')spi.read(5) # receive 5 bytes on the busrbuf = bytearray(5)spi.write_readinto('hello', rbuf) # send and receive 5 bytes


I2C总线

参见 machine.I2C.

from machine import I2C# configure the I2C busi2c = I2C(baudrate=100000)i2c.scan() # returns list of slave addressesi2c.writeto(0x42, 'hello') # send 5 bytes to slave with address 0x42i2c.readfrom(0x42, 5) # receive 5 bytes from slavei2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x10i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10


看门狗定时器 (WDT)

参见 machine.WDT.

from machine import WDT# enable the WDT with a timeout of 5s (1s is the minimum)wdt = WDT(timeout=5000)wdt.feed()


实时时钟 (RTC)

见机器. machine.RTC

from machine import RTCrtc = RTC() # init with default time and datertc = RTC(datetime=(2015, 8, 29, 9, 0, 0, 0, None)) # init with a specific time and dateprint(rtc.now())def alarm_handler (rtc_o):    pass    # do some non blocking operations    # warning printing on an irq via telnet is not    # possible, only via UART# create a RTC alarm that expires after 5 secondsrtc.alarm(time=5000, repeat=False)# enable RTC interruptsrtc_i = rtc.irq(trigger=RTC.ALARM0, handler=alarm_handler, wake=machine.SLEEP)# go into suspended mode waiting for the RTC alarm to expire and wake us upmachine.lightsleep()


SD卡

参见 machine.SD.

from machine import SDimport os# clock pin, cmd pin, data0 pinsd = SD(pins=('GP10', 'GP11', 'GP15'))# or use default ones for the expansion boardsd = SD()os.mount(sd, '/sd')


无线局域网 (WiFi)

请参阅 network.WLANmachine.

import machinefrom network import WLAN# configure the WLAN subsystem in station mode (the default is AP)wlan = WLAN(mode=WLAN.STA)# go for fixed IP settingswlan.ifconfig(config=('192.168.0.107', '255.255.255.0', '192.168.0.1', '8.8.8.8'))wlan.scan()     # scan for available networkswlan.connect(ssid='mynetwork', auth=(WLAN.WPA2, 'mynetworkkey'))while not wlan.isconnected():    passprint(wlan.ifconfig())# enable wake on WLANwlan.irq(trigger=WLAN.ANY_EVENT, wake=machine.SLEEP)# go to sleepmachine.lightsleep()# now, connect to the FTP or the Telnet server and the WiPy will wake-up


Telnet 和 FTP 服务器

network.Server

from network import Server# init with new user, password and seconds timeoutserver = Server(login=('user', 'password'), timeout=60)server.timeout(300) # change the timeoutserver.timeout() # get the timeoutserver.isrunning() # check whether the server is running or not


心跳指示

wipy.

import wipywipy.heartbeat(False)  # disable the heartbeat LEDwipy.heartbeat(True)   # enable the heartbeat LEDwipy.heartbeat()       # get the heartbeat state


回复

使用道具 举报

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

本版积分规则

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

粤公网安备 44030702004354号

GMT+8, 2024-5-17 17:53 , Processed in 0.109200 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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