zoukankan      html  css  js  c++  java
  • Pyboard基本功能快速浏览

    基本功能快速浏览

    1.通用控制

    >>> import pyb        #导入pyb包
    >>> pyb.delay(50)   #延时50ms
    >>> pyb.udelay(50) #延时50us
    >>> pyb.repl_uart(pyb.UART(1,9600))  #指定REPL到串口1,波特率为9600
    >>> pyb.millis()       #返回开机后的运行时间毫秒
    4954710
    >>> pyb.freq()    #返回CPU和总线的频率
    (168000000, 168000000, 42000000, 84000000)    
    >>> pyb.freq(168000000)    #设置CPU频率为168MHZ
    >>> pyb.wfi()      #暂停CPU,等待中断唤醒
    >>> pyb.stop()     #停止CPU,等待外部中断唤

     2.LED控制

    >>> from pyb import LED
    >>> r_led = LED(1)  #1=红, 2=绿, 3=黄, 4=蓝
    >>> r_led.on()        #点亮
    >>> r_led.off()       #熄灭
    >>> r_led.toggle()    #翻转
    >>> b_led = LED(4)
    >>> b_led.intensity(128)  # LEDs 3 和 4 支持 PWM 调节亮度 (0-255)
    

     3.Pin和GPIO的使用

    >>> from pyb import Pin
    >>> output = Pin('X1',Pin.OUT_PP)  #X1设置为输出
    >>> output.name()  #获取该端口在芯片上对应引脚的名称
    'A0'
    >>> output.names()  #获取该引脚定义好的全部名称
    ['A0', 'X1']
    >>> output.high()  #设置该引脚上的电平状态为高电平
    >>> output.low()   #设置该引脚上的电平状态为低电平
    >>> output.value() #获取该引脚当前的电平状态
    0
    >> intput = Pin('X2',Pin.IN,Pin.PULL_UP)  #设置X2引脚为输入,并使能内部上拉电阻
    

     4.舵机控制(Servo)

    >>> help(pyb.Servo)
    object <class 'Servo'> is of type type
      pulse_width -- <function>
      calibration -- <function>
      angle -- <function>
      speed -- <function>
    >>> from pyb import Servo
    >>> servo1 = Servo(1)  #板子上共有四个servo可以直接使用(X1-X4)
    >>> servo1.angle(45)   #旋转到45°位置
    >>> servo1.angle()      #获取舵机轴的位置角度
    44
    >>> servo1.angle(-45,2000)  #2000毫秒内从当前的角度转到-45°角的位置
    >>> servo1.speed(50)   #以速度50转动
    >>> servo1.speed()
    50
    >>> servo1.speed(100)
    >>> servo1.speed()
    100
    >>> servo1.pulse_width(2200)  #改变PWM的脉冲宽度
    >>> servo1.calibration()  #参数校准
    (640, 2420, 1500, 2470, 2200)
    #servo1.calibration()里面五个形参依次如下:
    #pulse_min 是允许的最小脉冲宽度
    #pulse_max 是允许的最大脉冲宽度
    #pulse_centre 是对应于中心/零位置的脉冲宽度
    #pulse_angle_90 是对应于90度的脉冲宽度
    #pulse_speed_100 是对应于速度100的脉冲宽度

     5.外部中断 

    >>> from pyb import Pin,ExtInt
    >>> callback = lambda e: print('interrupt!')  #此处的e必须要,不然会报错
    >>> ext = ExtInt(Pin('Y1'),ExtInt.IRQ_RISING,Pin.PULL_NONE,callback)
    >>> interrupt!
    interrupt!
    interrupt!
    interrupt!
    interrupt!
    interrupt!
    interrupt!
    interrupt!
    interrupt!
    .....

     6.定时器

    >>> from pyb import Timer
    >>> tim = Timer(1,freq=1000)  #定义定时器1,工作频率是1000hz
    >>> tim.counter()  #读取计数值
    19666
    >>> tim.freq(0.5)  #设定定时器频率为0.5hz
    >>> tim.callback(lambda t:pyb.LED(1).toggle()  #设置回调函数
    >>>

    7.脉宽调制模块(PWM)

    >>> from pyb import Pin,Timer
    >>> p = Pin('X1')  #X1是定时器2的CH1
    >>> tim = Timer(2,freq=1000)
    >>> ch = tim.channel(1,Timer.PWM,pin=p) #设置PWM引脚
    >>> ch.pulse_width_percent(50)  #设置PWM输出占空比

     8.模数转换(ADC)

    >>> from pyb import Pin,ADC
    >>> adc = ADC(Pin('X19'))  #设定ADC输入引脚
    >>> adc.read()  #读取ADC转换结果,默认12位方式,参数范围0~4095

    9.数模转换(DAC)

    >>> from pyb import Pin,DAC
    >>> dac = DAC(Pin('X5'))  #设置DAC输出引脚
    >>> dac.write(120)  #设置输出电压,默认8位模式,参数范围0~255
    

     10.UART(串口)

    >>> from pyb import UART
    >>> uart = UART(1,9600)  #设置串口号以及波特率
    >>> uart.write('hello')  #输出
    5
    >>> uart.read(5)  #最多读取5个字节

    11.SPI总线

    >>> from pyb import SPI
    >>> spi = SPI(1,SPI.MASTER,baudrate=200000,polarity=1,phase=0)  #设置SPI参数
    >>> spi.send('hello')  
    >>> spi.recv(5)  #读取5个字节
    b'xffxffxffxffxff'
    >>> spi.send_recv('hello')  #发送并接收5个字节

     12.I2C总线

    from machine import I2C
    
    i2c = I2C('X', freq=400000)                        # 定义硬件I2C对象
    i2c = I2C(scl='X1', sda='X2', freq=100000)         # 定义软件I2C对象
    
    i2c.scan()                          # 返回扫描到的从机地址
    i2c.writeto(0x42, 'hello')          # 往地址为 0x42 的从机写5个字节
    i2c.readfrom(0x42, 5)               # 从地址为 0x42 的从机读取5个字节
    
    i2c.readfrom_mem(0x42, 0x10, 2)     # 从设备地址 0x42和存储器地址为0x10中读取2个字节
    i2c.writeto_mem(0x42, 0x10, 'xy')   # 从设备地址 0x42和存储器地址为0x10中写入2个字节

     13.板载三轴加速度传感器

    >>> from pyb import Accel
    >>> accel = Accel()
    >>> accel.x()  #读取X轴的数据
    8
    >>> print(accel.x(),accel.y(),accel.z())  #打印三轴的数据
    8 -8 -18

     14.板载按键

    >>> from pyb import Switch
    >>> switch = Switch()
    >>> switch.value()
    False
    >>> switch.callback(lambda:pyb.LED(4).toggle())

     15.实时时钟(RTC)

    >>> from pyb import RTC
    >>> rtc = RTC()
    >>> rtc.datetime((2019,9,10,2,18,56,0,0))  #设置年、月、日、星期、时、分、秒、次秒
    >>> rtc.datetime()  #返回当前系统时间
    (2019, 9, 10, 2, 18, 56, 9, 188)

    16.CAN总线 (区域网络控制)

    >>> from pyb import CAN
    >>> can = CAN(1,CAN.LOOPBACK)
    >>> can.setfilter(0,CAN.LIST16,0,(123,124,125,126))
    >>> can.send('message!',123)  #发送ID为123的消息
    >>> can.recv(0)  #在FIF0 0 上接收信息
    (123, False, 0, b'message!')
    

     下一章节:Pyboard基础功能详细探索---LED

  • 相关阅读:
    MongoDB 释放磁盘空间 db.runCommand({repairDatabase: 1 })
    RK 调试笔记
    RK Android7.1 拨号
    RK Android7.1 移植gt9271 TP偏移
    RK Android7.1 定制化 itvbox 盒子Launcher
    RK Android7.1 双屏显示旋转方向
    RK Android7.1 设置 内存条作假
    RK Android7.1 设置 蓝牙 已断开连接
    RK Android7.1 进入Camera2 亮度会增加
    RK 3128 调触摸屏 TP GT9XX
  • 原文地址:https://www.cnblogs.com/iBoundary/p/11495199.html
Copyright © 2011-2022 走看看