pwm examples needed

Forum for problems or queries regarding other Flowcode Components. Eg LEDs, Switches, LCD, Gfx LCD etc

Moderators: Benj, Mods

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

pwm examples needed

Postby chevy6600 » Sat Feb 23, 2008 12:02 am

Hi all, from what i can see on this forum there are a few like myself who would like to know more on how to use the pwm function. I have already picked up the fact that flowcode will only display 2 pwm functions even if the chip has more!....hope this is put right in the next update!.....I have already seen the help file and seen the existing examples but they do not fully explain how best to use the pwm function.
So my request is: Please can you supply more examples to show how to use the pwm function and to show how to get around the `only 2 pwm function ` restriction.
Examples are better than individual questions and less frustrating.
So thanks in advance. :|

nils
Posts: 33
Joined: Fri Nov 02, 2007 11:03 am
Contact:

Re: pwm examples needed

Postby nils » Sat Feb 23, 2008 10:49 am

Hi

I played also around with PWM but needed mopre control over it, so I chose to implement it by myself in the Custom Component as functions that I can call then by the Component Macro inside Flowcode. the problem is, that as soon as you use C code in Flowcode it does not simulate right anymore, but I can live with that.
So if you are interested in implementing something yourself have a look at the Custom_Code.c file in the Components directory and my post about the Perl script I wrote so you only need to focus on how to write your implementation.

good luck
nils
Custom_Code.c help needed? -> http://tinyurl.com/2y2mzq
Flowchart high quality vector graphic output =>>http://tinyurl.com/6sx2sx

User avatar
Benj
Matrix Staff
Posts: 14139
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4074 times
Contact:

Re: pwm examples needed

Postby Benj » Mon Feb 25, 2008 10:05 am

Hello

In the PWM component properties you can select the period frequency. this controls how often the PWM runs through its cycle. Also in the properties is a count value. This is the maximum value that can be used for the PWM count.

Eg if you use a count of 2 then using a parameter of 0 will equal 0%, 1 will equal 50% and 2 will equal 100% of the duty cycle.

There is an example available from here.
http://www.matrixmultimedia.com/Flowcode_Examples.php

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

Re: pwm examples needed

Postby chevy6600 » Tue Feb 26, 2008 1:54 am

