14M2 Plusout AND PWM

radiogareth

Senior Member
I have a 14M2 running 8 LEDs on ports b.0-5 and C.0,C.1. ADC c.4 fed with a potentiometer.

I have all three PWM channels doing what I want, ie dimming the LEDs and I'm trying to get the best control/dimming capability out of the other 5 channels.
My understanding is that the PWM command uses an internal module and once set up, "does not get in the way of other code (much)", whereas pulsout deals with each pin I have allocated in turn. I THINK the highest duty cycle for the pulsout will be 20%, (5 pins running the command|) plus a bit of readadc time. The 14M2 is going flatout at 32Mhz and the flicker can't be seen, but of course, each LED is only coming up to a 1/5th of its capability (apart from the PW ones).

I think I'm asking a bit much of the poor little 14M2 and would perhaps be better just switching on/off the 5 non pwm channels. There is a limit, after all!!
Code:
setfreq m32

	Do
 	ReadAdc c.4,b0
	pulsout B.0,b0
	pulsout B.1,b0	
	pulsout B.3,b0
	pulsout B.5,b0
	pulsout c.1,b0
  
  If b0 <> b1 Then
  b2=b1+85
  b3=b2+85
    b1 = b0 min 2
    if b1=2 then
    b1=0
    end if
    PwmOut c.0, 63, b1
    PwmOut b.2, 63, b2
    PwmOut b.4, 63, b3
  End If
Loop
Comments and code invited :)

Gareth
 

PaulRB

Senior Member
It may be possible to do a crude pwm in software at 32mhz, with perhaps 16 levels per output. But what for exactly? Just playing? In your code at the moment, all channels fade together anyway, so why not just connect them all to a single hwpm with a driver transistor? Do you have an idea or application in mind, even if its just a bit of fun?

Paul
 

radiogareth

Senior Member
Paul, I have a board set up for driving 8 lengths of 12 volts LED tapes, RGB & white * 2 each. (C.1, C.2 & B0-5). ADC on C.0 and C.4, Push button gives high to C.3.
The ideas is to be able to selects one of 8 channels with one pot then set the brightness of that channel with the other. The outputs were all originally staggered but adding 32 to 7 variables slowed the loop down a bit too much. There is probably a better way to do that.
I could step through the channels with the push-button, setting the brightness with the pot on C.4. At this point its really feeling around, 3 PWM and 5 on/off isn't bad, but 8 PWM (well 3 real plus something similar) would be stunning, especially if it remembered the settings in eeprom when perhaps the memory button gets pressed, or a timer fills up.
For use showing pupils what can be done with humble micro-controllers and a few bytes :) All good stuff....
 

PaulRB

Senior Member
The 14m2 does actually have 4 hwpm channels...

The 4th one is on the B port. That makes it a bit trickier to do software pwm on the B port without affecting it...


My idea would be to switch on 4 ouputs at the same time, then loop round 16 times, switching off each of the 4 outputs after a preset count. Repeat this over and over at 32mhz and it might not flicker too badly.

Or...

2 x 14m2, communicating via serial, 8 hwpm channels.
 
Last edited:

MikeAusP

Member
Pulsout seems to work well in Parallel Task mode.

You could have 4 different Pulsouts in four separate tasks running simultaneously, at different pulse widths and at different repetition rates.

It's documented in Manual 1 under Parallel Task Processing

It's hard to find in Manual 2 because it's only mentioned under Suspend and Resume. It would be easier to find if it was also referenced under "Start".
 

radiogareth

Senior Member
'Tis in vain to do with more that which can be done with less' :)
Not thought about parrallel processing, what little I read of it was that if one task is busy doing something, the other one(s) have to wait.
Time to refer to Section 1.

Thanks all.

Gareth
 

Bill.b

Senior Member
Hi Gareth

I persomally would multiplex the outputs as in the schematic below.

the three PWM outputs can be changed for each drive instance giving an unlimited colour control.
you would have to drive the multiplexer at a frequency so the eye will not perceive any flickering.
I wopuld suggest running the 14m2 at 16meg or 32meg.

output pins C.0 , b.1 and b.3 are direct PWM outputs , C.0 runs form a different timer then b.1 and b.3, if you
set the PWM to the same duty and only change the cycle there should nor be any problem.

I have not had time to look at any code yet.

ledtest.jpg

Bill
 

MikeAusP

Member
Why not just drive the 3 outputs directly. e.g. to drive Red at 15%, Green at 80% and Blue at 40% try this

Do
High Red : High Green : High Blue
Pause 15
Low Red
Pause 25
Low Blue
Pause 40
Low Green
Pause 20
Loop

Of course you'll need to do some calculations first to check if R, G or B have the highest Duty cycle

Multiplexing has the disadvantage that you can never light the LEDs to full rated brightness.
 

radiogareth

Senior Member
Thanks for the input but I wanted to keep it simple (and cheap). My drivers are BC337s at 2.5 pence each, with an option to use some 1 amp E-lines if I need more. Its really about getting the most out of the 14M2, so I'll keep playing with software for the moment. Just got to read the other thread that's started about PWM/HPWM to see what that's all about as I too did not understand the manual descriptions. (edit:now posted a question on there)
As a minimum it looks like I can manage 3 full PWM channels, with 5 individual on/offs, and maybe some coarse dimming steps too, all stashed in eeprom ready for power-up/down.

