Automatic Power Factor Correction Systems

The power factor is one of the problems in electric power quality improvement. In many cases, low power factor causes the waste of electrical energy. In large commercial and industrial sectors, usually used in a centralized system of capacitors in a room for power factor correction. However, changes in the electric power consumed by electrical equipment operated at any time require changes in the capacity of capacitors which must compensate for the inductive nature of the electrical equipment is being operated. A value system that can provide appropriate capacity of capacitors to improve power factor is needed on systems with dynamic changes in electrical load.
Ideally, all customers of electric energy should have a good power factor (close to the value 1) to reduce the cost of electrical energy production. However, power factor corrections should be done the right way for any equipment used. This raises a new problem: how to design a power factor correction systems that can be used for all electrical equipment used. And how for power factor correction systems can also be operated for a variety of electrical load, so it does not need to be plugged at any existing electrical equipment. With this system, the expected power factor can be fixed easily and the system can be used for every electricity customer. With the corrected power factor, the electricity companies no longer must be generated electrical energy that is greater than the needs of customers.
Power factor is one parameter determining the quality of electric power. A good quality electrical power will increase efficiency in the electricity system. With the same production at the customer side, the total electrical energy consumed by customers will be reduced. This will be followed by a decrease in the voltage loss in the distribution system, thus decreasing the voltage drop on the customer side. On the generation of electrical energy, without reducing the electrical energy dollars are sold, the total energy generated will be smaller so that the operational costs of electricity generation will be reduced. With the increased efficiency of electric power, rising power demand next few years can still be supplied by the current generator, so the electricity deficit in this country for a while can be forgotten and needs additional power plant may be delayed.


Using ATmega8535 AVR Microcontroller
Heaviest Electrical Loads for the System 
Modeling using MATLAB
The Performance of the APFC

Using ATmega8535 AVR Microcontroller

APFC system uses AVR microcontroller family as the main component that has an internal ADC. ATmega8535 microcontroller is used here. To be able to record voltage and current signals, we use a voltage-to-voltage converter and current-to-voltage. A signal conditioning is used to make the both output signal voltage of the converter in accordance with the characteristics of the ADC inputs. This system uses a capacitor bank (C-bank), which consists of four capacitors. These capacitors are used for power factor correction.


The need for Automatic Power Factor Correction (APFC) Systems
Heaviest Electrical Loads for the System  

Modeling using MATLAB
The Performance of the APFC

Heaviest Electrical Loads for the System

For this system, the greater the power used, effort should be made for power factor correction is also more severe. Capacitor capacity value should be used for power factor correction will be even greater.
At the same apparent power, the lower the power factor, power factor correction efforts are also increasingly heavy. Attempts to raise the power factor will require a capacitor with larger capacity as well. Low power factor due to the large value of phase difference between voltage and current.
Heaviest electrical loads for the system occurs when the load has largest apparent power and smallest power factor. All capacitors in the C-bank should be able to correct this condition so that the value of power factor becomes one.
Modeling using MATLAB
This model is created using Simulink in MATLAB version 6.5.1. Models of automatic power factor correction systems can be seen in the following figure. This model consists of several subsystems and the S-Function. This system is also equipped with input devices that can retrieve data from a file and the output device (data logger), which can save data to multiple variables in a workspace that can be saved to a file for further processing. Listing program written in C language.


The need for Automatic Power Factor Correction (APFC) Systems
Using ATmega8535 AVR Microcontroller
Modeling using MATLAB
The Performance of the APFC




Modeling using MATLAB

This model is created using Simulink in MATLAB version 6.5.1. Models of automatic power factor correction systems can be seen in the following figure. This model consists of several subsystems and the S-Function. This system is also equipped with input devices that can retrieve data from a file and the output device (data logger), which can save data to multiple variables in a workspace that can be saved to a file for further processing. Listing program written in C language.




The need for Automatic Power Factor Correction (APFC) Systems
Using ATmega8535 AVR Microcontroller
Heaviest Electrical Loads for the System 
The Performance of the APFC

The Performance of the APFC

Assuming the capacitor value of automatic power factor correction systems can be any, or in other words the number of variations of the value of the capacitor capacity is infinite, the higher the apparent power of load or the lower the power factor of load, power factor correction efforts will be heavier and value output power factor can still be worth far below one.
But with the limited number of variations in the value of the capacitor capacity, as long as the maximum limits of correction, the higher the load apparent power, power factor corrected values would be guaranteed getting closer to one and have a fairly small fluctuations.
In order to achieve the value of power factor as close to one, the determination of the total capacity of capacitors in the C-bank should be based on the largest electrical load that happens, both electrical load with the greatest apparent power and the smallest power factor, and not based on installed power capacity.
In conditions with heavier loads than specified, the system will perform power factor correction by using all of available capacitors and there is the possibility of power factor corrected still much smaller than one.
With the same power factor value, the lower the load apparent power, power factor corrected values will be more varied and this value can be far below the value of one. The low power factor at low power will not be harmful because the reactive current that occurred only a little.
The model has been developed can be implemented into a prototype of automatic power factor correction that can be used for many consumers.
For overall power factor correction, should be considered the harmonic components. This research can proceed with efforts to correcting harmonic component.