Hi, tried your `adc controlled pwm` program, i can see how it all works but as proof of concept i thought that it should be possible to have the signal opperate a led or leds. So i thought all that was needed was to slap on a row of leds on port B and i should be able to see port B bit 0 and bit 3 come on (looking at the chip data file) if the pwm signal was high, and flashing if the signal was half high/low!
But this does not work!
How do you get the pwm signal out?..... in the flowcode program we obviously can only operate leds, lcd etc.
Another question: in the window `properties: component macro` , selecting the pwm component, and macro, the parameters need filling in `nltdx(BYTE)......what is this?

P.S i was using a 16f88 to simplify to try it out, hence the port B bit0/3 ! in the above

User avatar
Benj
Matrix Staff
Posts: 14139
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4074 times
Contact:

Re: pwm examples needed

Postby Benj » Tue Feb 26, 2008 10:40 am

Hello

The PWM will directly drive an LED in hardware but in simulation this would be very intensive on the processor. The PWM signal comes out to a CCP1 or CCP2 output. The CCP PWM outputs are marked on the chip diagrams. These are driven by the hardware PWM modules so you do not have to worry about actually sending out the signals your self.

The nIDX byte is the PWM channel which is 1 or 2 depending on which PWM channel you want to control.

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

Re: pwm examples needed

Postby chevy6600 » Tue Feb 26, 2008 12:09 pm

Hi again, well o.k. so the software cannot flash the leds but should it not be possible to make them come ON or OFF.
I setup a simple program (chip 16f88), that uses a switch to switch a pwm to max of 255 when pressed and to set pwm to min of 0 when not pressed. I setup a row of leds on port B so that they could catch any thing happening. I expected a led to light up on the ccp1 port which in the chip data is port B bit0 and bit3 when the port goes high, and when the button causes the port to go low ( pwm at 0) the led should be off.
The result is , in flowcode NO leds ever light up, and when i down load it onto the `ver3 development board` i get bit0 led, only, on all the time!

The ideal program was to get the led on the `ver3 development board` to flash so fast it would appear to dim in relation to the pwm.

User avatar
Benj
Matrix Staff
Posts: 14139
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4074 times
Contact:

Re: pwm examples needed

Postby Benj » Tue Feb 26, 2008 12:52 pm

Hello

On the 16F88 the PWM output is RB0 only. The PWM simulation in Flowcode does not currently switch on and off the LED. Would turning the simulation pin and LED on or a different colour be a good way to simulate this.

The RB0 is on all the time because you are assigning a value of 255. Try creating a loop that increments the value with delays (eg 20ms) so you can see the change in brightness.

loop
{

Calculation - bytevar = bytevar + 1

Hardware Macro - PWM - 1, bytevar

Delay - 20ms

}

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

Re: pwm examples needed

Postby chevy6600 » Tue Feb 26, 2008 1:38 pm

Hi benj, thanks for the input but you miss understand my intentions, the reason for me to try and operate the led in flowcode is because there is no servo component, and i hope a led would make use of the pwm function, it is the pwm function i need to get to grasp. I hoped to be able to see flowcode use the pwm in some way, motor/led /lcd/anything! so long as it was a gradual effect.
I have the ver3 development board which has leds on, so before i start connecting my own servo etc. i wanted to see if the leds could be controlled first, ie. slowly getting dimmer would show that the pwm was having an effect. it was just a proof of concept using leds.

User avatar
Benj
Matrix Staff
Posts: 14139
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4074 times
Contact:

Re: pwm examples needed

Postby Benj » Tue Feb 26, 2008 1:49 pm

Hello chevy

There is a ADC controlled PWM example available from the downloads section of the website that will show you the LED getting dimmer and brighter with regards to the POT on AN1. You will have to switch the RA1 jumper from Digital to Analogue on your dev board to see it working.

Its looking like servos are fairly popular at the moment. If I get time I will try to mock up a beta servo component for you to test.

With my suggested program above the led would start out as 100% off and then slowly work its way to 100% on and then repeat.

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

Re: pwm examples needed

Postby chevy6600 » Tue Feb 26, 2008 1:57 pm

Hi again, i think i am getting closer !
O.k. i can get the led on the development board to dim according to a pwm duty cycle setting, but i would like to get straight why the delay is needed, as if it is needed for the led to be seen working, would i need the delay if i connected it to a servo?
Can you explain more of the reason in using the delay?
Thanks

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

Re: pwm examples needed

Postby chevy6600 » Tue Feb 26, 2008 2:06 pm

Benj wrote:Hello chevy

There is a ADC controlled PWM example available from the downloads section of the website that will show you the LED getting dimmer and brighter with regards to the POT on AN1. You will have to switch the RA1 jumper from Digital to Analogue on your dev board to see it working.

I have been playing with this this morning but although i see the pwm graph-component there is no leds connected! that is why i started to try to attach my own leds, and the reason i started down this path.

User avatar
Benj
Matrix Staff
Posts: 14139
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4074 times
Contact:

Re: pwm examples needed

Postby Benj » Tue Feb 26, 2008 2:37 pm

Hello

The reason for the delay is that without it the program would run so fast that you would not be able to see the LED changing brightness.

Therefore you would also need this for the servo control.

The PWM is always running at the exact frequency once it is started. It is the updating of the mark/space value that you want to delay to be able to see the effects or to allow the servo time to move into position.

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

Re: pwm examples needed

Postby chevy6600 » Tue Feb 26, 2008 2:58 pm

EUREKA! :lol:
Hi, thanks benj, i have put together a sightly simpler version of your `ADC CONTROLED PWM` program by editing it (as proof i understand it and to change the chip).
I have got a led to work dimming in proportion to the pot an1.
What i did not realise when i used it before you explained about the software led not working, was that although the program did not include a led component, the development board would still work!.
Might i suggest that a comment be added into the `adc controled pwm` program mentioning this, then other new folk, like miself, would understand.
Thanks again.

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

Re: pwm examples needed

Postby chevy6600 » Thu Feb 28, 2008 3:27 pm

Hi all, well i`m sorry to say i could do with some help again,after setting up a simple program to use pwm to operate a led, working off the potentiometer on the ver3 development board, all seemed ok as the led dimmed as expected.
Next step was to replace the led with a servo as found in r/c modelling, but the servo moves in a haphazard way and gives off a high pitched whistle and over heating! :evil: , ie. it is just not working right...it should normally not make any sound until the motor works, and then it is only a `grunt` kind of sound.

I guess a failed to remember that PWM means different things to different people, to me it means r/c signal which is a high pulse of 1ms to 2ms and then low for the rest of the 20ms (even as much as 50ms).
To others PWM means high/low pulses....and all manor of cycles and/or frequencies :?: !

