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 support@ccsinfo.com

PWM/DAC...etc General advice required.
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kcj



Joined: 05 Oct 2011
Posts: 33

View user's profile Send private message

PWM/DAC...etc General advice required.
PostPosted: Tue Feb 14, 2012 4:17 pm     Reply with quote

Hi All,

I have a general question regarding the best way to output a 10bit number on a PIC pin, the number is related to a force sensor voltage whose value changes every 10th of a second, voltage increases then decreasing and looks like a sine wave when connected to an oscilloscope. The frequency varies depending on the cycle of force been applied to the sensor. The voltage range is from 0v to 1.2v maximum which = 750N. The sensor data is transmitted via xbee to a coordinator xbee via its uart to Tx/Rx PIC18F25k22, which deciphers the byte packets and changes the volt data into a 10bit number.


I wanted to know if you could pick any PIC and use any method to output a 10 bit number on a pic pin relating to 0 to 1.2v, that changes every 10th of a second and frequency varies. How would you do it?

Currently i have outputted the voltage using PWM, which creates a square wave output. I used a variety of different RC filter ranging from 0.0047uf to 2200uf and resistor values from1k to 10M to get a suitable wave form, which a voltage datalogger can be connected to display the cycle of force. Unfortunately it is not very realistic graph. The response is either square wave quick response with a bit of the edges rounded off or a a slow steady response depending on RC value. Also by increasing the frequency of applied force the response is constant. Also thought about the DAC, but with only 32 steps, its maybe not the best option either.

Any advice greatly appreciated
Mike Walne



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

View user's profile Send private message

What do you want?
PostPosted: Tue Feb 14, 2012 4:59 pm     Reply with quote

You've presented a lot of infromation, but I still don't understand exactly what it is you want

(1) What is going into your PIC?
(2) What do you want to come out of your PIC?
(3) What options are available for the output?
(4) Do you want a smooth DC output?
(5) If it's DC you want, what is the range required?
(6) Is a raw square wave (PWM) acceptable?
(7) Do you want a sine wave?
(8) Are all the references to xbee really irrelevant to the real problem?

Please be a little more precise.

Mike
kcj



Joined: 05 Oct 2011
Posts: 33

View user's profile Send private message

PostPosted: Tue Feb 14, 2012 5:36 pm     Reply with quote

Hi Mike,

Sorry yes it is alot of information, to clarify.

1) The only input into the PIC is from the coordinator xbee. Tx/Rx Pin. A router xbee is connected to a flexiforce sensor via an OPAMP. That xbee has an AD converter on it that converts and transmits the data from the force sensor to the coordinator. The transmission is in the form of 22 bytes, byte 19 and 20 are the LSB and MSB of the sensor data. To convert this into a 10 bit number, i use the following formulea-

num1=((data19*256)+data20) to combine the LSB and MSB into one variable called num1, which has a range of 0 to 1023

I want to output this value relating to a voltage range of 0v = 0 number and 1.2v = 1023 on a pic pin.

I have a 16x2 LCD display also connected to the PIC which shows the volt data(num1)on it.

2) I want to output the value, so i can connect an PICO oscilloscope to show the change of volts of the sensor over time. I have already calculated another formulea which related the volts to a force in Newtons.

If you connect the output from the sensor OPAMP circuit to an oscilloscope befor transmission, the output is a sin wave. The force increases then decreases. ranging from 40 to 100RPM.

3) What options are available?

Well i have outputted the value using a PWM @ 19.5Khz and an RC integrator to smooth the square wave into a useable waveform for the oscilliscope. The trouble is the RC circuit does not give a good visual display of the output with any RC combination at any frequency.

I am not sure of any other options, that is why i am asking the question to you what other options are there available????

4) yes, i would like a smooth DC voltage that changes when force is aplied to the sensor. So if 750 Newtons is applied to the sensor for 10 seconds i want the PIC to output 1.2v( 1023 number) on a pin for 10 seconds.

5) Range 0N=0v=0 number to 750N = 1.2v = 1023 number .

6) if i could find a way of changing the PWM wave so the PICO oscilloscope could understand it, and the voltage changes with force over time, then yes it would be acceptable. Currently if i connect it directly from the PIC pin, all you see is the square wave!

7) Ideally, the output from the sensor will be a sin wave form. the sensor is used in a bicycle pedal, so the force changes over time like a sin wave. The purpose of this project is to display the changes of force over time on a graph in real time, my presentation is in a few wks. I could connect the oscilloscope to the circuit directly but a more impressive way would be doing it would be wirelessly. Hence my problem?

8) The xbee is an important part of the project.

I have being working on this for 5months now and i am so used to it i am not sure if i am giving a good explanation to the problem at hand.

Thanks
Mike Walne



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

View user's profile Send private message