The need for Automatic Power Factor Correction (APFC) Systems
Using ATmega8535 AVR Microcontroller
Heaviest Electrical Loads for the System 
Modeling using MATLAB

Measure the Frequency of Grid Voltage and Phase Difference between Current of Load and Grid Voltage


Voltage range     : 40 V ~ 280 V (Vrms)
Current range     : 0 ~ 200 mA
Frequency          : 35Hz ~ 70Hz
Phase differnece : 0° ~ 90° (lead or lag)


        Voltage grid should be is 50Hz or 60Hz. But in reality the frequency of the grid are sometimes shifted slightly, so be below or above 50Hz or 60Hz. The stability of the voltage frequency is one indicator of the quality of electrical voltage. The stability of the power grid's frequency is a health indicator of the grid's ability to respond to changes in supply and demand of electricity. Although there are other grid characteristics that can be measured,
frequency is less affected by local disturbances than other attributes like voltage and current.

       As we develop more energy resources, these resources need to be integrated efficiently and cost effectively with our existing energy infrastructure – the power grids. Alternating Current, or AC, power generators need to be synchronized to provide optimal service and electric energy supply. When we consume electricity, we place a load on the power grid. We can develop smart appliances that can measure the grid's frequency and the health of the grid's power supply; then respond appropriately by scheduling or reducing load to help maintain grid stability.
      Most of the electrical load current draw currents that have a different phase with the voltage supplied to him. Current on inductive load is lagging behind the voltage. Current on incandescent sometimes precedes the voltage of grid. Fact, the current in the electrical load that use of switching power supply is extremely precede the voltage.
      In order to achieve maximum efficiency in electricity usage, the phase angle between the currents must be equal to the voltage, in other words, the current must be in phase with voltage. Real power comparison of voltage multiplied by current is called the power factor.
Ideally, power factor of load is to be one.
      Measuring instrument of real power and power factor both analog and digital has been widely available. However, a measurement to the phase difference between voltage and current flowing in the grid is still rarely found. The need for these measuring devices in the lab sometimes still constrained. This paper describes the results of the implementation of the ATmega8535 microcontroller as a major component of the device for measure the frequency of the grid voltage and measure the phase difference between voltage and current flowing in the load. Furthermore, the expected applications can be developed for the measurement of the values of voltage and current RMS, power and power factor, even down to the power analyzer  based microcontroller.
Programs in this microcontroller can be applied to several other AVR microcontroller series if this program will not be developed further.

The Need for Phase Difference Measuring Instrument
Measure the Frequency of Grid Voltage rather than Measure the Frequency of Current
Voltage and Current Converter
The Signal Conditioning
The use of the microcontroller ADC
Frequency Measurement
Phase Difference Measurement
Output Display Format

click here for more information

Measure the Frequency of Grid Voltage rather than Measure the Frequency of Current


      The fundamental frequency of the current flowing to a load will be the same to the fundamental frequency of the voltage supplied to it. Due to the current usually have more harmonic components than voltage, frequency measurement easier done on the voltage supplied to it.
      The frequency of the grid voltage in Indonesia is 50 Hz, whereas in some other countries is 60 Hz. To reduce the calculation errors caused by harmonic components, frequency range of the measurement values can be restricted so that the harmonic frequencies will be ignored. To increase accuracy, measurements of the frequency value for the low frequency signal, is done by first measuring the value of the period, and not by counting the occurrence of waves. Nevertheless, the accuracy of the measurement result is displayed in the format of three digits such as the frequency measuring instruments for grid voltage.
best to avoid using voltage and current sensors
      Detection of voltage and current values does not use a voltage and current sensor. Generally, the voltage and current signal was converted to DC voltage signals by a sensor and then fed to the ADC input. But this will make the voltage and current cannot be analyzed further more.
      In this application, the value of alternating voltage which has been normalized is fed directly to the ADC input of the microcontroller. Current can be detected by the ADC after converted into a voltage. 



The Need for Phase Difference Measuring Instrument  
Measure the Frequency of Grid Voltage rather than Measure the Frequency of Current
Voltage and Current Converter
The Signal Conditioning
The use of the microcontroller ADC
Frequency Measurement
Phase Difference Measurement
Output Display Format

Voltage and Current Converter

Voltage-to-Voltage Converter:
                  Input                      Output
