Help required running two PWM channels

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

Moderators: Benj, Mods

Post Reply
medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Help required running two PWM channels

Post by medelec35 »

I am a bit stuck with running two PWM channels. Target chip is 16F73. When I click on PWM icon PWM(0) appears at to left corner of dual display window. If I then click properties and label both PWM1 and PWM2, these labels both appear below each box of dual display window.
The problem I have is when I create a macro for both PWM channels, only PWM(0) is shown in variable list. The bottom window shows 'Disabled. Since there is not PWM(1), I tried setting up 2nd PWM by adding variable of duty cycle (nldx(BYTE) of 3 for 1st parameter and 4,Variable instead of 1,Variable for 2nd parameter. The bottom display just showed a permanent 100% duty cycle, which is wrong.
My question is: How do I set up 2nd PWM channel?
Can I create and use PWM(1) from Variable list instead of the using PWM(0), and just use PWM(0) for channel 1 only?
Martin

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

Re: Help required running two PWM channels

Post by Benj »

Hello

The PWM component has 2 channels when used with the 16F73.

To enable the channels use two hardware macros. One to enable channel idx 1 and one to enable channel idx 2. You can then change the mark / space of the PWM by calling the SetDutyCycle function and referencing channel idx 1 and channel idx 2.

I've included a quick example to show you what I mean.
Attachments
QuickPWM.fcf
(3.5 KiB) Downloaded 591 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Help required running two PWM channels

Post by medelec35 »

You have made it easier to understand. Reason I was confused is I was looking and ADC Controlled PWM ,but had set the target chip for a chip which has only one PWM channel.
So I wrongly thought you needed x and Y for each PWM. So I wrongly thought for two PWM channels, I will need: 1,dutyx + 2dutyy and 3dutyx + 4,dutyy
After looking example you posted, I realised it is far simpler than that.
So I changed the target chip back to original, and Two correctly displayed waveforms appeared!
So I am sorted now.

Thank you
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Help required running two PWM channels

Post by medelec35 »

Although I have progressed rapidly since above post, My application is working apart from 10 bit resolution from ccp2.

If ccp1 is set as 10 bit using INT for variables, all is well with controlling o/p from PWM 1.
However , if ccp2 is set as 10 bit using INT for variables then there is no o/p from PWM 2.
I made sure all calculations for duty were identical for both channels.

If I changed both channels from 10 bit to 8 bit, again with identical values of duty, then both channels work 100%, but obviously with less resolution.

It's vital for both channels to have 10 bit resolution for application I am working on.

I am Still using 16F737. To get 10 bit resolution I used PWM_code.c from:
http://www.matrixmultimedia.com/mmforum ... 4681#p8602

I know Flowcode simulator won't simulate 10 bit resolution, so I am only talking about programmed hardware.

Any help in resolving this is greatly appreciated
Martin

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

Re: Help required running two PWM channels

Post by Benj »

Hello

What speed or period are you running the PWM at? In the datasheet it says that the PWM can only run in 10-bit mode if the period speed is 19.53khz or less at a clock speed of 20MHz.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Help required running two PWM channels

Post by medelec35 »

Clock speed = 4MHz
period = 500 Microseconds = 2kHz
Last edited by medelec35 on Thu Feb 12, 2009 10:29 pm, edited 1 time in total.
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Help required running two PWM channels

Post by medelec35 »

To take all calculations out of equation, I decided to just have two variables labelled ADC_F1 & ADC_F2. Both assigned with a value of 60.
So did normal enable pwm(0) 1 & pwm(0) 2
Then SetDutyCycle pwm(0) 1,ADC_F1
SetDutyCycle pwm(0) 2,ADC_F2
Programmed chip, powered up etc.
Duty cycle of pwm1 was measured and = 48% and period = 101.73 microseconds
Duty cycle of pwm2 measured and = 48% and period = 101.73 microseconds
So Real chip behaved exactly as Flowcode simulator.
All I did then was change both SetDutyCycle’s to SetDutyCycle10Bit
Re-flashed chip. Pwm1 is now measured at 12%, so I would expect pwm2 to run at 12% since both ran at 48% prior to setting to SetDutyCycle10Bit
But as stated in a previous post, only pwm1 has any output. Nothing at all from pwm2.
Is there a problem with SetDutyCycle10Bit PWM_Code.c?
Or is it me overlooking something?
Internal clock is set at 4MHz and frequency of pwm = 2KHz
Last edited by medelec35 on Wed Feb 11, 2009 11:19 pm, edited 1 time in total.
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Help required running two PWM channels

Post by medelec35 »

I Know a bit about asm, but know very little about C.
So I loaded my compiled code from Flowcode (using 10bit PWM) into a simulator,
and this is what I have found so far.
First CCP1.
If i use 0x1234 for first PWM channel duty cycle variable. Both pwm1 duty and pwm duty variables are stored ok in reg 0x24 , 0x23 and 0x26 & 0x25.
then 34 is loaded into reg 2A, 2 is loaded into 2B, then is manipulated, then 5 is loaded into 2B, basically result 8D (which derived from 8 MSB) is loaded into CCPR1L, which leaves 2MSB for CCP1CON<5:4>. (but that was loaded with 0C), so <5:4> = 00, which im not saying is wrong but I thought a 1 should be there. However since PWM 1 is working OK, I checked PWM2 calculations.

