logo

用到一个定时器,一个pwm,出现报错

2019-04-04 09:36
L
clgd

用到一个定时器,一个pwm,出现报错ValueError: Pin(D12) doesn't have an af for Timer(2)

import sensor, image, time
from pyb import Pin, Timer,LED

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.

RUN = LED(4)
STATUE = LED(1)
tim = Timer(4, prescaler=83,period=370)
#驱动蜂鸣器
def BeepOn():
    tim.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width=187)
#关闭蜂鸣器
def BeepOff():
    tim.channel(1,Timer.PWM, pin=Pin("P7"), pulse_width=0)

def tick(timer):            # we will receive the timer object when being called
    RUN.toggle()
    
tim = Timer(2, freq=1)      # create a timer object using timer 4 - trigger at 1Hz
tim.callback(tick)          # set the callback to our tick function

BeepOn()
while(True):
    img = sensor.snapshot()         # Take a picture and return the image.
                                    # to the IDE. The FPS should increase once disconnected.