The real problem
PostPosted: Tue Feb 14, 2012 6:36 pm     Reply with quote

It's gone mid-night here in the UK. I'll do some pSpice simulation tomorrow to see if I can offer you a solution.

(8) I understand that xbee is integral to your complete system. What I meant was that I don't have to consider it as a part of the problem which you want solving.

Is what you really need just some way of faithfully recovering the original low frequency signal from your force sensor?

Mike
Mike Walne



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

View user's profile Send private message

More questions
PostPosted: Wed Feb 15, 2012 2:17 am     Reply with quote

OK. I've slept on it.

I can see three possible solutions, and the fundamental reason why you have a problem.

Two solutions are small, the third depends on the constraints you are under.

First, I need the answers to more questions:-

(1) Must your solution be entirely passive (i.e. no OPA)?
(2) If you are allowed an OPA, must it operate from the PIC's supply rail?
(3) Do you have available power supplies of say +/- 12V to drive an OPA?
(4) Is an output delay of say half a second acceptable?

(5) What kind of a project are we dealing with here (student, industrial...)?
(6) Is the 10bit value an average of the previous 100ms's values or an instantaneous sample?



Mike
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Feb 15, 2012 8:10 am     Reply with quote

To output an analog voltage on a PIC pin you have 3 basic choices:

1) Some PICs have a DAC but all I have seen are pretty crude.
2) Output PWM into a filter. A single stage RC filter may work if properly designed, but a multistage one will work better. To do a good job takes some engineering. I have never found a simple filter desgin program that works well in this situation. You will probably need to use an op amp, and to get good reponse down to 0V you will need a negative supply. If you could output 0.2V to 1.2V or 0.2V to 1.4V the problem would be considerably easier.
3) The PIC could output serial data to a real DAC.

If there is any way your PICO scope could read a frequency input instead of a voltage input this problem would be much easier.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Mike Walne



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

View user's profile Send private message

What is the question?
PostPosted: Wed Feb 15, 2012 9:11 am     Reply with quote

Hi, its me again.

I've coming to the conclusion that you're asking the wrong question! As a consequence you are never going to get the 'right' answer.

You've been offered, and have tried several, solutions to your original question. Your feedback is that they are not satisfactory for various reasons.

What you seem to be looking for is a reconstruction of the original sinusoid presented to your primary ADC.

Your fundamental problem is that you are taking 10 samples / second and hoping to regenerate a 100RPM (1.6Hz) sinewave.

In essence you've got around 6 samples per cycle and pushing up against Nyquist limits.

It's more than probable you're losing some information in the conversion process.

I will try to help but I need some feedback about what level of degradation would be deemed acceptable.

I look forward to your reponse.

Mike
kcj



Joined: 05 Oct 2011
Posts: 33

View user's profile Send private message

PostPosted: Wed Feb 15, 2012 3:11 pm     Reply with quote

Hi Mike, SherpaDoug,

Sorry for the late reply, Wednesday and Thursday are lecture and training full, so I haven't had a chance to reply until now.

Answers to original posts

1) Not entirely sure what the question means > NO OPA??
3) I don't have 12v to drive a OPA.
4) An output delay of 0.5 sec is acceptable

Answer to your next post

I think Mike, you are exactly right when you say all I want is a replication of the original sin wave before ADC. I am sure there is a Graphic LCD that could display the value, but that is beyond the scope of this student project. I intend on bringing a bike in a lecture theatre and hooking this thing up to the projector for a visual demonstration. Much more impressive. I can sample at a greater rate, but I am not sure that is the problem? The xbee doesn't send the frequency data. I am sure there is probably a way to solve the problem using code.

Before the voltage is converted, the graph gives a great realistic visual display of how the force change in real time. The frequency represents an increase or decrease in RPM, you can see how much you are pushing down pulling up/ how efficient your technique is... So I was hoping the transmitted version would be similar representation. But time is limited so close enough may have to do.

Yes i have tried several attempts to solve the problem and have run out of possible solutions. I am new to PICS and electronics since last September, so my knowledge is fairly basic but I have learned alot, thats why I asked the question here. You guys deal with this sort of thing daily and have years of experience.


SherpaDoug,
I have tried 1 & 2 of your suggestions but not 3. The problem would be outputting just 10 bit voltage number, the xbee sends a lot of other information that is not required. Unless you could program the real DAC to understand the data. I am not sure how useful it would be. Does it understand a PWM signal?

Regarding option 2, By multistage do you mean two RC integrator's and a buffer?? The voltage range is limited to 2.5v, as this is the maximum the PICO can read. So any voltage range from 0 to 2.5v or 0.2v to 2.5,. As long as the graphs match up.The scale of the voltage can be worked out after.

I think the PICO data can read frequency of the signal, I am not sure how useful that would be, as a PWM has a fixed frequency set by Timer 2. But I can't test that out until tomorrow.

