Prox / RFID
Ladder Logic
[interfacing] †
Tube Joints
Key Code From Photo
SolveSpace (3d CAD)
SketchFlat (2d CAD)
Resume / Consulting
Contact Me

LDmicro Forum - can't set pwm

(you are viewing a thread; or go back to list of threads)

can't set pwm (by Jose Sanchez)
Hi Jonathan:

Thanks for this software. I have a question for you. ┐how can I set the pwm for a microcontroller, especifically atmega8?. When I open the window for set a value for a duty cycle, I put a value between 0 and 100, but when I compile and program the micro, doesn't work.
I know that the atmega32 have a PWM too. But I can't set PWM output, because the program says me that doesn't have a PWM.
I attach the file. And I can't simulate the pwm.
Thanks for the reply. Have a Nice day.

p.d. Excuse my english.
Thu Jun 25 2009, 20:27:55, download attachment testpwm.ld
(no subject) (by Jonathan Westhues)
The PWM instruction accepts a variable for its duty cycle, not a literal number. So your example sets the duty cycle equal to the value of the variable '100'. That variable doesn't exist, so the program doesn't work.

See the attached example, which changes between 50% and 75% duty cycle on the PWM output, depending on the state of digital input Xin.

Any micro with a capture and compare peripheral is capable (in hardware) of generating PWM output. But LDmicro uses one capture and compare peripheral to measure the PLC's cycle time. So PWM is supported only on processors that have at least two CCPs.
Thu Jun 25 2009, 21:53:04, download attachment pwm-simple.ld
Thanks (by Jose Sanchez)
Hi Jonathan:

Thanks for the example, works very well. I can see my mistake.

And I See the data sheet for Atmega8 and Atmega32, It's very seems, and Atmega32 has more pheripeals and flash memory.
So, Am I in a mistake? Atmega32 has four timers counters with Compare Units for each one. And... Well, is it correct?.

Thanks a lot.
Fri Jun 26 2009, 03:50:22
(no subject) (by Jonathan Westhues)
You're right, looks like the ATmega32 could support PWM, using OCR2 and Timer2. It would just require the code changes to turn it on (register locations, etc.) for that chip, and I never bothered.
Fri Jun 26 2009, 23:46:21
Software PWM (by Ingvar Sande)
If you want PWM on any pin on the processor you can use a "software PWM"

Here you can see that the variable "duty_cyle" roll from 0 to 100. Then it compare the analog input with the "duty_cycle"
It is important to have a fast cycle time, my cycle time is 0.1 ms. I have tested this program on a bilge pump and as a dimmer on a diode lamp.(Just ignore the comment in the program if you cant speak norwegian.)
Tue Jun 30 2009, 05:54:53, download attachment PWM.ld
thanks (by Jose Sanchez)
Hi Ingvar:

Thanks for the reply. I see the program, you're generating a "triangular waveform" with a incremental and decremental counter, it's just the principle of pwm, is it correct right?
And then you compare this "waveform" with a "reference" in this case, the analog input. Its very useful you're application.

But I have a question. In the LDmicro program, In edit menu, when you select "micro parameters" Sais that "Cicles time between 10 and 100 mS is normal"... And you use a 16 Mhz cristal frecuency. ┐Is it correct use a 0.1 mS for a cycle time?
and... How can I calculate a cycle time? Thanks.

Have a nice day. Are you from Noruega?
Tue Jun 30 2009, 14:50:27
excuse my english (by Jose Sanchez)
I have a gramatical and syntactic errors. Sorry for that.
Tue Jun 30 2009, 14:57:20
(no subject) (by Jonathan Westhues)
As long as the cycle time is longer than the time required to execute your program, everything's fine. So if your program is relatively short, and your clock speed is fast, shorter cycle times are okay.

LDmicro uses whatever cycle time you specify. If you specify a cycle time that's too fast, then the generated code just won't work. Unfortunately, there's no error signaled when you compile, since it would be relatively complex to calculate the run time for the program at compile time.
Tue Jun 30 2009, 16:21:36
Ok (by Jose Sanchez)
Ok Jonathan, I see. Thanks.
Tue Jun 30 2009, 23:31:36
Measure cycle time (by Ingvar Sande)
Hi Jose.
I dont know how you can calculate cycle time, but you can measure cycle time.

