How to output a pulse train while processing
GICU812
Posts: 289
For my project, I need to output a 20khz pulse train at %50 duty cycle AND a 2khz pulse train at 25% duty cycle WHILE processing inputs. Is this even possible with a stamp? Im using a 2p40
What would be the best way of going about this? Sample code would be great, I can modify it for what I need once I get the basic idea figured out.
Thanks,
Adam
What would be the best way of going about this? Sample code would be great, I can modify it for what I need once I get the basic idea figured out.
Thanks,
Adam
Comments
It sounds like you need something like the PWMPAL (see Parallax's webstore).
There are certainly a number of different Stamp-compatible co-processors that will more than fit your requirements. Ask if you would like more information on same.
Alternatively, an simple, external 555 timer should be able to provide the pulse train as well.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
My goal is to live forever...Or die trying.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
Example: 25% DC
Train 1:Train2
- - _ _ : _ _ _ _
Example 87.5% DC
- - - - : - - - _
I didn't mention this granularity initially just for simplicity. I need to be able to at least set the 20 khz DC at either %50 or %25, and control the 2 Khz in least 10% DC increments
Id prefer an option that would let me use the Stamp 2p40 I have, though I may downsize to a 24 for production. I can even wire the PWMPAL onto my 40 no problem if that works.
Im curious as to how I could use the 555 timer, but the less hardware the better.
Thanks,
Adam
The red channel is the stamp turning on and off the chip in a loop. The blue channel is the 20khz signal turning on when portb2 goes high.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
Im controlling an electric motor through a proprietary controller that cannot be modified. This controller expects to see 3 signals, we'll call them A, B and C.
Signal A is a simple logic switch. Normally high, it is drawn low when the motor is used in any way, simply an on-off switch if you will. I got that one figured out, [noparse];)[/noparse]
Signal B is essentially another on-off switch, but digital. This is the 20Khz signal. It needs to normally sit at 50%DC. When the motor is used, it needs to be dropped to %25 DC.
Signal C is the "speed" controller. Its a 2khz signal, resting at 50%DC. For full power, it needs to scale to %90, for full reverse (actually regeneration in this case) it needs to scale to %10 DC. I would be happy with %10 increments but the finer control the better.
So say I want to demand full power, A needs to drop low, B needs to be 20Khz/25%, C needs to be 2Khz,%90
Go back to idle, A = high, b=20khz\50%, c=2Khz\50%
Full regen A=low, b=20Khz\25%, c=2khz\10%
And obviously C needs to scale so I can set any desired amount of power \ regeneration I want between %10 and %90
The main stumbling point for me was trying to get that 20khz signal since it is so fast in relation to the stamp.
At this point, it might be easier just to use external hardware, especially for the 20khz signal, so, since you guys are obviously the experts, you tell me what you would do. I like figuring things out for myself, but this time it might be better to follow your advice.
An 8 bit PWM signal at a specific frequency can be adjusted in 254 duty cycles in .39% increments
so you want 50% on B you send 127 at 10% you send 25 for example.
16 bit timers would give you finer resolution if you need it.
Would that work?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
I'll be using a single joystick or pot to control the motor. So it wont be super precise, doesnt need to be. I would be happy with 10 increments from 10 to 90 so yea, 254 will be great. At least it will allow for smoother transistions.
I used the stamps pulsout command for communication. The tiny45 reads the width of the pulse to set the duty cycle.
You can set from 1 to 99% in 1% increments. Send a pulse of 110 (220us) to turn the pin off (low)
Should be able to change duty cycles 50 times per second
http://www.acousticlights.com/20KHZ.html
outputs from 1 to 100% duty cycle. Over 106 pulsout turns PWM off.
Since the PWM cycle is 248 ticks at 10mhz there is not an exact step of 1% between duty cycles, also there is some clipping at the top values so that a·off cycle of 1 and on cycle of 247 (1/247) gives almost 100% on.· If you send a longer pulse the program will hold off reading any new·puls widths until a low signal is again seen on the line.
Problem is , when i went to do the second timer, it's output is on one of the resonators pins as well as it doesn't have the correct setup for this type of PWM. I will just use a tiny2313 its a 20pin chip instead of 8. By this weekend i will have one ready to go. The 2313 also has a USART so I will use serial communication, that will allow me to make a generic chip that has 4 PWM outputs so i will add the ability to set the frequency and pulse width, like a mini pwmpal I guess.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
Post Edited (metron9) : 11/1/2007 6:53:47 AM GMT
I picked up a couple of 555 Timers, not knowing the first thing about them, but they're cheap ya know.
Reading about them, I guess you cannot make a pulse train in excess of 50% DC, is this correct?
If you use two or a dual unit, you use one as a monostable that puts out a variable width pulse and you use
the other as an asynchronous trigger that sets the repetition rate. The two settings are independent and I
think the width can be quite close to the repetition rate.
I will finish that chip for you tonight.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
Town motto is Keep the doors locked and the guns loaded.
Good news is you obviously have the skills to make a great security system cheap. I've always wanted to interface one to a cell phone, so it could call or text me with the status, I just could never justify the expense of dedicating a cell phone to it.
About the 555 timer, I could have read the datasheet wrong I dont know. I picked up a 555 and a 556 dual timer.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
-Phil
Since i don't have any chips with three timers I decided to use 2 chips. You will need two stamp pins for setting the duty cycle and one pin to connect to the enable pins assuming you want to enable and disable both chips at the same time. An alternitive to the enable pin would be to set a lower limit of a few percent to turn off the output. Let me know if you want to do that instead of the enable pins.
The 20khz input pulse from pulsout ranges from 1 to 99 for 1 to 99% duty cycle
The 2khz input pulse from pulsout ranges from 1 to 124 for 1 to 99% duty cycle
The hardware timer is an 8 bit timer for both of them. The 20khz rolls over at 202 and the 2khz rolls over at 249.
The input pulse from the stamp is 2us per unit. I used a second timer to time the pulse with that timer divided by 8 so the timer value is put directly in the compare register of timer 0.
I need to do a little bit more code to allow for the numbers outside each frequencies range, for example if the timer goes over 201 while reading the pulsout from the stamp·it will be reduced to 201 in the case of the 20khz timer. Also I will put in a enable pin to turn on and off the output. When off the pins will all be set to input.
I will try and send them Wednesday.
The only code that runs is the interrupt code that loads the duty cycle compare register. I simply reset the timer to 0 and wait for the the pin to go low to store the value. The timer is using mode 5 PWM phase correct.
i initially tried to make a two timer software solution but I don't have any chips fast enough. I suppose you could do it with an sx but the power needed to run that chip is pretty high if you are using a battery.
Forgot to ask if you need a sleep mode to conserve power.
I also programmed up a tiny2313 that allows serial input from the stamp and gives you 4 PWM outputs that run at 28khz. You simply use SEROUT 0, baud, [noparse][[/noparse]i,j,k,l] to set all 4 PWM outputs. Glitch free at 38.4kbs. 255 different duty cycles for 1% to 99%. I am going to·configure·some chip select pins so you can connect up to 4 of them (16 PWM channels) on one serial line. In that case you would need to send 16 bytes of data and each chip would take the 4 bytes assigned to it. Chip 1, first 4 bytes, chip 2 next 4 bytes etc. A simple High/Low resets the chips byte counters. These work great for dimming LED's.
i will toss in one of those if you would like to test it out for me.
ext_int0:
out tcnt1,timestart
int0lp:
sbic pinb,2
rjmp int0lp
in inttime,tcnt1
out ocr0b,inttime
reti
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
Post Edited (metron9) : 11/13/2007 10:02:44 AM GMT
As of right now I have no foreseeable need for more than 2 outputs, so I should be okay. Two chips will be fine, im not real crunched for space.
As for the lower enable, I have no need for 20Khz DC below 25%, or the 2khz below, say, 5% DC so if you can work with that, less pins is always better. However, since I am intercepting a signal, I'll be using a 2 pole relay to break the two wires and connect my stamp, so I can just use stamp output to do both the relay and enable. Are they high enable or low? Doesnt matter I suppose. Any suggestions on a good place to get a 2 pole relay?
I also need some A\D converters. I need to snoop a couple of signals, but its absolutely imperative I do not alter the signals, even the least. Anyone suggest a chip that can handle multiple inputs? I suppose I could just get a standard A\D and a multiplexer, didnt know if there was a all in one chip. I wouldnt need more than 8 signals.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
I can email the Visio to anyone who wants it, if I really messed up, lol
Post Edited (GICU812) : 11/13/2007 8:04:55 PM GMT
You can set the duty cycle when the chip is off as well. For example set to 50% then turn on and off it will hold the last pulse width. Set the pulse with to 0 before enabeling the chip so you don't have an unknown duty cycle.
http://www.acousticlights.com/2khzscope.html
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
Post Edited (metron9) : 11/14/2007 8:03:54 AM GMT
I PMed you my info.
If I leave the original ECM signals on the line, and superimpose my new ones over top, it works okay, It will accellerate at my commanded level, though I dont think it will decelerate. This is probably because while I can always raise the level with more DC, I cannot lower the level below what the ECM is putting on the line, which is normally %50 for "neutral"
The problem comes when I try to use the circut as designed. When I cut off the ECM from the path, and just use my signals, it will either instantly go full accell, or full regeneration, or sometimes nothing, then a second later, trip the IMA warning light and disable the system. Looking at them on the scope, the signals are identical, though my supplemented signal's low level is a few hundred Mv below the ECM's "low" signal.
I tried just hijacking one signal or the other, and while I wont always get the accell\regen response, it always trips the IMA light and shuts down the system. Its obvious it doesnt like my signals. To prove it wasnt the switchover blip, I can flip the relays for up to 1 second, then flip them back, and it wont trip the IMA light, even over and over again, but if it sees my signals for more than 1 second, it will trip.
I thought maybe the ECM was seeing a dead line and signaling the error, so I put a pullup resistor to 5v on the ECM side of the line. I will have to try this again tonight, as I was in a hurry at the time, and I just got a dead line on the scope when I checked it still. To explain, the MCM pulls the line high through a resistor, then the ECM pulses the line low to communicate. I dont think the ECM could sense a dead line, but I dont have details to that effect either. But since I am just dropping the ECM line, it might be an idea.
Or it could be in the signals. how do I raise the low threshold of my signal, a resistor in line to ground? I dont understand why my signals would be getting pulled lower than the ECM's signaling does in the first place...
Now it works fine, except I cannot get it to regen yet, I think I need to change some ordering in the code, that outta do it.
Thanks a lot for all the help!