Vrms      0 ~ 280V        0.57 ~ 1.77V with 5VDC offset or
Vpp        0 ~ 400V        0.80 ~ 5.00V with 5VDC offset

Current-to-Voltage Converter:
                  Input                      Output
Irms      0 ~ 500mA      0.00 ~ 1.77V with 5VDC offset

      In order for the grid voltage and current flowing in the load can be read by the ADC microcontroller, voltage signal are converted using a voltage-to-voltage converter that is step-down transformer. While the current signal converted using current-to-voltage converter that is a step-up transformer which has very low inductance at primary coil and high inductance at secondary coil. For this purpose, we can use the adapter transformer. In this case, I_out and I_in fed to the low voltage coil and we get v0 and ground from high voltage coil as Figure 1.

Figure 1
      At the ADC input of microcontroller, we found clipper diode that can be utilized as a main component to build a zero cross detector that can be used to measure the period of input voltage. But in this study, the input voltage that is usually sinusoidal form will be entered as a whole, so that in future this system can be developed for the calculation of RMS values and power.

The Need for Phase Difference Measuring Instrument
Measure the Frequency of Grid Voltage rather than Measure the Frequency of Current

The Signal Conditioning
The use of the microcontroller ADC
Frequency Measurement
Phase Difference Measurement
Output Display Format

The Signal Conditioning


      v0 and v1 fed into the Signal_Conditioning block to have normalized so that the maximum voltage is Vpp = 5V. In this block, we add 2.5 V DC offset voltage to both signal so they have a voltage range of 0 to 5 V according to the ADC limits.
      The phase shift will occur while converting grid voltage to low voltage and converting current to voltage. To compensate them, on the block Signal_Conditioning, there is the phase shifting circuit that will adjust the phase again. This circuit also functions as a first-orde high-pass-filter. These filters are used to reduce the amplitude values of harmonic components which might make the calculation value of the frequency and phase difference to be wrong.
     The output of this block is a voltage signal v0' that it’s shape like the grid voltage, and voltage signals v1' it’s shape like the current grid. Phase shift between v0' and v1' equal to the phase shift between voltage and current grid.


The Need for Phase Difference Measuring Instrument
Measure the Frequency of Grid Voltage rather than Measure the Frequency of Current
Voltage and Current Converter

The use of the microcontroller ADC
Frequency Measurement
Phase Difference Measurement
Output Display Format

The use of the microcontroller ADC



     v0' fed to the input channel 0 of the ADC, while v1' fed to the second channel of the ADC input. Another channel ADC inputs are connected to the Ground to reduce the effects of cross-talk between the two entries. Voltage signal v0’ which represent the grid voltage and signal voltage v1’ which represent the load current, sampled by the ADC of microcontroller.
     The higher the ADC clock, so sampling process will be faster, but tend to diminish the accuracy of the ADC. In order to obtain fast conversion time but the accuracy of eight bits, then the ADC is run with prescaler = 16. This means that the ADC is run using a clock frequency of 691 kHz. Time to do a conversion or the conversion period (tc) is 14 times the clock period or about 20 microseconds. At 50 Hz input signal, sampling the voltage signal at intervals of 20 microseconds will provide a maximum error 0.1%


The Need for Phase Difference Measuring Instrument
Measure the Frequency of Grid Voltage rather than Measure the Frequency of Current
Voltage and Current Converter
The Signal Conditioning
Frequency Measurement
Phase Difference Measurement
Output Display Format

Frequency Measurement



     Measurement of the frequency of the grid voltage is done by first measuring the time between two successive foot of the mountain, as Figure 2. At the time of the grid voltage moves up above 2.5 V, a timer is run in normal mode. And when the value of grid voltage moves up on the 2.5 V anymore, timer 1 is stopped.  Timer 1 run during Δt1.
 
Figure 2
     Timer 1 is 16-bit timers. This timer is operated with prescaler = 8. Time for counting one digit is tT1 = 0.723 microsecond. The result of count is 16 bit hexadecimal value stored in a register TCNT1 that formed by pairing 8-bit registers TCNT1H and TCNT1L.
For 50 Hz input signal, the counting result is 6C00h. To avoid detection of the harmonic frequency, the frequency restricted from 35 Hz to 70 Hz. So that, the valid value of TCNT1 is from 9A49h until 4D24h. Maximum error of this counting is 0.004%.



The Need for Phase Difference Measuring Instrument
Measure the Frequency of Grid Voltage rather than Measure the Frequency of Current
Voltage and Current Converter
The Signal Conditioning
The use of the microcontroller ADC
Phase Difference Measurement
Output Display Format

Phase Difference Measurement

      After the timer 1 is stopped, the value TCNT1 immediately saved to memory. Furthermore, the timer run again for detecting the phase difference. This timer is run in CTC mode (Clear Timer on Compare Match), which means the timer will be reset when the count equals to the OCR1 register value. OCR1 value assigned half of the value of the first count results as Equation 1.

 
    (Eq. 1)
 

     This timer will be stopped again when the voltage value v1' move to rise above 2.5 V as Figure 3. To be able to detect v1’, just after the timer run again, the ADC input multiplexer is changed to channel 2.  This timer will count for the duration of time °Î”t2. In theory, the phase difference (θ) for the case that the current lags behind the grid voltage is calculated using Equation 2.
    (Eq. 2)


Figure 3
     In  the program, TCNT11 is the result of a count by the timer during t1, TCNT12 is the result of a count by the timer during t2, and calculating the phase angle difference following the Equation 3. 
 
   (Eq. 3)
 

     The equation above can not be applied to cases that current precedes the grid voltage. In this case, the timer run until at v1' move falls below 2.5 V as Figure 4. Calculating the phase difference following the Equation 4.
    (Eq. 4)

Figure 4
     In  the program, TCNT11 is the result of a count by the timer during t1, TCNT12 is the result of a count by the timer during t2, and calculating the phase angle difference following the Equation 5.


    (Eq. 5)
      Since the equation for calculating the phase difference in the case where the current lagging against grid voltage and currents precede the grid voltage is different, then there must be an algorithm that can check the occurrence of such current. For that, after the timer is run a second time, the microcontroller immediately check the voltage at v1'. If
Δt2 starts with the value of voltage is less than 2.5 V as Figure 3, the current lags behind voltage considered.  Phase difference calculation was done using Equation 3. However, if Δt2 begins with a voltage value equal to or greater than 2.5 V as Figure 4, it is considered current precedes the voltage. Phase difference calculation was done using Equation 5.
Special cases the use of Equation 5 occurs when the current in phase with voltage.  This case resembles the illustration in Figure 4, voltage value slightly more than 2.5 V. In this case, the value of Δt2 will be equal to Δt1/2, so that according to Equation 5, the value of phase difference θ = 0.



The Need for Phase Difference Measuring Instrument
Measure the Frequency of Grid Voltage rather than Measure the Frequency of Current
Voltage and Current Converter
The Signal Conditioning
The use of the microcontroller ADC
Frequency Measurement

Output Display Format

Output Display Format


     Microcontroller operated with clock frequencies fcpu = 11.0592 MHz. With this frequency, we can set up communication with the computer using USART for different variations of the bit rate with 0% error. In this study, the frequency and phase difference value is stored in the log files using Hyperterminal. 
     Maximum errors caused by the ADC is 0.1%. While the error caused by the timer can be ignored because it is much more smaller than the error caused by the ADC. So that the value of the frequency of the grid voltage and the value of the phase difference between voltage and current can be displayed in the format of three digits. The value of the frequency range from 35.0 Hz to 70.0 Hz can be displayed with format XX.X Hz, whereas the phase difference value with a range from 0 to 90° shown enough with the format XX°. View the output can be coupled with information that the electric current is in phase, lagging behind the voltage or preceding against to the grid voltage.

Calculate True RMS Value (2)


This program is an extension of the previous program. This program will calculate the true-RMS value of voltage at the input ADC0 and ADC1 as much as 10 times a second. Thus, data from both input sampling will be updated every 100ms. To make the appropriate updates 10 times per second, we use the timer 0. This timer is precision operated (like we operate the precision frequency generator) to schedule the sampling process so that all sampling process on both inputs will take place at intervals 100ms. Every 100ms portc2 will become high. This activity is regulated by timer0. High logic of that port will activy sampling subroutines. After the sampling process completed, portc2 will be low again.
To observe the accuracy sampling schedule, users can measure the output frequency of this port. This port emits a clock signal with frequency of 10Hz. During the sampling process, this port will be high. This high logic duration (tH) is the sampling period, i.e. 20ms. Users can check the accuracy of this port to determine the sampling duration.

Note that sampling duration must equel to the voltage period.
Clock signal duty cycle in this port describes the load time of the microcontroller. The higher clock signal duty cycle, the higher the load time of the microcontroller. If the duty cycle is low, we still can insert other subroutines to run on the sidelines of the overall sampling process. These subroutines will fill the remaining time until the hose is fully loaded, that is 100ms. In fact, although the duty cycle approaching 100% (microcontroller load time already high), we were still able to insert other subroutines to be executed when the CPU waiting for the completion of the ADC conversion.
In this program, the one period of voltage (50Hz) was sampled 64 times. This means, each of the input signal would be sampled with a frequency of 64X50Hz = 3200Hz. This program sampled two inputs. So, the total frequency of sampling is 2X3200 Hz = 6400Hz. This means that the sampling process should happen every 156.25 microseconds. The sampling period is set by a timer 1 had been made in precision. 


See also Measure true-RMS of Voltage and Current Grid


Download .asm and hex code.


