![]() |
Forum Index : Microcontroller and PC projects : changing between PWM and A/D
Author | Message | ||||
sattress Newbie ![]() Joined: 13/11/2017 Location: AustraliaPosts: 7 |
First time post in the BackShed I make sculptures out of junk, making them kinetic and interactive. The Micromites, Nanomites and Picomites have been a revelation and a revolution. Given the demand on pins for A/D lines and PWM outputs in these pieces, I was hoping in the latest refurbishment of a mechanical lyre bird to have each PWM output also available as an A/D input. The micromite manual suggests that using the LOCAL command in a SUB will work. And it does. A hall effect sensor, via a resistance bridge and a diode provides position sense, and when a SUB with a LOCAL SETPIN command is invoked, a PWM output appears on the same pin. But only once. From that point, going back to the sensor part of program, there is a zero input, which continues to alternate with the PWM output, apparently still okay (on an oscilloscope), A restart gives another successful episode of sensing, followed by PWM output, but again only once. I have no training in any of the skill sets that many BackShed members have. I have tried adding a second LOCAL command as the SUB is about to end (not allowed), I tried GOTO commands but LOCAL is not allowed (?). Any suggestions for what I suspect is a trivial matter, but has got me stumped. Best Paul |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4044 |
Posting some code may help. John |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
I think you are asking more than was ever anticipated. Rather than using LOCAL, You could try a PWM STOP before setting the pin as an input and then setting the PWM again at the end of the SUB Jim VK7JH MMedit |
||||
sattress Newbie ![]() Joined: 13/11/2017 Location: AustraliaPosts: 7 |
John Thank you for help. I hope the following code does not produce too much laughter. This is running on a test rig using a 28pin micromite and only has a single input to pin4 Paul CPU 48 OPTION EXPLICIT OPTION AUTORUN ON DIM AS INTEGER COUNTA SETPIN 4, AIN SENSOR: COUNTA = 0 Do PAUSE 1 COUNTA = COUNTA + 1 PRINT PIN(4) PAUSE 1000 IF COUNTA > 5 THEN COUNTA = 0 MOTOR END IF LOOP SUB MOTOR: LOCAL SETPIN 4, DOUT PWM 1, 25, 50 PAUSE 3000 PWM 1, 25, 0 END SUB |
||||
sattress Newbie ![]() Joined: 13/11/2017 Location: AustraliaPosts: 7 |
Jim, 'you are asking more than was ever anticipated' is a bit ominous. With the sculptures I use up the PWM ports quickly, followed soon after by the spare A/D only ports. I have got around this problem by using multiple processors, sometimes many of them. It works, but inter processor communications are very challenging for me and perhaps I can do better by giving ports dual tasks. I shall keep 'ominous' in mind. I shall give the PWM STOP command a go. Good idea. Very grateful Paul |
||||
sattress Newbie ![]() Joined: 13/11/2017 Location: AustraliaPosts: 7 |
Jim, The PWM STOP command improves performance. Without it, when trying to swap between a PWM output and an A/D input, the PWM output occurred when it should but the A/D input seemed to be clamped at 0volts. With the PWM stop command, the PWM output continues to occur when it should, and now the A/D input is floating free and responding to the sensor properly on the oscilloscope. Unfortunately, while there is no clamp, the A/D looks like it is behaving as a Digital Input rather than an Analogue input as far as the Micromite is concerned. It is producing 1 or 0 at the right time when moving a magnetic slug over the Hall effect sensor. Linking this input to a different PWM output suggests it is digital as there is no fine modulation. Your 'ominous' has moved from something vague, distant thunder perhaps, to lightning less than a mile away. Any other thoughts? Very grateful for the learning to this point. Best Paul |
||||
disco4now![]() Guru ![]() Joined: 18/12/2014 Location: AustraliaPosts: 1003 |
Paul, make sure the SETPIN 4, AIN is executed again after the PWM STOP. I think PWM will set the pin as a output by default, but getting it back to analogue will need SETPIN 4, AIN to be executed each time. It seems to be outside the main loop so would only run once. regards Gerry Latest F4 Latest H7 FotS |
||||
sattress Newbie ![]() Joined: 13/11/2017 Location: AustraliaPosts: 7 |
make sure the SETPIN 4, AIN is executed again after the PWM STOP. I think PWM will set the pin as a output by default, but getting it back to analogue will need SETPIN 4, AIN to be executed each time. It seems to be outside the main loop so would only run once. regards Gerry Gerry, Jim and John, Success. Works like a charm. PWM STOP and a SETPIN 4, AIN are both required. Many thanks Paul Code that works is : CPU 48 OPTION EXPLICIT OPTION AUTORUN ON DIM AS INTEGER COUNTA SETPIN 4, AIN SENSOR: COUNTA = 0 Do PAUSE 1 COUNTA = COUNTA + 1 PRINT PIN(4) PAUSE 1000 IF COUNTA > 5 THEN COUNTA = 0 MOTOR SETPIN 4, AIN END IF LOOP SUB MOTOR: SETPIN 4, DOUT PWM 1, 25, 50 PAUSE 3000 PWM 1, STOP END SUB |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4044 |
Excellent! (And no-one laughed. They're not likely to do that.) John |
||||
sattress Newbie ![]() Joined: 13/11/2017 Location: AustraliaPosts: 7 |
(And no-one laughed. They're not likely to do that.) John John and others, A unique experience for me today, and one that I shall treasure. All your help will transform 'Lyre'. I would attach a picture but not sure if it is allowed, or possible. Currently Lyre responds to shadows, or high frequency sounds (air brakes on trucks work well) by head nodding and if pushed, by moving tail over its head. Its PIC was stretched by this but a 28pin micromite, with the tricks I have learnt from the BackShed today will do all of that and much more. The Mite will not have to breath deeply, probably wont get above idling. Very grateful to everyone. Paul |
||||
Azure![]() Guru ![]() Joined: 09/11/2017 Location: AustraliaPosts: 446 |
Paul You might want to make things simpler in your code when you need to split a pin between AIN and using it as a PWM output by passing the pin number to the subroutine and changing it to PWM and back to AIN within the subroutine. You could also pass some of the other parameters like pwm channel#, freq, duty cycle and pause length to make the main code simpler. Also just a tip to make you code more readable at every nesting level add 2 spaces. CPU 48 OPTION EXPLICIT OPTION AUTORUN ON DIM AS INTEGER COUNTA SETPIN 4, AIN SENSOR: COUNTA = 0 DO PAUSE 1 COUNTA = COUNTA + 1 PRINT PIN(4) PAUSE 1000 IF COUNTA > 5 THEN COUNTA = 0 TEMPPWM(4) END IF LOOP SUB TEMPPWM(NBR%) SETPIN NBR%, DOUT PWM 1, 25, 50 PAUSE 3000 PWM 1, STOP SETPIN NBR%, AIN END SUB |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |