You are on page 1of 23

DEVELOPMENT OF A STEP-UP CONVERTER FOR A PV SYSTEM

(ELEC5510 PRACTICAL LABORATORY SESSION 1)

WRITTEN BY

OLADAPO Opeoluwa Ayokunle


(200581534)

SUBMITTED TO

CHONG Ben

SCHOOL OF ELECTRONIC & ELECTRICAL ENGINEERING THE UNIVERSITY OF LEEDS LEEDS LS2 9JT

11 April 2011

1. INTRODUCTION
The issue of cleaner energy production and sustainability has increased the demands for energy production using renewable technologies including; wind energy, hydro energy, bio-energy and solar energy. The solar energy conversion system exploits the photovoltaic effect (PV) for the generation of power and this has become a very popular source of energy due to the limitless source i.e. the sun. Although, with the advantages of having a limitless source, no emission of greenhouse gases and little maintenance costs, the PV energy system has the disadvantages of variation in the availability of the sun, low energy conversion (due to the efficiency of the PV cells, typically 13%) and a high cost of installation. The manufacturing technology of the PV cells is been improved to help increase the energy conversion efficiency and government policies have also helped in subsidizing the cost of installation. These factors make the solar energy generation very attractive. The variation in the availability of the sun is a disadvantage which is being mitigated by the use of power electronic devices for ensuring stability in the level of energy delivered to the load. Thus, this laboratory session investigates the development of a step-up converter for a PV system aimed at stabilizing the level of the PV energy system for the load requirements. The aim of this laboratory exercise is enumerated as follows; Implementation of pulse width modulated (PWM) signal generation technique using the dsPIC microcontroller. Analysis and implementation of the switching circuit consisting of an opto-coupler for driving the power electronic device (i.e. the MOSFET switch). Investigation of DC-DC voltage regulation via switching duty cycle variation of the converter and application to the PV system. Learn the various measuring techniques of the I-V characteristics of a PV system. Learn the effect of weather conditions on the I-V characteristics if a PV system.

2. THEORY
2.1. The Photovoltaic (PV) System The photovoltaic (PV) module converts solar radiation to electrical energy directly, without any processes in between by exploiting the advantage of the electrical properties of doped semiconductors p-n junction devices which can be triggered by light. The basic steps in the operation of a PV module are: The generation and collection of light-generated carriers to generate a current; The generation of a very large voltage across the PV module; and The dissipation of power in the load and all associated resistances in the PV module.

Since the conversion to electrical energy involves no moving parts, the PV module is advantageous as very little maintenance is required. Also, the semiconductor materials are very abundant in nature and renewable sources (especially silicon) and produce no known harmful waste products. In spite of all the outlined advantages the most significant disadvantage is the typical module efficiency of up to 15% for commercial crystalline silicon PV modules (although up to 25% efficiency has been recorded in the laboratory) [1] which can be attributed to semiconductor materials and varying environmental conditions. A PV module usually consists of individual solar cells electrically connected (in parallel to increase the current and/or series to increase voltage) to increase their effective power output, since a single solar cell has a voltage output between 0.5V and 0.7V which is relatively low for power applications. The electrical operation of a PV cell can be estimated by the equivalent circuit shown in figure 1 while the I-V characteristics is shown in figure 2.
I ID ISH RS RP RLOAD

Figure 1: Equivalent circuit model of a single photovoltaic cell

Diode

ISC

I
ISC IMPP

Load Line

MPP

ISC short-circuit current ID diode diffusion current ISh shunt current (ohmic losses) I output current RP shunt resistance RS series resistance RL load resistance MPP Maximum Power Point IMPP Current at MPP VMPP Voltage at MPP

VMPP

VOC

Figure 2: I-V characteristics of a PV cell showing the maximum power point and load line

2.2. The Boost Converter implementation The DC-DC boost converter is an example of a classical switched-mode power supply circuit which is mostly used for power applications having the output voltages higher than the input voltages. The DC-DC converter is also used to regulate the dc output voltage under varying load and input voltages. Equivalent Circuit model of a DC-DC Boost converter The circuit of a boost converter is shown in figure 1 below; L Do iL + VL Vi + VSW Figure 3: DC-DC Boost converter circuit

