CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PWM with changing frequency
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 20061

View user's profile Send private message

PostPosted: Sat Dec 19, 2015 11:56 am     Reply with quote

Honestly think again.
That is wasteful code. Dozens of tests, each with different outputs. No wonder the chip runs out of space.

Single test looping through a constant array, till the array entry exceeds the value. This array containing 40, 43, 59 etc.

Then with the index from this loop, access five 'mode' arrays, giving the widths required.

Since each of your current tests, has five tests inside, each of these will use dozens of instructions. Then there are dozens of tests. Dozens*dozens=huge code. CCS produces code that is only a few percent larger (in some cases no larger), than assembler. Your approach is large....

It's made worse by redundant tests. If adc_volts is not <=40, then there is no point in testing for it being >40 in the 'else' case. Same applies in each of the other tests. So a large percentage of the code is just waste....
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Dec 19, 2015 6:08 pm     Reply with quote

As I see it you've only programmed for one mode value, yet you're running out of code space.

There has to be a simpler way.

Tell us the relationship between pulse_widths and ADC values for each mode value.
Then (maybe) we can start to really help you.

Mike
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Mon Dec 21, 2015 3:20 am     Reply with quote

Thanks for your answers Smile
Really this program is written complex.
As the first I told, this is a old circuit in sewing machine, which I design the original Smile
About this circuit, it have a POT to read ADC value and make an output pulse in 5 modes (choosing mode by DIP switch) same with the video I attached above
My method to build a new circuit to replacing is using PIC.
I measured a output pulse in 5 mode and write a new program as above.
Really, I also see it's bad code, but I don't know a best method for solving it.
I detect onces to onces condition, and write a new program to create a pulse same with original.
But it take me have a difficult when near out of memory but not finish test case.
Really give a thanks for yours helping for me Smile
Thanks.
P/s:
This's a circuit I have renew design with PIC16F684.
I have edited some case in test ADC, now it take 79% ROM and I trying to finish it Smile

https://drive.google.com/file/d/0Bz215_lkn4g-VkY3bFFjdmdPMFU/view?usp=sharing
_________________
Begin Begin Begin !!!
Ttelmah



Joined: 11 Mar 2010
Posts: 20061

View user's profile Send private message

PostPosted: Mon Dec 21, 2015 4:25 am     Reply with quote

First, why are you hooked on using the 684?. The chip you are trying to replace, has twice the code space. No wonder things get tight. In all honesty, for anything where 'mass production' is not involved it is always far better to go for a PIC that is larger than you need, given that the price difference is typically a few cents (if that). A chip like the 16F18323, has more than four times the ROM, and is actually cheaper than the 684 (because it is a newer chip being sold in larger quantities now). It also has a whole suite of peripherals that may well be able to do some of the work for you.

Then you are ignoring the key question being asked. What is the relationship between the ADC and timings?. If these are a set of measurements that you have made (so voltages and pulse widths), then post these. The odds are that you could put this data into a spreadsheet, and calculate a simple formula, so a width is perhaps (1024-adc)*AConstant (given that the relationship seems to be for shorter times with lower ADC readings). This will take an enormous amount less space to code.

Then you are still not really telling us what the actual pulse train wants to look like. You have shown one example, but, not which times change for what. I'd suspect that this might be a simple fixed frequency pulse train, that is enabled and disabled for varying times. If this is the case, then instead of using delays, I'd be programming the PWM to generate the 'train', and turning this on and off at the times required. A lot less code.

Always the _first_ thing when programming is to get the actual operation clear. What has to happen when, and as a result of what. I'd usually start with a sheet of paper, and draw the waveforms, put on this, what values are variable, and what each of these depends 'on' (mode, ADC value etc.). You only start to think about code, once you understand the problem. You are giving us no chance to help, since you are only giving 'snapshots' of little bits of what is actually needed.
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Mon Dec 28, 2015 1:11 am     Reply with quote

