• Forums
• Posts
Latest Posts
Active Posts
Recently Visited
Search Results
• Page Extras
• Forum Themes
• AVR Freaks

### Hot!dsPIC33EP : ADC Averaging of Sin Wave

Page: 123 > Showing page 1 of 3
Author
karan123
Super Member
• Total Posts : 996
• Reward points : 0
• Joined: 2009/02/02 11:11:21
• Location: 0
• Status: offline
0

# dsPIC33EP : ADC Averaging of Sin Wave

Hi,

I am planning to use dsPIC33EP Series MCU.

I have gone through below thread similar to my interrupt...

https://www.microchip.com...aspx?m=1105045&p=1

Can Any body give me suggestions/AppNote?

How to do average measurement of Sin Wave
Cycle by Cycle 10ms (50 Hz) of Full Wave rectifier between two ZCDs from precision rectifier ( with sampling After 100us for each cycle )?
For making Constant Current Source ?
Not for measurement.

--
Thanks..
Karan
post edited by karan123 - 2020/07/20 10:51:29

PStechPaul
Super Member
• Total Posts : 2910
• Reward points : 0
• Joined: 2006/06/27 16:11:32
• Location: Cockeysville, MD, USA
• Status: offline
4 (1)
The link you supplied seems to cover the topic pretty well, and not much to add, really. For measurement, a true RMS calculation over an integer number of half-cycles will give you the best result, but an average is perhaps a bit quicker and easier, as it does not necessarily involve floating point (square root). You can just take the sum of, say, 16 samples between zero crossings, and then perform whatever scaling you require, still using integer math.

You say you want to make a constant current source. Please elaborate and explain just what you need. It sounds like you may be wanting to perform power factor correction (PFC), which essentially is a regulated DC-DC boost converter operating directly on the rectified AC waveform, such that it uses a maximum duty cycle PWM when the waveform is close to zero, and progressively lower duty cycle as the voltage rises.

karan123
Super Member
• Total Posts : 996
• Reward points : 0
• Joined: 2009/02/02 11:11:21
• Location: 0
• Status: offline
0
PStechPaul
The link you supplied seems to cover the topic pretty well, and not much to add, really. For measurement, a true RMS calculation over an integer number of half-cycles will give you the best result, but an average is perhaps a bit quicker and easier, as it does not necessarily involve floating point (square root). You can just take the sum of, say, 16 samples between zero crossings, and then perform whatever scaling you require, still using integer math. You say you want to make a constant current source. Please elaborate and explain just what you need. It sounds like you may be wanting to perform power factor correction (PFC), which essentially is a regulated DC-DC boost converter operating directly on the rectified AC waveform, such that it uses a maximum duty cycle PWM when the waveform is close to zero, and progressively lower duty cycle as the voltage rises.

Thanks for update...

I have to fire Thyristor(s) . Measure the Current across load.

If Current decreases in one cycle, then increase firing angle to next cycle.
If Current increases in one cycle, then decrease firing angle to next cycle.
For Constant Current Source.

Do I need to use Timer interrupt with ADC interrupt for N sample as you said 16 with ZCD Interrupt?

Thanks
Karan
dan1138
Super Member
• Total Posts : 3825
• Reward points : 0
• Joined: 2007/02/21 23:04:16
• Location: 0
• Status: offline
5 (2)
Do not be afraid of doing a true RMS computation using integer math. An integer square root can be computed almost as quickly as a 32-bit divide.

In this Wikipedia Methods of computing square roots article you will find this code to compute an integer square root:
`int32_t isqrt(int32_t num) {    assert(("sqrt input should be non-negative", num > 0));    int32_t res = 0;    int32_t bit = 1 << 30; // The second-to-top bit is set.                           // Same as ((unsigned) INT32_MAX + 1) / 2.    // "bit" starts at the highest power of four <= the argument.    while (bit > num)        bit >>= 2;    while (bit != 0) {        if (num >= res + bit) {            num -= res + bit;            res = (res >> 1) + bit;        } else            res >>= 1;        bit >>= 2;    }    return res;}`
This method tends to be fairly quick on controllers that use integer math libraries as it uses shifts not divides.
stumichaels
Super Member
• Total Posts : 561
• Reward points : 0
• Joined: 2007/03/13 21:03:10
• Location: Commack, NY
• Status: offline
4.5 (2)
Seems to me that if you are just wanting to see if the current increases or decreases, you do not need to take the square root at all.
dan1138
Super Member
• Total Posts : 3825
• Reward points : 0
• Joined: 2007/02/21 23:04:16
• Location: 0
• Status: offline
5 (1)
stumichaels
Seems to me that if you are just wanting to see if the current increases or decreases, you do not need to take the square root at all.

This would be true if that is all the Original Poster needs to get done with the dsPIC33EP controller.