Here lies my problem, the high pitched whistle only happens when the signal is connected to the servo (power on circuit/servo all the time) so the servo is being pulsed in an unusually high way, but the program is set up to give out the longest pulse i can manage which is 16.384ms...not the ideal 20ms, but should be close enough!....this was setting the 16f88 pic to its lowest xtl-speed and using the maximum `255` in the PWM component: properties, known as the `period`.
I suspect that the high going pulse is itself a higher frequency pulse train!...i just don`t know! :roll:

Next problem i can see is that i need to run the pic chips as fast as possible but this causes the PWM to be far too fast a cycle/ frequency.

There does not seem to be enough speed choice in the PWM component: property setup to allow me to get the 20ms to 50ms that i need to get for one cycle.

I tried using a delay in the program but it did not make any difference. ...how come :?:

What i relay need is the 16F88 pic to run at its max speed to enable me to run the eventual program monitoring of different inputs and other formula, and speed is paramount importance here.
The pic`s PWM function to get on with job of outputting a signal conforming with 1ms to 2ms high pulse with a cycle of between 20ms to 50ms. as per r/c signals.
Am i expecting too much from flowcode and ver3 Development board?
I confess that i am a complete beginner with programing pics...that`s why i bought flowcode!! :mrgreen:

User avatar
Benj
Matrix Staff
Posts: 14139
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4074 times
Contact:

Re: pwm examples needed

Postby Benj » Thu Feb 28, 2008 4:47 pm

Hello Chevvy

We have a 8 channel servo example that Sean has put together. This uses the Timer1 module and two hardware capture compare interrupts.

The CCP1 Interrupt controls the individual Servo positions and the CCP2 interrupt controls the overall cycle of the timer1 counter (eg to fit in 8 different servos)

The outputs are driven from the CCP1_isr macro. This currently uses PortC as the output port. Eg servo0 would connect to RC0 and so on.

The servos are each controlled by an array of bytes. Eg to change the position of Servo0 you would change the variable servo_pos[0] = 0 using a calculation icon.

The example uses a 16F877A running at 19.6608MHz. A 16F88 will not work as it only has the CCP1 module. (eg PWM channel 1)

Hope this helps.

Let us know if you have any problems.
Attachments
MultiServo.fcf
(6.5 KiB) Downloaded 1241 times

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

Re: pwm examples needed

Postby chevy6600 » Thu Feb 28, 2008 4:58 pm

Thank you very much benj and sean, i shall have a little play with your prog for a while.
Glad that it is me not getting it right! and not expecting too much from the flowcode! :lol:
I have the pic 16F877A that came with the development board.

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

Re: pwm examples needed

Postby chevy6600 » Fri Feb 29, 2008 5:26 pm

HI me again, i have been playing around wth the prog you made up. Works ok. :mrgreen: but i have a few questions you may be able to shed some light on. I set up a program based on yours, with a button which puts a 5 into the servo position when NOT pressed, and a potentiometer which gives it`s number when the buttun IS pressed. The numbers are also then displayed onto the LCD.
The servo works with numbers from 0 to 116 and numbers higher upto 180 do not move the servo any further, and numbers higher than 180 cause the servo to revert to it`s state where it thinks is 0.
Please explain the significance of the numbers.?
Can i get a 255 resolution?
THanks.

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: pwm examples needed

Postby Sean » Sat Mar 01, 2008 1:47 pm

The program in the earlier posting was a simple demo program written some time ago. I will take this opportunity to improve it and post an updated version in the next few days.

The program uses the 16-bit Timer 1 and both of compare registers (CCP1 and CCP2).

The Timer 1 clock source is the 19.6608MHz xtal, divided by 4 by the system and divided by 8 by the prescaler, resulting in a 614400Hz signal (1.627us period).

CCP1 is configured to reset Timer 1 and generate an interrupt every 2.5ms (1536 clock cycles), set the next servo output and load CCP2 with the next servo period.

CCP2 is configured to generate an interrupt at the end of each servo pulse and clear all the servo output.

The system cycles round 8, 2.5ms servo cycles, and 8 servo outputs, giving the overall repeat period of 20ms.

Each servo pulse period is read in turn from the data array. The value 77 is added to give the 1ms minimum pulse width and the result is multiplied by 8 as it is loaded into CCP2.

(77 x 8 = 616, 616 x 1.627us = 1.002232ms)

The adjustable ranhe of the pulse (1ms to 2.5ms) is covered by the data range 0 to 115

(115 x 8 = 920, 920 x 1.627us = 1.49684ms)

Values above 115 will cause problems as the servo pulse periods will begin to run into each other.

I should be able to change the multiplication factor to 4, giving a pulse adjustment, from 0 to 255, of 1.66ms.

I will also add an array for the minimum pulse width settings so they can be adjusted for each servo (154 = 1ms).

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

Re: pwm examples needed

Postby chevy6600 » Sat Mar 01, 2008 6:41 pm

Hi sean, thanks for the effort you have given this and for the explanation, i do appreciate it. I note that you have been here on the forum today - a saturday. i thought i might let you know what i have done , i tweaked the `77` to a `52` which adjusted the potentiometer on the development board to be more aligned with the servo i am playing with, this now works very nicely so long as the pot value is not pushed over it`s max value :mrgreen: ...0 starts now at the 0 position of the pot. I know a limit can be added that's for later on in the programing.
You may be interested to know a little more what my intentions are, my thoughts are looking at ways of being able to receive an input from hall sensors and work the servos in a logarithmic way (from the centre position). I`m guessing i may go the `table root`. Other sensors and other program formula adjusting the servo position as and when needed.
I am looking forward to seeing your new program.
Thanks.

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: pwm examples needed

Postby Sean » Mon Mar 03, 2008 11:05 am

Nice to know our tireless efforts are appreciated :)

Here is the updated program.

MultiServo2.fcf
(5.49 KiB) Downloaded 1352 times


Port C 0 to 6 have static servo values at equal intervals across the adjustment range.
The signal on Port C 7 is controlled by the potentiometer on the development board (RV3). Jumper J14-1 must be moved to the ANA position to allow the voltage to be read by the ADC on the PIC.

A second array has been added to allow the minimum pulse width for each servo channel to be set. The figure 104 centres the adjustment range at 1.5ms (servo value = 127). The total adjustment range is approximately 1.6ms (0.7ms to 2.3ms with the figures provided). This gives approximately 150 degrees of rotation with a Futaba S3001 servo.

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

Re: pwm examples needed

Postby chevy6600 » Mon Mar 03, 2008 6:59 pm

Hi sean, i have been playing around with your new prog, i must say it is a lot better. :mrgreen:
i did a rough bench mark with the other one...sort of similar setup...and the first prog you sent to try (as i set it up with my altered version :P ) came out at doing a 200 cycle in 10 seconds. Your new prog with my additonal bits doing the same 200 cycle came in at 4 seconds :mrgreen: :mrgreen: ...Apart from that it looks a cleaner and more easy to follow prog...Time well spent....thanks very much......i`m sure others will enjoy as well, athough no one else has piped in yet, though i see they are looking! :lol:

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