Thanks for your supports, Ttelmah.
Sorry about my careless when reply for you, because my english is bad.
About the space of MCU, i'm stupid when choosing a PIC16F684 to write a program, from your advices, I change to PIC16F688 with same cost but have a twice of space ( in my location, it's difficult to buy 16F18323 as you told). Now, I have a more space to finish a program. It run but I want optimized to shortest ( maybe a happy with me)
Quote:
What is the relationship between the ADC and timings?

" timing" as you told is it a sample time to get adc value, or time to read an adc?
In original circuit, have a var resitor (50K Ohm) with about 40 step, when you turn resistor, the waveform of pulse change dependon mode choosed.
It have a five mode ( mode0-> mode4) choosing by Dip-switch.
My method is measure an waveform of output in once mode when turn var resistor step to step, note the positive pulse, negative pulse width, count of pulse loop, delay time after string of pulse.
You can think pulse output like as ( 10 10 10 10 delay 10 10 10 10 delay..)

Quote:
Then you are still not really telling us what the actual pulse train wants to look like. You have shown one example, but, not which times change for what. I'd suspect that this might be a simple fixed frequency pulse train, that is enabled and disabled for varying times. If this is the case, then instead of using delays, I'd be programming the PWM to generate the 'train', and turning this on and off at the times required. A lot less code

Yes, the frequency may be not change, but the loop of pulse and delay time change. I dont know PWM can generator a pulse like as me want.

I known all of yours are ardorly for helping me. Really thanks for all.
Have a nice holiday Smile
Thanks you.
_________________
Begin Begin Begin !!!
Ttelmah



Joined: 11 Mar 2010
Posts: 20061

View user's profile Send private message

PostPosted: Mon Dec 28, 2015 1:45 am     Reply with quote

Post up a table then, of the forty rows, with the five modes across the top, and the times needed in each position.
We may then be able to work out what describes the relationships.
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Mon Dec 28, 2015 2:01 am     Reply with quote

Ttelmah wrote:
Post up a table then, of the forty rows, with the five modes across the top, and the times needed in each position.
We may then be able to work out what describes the relationships.

Hi.
I send you a sheet of timing pulse.
Thanks you
https://drive.google.com/file/d/0Bz215_lkn4g-aldVVkdCdEVjV00/view?usp=sharing
_________________
Begin Begin Begin !!!
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Dec 28, 2015 4:11 am     Reply with quote

You're being asked to fill in a timings table which looks like the one below
Code:
 --------------------------------------------------------
| No | ADCvolts |  Mode==1 | Mode==2 | Mode==3 | Mode==4 |
 --------------------------------------------------------
| 01 |          |          |         |         |         |
 --------------------------------------------------------
| 02 |          |          |         |         |         |
 --------------------------------------------------------
| 03 |          |          |         |         |         |
 --------------------------------------------------------
| 04 |          |          |         |         |         |
 --------------------------------------------------------
| 05 |          |          |         |         |         |
 --------------------------------------------------------
| 06 |          |          |         |         |         |
 --------------------------------------------------------
| 07 |          |          |         |         |         |
 --------------------------------------------------------
| 08 |          |          |         |         |         |
 --------------------------------------------------------
| 09 |          |          |         |         |         |
 --------------------------------------------------------
| 10 |          |          |         |         |         |
 --------------------------------------------------------
| 11 |          |          |         |         |         |
 --------------------------------------------------------
| 12 |          |          |         |         |         |
 --------------------------------------------------------
| 13 |          |          |         |         |         |
 --------------------------------------------------------
| 14 |          |          |         |         |         |
 --------------------------------------------------------
| 15 |          |          |         |         |         |
 --------------------------------------------------------
| 16 |          |          |         |         |         |
 --------------------------------------------------------
| 17 |          |          |         |         |         |
 --------------------------------------------------------
| 18 |          |          |         |         |         |
 --------------------------------------------------------
| 19 |          |          |         |         |         |
 --------------------------------------------------------