How to extend this work forward?
We can display true-RMS value to the LCD, or send the data to a PC via the serial port.

We can also increase the sampling frequency or use full resolution of ADC.

Precision Frequency Generator


Frequency generator usually has certain accuracy. Analog frequency generator typically has a relatively low accuracy. Errors that arise sometimes still above 1%, while digital frequency generator typically has higher accuracy with the error below 0.05%.
Many applications of frequency generator are created using the AVR microcontroller to build AVR-based frequency generator. But most have low accuracy. AVR can be operated as frequency generator by using a timer. CTC (Clear Timer on Compare) mode is usually used for this purpose. Theoretically, the output frequency fOCn of a timer will fulfill the following equation.
Where N is the prescaler value and OCRn is the match compare register. These equations will yield values that are not high accuracy. For example, we use the timer 0 to generate the frequency of 10Hz. There are several options for the value of N and OCRn.
 
      N    OCRn (dec)    OCRn (hex)

      8    69119            10DFF
    64      8639              21BF
  256      2159                86F
1024        539                21B

By looking at OCRn value in the table above, the generation of these frequencies is only possible with prescaler equal to 64, or 256 or 1024 and must use 16-bit timer, which timer 1. This is a conventional way.
With a prescaler 64, or 256 or 1024, we can use 8-bit timer, which is
timer 0 or 2. This may occur in large applications where the timer 1 is used to trigger another subroutine. With 8-bit timer we just need to fill the low byte of OCRn to OCR0 or OCR2 register. Furthermore, we run the timer as many as M times, where M is the high byte of OCRn value.
For example, to generate a clock with a frequency of 10Hz, the prescaler value can be filled 64, the value OCR0 = BFh and timer 0 is repeated as many times 21h.
But after a try, sometimes inaccurate output frequency 10Hz could be occured. For that, we need to do calibration. The trick is to shift the value OCRn. By increasing the OCRn value, the frequency will be lower; and by decreasing the OCRn value, the frequency will be higher. Ideally, the increase or decrease the output frequency value is as small as possible. But the time for single-digit increase of TCNTn (t) cannot be small as possible. It depends to prescaler which is used as the following table.

      N    t (us)
      8       0,72
    64       5,79
  256     23,15
1024     92,59

To fix it, shift the OCRn
value on the last count. Another example, for the generation of 5Hz frequency, we can operate the timer 0 with CTC mode and prescaler=64. In theory OCR0 value = 255. After the timer 0 count as much as 67 times, the value OCR0 changed to 128. With the simulation in AVR Studio, we get the output frequency to 5.0003 Hz. The value of these can be calibrated by decreasing the value of OCR0 be 127 so that the value of output frequency is 5.0000 Hz. Output frequency will have a maximum error of 0.01%. 

Download .asm and hex code


Does the error still can be reduced? Of course we still can!
We still use the prescaler = 64. With prescaler = 8, the error rate can be reduced.
But for now it might not need to do ....



What this routine can be applied for?


We have applied it for:
- To fix sampling periode for True RMS Calculation.
- To fix the periode of interval of sending data via serial port.

Data Logger to PC via Serial Port


A data logger is an electronic device that is used to record the value measured over time. It is simply plug into a serial port on your PC. By connecting suitable sensors, data logger can be used as acquisition products that can be used to measure temperature, pressure, relative humidity, light, resistance, current, power, speed, vibration... in fact, anything that you need to measure. The output data from data logger is a periodic stream in simple text or hexadecimal with customized data format we need. Simply capture the data to a text file and then import it into Excel to produce tables and charts over time. 
If the data sent to the PC is a text data, you can use HyperTerminal to receive the data. Some control characters like tabs and carriage returns can be used here. For this purpose, the data which is usually a binary or hexadecimal numbers must be converted into ASCII format. Here you can use the binary / hexadecimal to ASCII data converter .
In order to obtain higher transmission speeds, the data sent should still use binary or hexadecimal format. To receive this data, we should use receiver that can receive binary or hexadecimal data. We can make this program with programming tools such as Delphi, Visual Basic or Visual C. If necessary, the program can convert binary / hex to ASCII.
In the application we build, data from microcontroller ATmega8535 was sent to the PC using a serial transmission. This data is fed to PC via serial port. Data emited from TXD pin (PD1) of microcontroller. In the future, we must also be able to receive data from the PC. Such data can be received through the RXD pin (PD0) microcontroller. Of data to and from the microcontroller using TTL level, while data from and to the PC serial port using RS-232 level. To interface between the microcontroller and a serial port, we can use the CMOS / TTL - RS-232 converter, for example is MAX-232.
We can determine the speed of data to be sent. Here we use the speed of 9600bps. Because a single ASCII characters using 8-bit data format, then we can send about 1000 ASCII characters per second. The speed of data and data length can be changed. In this application, the microcontroller is operated at a clock frequency of 11.0592 MHz. On this frequency, some variation in the data transmission speeds will have 0% error.
The data that we send do not have to be continuous. Here, we will send data every 100ms. Data that we send are ASCII characters that located at memory location pointed to by the index_start until index_end constant. Such data will be sent 10 times per second. This data transfer speed is usually quite adequate for a data logger. However, if necessary, we can increase the speed.
In some applications like data logger, it may take time high accuracy in data transmission speed. A precision data logger can send appropriate data 10 times per second which means exactly 100 times per 10 seconds or right 36 000 times per hour. We can make the data transmission speed precisly. We can use 8-bit timer for this purpose. This is similar to building a precision frequency generator. Further discussion of this matter, you can click here.
This application can be combined with other applications that shown in this blog. By integrating the application with a True RMS meter, we can build a True RMS meter with data logger to a PC. By integrating these applications with the phase difference meter, we can build the phase difference meter with data logger to a PC. And
by integrating these applications with the compass tracker, we can build the compass tracker with data logger to a PC.