In this programm i have made a cycle time test on rung 7. The cycle time is set to be 1 ms, this mean that in 1 second you will have 1000 scans if everything is working.

If you connect the output on rung 7 to a LED it should go on and off every 10 second, because 10000\1000=10, measure this with a clock. If the program become to big or the cycle time is to fast then you will see this when the LED blinking is slower.

To illustrate what happen when you have to fast cycle time you can set the cycle time to 0.1 ms. The LED should go on and of every 1 second because you have 10000 scans in every second and 10000\10000=1, but when you measure this you will have 10 blinks after 16.5 second this mean that you have to fast cycle time or to big program.

If you delete rung 1 to 5 and do the same test again you will have 10 blink on 10 second and you know that the program is not to big.

Yes i am from Norway and that is the reason my english is not good.
Wed Jul 1 2009, 10:47:00, download attachment PWM_test2.ld
Thanks (by Jose Sanchez)
Hi Ingvar:

I see the program. thanks for the explanation. I have some questions:
How big can write programs that run LDmicro and at a speed "appropriate"? Is it possible to use the full capacity of an Atmega128?
And another question. What hardware are you programming your atmega128? Anything and the stk500 or something? Is that I have some atmega128 but I have not programed, because I have to make a base, so the packaging. Greetings and your English is better than mine.
Wed Jul 1 2009, 23:05:54
(no subject) (by Ingvar Sande)
It is hard question to answer. To find a clue on how big a program can be vs cycle time we have to give the microcontroller a lot of work to execute,
then do the test i explain on my post above. Maybe i will do some testing on this later.

I am using this MCU: http://www.futurlec.com/ET-AVR_Stamp.shtml and programming it through the parallel port with PonyProg2000.

This is probably a better way to program the MCU: http://www.futurlec.com/ET-AVR_JTAG_USB.shtml
but then you will also need the free AVR Studio IDE and ET-AVR Stamp Board

This is an interesting site if you want to make your own programmer:
Thu Jul 2 2009, 18:43:58
ok (by Jose Sanchez)
Thanks Ingvar. I see the links. I have an Atmega128, but I need build a base similar to yours.

Maybe, We need another post for talk about this topics...

See you... Have a nice day
Thu Jul 2 2009, 23:54:59
execution time (by Jonathan Westhues)
The ATmega parts go up to 16 MIPS. So you could execute 128k instructions in 128k/16M ~ 8 ms. So if the code contained no backward branches, then 8 ms would be a long enough cycle time for any program that fit in an ATmega128 at 16 MHz.

But some ops do have backward branches (like multiply or divide), or have delays while they wait for a peripheral (like A/D conversions). So it's not quite that simple, but you can still consume a decent fraction of the mega128's memory with a practical cycle time.

Atmel has a cheap "official" programmer now, the AVRISP mkII. It's available from Digikey and others for about 30 USD.
Fri Jul 3 2009, 16:50:29
Thanks (by Jose Sanchez)
Ok Jonathan, I see the answer, Thanks again.
Fri Jul 3 2009, 22:47:35
can' set 500hz to1khz pwm for atmega8 pwm pin? (by 80proof)
hi sir jonathan,
why can't i set 500hz to1khz pwm for atmega8 pwm pin?
i attached the error display on ldmicro compilation for . hex and can i use the other pin 16 and 15 for pwm on atmega8?
in my project, i want to control servo motors.

thanks in advance.
Wed Dec 21 2011, 23:05:11, download attachment pwm_atmega8.png
servo controlling rotation problem. (by pekto)
hi sir jonathan can you help me with my project? right now?i want to control servo motor rotation using towerpro sg90 micro servo for rc. when i burn the program attached below, the mcu atmega8 is not responding the correct pwm signal that should be out at pin 27. thanks in advance =)
Thu Dec 22 2011, 07:08:47, download attachment servo controlling rotation problem.ld
servo controlling rotation problem (by MGP)
If you look at the servo signals, you must have 20mS between the pulses.
Your program has only 5mS + signal time!