iO iC C VC VO R

de

The switching signal to the switch (typically a MOSFET switch) is usually a pulse-width modulated (PWM) signal which consequently turns the switch ON and OFF. The duration of the ON and OFF (i.e. the switching sequence) is determined by the duty ratio of the PWM signal and their sum is equal to the switching period. The circuit goes from its initial transient state when the inductor current and capacitor voltage build up to a steady state. When the switch is ON, the diode is reverse bias and the circuit is separated into two parts as shown in figure 2.The left part of the circuit shows the dc supply voltage charging the inductor while the right shows that the output capacitor maintains the output voltage using previously stored energy. When the switch is OFF as seen in figure 3, both the dc supply voltage and energy stored in the

inductor will charge the output capacitor and also supply power to the load hence the output voltage is boosted as shown by equation 1.

where

is output voltage,

is input voltage and

is the duty ratio.

Once more, the output voltage can be maintained at the constant required level by changing the duty ratio and consequently controlling the switching sequence. L + vL VI
L1

iL ic vc

io

C Vo
L2

Figure 4: DC-DC Boost converter circuit (when switch is ON)

L + vL VI
L3

iL

D ic C

P
+v
c -

io

Vo
L4

Figure 5: DC-DC Boost converter circuit (when switch is OFF)

The boost converter specifications The implemented boost converter for this exercise is of the specifications below and a block diagram is also shown in figure 5 below. Input Voltage, Vin (from the PV panel) = varying Load Resistance, RL = 50.4 Switching frequency, fs = 20 kHz Capacitors, C1 and C2 = 82F Inductor, L = 1mH Duty ratio, k = varying

Microcontroller

Opto-Coupler

Driver

Boost Converter

Figure 6: Block diagram of the implemented microcontroller-based boost converter

The principles of operation of the implemented boost converter The switching signal to the MOSFET switch (STB24NF1) of the boost converter is supplied by a switching circuit comprising of a microcontroller which generates a digital PWM signal, an optocoupler which provides electrical isolation between the microcontroller and the boost converter, and a gate driver which is used to provide the required voltage and current level for powering the MOSFET. 2.2.1 The Microcontroller

A microcontroller is an integrated chip designed to execute only a single particular task by running one specific program (stored in the read-only memory; ROM) to control a single system. Similar to a standard computer, it includes; timers, input/output ports, memory (readonly, random access) and a central processing unit (CPU). The microcontroller being used is the 18-pin dsPIC30F2011 microcontroller manufactured by MicroChip. The basic specifications and the pin diagram of this microcontroller are shown below; SPECIFICATIONS Random Access Memory (RAM) Read-Only Memory (ROM) Program length (approximate) Input / Output (I/O) Pins Speed 1024 Bytes 12K Bytes 4000 instructions 18 15 million instructions/second

Table 1: Table showing the specifications of the dsPIC30F2011 microcontroller

Figure 7: Pin diagram for dsPIC30F2011 microcontroller

2.2.2

The OptoCoupler

An OptoCoupler is an electronic device designed to transmit electrical signals by using light waves to provide connection with electrical isolation between its input and output. The major function of the OptoCoupler is to provide isolation between the two circuits by preventing overvoltage damage on the low-voltage side. An OptoCoupler is usually consists an optical transmitter and an optical receiver, separated by a transparent barrier impedes any electrical

current flow but allows the passage of light. The OptoCoupler used for this exercise is the HCPL4503 and the pin diagram is shown in figure 8. When the LED is on, the output voltage is low and when the LED is off, the output voltage is high as seen in figure 8.

Figure 8: Pin diagram for the HCPL-4503 OptoCoupler

2.2.3

The Gate Driver

