Maximum number of PWM or how to get more

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
clack
Posts: 20
Joined: Sat May 03, 2014 11:09 am

Re: Maximum number of PWM or how to get more

Post by clack » Fri Feb 06, 2015 10:54 am

Damien wrote:
clack wrote: I can call the same frequency on all pins except the blue LED as it is fixed?
I can change the timer code so it allows you to control timer 3. If you don't need usb then it's not a problem changing the frequency of this timer. If you don't want to wait for a code change you can access the timer registers using the stm module.
Don't worry I can work around it, it was just if you knew a way to do it in its current state. The REPL over USB is far more useful! Thankyou for showing me what pins I can use, it really helped me to work through the code. Here is the class I wrote to control the pins. ( first time I wrote one so might be a bit clunky! ) map is borrowed from Arduino map function!

Code: Select all

from time import sleep
import pyb



class ledpwm:
	
	def __init__(self,pin,timer,channel,freq):
		
		self.freq = freq
		self.timer = timer
		self.channel = channel
		self.pin = pin
		
		
		if pin == 'X5' or pin == 'X6':
			self.pinpwm = pyb.Pin(pin)
		elif pin == 'P18':
			self.ch = pyb.LED(4)
		else:
			self.pinpwm = pyb.Pin(pin)
			timerset = pyb.Timer(self.timer, freq=self.freq)
			self.ch = timerset.channel(self.channel, pyb.Timer.PWM, pin=self.pinpwm)
	
	def pwm(self,PWM):
		if self.pin == 'X5' or self.pin == 'X6':
			buf = bytearray(100) 
			dac = pyb.DAC(self.pin) 
			for i in range(len(buf)):
				if i < PWM:
					buf[i] = 255
				else:
					buf[i] = 0
			dac.write_timed(buf,self.freq * len(buf), mode=pyb.DAC.CIRCULAR)
		elif self.pin == 'P18':
			PWM = self.map(PWM,0,255,0,256)
			self.ch.intensity(PWM)
		else:
			if self.pin in ('Y6','Y11','Y12'):
				PWM = int(self.map(PWM,0,100,100,0))
			self.ch.pulse_width_percent(PWM)
	
	
	def map(self,x, in_min, in_max, out_min, out_max):
  		return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
  		
  		
Damien wrote:
Clack wrote: When I unplug the USB ( and it is connected to 5v on Vin ) though it is does not boot?
Do the pyboard leds light up in any way? Did you check voltage level on VIN and 3V3? Did you try pressing RST button? :) If it boots via usb then is should boot fine without usb.
Thanks Dave and Damien, Sorry I was caught out by the double sided PCB I made, I had a disconnected through hole. It just seemed like it was not booting!

edit:

ooh one more thing, in REPL soft reboot stops working, I cant work out what pin it is, but is there a pin that breaks soft reboot when it is re-assigned?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Maximum number of PWM or how to get more

Post by dhylands » Fri Feb 06, 2015 4:03 pm

clack wrote: ooh one more thing, in REPL soft reboot stops working, I cant work out what pin it is, but is there a pin that breaks soft reboot when it is re-assigned?
Soft reboot (Control-D) is purely software.

My guess is that one of the interrupt handlers isn't being shutdown properly and the interrupt is firing after the heap gets re-initialized.

We already have code that's supposed to deinitialize the timers, but maybe the DAC insn't being deinitialized? (Just a guess).

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Post by Damien » Fri Feb 06, 2015 6:55 pm

When soft reboot doesn't work does that mean it crashes (like Dave suggests) or just dies nothing?

clack
Posts: 20
Joined: Sat May 03, 2014 11:09 am

Re: Maximum number of PWM or how to get more

Post by clack » Sat Feb 07, 2015 10:02 pm

It flashes yellow and red like an error then comes back after say 20 seconds.

Sometimes if I hit the DAC too fast it flashes all 4 onboard led's and I have to press hard reset.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Maximum number of PWM or how to get more

Post by Damien » Sun Feb 08, 2015 11:06 am

clack wrote:It flashes yellow and red like an error then comes back after say 20 seconds.

Sometimes if I hit the DAC too fast it flashes all 4 onboard led's and I have to press hard reset.
So it is doing a soft reset, but then it crashes in the boot.py or main.py file (4 flashes for boot.py, 3 flashes for main.py). It's probably something to do with the DAC not reinitialising correctly.

clack
Posts: 20
Joined: Sat May 03, 2014 11:09 am

Re: Maximum number of PWM or how to get more

Post by clack » Thu Feb 12, 2015 11:15 am

Thanks for the help both of you! going to post images and details soon!

Are you saying this is a bug in micro python or is this something I need to correct in my code? I can see in the documentation if I am missing something out? should I write a script that does it and submit it as an issue?

When I write to the DAC outputs it crashes ( 4 led way not 2 led flash ) usually if it is too fast, say if I am doing it at a .1 second interval.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Maximum number of PWM or how to get more

Post by dhylands » Thu Feb 12, 2015 11:09 pm

I think that its a bug in MicroPython (gut feeling)

Gordon_Hardman
Posts: 68
Joined: Sat May 03, 2014 11:31 pm

Re: Maximum number of PWM or how to get more

Post by Gordon_Hardman » Mon May 11, 2015 4:29 pm

I have noticed this too. I get this on RealTerm, after a ctrl-d:

>>>
PYB: sync filesystems
PYB: soft reboot

And if I wait about a minute or so, I get:

Micro Python v1.4.2-57-gc4dc1b5 on 2015-05-04; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>>

I have a fairly large application, and at some point I noticed I had to do a hard reset each time. The only interrupt I am explicitly using is tim7. Also using I2C in master mode.

Post Reply