| 20 |          |          |         |         |         |
 --------------------------------------------------------
| 21 |          |          |         |         |         |
 --------------------------------------------------------
| 22 |          |          |         |         |         |
 --------------------------------------------------------
| 23 |          |          |         |         |         |
 --------------------------------------------------------
| 24 |          |          |         |         |         |
 --------------------------------------------------------
| 25 |          |          |         |         |         |
 --------------------------------------------------------
| 26 |          |          |         |         |         |
 --------------------------------------------------------
| 27 |          |          |         |         |         |
 --------------------------------------------------------
| 28 |          |          |         |         |         |
 --------------------------------------------------------
| 29 |          |          |         |         |         |
 --------------------------------------------------------
| 30 |          |          |         |         |         |
 --------------------------------------------------------
| 31 |          |          |         |         |         |
 --------------------------------------------------------
| 32 |          |          |         |         |         |
 --------------------------------------------------------
| 33 |          |          |         |         |         |
 --------------------------------------------------------
| 34 |          |          |         |         |         |
 --------------------------------------------------------
| 35 |          |          |         |         |         |
 --------------------------------------------------------
| 36 |          |          |         |         |         |
 --------------------------------------------------------
| 37 |          |          |         |         |         |
 --------------------------------------------------------
| 38 |          |          |         |         |         |
 --------------------------------------------------------
| 39 |          |          |         |         |         |
 --------------------------------------------------------
| 40 |          |          |         |         |         |
 --------------------------------------------------------

Or even the full version of this.
Code:
 ----------------------------------------------------------------------------------------------------------------
|    |           |        Mode==1        |        Mode==2        |        Mode==3        |        Mode==4        |
 ----------------------------------------------------------------------------------------------------------------
| No | ADC volts | neg_pulse | pos_pulse | neg_pulse | pos_pulse | neg_pulse | pos_pulse | neg_pulse | pos_pulse |
 ----------------------------------------------------------------------------------------------------------------
| 01 |           |           |           |           |           |           |           |           |           |
 ----------------------------------------------------------------------------------------------------------------
| 02 |           |           |           |           |           |           |           |           |           |
 ----------------------------------------------------------------------------------------------------------------
etc to
 ----------------------------------------------------------------------------------------------------------------
| 39 |           |           |           |           |           |           |           |           |           |
 ----------------------------------------------------------------------------------------------------------------
| 40 |           |           |           |           |           |           |           |           |           |
 ----------------------------------------------------------------------------------------------------------------
THEN we may be able to help you.

Mike
temtronic



Joined: 01 Jul 2010
Posts: 9633
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Dec 28, 2015 7:11 am     Reply with quote

gee Mike you're GREAT at ASCII Art !!

have to admit I'm lost trying to figure out the real purpose of the program....usually I'm pretty good even with the language barrier.

I know if he shows us what he needs we can help.

Jay
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Dec 28, 2015 7:58 am     Reply with quote

temtronic wrote:
gee Mike you're GREAT at ASCII Art !!

have to admit I'm lost trying to figure out the real purpose of the program....usually I'm pretty good even with the language barrier.

I know if he shows us what he needs we can help.

Jay

Its too much effort to get my head round the alternatives.
(Also when the hosting website fails, you lose the link.)

Pleased to see I'm not the only one struggling to figure out what its all for.
(Thought I was losing my touch or going senile.)

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 20061

View user's profile Send private message

PostPosted: Mon Dec 28, 2015 8:38 am     Reply with quote

I think most of us are having problems...

I actually think the pulse width doesn't change at all. My expectation is that it is a 'burst' of pulses, and what changes is the 'on/off' time of this burst. This would make sense for driving a TRIAC dimmer to control a DC motor.
The silly thing is that if we had real data, I'm pretty sure simple formulae could be used to calculate the required burst from pot position, and it'd just be a matter of setting up the PWM to generate the burst, and turning this on/off at the right times. Vastly simpler.

0 data = 0 sense....
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