It is using same temp variables so I used 0x5678 as an example for second PWM channel duty cycle variable. However none of 5678 was put into 2A or 2B, which meant 0 is placed into CCPR2L thus duty is = 0% which is why PWM2 is not working.
Could some one please look at the 10 bit PWM_Code.c and find out why any of 10bit variable are not being placed into CCPR2L and CCP2CON.
Thats if I have worked out correctly of course.
Has anyone used PWM component 10bit duty cycle for two CCP chanels?
Did both channels work ok on real hardware?
Attachments
pwm test10bit.fcf
(4 KiB) Downloaded 483 times
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Help required running two PWM channels

Post by medelec35 »

here is PWM_Code.c used.
I thought I would post it, since Original PWM_Code.c does not have 10bit support.
Attachments
PWM_Code.c
(7.78 KiB) Downloaded 413 times
Martin

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

Re: Help required running two PWM channels

Post by Benj »

Hello

Ok I have tackled the code and managed to worm out the bug from the code that was causing PWM channel 2 not to work in 10-bit mode.

Replace your PWM code file with the file attached to the forum and then restart Flowcode.

Let us know how you get on. Also thanks for the PM Medelec, dont worry it takes a lot to offend me :)
Sorry for the delay in replying its very hectic over here for us at the moment.
Attachments
PWM_Code.c
(7.81 KiB) Downloaded 432 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Help required running two PWM channels

Post by medelec35 »

Thanks Ben.
Although not had chance to test in the real world yet, the simulator oscilloscope is now showing correct waveforms for both ccp1and ccp2.
So thank you for solving problem. I am going to be learning C, so I can go a step further myself and solve C based problems :mrgreen:
Martin

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

Re: Help required running two PWM channels

Post by Benj »

Hello Medelec

No problem sorry it took so long to get to the bottom of this. Glad its all working now. I have confirmed that the code is working on a 16F877A target device.

The C code in the component was actually correct however due to some small inconsistencies with the BoostC compiler statements such as

var1 &= var2
var1 |= var2

do not work properly. Replacing these statements with

var1 = var1 & var2
var1 = var1 | var2

made the problems go away. Thats why it took me a while to spot the problem as the statements were working for channel 1 but for some reason not for channel 2.

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Help required running two PWM channels

Post by mimiyum »

Hello.

I am also trying to run 2 PWMs on a PIC16f877a being programmed on the HP488 but am really stuck.

When we send a 0 output to bit3 on port C and enable 1 PWM, we can measure the PWM with an oscilloscope. When we enable 2 PWMs, we can no longer get any PWM on either CCP1 or CCP2.

Please help!

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

Re: Help required running two PWM channels

Post by Benj »

Hello Mimiyum

Would you be able to post your FCF file and your generated C code file that is exibiting this problem and I will look into it for you.

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Help required running two PWM channels

Post by mimiyum »

Hi Benj

This is a very simple PWM program we made just to test the 2 PWMs on the pic16f877a which do not appear when measured with an oscilloscope.

Another question...when using the PIC16f877a to generate a single PWM, when measured with a digital meter, the maximum voltage output of the PWM is around 2v. We are using a 20MHz crystal on our board. What should we do to get a maximum of 5v when we set PWM dutycycle at 255(maximum)?

Thanks!
Attachments
FLOWCO~1.c
(5.61 KiB) Downloaded 323 times
Flowcode1.fcf
(4 KiB) Downloaded 354 times

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Help required running two PWM channels

Post by mimiyum »

Although I still have the 2 PWMs problem, we figured out already the cause of the lower voltage resulting from the PWM...it is caused by a 10k resistor on MCLR. When we removed that, we already get the correct voltage!

I will just wait for your feedback Benj on the 2 PWMs problem.

Thanks!

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Help required running two PWM channels

Post by medelec35 »

Im not an expert like Benj, but I had a look at your Flowcode, and compiled + simulated it on a different simulator.
Since simulator is running OK with a pwm o/p from both channels,your problem could be the configuration settings.
Since you have not set configuration settings in Flowcode, the default will be 3FFF
This will mean when you program chip it will be configured as:
OSC=RC
WD Timer = ON
Unless you have manually configured your settings then a solution could be as follows:
In Flowcode go to Chip menu then select Configure.
Enter following in the configure box
0x2007,0x3f7a
Save, then compile
Martin

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Help required running two PWM channels

Post by mimiyum »

Thanks Medelec. I'll try that out

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Help required running two PWM channels

Post by medelec35 »

mimiyum,
Did setting configuration in Flowcode work?
Martin

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Help required running two PWM channels

Post by mimiyum »

Hi Medelec,
No :cry: ...I need to try it again to give you the correct info on the settings that I tried.
I was able to get 2 PWMs on the 2 pins but they do not respond to the ADC input. I just don't remember which setting worked. It looked like it was getting the PWM info from somewhere else.
Anyway, will get back to you on that.

Post Reply