32-bit Square Root

This subroutine will calculate a square value of 32-bit value at r5:r4:r3:r2. Result is 16-bit at r17:r16 and will be rounded to nearest integer (0.5 rounds up).
Cycles include call & return: 271 - 316.

R17:R16 = sqrt(R5:R4:R3:R2)

sqrt32:    
    push  R18
    push  R19
    ldi   R19,0xc0
    clr   R18              
    ldi   R17,0x40
    sub   R16,R16

_sq32_1:   
    brcs  _sq32_2          
    cp    R4,R16
    cpc   R5,R17
    brcs  _sq32_3

_sq32_2:   
    sub   R4,R16
    sbc   R5,R17           
    or    R16,R18
    or    R17,R19

_sq32_3:   
    lsr   R19
    ror   R18             
    eor   R17,R19
    eor   R16,R18
    rol   R2
    rol   R3
    rol   R4
    rol   R5
    sbrs  R2,0             
    rjmp  _sq32_1          
    brcs  _sq32_4          
    cp    R16,R4
    cpc   R17,R5
    brcc  _sq32_5          

_sq32_4:   
    sbc   R3,R19           
    sbc   R4,R16
    sbc   R5,R17
    inc   R16  

_sq32_5:   
    lsl   R3               
    rol   R4
    rol   R5
    brcs  _sq32_6          
    cp    R16,R4
    cpc   R17,R5

_sq32_6:   
    adc   R16,R19
    adc   R17,R19     
    pop   R19
    pop   R18

    ret

The Purpose of This Blog

     The aim of this blog was to exchange our experiences in make use of microcontroller to facilitate our life. We used the 8051 and AVR microcontroller family. Your involvement was really hoped in giving the comment, the suggestion, the question and criticism for the development of applications that has we got up. This blog contain basic application. Several applications could be united with the other application to form the implement microcontroller-based digital measurement, such as: AC/DC Digital Voltmeter, True RMS Voltmeter, Apparent and True Power meter, Power Factor Meter, etc. All of them can use LCD to display the output value, and have data logger to log data to PC via serial port.
All of the application presented in this blog has been tested with best of my knowledge to make it error-free, but no warranty they are no mistake if they applied in the different environment, like quality of each component. Please send report to me if you find any error.
We also had several versions of each application presented in blog this. They had several differences, like: the clock speed, accuracy, sampling period, memory location and so on. Each of their variation could be have different schematic diagram. 

Most of our applications using assembler programming language. The language is much faster than the C language Assembler language also would have a hex code that is much smaller, so it can be executed by the AVR microcontroller, which mostly have SRAM just below 4 kbytes.
     All of the application is tested with best of my knowledge to make it error-free, but no warranty they are no mistake. Try it at your own risk!


Further Information

     What we show here is an overview of our application. More detailed explanation of this application which includes detailed specifications, result and accuracy, usability, electronic scheme, and program listings are also available. We also have several analyses of them. If you need it, please send a written request via the email below. Also please mention your profession and your goals using the application. 

freddykurniawan@yahoo.com.

Multiply 16-bit by 16-bit

This subroutine will multiply 16-bit value at r23:r22 with 16-bit value at r21:r20. Result is 16-bit at r17:r16.

    (r23:r22)
    (r21:r20)
    -----------X
    (r17:r16)   


r17:r16 = r23:r22 * r21:r20
Register usage: r0, r1


mul16by16:
    mul r22, r20 ; al * bl
    movw r17:r16, r1:r0
    mul r23, r20 ; ah * bl
    add r17, r0
    mul r21, r22 ; bh * al
    add r17, r0
ret

16-bit Square Root