Thanks for your input, much appreciated
Mike Walne



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

View user's profile Send private message

Waveform reconstruction
PostPosted: Wed Feb 15, 2012 5:31 pm     Reply with quote

You've asked for help converting a 10 bit number into a DC signal. SherpaDoug has offered you several possibles and you've tried some.

If we answer your question EXACTLY as you asked, the raw waveform looks roughly like this:-

Code:


10          --- D                       --- K                       ---R
           |   |                       |   |                       |
9          |    --- E                  |   |                    --- Q
           |       |                   |   |                   |
8          |       |                --- J   --- L              |
           |       |               |           |               |
7          |       |               |           |               |
        --- C      |               |           |               |
6      |           |               |           |               |
       |           |               |           |               |
5      |            --- F          |           |            --- P
       |               |           |           |           |
4      |               |           |           |           |
       |               |           |           |           |
3      |               |        --- I           --- M      |
       |               |       |                   |       |
2   --- B              |       |                   |       |
   |                   |       |                   |       |     
1  |                    --- G  |                   |    --- O
   |                       |   |                   |   |
0-- A                       --- H                   --- N


This waveform is your starting point.

Either the 10/12 bit DAC as suggested by SherpaDoug or your 19kHz PWM followed by some mild RC filtering will produce it.

With your 19kHz PWM, the PWM is effectively a carrier for your information signal, the squared up sine wave above.

Your real problem is, how do you get from the squared up sine wave to the more rounded original.

The REAL solution is to increase your sampling rate by at least an order of magnitude.

You have to accept that a lot of information has already been lost. When you try to recover the original there is bound to be some signal distortion. There will be a degree of either amplitude, waveshape or frequency error. You have to decide how much loss of fidelity is acceptable and the preferred compromise.

You've tried simple RC filtering, SherpaDoug has already warned you that a multipole filter takes quite a degree of design effort and the results rarely live up to expectation.

When I asked about passive solutions I wanted to know if you were restricted to using resistors, capacitors, and inductors. You've tried a range of one pole RC filters. A two pole LCR filter could give you better results but I doubt you have the design effort available. SherpaDoug has suggested a two pole active filter, but has warned that you will need a negative supply rail to get anything approaching satifactory performance, unless you impose some sort of voltage offset to compensate. I'd do a Sallen & Key, I find them fairly robust and stable but you do need to give the OPA an easy ride, hence the request for supplies outside the PIC's supply rails.

What you would like is to recover the original sine wave.

For starters:-

Suppose you joined up the points ABCDEFGH...... etc with straight lines.

Would that be a better waveform representation than the one you've got now?

To do that you derive the equation for the line from A to B , then B to C etc.

The equation for each section is of the form y = ax + b. Its simple interpolation.

To get the straight line output, you make a high frequency PWM signal follow the y = ax + b equation, followed by simple RC filter.

The next stage is to move from a straight line to a parabola. The gain here is that you achieve a modicum of waveform rounding. The equation for a parabola is of the form y = ax^2 + bx + c. To do this you calculate a,b,c from three points ABC, then BCD, CDE etc. When I was a lad it was school level maths.

The more points you use, the higher the order of the polynomial you get, and the better the waveform will look.

The ideal would be to do some form of FFT, but we don't need to go there.

Mike
Mike Walne



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

View user's profile Send private message

Polynomials
PostPosted: Thu Feb 16, 2012 10:24 am     Reply with quote

Right, I've tried a demo in Excel:-

(1) Started with 100RPM sine wave.
(2) Sampled at 100ms intervals.
(3) For first group of three sample points (i.e. 0,1,2) worked out coefficients of the parabola which joins them.
(4) Plotted parabola for points 0 to 1 on graph.
(5) For sample points 1,2,3 worked out coefficients of the parabola which joins them.
(6) Plotted parabola for points 1 to 2 on graph.
(5) Repeated for points 2,3,4 etc

It looks fairly reasonable at 100RPM and very good below 80RPM.

I think you may be able to get away with it. If not you certainly should with a third order polynomial.

Best wishes

Mike
Mike Walne



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

View user's profile Send private message

Filters & Polynomials
PostPosted: Fri Feb 17, 2012 7:30 am     Reply with quote

Some last thoughts on two pole filters.

If you kick a two pole filter (either LC or Sallen Key) its response is to generate SHM (or DHM) at a specific frequency (depending on Q). Unfortunately this is only a partial solution. You're also wanting to recover amplitude and frequency. There is inherently enough information in say six samples (~one cycle at 100RPM) to do just what you want but I suspect that's outside your scope in the available time.

And polynomials

A standard expansion for a sine wave goes as:-

sin (x) = x + (x^3)/!3 + (x^5)/!5 .......