Re: pwm examples needed

Postby chevy6600 » Tue Mar 04, 2008 1:02 pm

Hi, i wonder if you could suggest what could be done here. I setup the prog multi servo2 so that the potentiometer reading pointed to another number from a list, and this list/number is the one used to operate the servo. All works ok in flowcode but i could not down load it onto the development board, it looks as though it ran out of memory. So if it works in flowcode should it not work on the development board?...How do i gauge the use of memory?

p.s. I will add that this list consists of 255 variables, as in `variable`[255]... etc.

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: pwm examples needed

Postby Sean » Tue Mar 04, 2008 1:21 pm

Is this the lookup table you suggested in an earlier posting?

How many data elements have you declared?

There can be problems using large data arrays with PIC devices due to the paged memory structure. There are some tricks available to increase the effective storge capacity:

Use partial calculations and less data.
Use the data EEPROM
Embed static data in the program memory

If you want to send some more details, or a sample program, I will try to develop an alternative method for you.

User avatar
Benj
Matrix Staff
Posts: 14139
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4074 times
Contact:

Re: pwm examples needed

Postby Benj » Tue Mar 04, 2008 1:30 pm

Hello

You gauge the use of memory by looking at the messages in the compilation window.
You will get a list of the memory details similar to the following.

Memory Usage Report
===================
RAM available:368 bytes, used:126 bytes (34.3%), free:242 bytes (65.7%),
Heap size:242 bytes, Heap max single alloc:95 bytes
ROM available:8192 words, used:2468 words (30.2%), free:5724 words (69.8%)

chevy6600
Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm
Contact:

Re: pwm examples needed

Postby chevy6600 » Tue Mar 04, 2008 2:18 pm

Hi benj, the data output as you have suggested, does not realy relate to what i get, if i make variable string as `variable[111]` . This is the maximum size i can get working, this comes up with memory data as
`758 out of 8192 program words used
0 out of 256 data bytes used
That took 1.61 seconds


Return code = 0`

But if i made the variable string `variable[112]` ... that`s only one more! it would fail to down load.

pps.
There is something strange going on in flowcode.! If i made another string variable with 225, it would work ok even if have 2 or 3 variables with contents of 225, all at the same time!. The anomaly is when i use the original variable, which by the way looks like this ` graph[225]` and selecting the string parameter.
The only thing different i have done was, i used the `graph[225]` and selected parameter `byte` earlier on in my programing, and then i tried with the parameter `int`, before settling with the parameter `string`. I have not tried using the another variable as yet because i would have to re do all my table work to make use of the new variable!!
Any ideas?