Edit: Just twigged that there are 4 PWM outputs anyway on the 14M2 (not sure how I missed that) but I'm blocking one with an A/D. Still leave 3 genuine ones for Red,green, blue with my existing hardware.

Thanks
 
Last edited:

radiogareth

Senior Member
OK, after a few hours coding I have the following:
RGB LED tape on B2,B4 and C2.
5 White LED tapes on remaining drivers.
Saves RGB PWM to eeprom on cycling for re-load or reset.
Lots of light with colour if you want it.
Code:
'********************************************************
'* Sample PICAXE Software for use with PICAXE Micros    *
'* For educational use only. E & OE  29/4/2013          *
'* By R G Evans,known as Gareth or radiogareth on Forum *
'* For more information and PCBs visit www.g4xat.co.uk  *
'* One of the PIGGYAXE Family of functional fun projects*
'* 3 LEDtape individually adjustable (pot on ADC C.4) *
'* And 5 separate on/off channels with write to memory*
'********************************************************
'PIGGYAXE PIGGYMIDDLE with ADC,Pushbutton and 8 channel power drivers 'LEDs on PWM Outputs B.2,B.4,C.2
'Push to make LOW on INPUT C.3, turn on internal pull-up on C.3 'Potentiometer on Pin C.4 ADC.& Pin C.0
setfreq m4      'Defines clock speed
#picaxe 14m2    'Defines Processor
#no_data        'Speeds up download
pullup %000100000000000	'turns on the pullup on C.3
Read_OLD_values:
		read 0, b1:	if b1=0 then let b1=40:endif:	PwmOut b.2,63,b1	'Reads the saved values if any
		read 1, b2: if b2=0 then let b2=80:endif:	pwmout b.4,63,b2	'if not defaults to something
		read 3, b3: if b3=0 then let b3=120:endif:pwmout c.2,63,b3	'so you know its working
		goto  wait_for_activity    'just in case I move this part.
                
wait_for_activity:     
		    If pinc.3 = 0 then getset1	'waits for you to start the selection/brillaince process
                goto wait_for_activity  'goes round again until you do
				                
getset1:
                pwmout b.2, off
                for b10= 0 to 3   'little 'flash' routine so you know which LED is which!
                high b.2
                pause 100
                low b.2
                pause 100
                next b10
                goto set_one     'goes to first LED brightness set routine

Set_one:             'main loop of the programme LED on B.2
                readadc c.4, b1  'reads the postion from pot on input C.4 - ADC
                if b1<2 then let b1=0:endif 'trap here to ensure it switches off completely
                PwmOut b.2, 63, b1' Set LED brilliance
                If pinc.3 = 0 then getset2 'checks to see if you are finished and ready for the next one
                goto set_one          'and goes to second delay to allow you to remove finger

getset2:
                pwmout b.4, off
                for b10= 0 to 3
                high b.4
                pause 100
                low b.4
                pause 100
                next b10
                goto set_two

Set_two:                                                             'as per setone only for LEDs#2
                readadc c.4, b2
                if b2<2 then let b2=0:endif
                PwmOut b.4, 63, b2
                If pinc.3 = 0 then getset3
                goto set_two

getset3:
                pwmout c.2, off
                for b10= 0 to 3
                high c.2
                pause 100
                low c.2
                pause 100
                next b10
                goto set_three

Set_three:
                readadc c.4, b3 'as per setone only for LEDs#3
                if b3<2 then let b3=0:endif
                PwmOut c.2, 63, b3 
                If pinc.3 = 0 then set_rest
                goto set_three 

SET_Rest:
                for b10= 0 to 3
                high b.0
                pause 100
                low b.0
                pause 100
                next b10
                goto one_of_five
                
one_of_five:
                	readadc c.0, b4	'this loop is to switch on or off up to 5 other strings 1-2-3-4-5
                	If pinc.3 = 0 then save_it_for_later	'exits and saves PWM values
			if b4< 20 then low b.0,b.1,b.3,b.5,c.1 :endif   
			if b4> 60 then let pinb.0=1  :endif
			if b4> 100 then let pinb.1=1   :endif
			if b4> 140 then let pinb.3=1    :endif
			if b4> 180 then let pinb.5=1 :endif
			if b4> 220 then let pinc.1=1 endif
			low b.0,b.1,b.3,b.5,c.1
			goto one_of_five


Save_it_for_later:
			write 0, b1	'writes b1,b2,b3 to eeprom
			write 1, b2
			write 2, b3
			pwmout b.2, off	'Kills PWM 
                	pwmout b.4, off
                	pwmout c.2, off
                	low b.0,b.1,b.3,b.5,c.1 'Kills binary outputs
                	pause 1000	'gives you time to remove finger from button
			goto wait_for_activity 	'and back round we go
Not quite how I envisioned it but that can wait until another day :)
 
Last edited:
Top