A gate driver is a power amplifier that produces the required level of current gate drive for power switches from low level input of controller IC chips. They are mostly used in PWM applications where the PWM controller cannot provide the required level of output current for driving the connected MOSFET. The gate driver utilized for this application is the advanced TD351 gate drivers for IGBT and power MOSFET. It amplifies the low logic-level signal from the OptoCoupler (5V HIGH and 0V LOW) to the required high logic-level signal (15V HIGH and 0V LOW) for switching the power MOSFET. The pin diagram is shown in figure 9 below.

Figure 9: Pin diagram for the TD351 gate driver

3. PROCEDURE
The datasheet for the components were checked and the following parameters which define the limits for the designed circuit are stated below; Capacitor: Rated voltage - 160V DC to 450V DC. But for safety reasons, operating voltage is chosen as70% of rated voltage. Hence, Operating voltage range: 112.0 V to 315.0 V Inductor: Rated current: 1 to 6A Rated inductance: 0.7mH - 20mH DC resistance: 0.1 - 3.0

Power switch:

Maximum drain source voltage: 100V Supply voltage should be less than maximum drain source voltage Maximum gates source voltage: -20V or +20V Gate signal should be less than maximum gate-source voltage, Vgs Switch current should be less than 26A Gate threshold voltage: 3V Static drain source on resistance: 0.055 Switching time: Turn on delay time: 60ns Rise time: 15ns Turn off delay time: 50ns Fall time: 20ns

Power diode:

Reverse recovery time: 35ns Reverse recovery time should be faster than switching period Maximum forward current: 1 A at T=25oC and V=300V Threshold voltage: 0.74 V

Cout Capacitor

Cin Capacitor Socket 1: Input voltage, Vin

Socket 2: Load bulbs

Diode Inductor, L

MOSFET

Socket 3: Driver connection


Figure 10: External connections to the PCB layout

3.1.

The Switching Circuit

The dsPIC30F2011 microcontroller is programmed to generate the PWM signal using the C programming language. The flowchart of the program code is shown in figure 10 while the program listing is included as Appendix A. The requirement for operation of the microcontroller involves the conversion of an analogue input signal (varied between 0V 5V) supplied by a potentiometer) into digital signals at the output. The variation of the potentiometer results in the variation of the input signal between its maximum and minimum levels but within a specified duty cycle range of 0 60%.

START

DEFINE THE CONSTANTS FOR PWM GENERATION

DEFINE THE VARIABLES FOR PWM GENERATION

SAMPLING OF ANALOG SIGNAL.

IS SAMPLING DONE?

YES

NO

START CONVERSION OF ANALOG TO DIGITAL

CONFIGURATION OF PARAMETERS FOR PWM OPERATION

STOP

Figure 11: Flowchart for the dsPIC microcontroller program for PWM generation

The external connections to the microcontroller as implemented are shown in figure 11. The output is OC1 containing the PWM signal whose duty cycle is determined by the potentiometer voltage.

Figure 12: Diagram showing the external connections to the microcontroller

The output from the microcontroller is connected to the optocouplers input pin 2 via an input resistance Rin (functioning as the LED current limiter) of an approximate value of 220 which is calculated using equation 2. The terms VF and IF are device specific with values of 1.5V and 16mA respectively.

Also, the output pin 6 also has a pull-up resistor of 4.7k connected to VCC and a capacitor of 100nF is connected across VCC and ground. The device is supplied by two power supplies of 5V and 15V connected to pins 3 and 8 respectively. The external connections to the OptoCoupler circuit are shown in figure 12 below.

Figure 13: Diagram showing the external connections to the opto-coupler

The output of the opto-coupler is connected to the input pin 1 of the gate driver. For this exercise, a one-level turn off was utilized hence a 4.7k resistor is connected across pin 2 and 3 (reference voltage and delay pins respectively). Also, a 100nF capacitor is connected across the power supply terminals and a 47 resistor (gating resistor) is connected to the output pin 7

which provides the required current capacity for sufficient switching of the MOSFET. The gating voltage is connected to pin 5 which is a miller current clamp pin whose function is to reduce the miller effect and hence improving the bandwidth of the gate driver. The external connections to the gate driver circuit are shown in figure 13.