This subroutine will calculate a square value of 16-bit value at r3:r2. Result is 16-bit at r17:r16 and will be rounded to nearest integer (0.5 rounds up). The value at R3:R2 will be changed.
Cycles include call & return: 95 - 102.

R17:R16 = sqrt(R3:R2)
sqrt16:

    ldi   R17,0xc0     
    ldi   R16,0x40     
    clc                
sqrt16_1:
    brcs  sqrt16_2     
    cp    R3,R16       
    brcs  sqrt16_3     
sqrt16_2:
    sub   R3,R16       
    or    R16,R17      
sqrt16_3:
    lsr   R17          
    lsl   R2
    rol   R3           
    eor   R16,R17      
    andi  R17,0xfe     
    brne  sqrt16_1    
    brcs  sqrt16_4    
    cp    R16,R3      
    brcc  sqrt16_5    
sqrt16_4:
    sbc   R2,R17          
    sbc   R3,R16          
    inc   R16             
sqrt16_5:
    lsl   R2              
    rol   R3               
    brcs  sqrt16_6         
    cp    R16,R3           
sqrt16_6:
    adc   R16,R17          
    ret

Divide 32-bit by 32-bit

This subroutine will divide 32-bit value at r19:r18:r17:R16 with 32-bit value at r23:r22:r21:R20. Result is 32-bit at

r19:r18:r17:R16 and remainder is at r19:r18:r17:R16.
This routine uses r27:r26:r25:r24 as temporary registers.

    (r19:r18:r17:R16)
    (r23:r22:r21:R20)
    -----------------:
    (r19:r18:r17:R16)    remainder: (r23:r22:r21:R20)


div32by32:
    clr    r25
    tst    r23
    breq    udi10
    ldi    r24, 8
udi1:    lsl    r16
    rol    r17
    rol    r18
    rol    r19
    rol    r25
    cp    r17, r20
    cpc    r18, r21
    cpc    r19, r22
    cpc    r25, r23
    brcs    udi2
    sub    r17, r20
    sbc    r18, r21
    sbc    r19, r22
    sbc    r25, r23
    inc    r16
udi2:    dec    r24
    brne    udi1
    mov    r20, r17
    clr    r17
    mov    r21, r18
    clr    r18
    mov    r22, r19
    clr    r19
    mov    r23, r25
    ret

udi10:    tst    r22
    breq    udi20
    ldi    r24, 16
udi11:    lsl    r16
    rol    r17
    rol    r18
    rol    r19
    rol    r25
    brcs    udi12
    cp    r18, r20
    cpc    r19, r21
    cpc    r25, r22
    brcs    udi13
udi12:    sub    r18, r20
    sbc    r19, r21
    sbc    r25, r22
    inc    r16
udi13:    dec    r24
    brne    udi11
    mov    r20, r18
    clr    r18
    mov    r21, r19
    clr    r19
    mov    r22, r25
    ret

udi20:    tst    r21
    breq    udi30
    ldi    r24, 24
udi21:    lsl    r16
    rol    r17
    rol    r18
    rol    r19
    rol    r25
    brcs    udi22
    cp    r19, r20
    cpc    r25, r21
    brcs    udi23
udi22:    sub    r19, r20
    sbc    r25, r21
    inc    r16
udi23:    dec    r24
    brne    udi21
    mov    r20, r19
    clr    r19
    mov    r21, r25
    ret

udi30:    ldi    r24, 32
udi31:    lsl    r16
    rol    r17
    rol    r18
    rol    r19
    rol    r25
    brcs    udi32
    cp    r25, r20
    brcs    udi33
udi32:    sub    r25, r20
    inc    r16
udi33:    dec    r24
    brne    udi31
    mov    r20, r25           
    ret

Calculate True RMS Value

The RMS (Root Mean Square) value of a set of values (or a continuous-time waveform) is the square root of the arithmetic mean (average) of the squares of the original values (or the square of the function that defines the continuous waveform). The RMS over all time of a periodic function is equal to the RMS of one period of the function. The RMS value of a continuous function or signal can be approximated by taking the RMS of a series of equally spaced samples. Additionally, the RMS value of various waveforms can also be determined without calculus.
Measuring the true RMS value of a waveform is not easy. Inexpensive AC voltmeters simply rectify the waveform (by passing it through a diode, for example), measure an average value of the rectified waveform, and apply a correction factor (assuming a sine wave). Such meters, therefore, are only accurate for sinusoidal waveforms. They do NOT accurately measure any other waveform shape. So-called “True” RMS meters in the past have depended on some sort of power measurement to derive the correct RMS value. Now that calculation power is so much more economically available, meters can analyze a waveform’s shape and actually mathematically calculate the correct RMS value. In general, if an AC meter does not explicitly say it gives a “true” RMS reading, you can assume that it is accurate only for a sinusoidal waveform.
To calculate true RMS, we must do the following equation.