When did you ever see a post in this forum where the OP spelled out in comprehensive detail all they need to get working?

Just because you can see a camel's rope under the tent flap does not mean you will find a camel at the other end.
post edited by dan1138 - 2020/07/20 16:19:30
stumichaels
Super Member
• Total Posts : 561
• Reward points : 0
• Joined: 2007/03/13 21:03:10
• Location: Commack, NY
• Status: offline
5 (1)
And I thought I was a pessimist.  You've got me beat by a mile.Smile:
ric
Super Member
• Total Posts : 28299
• Reward points : 0
• Joined: 2003/11/07 12:41:26
• Location: Australia, Melbourne
• Status: online
0
dan1138
When did you ever see a post in this forum where the OP spelled out in comprehensive detail all they need to get working?

Indeed.
I suspect post#3 is a vast over-simplification of what the OP really wants to do.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
du00000001
Just Some Member
• Total Posts : 3942
• Reward points : 0
• Joined: 2016/05/03 13:52:42
• Location: Germany
• Status: offline
0
Re post #3:
Won't be what you'd consider a "constant current" source, but the concept to somewhat control a (quite high) current is clear. Actually no real need to calculate the RMS value - the peak voltage would do just as well. Measuring the current sourced is a significantly better means, the grid voltage measurement doesn't add much information.

dan1138
... a camel's rope ...

Might well be a camel, but don't expect it to be alive  :)
post edited by du00000001 - 2020/07/22 01:03:43

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
PStechPaul
Super Member
• Total Posts : 2910
• Reward points : 0
• Joined: 2006/06/27 16:11:32
• Location: Cockeysville, MD, USA
• Status: offline
0
Maybe it's Schrödinger's Camel...

Much depends on the rest of the circuitry, especially the load and any inductance or capacitance which will affect the waveform of the current to be controlled. There will be time delays and feedback parameters to consider, and probably involve a PID algorithm. I once designed a fairly stable 1 amp power supply with fixed outputs of 48 VDC, 125 VDC, and 250 VDC, and it was all analog, using inductors, capacitors, and op-amps driving SCR gates. No processors were involved - this was around 1980.

Bob White
Super Member
• Total Posts : 336
• Reward points : 0
• Joined: 2010/11/06 19:52:38
• Status: offline
5 (1)
karan123
I have to fire Thyristor(s) . Measure the Current across load.

If Current decreases in one cycle, then increase firing angle to next cycle.
If Current increases in one cycle, then decrease firing angle to next cycle.
For Constant Current Source.

Do I need to use Timer interrupt with ADC interrupt for N sample as you said 16 with ZCD Interrupt?

If this is truly your problem statement, then I don't see that you have any need to measure the input voltage, average or RMS, at all.

1. Detect a zero crossing of the input voltage
2. Measure the output current
3. Subtract from the desired value of the output to create a current error
4. Process the current error though a PI controller to create a time delay value
5. Use the time delay value with a timer to trigger the triac
6. Go to 1
Mark Yampolsky
Super Member
• Total Posts : 122
• Reward points : 0
• Joined: 2009/04/03 18:50:36
• Location: Russia Fryazino Moskow reg
• Status: offline
4 (1)
dan1138
Do not be afraid of doing a true RMS computation using integer math. An integer square root can be computed almost as quickly as a 32-bit divide.

Successive approximation method:

;--- SQRT (W1:W0 -> W0)
SQRT:
mov # 0x8000, W5
clr    W6
LoopSQRT:
ior    W6, W5, W6
mul.uu    W6, W6, W2
sub    W0, W2, W4
subb    W1, W3, W4
btss    SR, # C
sub    W6, W5, W6
lsr    W5, W5
bra     NC,  LoopSQRT
mov   W6, W0
return

For a 16-bit  Microchip's platform is very optimal ...
karan123
Super Member
• Total Posts : 996
• Reward points : 0
• Joined: 2009/02/02 11:11:21
• Location: 0
• Status: offline
0
PStechPaul
Maybe it's Schrödinger's Camel... Much depends on the rest of the circuitry, especially the load and any inductance or capacitance which will affect the waveform of the current to be controlled. There will be time delays and feedback parameters to consider, and probably involve a PID algorithm. I once designed a fairly stable 1 amp power supply with fixed outputs of 48 VDC, 125 VDC, and 250 VDC, and it was all analog, using inductors, capacitors, and op-amps driving SCR gates. No processors were involved - this was around 1980.

I have Inductive (Weld) Load. The Current Circuit include Op-Amp. The Output Waveform is fairly Sinosudial as seen from scope.
karan123
Super Member
• Total Posts : 996
• Reward points : 0
• Joined: 2009/02/02 11:11:21
• Location: 0
• Status: offline
0
Hi,