Figure 13: Diagram showing the external connection to the gate driver circuit

3.2.

The Boost Converter

The switching circuit output (i.e. the gate driver output pin 7) was connected to the gate of the MOSFET as shown in figure 13 above. The boost converter was a ready-built PCB model. The external connections to the boost converter (the switching circuit, the load bulbs and the input power supply) were made with the help of the PCB layout and the connections where made as shown in figure 14 below.

Figure 14: The external connections to the boost converter with the load as two electric bulbs

Since the circuit setup is now a high power set up, the supplies where switched off initially and the circuit connection was confirmed okay by the laboratory supervisor. Also, the safety precautions taken are enumerated below; The load resistance was measured and ensured it is not too high. This is to ensure the load is not open-circuited thus preventing the capacitor to charge fully.

The appropriate power supply for the low-voltage end and the high-voltage end is used and care is taken not to use a common ground between the two power supplies. The connections to the terminal block are ensured to be small to help avoid interference. 3.3. The identification of the current-voltage (I-V) characteristics of a practical PV panel and the investigation of boost converter as a controller for the PV terminal voltage For this task, a practical PV panel with a sun simulator, a rheostat, an ammeter and voltmeters were provided. The connection is made as shown in figure 15 below. To the identification of the I-V characteristic of the PV panel, the rheostat was varied from minimum to maximum and the values were observed and stored. For the investigation of the boost converter as a PV terminal voltage controller, the rheostat was replaced with a constant load and the duty cycle of the PWM signal to the converter was varied. The results were observed and stored also.

PV PANEL

RHEOSTAT

Figure 15: PV Panel external connections

4. RESULTS AND DISCUSSION


The microcontroller circuit, the opto-coupler circuit, the gate driver circuit and the boost converter circuit were singularly tested for potentiometer voltages of 1.5V, 2.5V, 3.5V and 4.5V. The input and output waveforms, frequency and duty cycle of each circuit at each potentiometer voltage was observed. 4.1. The Microcontroller, Opto-Coupler and Gate Driver Tests

The tests carried out on the individual circuits show that each circuit operates as expected when the duty cycle is varied. The input to the microcontroller is in the range 0V 5V while the output from the gate driver is in the range 0V 15V, which is enough to sufficiently switch the power MOSFET. The frequency value in each circuit stage is observed to be an acceptable variation of 1.25% from the 20 kHz given in the microcontroller code.

The output from the opto-coupler is observed to be inverted when compared with the output from the microcontroller and the gate driver. This expectably follows the truth table operation of the opto-coupler as earlier stated in figure 8 above. The measured values at the output of each circuit stage and the observed output waveforms at each circuit stage for the different potentiometer voltages is shown in table 2 and figure 14 17 respectively. Input Potentiometer (V) 1.5 2.5 3.5 4.5 Duty Cycle (TON/TS) 0.18 0.30 0.42 0.54 Microcontroller Output (V) 4.40 4.48 4.80 4.80 Opto-Coupler Output (V) 4.42 4.55 4.81 4.92 Gate Driver Output (V) 15.21 15.05 15.07 15.20

Table 2: Showing the variation of output voltages of each circuit stage with input potentiometer voltage
VOLTAGE

Microcontroller Opto-Coupler
TIME

Gate Driver

TIME

TIME

Figure 16: Output waveform at each circuit stage with potentiometer voltage of 1.5V

VOLTAGE

Microcontroller Opto-Coupler
TIME

Gate Driver

TIME

TIME

Figure 17: Output waveform at each circuit stage with potentiometer voltage of 2.5V
VOLTAGE

Microcontroller Opto-Coupler
TIME

Gate Driver

TIME

TIME

Figure 18: Output waveform at each circuit stage with potentiometer voltage of 3.5V
VOLTAGE

Microcontroller Opto-Coupler
TIME

Gate Driver

TIME

TIME

Figure 20: Output waveform at each circuit stage with potentiometer voltage of 4.5V

4.2.

The Boost Converter Test