That equation can be used for any periodic waveform, such as a sinusoidal or saw tooth waveform, allowing us to calculate the mean power delivered into a specified load. To do that, we must follow several steps.
1. Take the waveform and divide it into a “large” number of individual increments.
2. For each sample, square the voltage value.
3. Sum these squared values over all samples and then calculate their mean value.
4. Take the square root of this mean.
It is also possible to calculate the RMS power of a signal. By analogy with RMS voltage and RMS current, RMS power is the square root of the mean of the square of the power over some specified time period. This quantity, which would be expressed in units of watts (RMS), has no physical significance. However, the term "RMS power" is sometimes used in the audio industry as a synonym for "mean power" or "average power".
 


This application use ATmega8535 AVR microcontroller to sample two lines voltage that fed to channel 0 and 1 of its ADC. We use 3.2 kHz of frequency sampling generated by timer 1. So, at 50Hz power line input, we get 64 samples per period. Each sample saved at SRAM then multiplied and summed. After we calculate the mean, then a special routine will calculate the square root to find the RMS value of each voltage.

The maximum voltage can be applied to ADC input is 2.5 Vpp with +2.5 VDC offset. For 2.5 V DC or -2.5 V DC input, the output display must show the 2.50 value. To do this, we multiply the result by 100/512. So to calibrate the calculation, the above equation can be changed to:



The rms value of v0 and v1 in 8-bit hexadecimal and 3-character ASCII (X.XX volt) format are stored in following location:
Hexadecimal ASCII
v0 v0_rms v0_rms_ASCII
v1 v1_rms v1_rms_ASCII

The 1*64 elements array represents v0 during one period is stored at v0_data_index_start to v0_data_index_start+64; and the 1*64 elements array represents v0 during one period is stored at from v1_data_index_start to v1_data_index_start+64. They can be sent to PC via serial port (USART). From them, we can build a graph represents two voltages in one period like a PC-oscilloscope that show two channels waveform.
Please fetch them carefully, and make sure all data have been updated.

Note:
The result has 0.78% accuracy, so we must be care about the least significant digit; the result is suggested less than 2.25 digit format.


See also Measure true-RMS of Voltage and Current Grid
 
Download .asm and hex code


What’s next?

You can make a precise period of sampling and schedule when it happen...

What shall we do to make it more accurate? We can make it more accurate by making the sampling interval more precision (like making precision frequency generator).
We could increase the sampling frequency or increase the data length of data from 8-bit to 10 or 12 bit.

click here for more information

Divide 24-bit by 24-bit

This subroutine will divide 24-bit value at r20:r19:r18 with 24-bit value at r23:r22:r21. Result is 24-bit at r20:r19:r18 and remainder is 24-bit at r2:r1:r0.

    (r20:r19:r18)
    (r23:r22:r21)
    -------------:
    (r20:r19:r18)    remainder: (r2:r1:r0)

div24by24:
    clr     r0       
    clr     r1   
    clr     r2       
    ldi     r16,24       
    lsl     r18   
    rol     r19   
    rol     r20       
    rol     r0       
    rol     r1   
    rol     r2       
    cp      r0,r21   
    cpc     r1,r22   
    cpc     r2,r23   
    brcs    PC+5       
    inc     r18       
    sub     r0,r21   
    sbc     r1,r22   
    sbc     r2,r23   
    dec     r16   
    brne    PC-15       
    ret

Divide 16-bit by 16-bit

This subroutine will divide 16-bit value at r19:r18 with 16-bit value at r21:r20. Result is 16-bit at r19:r18 and remainder is 16-bit at r1:r0.

    (r19:r18)
    (r21:r20)
    --------- :
    (r19:r18)    mod    (r1:r0)

div16by16:
    mov     r19,r3
    mov     r18,r2       
    ldi     r22,0x0d   
    ldi     r21,0x80   
    rcall   div16by16   
    mov     r18,r2       
    mov     r19,r3       
    mov     r20,r4       

    clr     r0       
    clr     r1   
    ldi     r16,16   
    lsl     r18       
    rol     r19       
    rol     r0       
    rol     r1       
    cp      r0,r21   
    cpc     r1,r22   
    brcs    PC+4       
    inc     r18   
    sub     r0,r21   
    sbc     r1,r22   
    dec     r16       
    brne    PC-11       
    ret

Multiply 16-bit by 8-bit

This subroutine will multiply 16-bit value at r19:r18 with 8-bit value at r20. Result is 24-bit at r4:r3:r2.

  (r19:r18)
      (r20)
 ---------- X
 (r4:r3:r2)

mul16by8:
    mul     r18,r20
    mov     r2,R0
    mov     r3,R1
    mul     r19,r20
    mov     r4,R1
    add     r3,R0
    brcc    NoInc
    inc     r4
NoInc:
    ret