Where ! is a standard nomenclature for factorial. !3 = 3 * 2 * 1 ........... !5 = 5 * 4 * 3 * 2 * 1 .........etc.

For small values of x, the higher order terms very rapidly become insignificant.

Have a play with Excel. Try generating the above series with different numbers of terms and compare the results with the built in sin(x) function. You will very quickly get a feel for how many terms you need to get close enough over a range of x values. For example, the first term only is good to better than 1% for 20 degrees or so.

At your maximum 100RPM the samples are spaced at ~60 degree intervals. At lower RPM your samples are spaced at smaller angular intervals, so your accuracy will improve. The advantage (as I see it) with using polynomials is that below a critical RPM the method is not frequency sensitive.

Mike
kcj



Joined: 05 Oct 2011
Posts: 33

View user's profile Send private message

PostPosted: Fri Feb 17, 2012 7:08 pm     Reply with quote

Hi Mike,

Thanks for your input, i am still working on the problem and am trying some of your suggestions, which make interesting reading.

I had an idea of an alternative solution. Considering the PWM frequency is constant, the only thing that changes is the duty cycle, i have heard of a chip that measures pulse width, maybe using that to time the pulse and convert back into a voltage that changes with time. Just an idea.

thanks for the input. I will keep you updated...but any ideas brainstorming ideas welcome.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Feb 17, 2012 8:17 pm     Reply with quote

There are F to V converters which do the same sort of thing we are talking about, and they have the same sort of problems. But it might be worth looking into them.

You can also look up "Dwell Meter" as people working with breaker points on engines have been struggling with this for about 100 years.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Mike Walne



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

View user's profile Send private message

Polynomials
PostPosted: Sat Feb 18, 2012 3:14 am     Reply with quote

Sorry, you're missing the point again. You've got yourself into one way of thinking and can't get out of it.

What I'm suggesting is curve fitting with PWM, its a complete solution.

Suppose you want to do a second order fitting.

Your original sample interval is 100ms.
Divide that up into say 10 time slots, i.e 10ms time slots (or maybe 8, 16 ,32, you choose). You've now got a 10ms sub-sample interval.
From your original 100ms samples you calculate 10 off sub-samples for each of the 10ms sub-sample intervals. (You don't need 11.)
You run your PWM at a fixed frequency and change the duty ratio at 10ms intervals.
For a 10 bit PWM your duty is the same value as the sub-sample.
With a modest amount of RC filtering you'll get a DC voltage which resembles the parabola joining the corners of your squared up sine wave.

You've already got all the bits and pieces you need, you don't need anything else!

The procedure goes as:-

(1) Wait for the first three off 100ms samples ( A, B, C).
(2) Work out the coefficients a,b,c for the parabola that joins A,B,C.
(3) Calculate the first set of 10 sub-samples.
(3) Now make the duty ratio equal to the sub-sample at the 10ms sub-sample interval.
(4) After 10 sub-samples your next main sample will have arrived and you can now work with samples B,C,D. i.e. back to (1).

So you will get a smooth curve which is lags the original by 200ms plus the time taken to solve for the coefficients. Using integer maths throughout you should be able solve for a third or fourth order polynomial with delays of 300ms or 400ms.

Mike
kcj



Joined: 05 Oct 2011
Posts: 33

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 8:44 am     Reply with quote

Hi Mike,

Okay. At 100RPM there is 6 samples per revolution, this will correspond to half a sin wave depending where the first sample was taken. ie from o to pie.

The ADC converter change this and is transmitted, the pic converts it back to a number, to be outputted at a pin. So for example if i am cycling at constant 375N maximum down force@ 100 RPM the calibration is setup to be 50% duty outputted which is 1.56v at the pic pin. A perfecf square wave.

I thought a PWM outputted at the pin is always a square wave and you could not get a sin wave output. If i hooked up the oscilliscope and put the setting a real time. ie 1/sec per division, all i see is a mass of lines. Afterall one period is 51.2 microseconds.

If i am understanding you correctly, and i do some maths to divide each 100ms into 10 subsections then work out an equation to form a sin wave then output that number at 10ms interval to the pin.

HOW DO YOU CHANGE THE DUTY ratio to output that number at 10ms.
The CCS command is

set_pwm1_duty(num1);

where num1 is the 10bit number and related to 100ms sampling.

what controls the pulse width? One revolution takes 0.6 seconds. I suppose that means 0.1ms between a possible number change in pic which has just done 1950 pulses with the same pulse width.

Wont i still see a square wave, a small RC filter might regulate it.
But will it just be the same as what i have already done, just with more information outputted?

Ah i am beginning to get your point. The duty cycle numbers are more like a sin wave before being outputted. The pulse width is changing quicker. ie every 195 pulses with 10 sub samples that are related to each other.

Well i think i get it.
I will get working on this.
Thanks
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 1, 2  Next
Page 1 of 2

 
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