I have written small snip-short as below between two crossings:
` // Feeding Square Wave of 10ms or Less // Because Firing of Thyristor Various// Thus Current Varies// To ADC_Channel AN0 (Continuous Conversion) #define MCU_ADCRegister volatile int StartAverging = 0 ; volatile int EdgeDetectedFlag = -1 ;  volatile int Postive_Edge_Detected = -1 ;volatile int Negetive_Edge_Detected = -1 ;   volatile long ADCAverging = 0 ; volatile long ADCResult = 0 ; volatile int Counter = 0 ; // ZCD : Highest Priority  // Timer : Middle Priority // ADC : Continuous Conversion Modevoid _ZCD_ISR() // Every 10ms{ if(Postive_Edge_Detected) { PostiveEdgeDetectedFlag = 1 ;  } if(Negetive_Edge_Detected ) { NegetiveEdgeDetectedFlag = 0 ;  }}void _Timer_ISR() // 100us Timer Interrupt{ if(Postive_Edge_Detected == 1 )  { ADCAverging += MCU_ADCRegister ; Counter++ ;  } if(Negetive_Edge_Detected == 0 ) { ADCAverging += MCU_ADCRegister ; Counter++ ; } if(Counter == 100) { ADCResult = ADCAverging/100.0 ; // Use This Result to Trigger Thyristor on Main Loop. ADCAverging = 0 ; // As Read Value Counter = 0 ; } `

Please give suggestions for that .
How to Set Counter Value as Variable if ZCDs will happen below than 10ms as rising edges as not fixed?

--
Karan
post edited by karan123 - 2020/07/21 05:59:44
PStechPaul
Super Member
• Total Posts : 2910
• Reward points : 0
• Joined: 2006/06/27 16:11:32
• Location: Cockeysville, MD, USA
• Status: offline
4 (1)
You can't (or at least shouldn't) perform floating point operations in an ISR running every 100 uSec, and you don't need to. You can just increment the (long) ADCAveraging and (int) counter variables there, during the time between PositiveEdgeDetected and NegativeEdgeDetected. Once that has elapsed, your main loop can perform floating point math, if necessary. But if you are only seeking to determine if the current is above or below a desired level, just use the ADCAveraging value directly, and adjust phase angle accordingly.

karan123
Super Member
• Total Posts : 996
• Reward points : 0
• Joined: 2009/02/02 11:11:21
• Location: 0
• Status: offline
0
Thanks for Update..

1) Do I need 100us Timer Interrupt?

2) How to take average or how to increment counter value "up" to maximum as my rising edge is not fixed. every time ?

3) The Counter Variable reaches
If ( Counter == 100)
{ }
The "100" should be variable I think

--
Karan
post edited by karan123 - 2020/07/21 23:48:03
ric
Super Member
• Total Posts : 28299
• Reward points : 0
• Joined: 2003/11/07 12:41:26
• Location: Australia, Melbourne
• Status: online
5 (1)
So as I suggested, you are just offering over simplifications of your real requirement.
It's difficult to give "hard" answers with "soft" information.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
karan123
Super Member
• Total Posts : 996
• Reward points : 0
• Joined: 2009/02/02 11:11:21
• Location: 0
• Status: offline
0
ric
So as I suggested, you are just offering over simplifications of your real requirement.It's difficult to give "hard" answers with "soft" information.

Thanks you too..
Let me again explained.
I have to two ZCDs:
1) One for voltage every 10 ms.
2) Another for Current 10 ms or less.
For firing or Two Back to Back SCRs.
The Starting point of SCRs will depend upon
voltage ZCD use as reference point.

As for constant current source..
I am measuring current (rectifed wave) in which rising edge is not fixed.

So If Current increases one cycle from set value, then it decreases to immediatel next cycle as read as 2) or vice versa.

Hope This time I am clear to you.

--Karan
karan123
Super Member
• Total Posts : 996
• Reward points : 0
• Joined: 2009/02/02 11:11:21
• Location: 0
• Status: offline
0
ric
So as I suggested, you are just offering over simplifications of your real requirement.It's difficult to give "hard" answers with "soft" information.

Thanks you too..
Let me again explained.
I have to two ZCDs:
1) One for voltage every 10 ms.
2) Another for Current 10 ms or less.
For firing or Two Back to Back SCRs.
The Starting point of SCRs will depend upon
voltage ZCD use as reference point.

As for constant current source..
I am measuring current (rectifed wave) in which rising edge is not fixed.

So If Current increases one cycle from set value, then it decreases to immediatel next cycle as read as 2) or vice versa.

Hope This time I am clear to you.

--Karan
du00000001
Just Some Member
• Total Posts : 3942
• Reward points : 0
• Joined: 2016/05/03 13:52:42
• Location: Germany
• Status: offline