With an input supply of 5V, 2A the duty cycle of the PWM signal was varied between 0% and 60% and the output of the boost converter was observed. As the duty cycle of the PWM signal is increased from 0% to 60%, it was observed that the load bulb brightness also increases, indicating a proper working condition of the boost converter.

Also, the voltage across the load bulb was observed not to satisfy the input-output relationship of the boost converter as shown in equation 1. This variation from the theoretical values is because the equations assume ideal components but the components are non-ideal. A detailed analysis of each component shows the following; Although the diode is a fast recovery diode which reduces turn-on loss in the commutating switch, it still has an internal resistance of 17.7m which causes a voltage drop of 0.74V across the diode. The MOSFET has a turn-on resistance of 0.06 which causes a voltage drop of approximately 1.56V (at a drain current of 26A). Therefore, the approximate voltage drop across the non-ideal components is 2.3V The theoretical and measured values of the boost converter output for a given duty cycle is shown in table 3 below. Input potentiometer voltage (V) 1.5 2.5 3.5 4.5 Duty Cycle (TON/(TON + TOFF)) 0.15 0.30 0.42 0.54 Measured converter output voltage (V) 4.59 5.51 6.38 7.81 Calculated converter output voltage (V) 5.88 7.14 8.62 10.87

Table 3: Converter output voltages at varying duty cycles and input potentiometer voltages (measured and calculated)

The difference between the measured and calculated output voltages of the converter is noticed to vary. This is because of the different ON times as it is observed that an increase in the ON time satisfies the approximate voltage drop stated earlier. 4.3. The identification of the current-voltage (I-V) characteristics of a practical PV panel The I-V characteristics of the PV panel was derived by the variation of the rheostat from minimum to maximum and the measured values were recorded for two sets of temperature (low and high temperature) under three irradiation levels. The recorded values are shown in table 4 and 5. The recorded values are plotted and shown in figure 20 and 21. The obtained I-V characteristics is related to the resistance value of the of the rheostat with the linear relationship stated below;

A straight line from the origin through each data point (a set of I-V) is referred to as the load line and if defines a single operating point of the PV panel. T1 (24.00 ) G1 (140W/m2) 40% VPV (V) 0.334 2.450 8.520 10.420 12.350 14.180 15.820 17.410 18.040 18.670 18.870 18.940 19.010 IPV (A) 1.229 1.212 1.134 1.099 1.059 1.030 1.000 0.916 0.700 0.336 0.226 0.172 0.127 T2 (25.00 ) G2 (210W/m2) 60% VPV (V) 0.419 4.930 9.420 11.870 13.460 15.910 16.370 18.050 18.670 18.890 18.990 19.210 19.310 IPV (A) 1.790 1.750 1.710 1.638 1.583 1.524 1.472 1.392 1.239 1.108 0.414 0.216 0.128 T3 (25.75 ) G3 (370W/m2) 100% VPV (V) 0.606 5.880 10.770 14.940 17.360 18.130 18.580 18.900 19.150 19.210 19.414 19.560 19.630 IPV (A) 2.980 2.874 2.769 2.684 2.484 1.867 1.406 1.009 0.927 0.725 0.623 0.221 0.130

Table 4: PV Panel I-V measurement for low temperature (23 - 26 ) at different weather conditions (G1, G2, and G3)

3.5 3 2.5

Ipv

2 1.5 1 0.5 0 0 5 10

G1 (140W/m2) G2 (210W/m2) G3 (210W/m2)

Vpv

15

20

25

Figure 9: I-V curve for low temperature (23 - 26 ) at different weather conditions (G1, G2, and G3)

T1 (33.00 ) G1 (140W/m2) 40% VPV (V) 0.371 4.270 9.600 13.740 15.370 16.150 16.470 16.650 16.960 17.150 17.460 17.700 IPV (A) 1.246 1.220 1.127 1.097 1.018 0.843 0.738 0.656 0.520 0.421 0.253 0.118

