def tone(pin, frequency, duration=None): """Generates a square wave of the specified frequency on pin. Args: - pin(PWM1 to PWM6): A PWM pin - frequency(int): A value between 1 and 500000 - duration(int): tone's durations in second """ global pwm_pins if pin not in pwm_pins: print "Pin must be one of:" for key in pwm_pins: print key exit(1) if frequency < 1 or frequency > 500000: print "Invalid frequency" exit(1) initMatch(0, 1000000/frequency) initMatch(pwm_pins[pin], 500000/frequency) PWM_ChannelCmd(LPC_PWM1, pwm_pins[pin], FunctionalState.ENABLE) PWM_ResetCounter(LPC_PWM1) PWM_CounterCmd(LPC_PWM1, FunctionalState.ENABLE) PWM_Cmd(LPC_PWM1, FunctionalState.ENABLE) if duration: t = threading.Timer(float(duration)/1000.0, noTone, [pin]) t.start()
def analogWrite(pin, value): """Writes an analog value (PWM wave) to a pin. Args: - pin(PWM1 to PWM6): A PWM pin - value(int): Duty cycle, a value between 0 and 255 """ global pwm_pins if pin not in pwm_pins: print "Pin must be one of:" for key in pwm_pins: print key exit(1) if value < 0 or value > 255: print "Invalid duty cycle" exit(1) # 490Hz period = 1000000/490 pulse_width = period*value/255 initMatch(0, period) initMatch(pwm_pins[pin], pulse_width) PWM_ChannelCmd(LPC_PWM1, pwm_pins[pin], FunctionalState.ENABLE) PWM_ResetCounter(LPC_PWM1) PWM_CounterCmd(LPC_PWM1, FunctionalState.ENABLE) PWM_Cmd(LPC_PWM1, FunctionalState.ENABLE)
def add_channel(self, raw_channel, init_pulse): """Add PWM channel with a 1.5ms pulse. """ try: channel = int(raw_channel) if channel < 1 or channel > 5: raise InputError except: self.log.warning("%s is an invalid channel." % raw_channel) return None # Check if channel has already been added if channel in self.channels: self.log.warning("Channel %d has already been initialized." % channel) return None self.channels.append(channel) initMatch(channel, init_pulse) PWM_ChannelCmd(LPC_PWM1, channel, FunctionalState.ENABLE) #TODO: might need counter commands here. check this first. self.log.info("Initializing BLDC pwm channel %d." % channel)
def __init__(self, config): log = config.get('log_facility') period = config.getint('bldc', 'pwm_period') log.info("Set pwm period to %d" % period) initMatch(0, period) PWM_ResetCounter(LPC_PWM1) PWM_CounterCmd(LPC_PWM1, FunctionalState.ENABLE) PWM_Cmd(LPC_PWM1, FunctionalState.ENABLE) self.log = log self.config = config self.channels = [] channels = config.getlist('bldc', 'channels') init_pulse = config.getint('bldc', 'init_pulse') log.info("Attempting to add channel(s) %s at pulse %d" % (channels, init_pulse)) for channel in channels: self.add_channel(channel, init_pulse) log.info("Initialized BLDC motor controller.")
def initPeriod(period): initMatch(0, period)
def initPulse(channel, pulse_width): initMatch(channel, pulse_width)