Another thing is, you use a 10K pulldown resistor. Always use pull-up resistors and switch to the ground (invert the inputs if needed)
Sat Dec 24 2011, 05:07:58
servo controlling rotation problem (by pekto)

ok sir, i will try adjusting the the "DELAY TURN OFF" tomorrow and burn this program if it will work and update you if it works.about the post of "80proof" above i also try that but the pin out for atmega PWM is always at pin 17 but looking at the datasheet the pwm is available at pin 15,16 and 17. how can i set the pwm on other pin like 15 and 16? waiting for your reply sir.thanks a lot!
Sat Dec 24 2011, 22:17:28
servo controlling rotation problem (by MGP)

you cannot use PWM on a sg90 micro servo, PWM is for DC motors only.
These servos require time-based pulse, not PWM

Pin 17 is the only pin with PWM output, pins 15,16 are not supported in LDMICRO.
If you need more than 1 PWM output, use another programming language or a software PWM, like the program on top of this thread.
Sun Dec 25 2011, 05:21:41
servo controlling rotation problem (by pekto)
ah ok..i thought i can use this pwm on servo motors, thanks a lot sir! have a good day!! =)
Sun Dec 25 2011, 18:16:20
servo controlling rotation problem (by 80proof)
sir,i still have this problem,my atmega8 external xtal is 8Mhz. can you give me sample ladder program that can make tower pro servo sg9 rotate clockwise and counterclockwise using single button or two buttons. thanks a lot! i really need this project to work because im planning to control pick an place robot arm for my plc with atmega8.
Wed Dec 28 2011, 23:54:38
servo (by MGP)
As I said, LDmicro is too slow for servos.
The following program is made for fun!!
Thu Dec 29 2011, 15:38:47, download attachment servotest.ld
(no subject) (by 80proof)
sir, i was able to rotate my servo TOWER PRO SG90 180deg.Clockwise and 180deg Counter clockwise. if i increase the time 20ms that you told me my servo rotate but not smoothly,its like lagging in every receive of the signal time, so i made adjustments and i still hook on 5ms and it rotate in smoothly manner. i attached my new program. i adjusted the cycle time from .2ms to .4ms.it it works now. the only problem last time on my program is the cycle time is .2ms and i change 12MHZ TO 8MHZ external xtal.
Thu Dec 29 2011, 17:53:18, download attachment SERVO_CW_CCW.ld
(no subject) (by MGP)
I do not think you understand how servos work.
The timing of your signals are not correct.
Fri Dec 30 2011, 03:32:16
servotest.ld (by Edu)
Mr. MGP performed the hardware test with your program, what happens is that the ldmicro can not compile the HEX file, if the cycle time set is less than 0.5 ms .... otherwise I think would work perfectly for fun with a hardware ............. I'm not a programmer, but I like to have fun with electronics and I understand a little ladder language, the SR would have put some more solutio for this super little program in action. thank you ....
Tue May 22 2012, 10:23:27
how to rotate my servo motor (by mierazizie)
hello :)

how to rotate my servo motor by using 4 button.
1st button to rotate 90 degree.
2nd button to rotate 180 degree.
3rd button to rotate 270 degree.
and 4rd button to rotate 360 degree and as a reset button to.
im using pic16f877a in proteus.
need your answer asap.

thanks :)
Sat Mar 16 2013, 11:20:31
servo <-> steppermotor (by MGP)
You cannot use servo's with LDMICRO, ldmicro too slow.
Use stepper motors instead, or dc motors with PWM.
Sat Mar 16 2013, 12:29:52
i cant set pin on processor for PWM (by indra )
i can't set pin on processor for PWM.
out of order " can only assign pin number to input/ output pin(xname or yname or Aname)
Wed Nov 4 2015, 07:23:46
(no subject) (by MGP)
Wed Nov 4 2015, 08:35:52
i can't set pin on processor for PWM (by indra )
so , what should i do to the pwm to work on atmega 16 . I am new at microcontroller.
Fri Nov 6 2015, 09:18:18
Post a reply to this comment:
Your Name:
Your Email:
(no HTML tags; use plain text, and hit Enter for a line break)
Attached file (if you want, 5 MB max):