T2 (34.00 ) G2 (210W/m2) 60% VPV (V) 0.380 4.140 11.360 14.870 16.320 16.720 16.800 17.110 17.430 17.670 17.730 18.020 IPV (A) 1.800 1.832 1.685 1.613 1.364 1.118 1.069 0.851 0.591 0.413 0.294 0.120

T3 (33.00 ) G3 (370W/m2) 100% VPV (V) 0.420 5.630 10.060 14.910 16.090 17.364 17.729 17.450 17.900 18.000 18.370 18.730 IPV (A) 2.920 2.831 2.742 2.622 2.513 2.295 2.017 1.840 1.645 0.987 0.520 0.129

Table 5: PV Panel I-V measurement for high temperature (31 - 35 ) at different weather conditions (G1, G2, and G3)

3.5 3 2.5

Ipv (A)

2
1.5 1 0.5 0 0 5 Vpv (V) 10 15 20

G1 (140W/m2) G2 (210W/m2) G3 (370W/m2)

Figure 10: I-V curve for high temperature (31 - 35 ) at different weather conditions (G1, G2, and G3)

It is observed that the variation of the rheostat resistance conforms to the relationship stated in equation 3 above. However, the difference between the measured and the theoretical I-V characteristics is due to the variation of the temperature of the PV panel under the various weather conditions. The values of the open-circuit voltage and the short-circuit current also varies with the changing weather conditions according to equations 4 and 5 below.

The maximum power point for each weather condition and temperature is listed in table 6 below; IRRADIATION LEVEL G1 (140W/m2) G2 (210W/m2) G3 (370W/m2) LOW TEMPERATURE Vpv (V) Ipv (V) MPP (W) 17.410 0.916 15.948 18.050 17.360 1.392 2.484 25.126 43.122 HIGH TEMPERATURE Vpv (V) Ipv (V) MPP (W) 15.370 1.018 15.647 14.870 16.090 1.613 2.513 23.985 40.434

Table 6: Showing the maximum power point variation with varying weather conditions at different temperatures

Therefore, it is worth noting the effect of increasing temperature as; Significant decrease in the open-circuit voltage and a slight increase in the short-circuit current but an overall decrease in the power output and the maximum power point. Also, it is worth noting the effect of increased irradiation level as; Significant increase in the short-circuit current and a slight increase in the open-circuit voltage but an overall increase in the power output and the maximum power point. 4.4. The investigation of DC-DC converter as a controller for the control of the PV terminal voltage The rheostat was replaced with a constant load of 34.3 and the maximum power point at each condition is measured and recorded. The maximum gate drive voltage ( ) across the

MOSFET is 15V and this is used to calculate the duty cycle at each test stage. The equivalent resistance seen from the input of the converter determines the current the converter supplies thus by changing the equivalent resistance; the current supplied to the constant load can be varied accordingly to maintain a constant voltage across the load. Analysing the converter as a lossless circuit, it can be assumed that the input power is equal to the output power. And thus a relationship between the equivalent input resistance and the constant load resistance can be derived as follows;

If

, substituting equation 1 and resolving the equation gives the following;

The weather conditions were varied and the results were observed and recorded in table 7 below; Weather condition 40% 60% 100% 3.540 5.570 7.690 0.236 0.371 0.513 17.190 17.330 17.410 0.902 1.440 2.640 (measured) 19.060 12.030 6.590

(calculated) 20.020 13.570 8.134

Table 7: Showing the variation of measured and calculated equivalent input resistance of converter with weather condition

It is observed that the DC-DC converter functions as a controller for holding the terminal voltage of the PV panel at the requirement of the connected load. The difference in the measured and calculated values is due to the assumption made that the converter is a lossless circuit which in reality is not. The regulation of the duty cycle varies the equivalent resistance seen from the input of the DCDC converter. This action of the duty cycle regulation ensures that the voltage across the PV terminal and thus the load is kept constant by regulating the current supplied by the converter to the load. It is observed from table 7 above that as the current supplied to the load increases the equivalent input resistance reduces according to the basic ohms law (V=IR) thus keeping the terminal voltage constant.