update the photos of waveform
PostPosted: Mon Dec 28, 2015 7:28 pm     Reply with quote

Ttelmah wrote:
I think most of us are having problems...

I actually think the pulse width doesn't change at all. My expectation is that it is a 'burst' of pulses, and what changes is the 'on/off' time of this burst. This would make sense for driving a TRIAC dimmer to control a DC motor.
The silly thing is that if we had real data, I'm pretty sure simple formulae could be used to calculate the required burst from pot position, and it'd just be a matter of setting up the PWM to generate the burst, and turning this on/off at the right times. Vastly simpler.

0 data = 0 sense....

Thanks for yours helping.
Sorry, maybe my show not clear for yours.
Now, I send a picture ( waveform) in 5 modes and edited the sheet file ( from excel file in the last post, I write the data in sheet 2, maybe yours don't see it).
In my circuit I have 5 modes:
- Mode 0 : a string pulse sequentially, don’t have a delay time between of strings pulse, it's only change the pulse width when turn variable resistor.
- Mode 1,2,3 : have a delay time between strings of pulse, and change the loop of pulse when turn variable resistor.
- Mode 4: like as mode 1,2,3 but have a one pulse (different positive and negative pulse widths) in the end of string pulse.

In sheet above:
- P+: positive pulse width.
- P-: negative pulse width.
- Loop: the number of pulse loop before the delay time.
- Delay: the time delay between strings of pulse.

Everybody can reference the photos and sheet again. If it's not clear, please ask me again, it will show the sheet like as above method.
Thanks for yours helping.
+ The sheet file
https://drive.google.com/file/d/0Bz215_lkn4g-NXA5MDFuRTNsZkU/view?usp=sharing

+ The photos of waveform:
https://drive.google.com/folderview?id=0Bz215_lkn4g-M2x5YWowWTc0ams&usp=sharing
_________________
Begin Begin Begin !!!


Last edited by tienchuan on Sun Jan 03, 2016 6:47 pm; edited 1 time in total
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Dec 29, 2015 8:48 am     Reply with quote

You've got the links swapped.

(The first supposedly links to the wave-forms which I can't see!
I can see the list of .png files, but that's all.
The second links to a spreadsheet, which I've not had time to decode)


Mike

EDIT

You're not making it easy.
I can't copy and paste the spreadsheet.
I've tried to print it, and only got a small part of it.

It gets worse. ( I've just found your 'scope trace at the foot of the spreadsheet.)

For each mode you've now appear to have:-

positive_pulse_width, negative_pulse_width, delay, number_of_loops.

This is like pulling teeth.

Is there any more you're not telling us?

Maybe you need to present the data in a table like my ASCII art!
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Dec 29, 2015 6:09 pm     Reply with quote

OK. I've had time to look at some of the spreadsheet data.

What appears to be happening (most of the time) is you've got:-

1) A PWM signal which is being turned on and off.
2) An off interval which changes by almost a whole PWM period.

I can't imagine the timings are critical.
In which case all you need is:-

1) Set up a PWM signal which generates an interrupt.
2) Use the interrupt to turn the pulses on and off.
3) Use the interrupt to count the off interval (the delay you've got)
4) Use the interrupt to count the on interval (the number of loops).

You should be able to hold all the data needed in a relatively small array, or calculate from a simple formula as required.
There will be a minor tweak needed to create the odd pulses for mode 4.
The whole thing should be achievable in ~20 lines of code.

You're still not telling us how this pulse train performs its control function.
(Or is it a state secret?)

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 20061

View user's profile Send private message

PostPosted: Wed Dec 30, 2015 3:07 am     Reply with quote

Thanks Mike.

As I said right near the start:

"I'd suspect that this might be a simple fixed frequency pulse train, that is enabled and disabled for varying times. If this is the case, then instead of using delays, I'd be programming the PWM to generate the 'train', and turning this on and off at the times required. A lot less code."

This makes total sense for what is needed to control a triac feeding a DC motor...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group