5. CONCLUSION
The microcontroller-based generation and control of a pulse width modulated (PWM) signal has been learnt and the implementation of the switching circuit used to appropriately drive power electronic devices has been learnt. The use of opto-coupler and its conversion efficiency has also been learnt. From earlier studies of the PV energy system, the measurement of the terminal voltage of a practical PV panel has been learnt and the variation of the I-V characteristics with different weather conditions (temperature, irradiation level e.t.c.) has been practically identified. Although the theory of the control of the terminal voltage of a PV system has been earlier learnt, this exercise has helped in the practical application of this knowledge by regulating the terminal voltage of the PV panel using duty cycle regulation. The efficiency of the DC-DC boost converter can be improved by carefully choosing the input inductor and the output capacitance as they determine the ripple effect of the whole converter circuit which consequently determines the efficiency of the converter. The efficiency can also be improved by carefully choosing the sampling time of the PWM signal driving the power MOSFET switch as this also has an effect on the ripple content of the converter.

REFERENCES
1. http://fayazkadir.com/blog/?page_id=460 : DC-DC Converter, accessed February 24, 2011 2. Ned Mohan, Tore M. Undeland and William P. Robbins, Power Electronics: Converters, Application and Design, (John Wiley and Sons Inc., Second Edition), pp. 61-74, 172-177 3. Li Zhang (2010): Handout ELEC5564 Power Generation By Renewable Sources, Section 2, Power Converters and Applications to Renewable Generation 4. http://www.iea-pvps.org/pv/materials.htm : Photovoltaic Cells, accessed February 5, 2011 5. William Shepherd, Li Zhang, Power Converter Circuits, Marcel Dekker Inc. ISBN 0-8347-50543, 2004

APPENDIX A
Program Code for Microcontroller-based PWM generation
#include <p30f2011.h> #define FCY 29480000 //FRC w/ PLL 16x instruction cycle clock rate 7.37MHz //See Note 3 of Table 20-16 in Pg 161 of datasheet #define MILLISEC 29480 // 1 mSec delay constant #define SCALE 0.3597 //Scaling factor for PWM generation int main(void); /* function declaraions */ void init(void); void DelayNmSec(unsigned int N); int ADCValue; /*Variable declarations*/ int DutyCycle; int main() /*Main Program starts here*/ { init(); /*Program initialization Jump to this function*/ while (1) { ADCON1bits.SAMP = 1; // start sampling ... DelayNmSec(100); // for 100 mS ADCON1bits.SAMP = 0; // start Converting while (!ADCON1bits.DONE); // conversion done? ADCValue = ADCBUF0; // yes then get ADC value 12-bit DutyCycle = ADCValue*SCALE; OC1RS = DutyCycle; asm ("clrwdt"); /* clear watchdog timer */ }// repeat return 0; } /* ***** INITIALISATION ***** */ void init(void) { //setup ADC see Page 111 117 of data sheet for reference ADPCFG = 0xFFFB; // all PORTB = Digital; RB2 = analog ADCON1 = 0x0000; // SAMP bit = 0 ends sampling ... // and starts converting ADCHS = 0x0002; // Connect RB2/AN2 as CH0 input .. // in this application RB2/AN2 is the input ADCSSL = 0; //Channel scanning is disabled ADCON3 = 0x0003; // Manual Sample, Tad = internal 2 Tcy ADCON2 = 0; // ADCON1bits.ADON = 1; // turn ADC ON // config o/p compare module for pwm operation - refer to Page 8890 of datasheet OC1CONbits.OCM = 6; // set output compare (PWM duty cycle) value OC1RS = 1178; //(80%) refer to Eqn 12-1 and figure overleaf //setup timer2 refer to Page 77 81 of datasheet T2CON = 0; //reset timer2 control bits Pre-scale 1:1 /* set Timer 2 period register */ PR2 = 1473; /* 20kHz */ /* select internal timer clock */ T2CONbits.TCS = 0; T2CONbits.TON = 1; } void DelayNmSec(unsigned int N) { unsigned int j; while(N--) for(j=0;j < MILLISEC;j++); }

You might also like