You are on page 1of 36

The University of Leeds

School of Electronic & Electrical Engineering

ELEC5451 Mini Project - Digital Implementation of PWM Generation Circuit and Control for a PV-Converter System

Objectives: Learn to establish PIC-microcontroller based control for PWM Learn to implement the switching circuit for driving power electronic devices. Learn to appreciate the DC-DC voltage regulation and apply it to a photovoltaic (PV) energy system. Learn to measure the I-V characteristics of a practical PV panel Learn to identify the effect of various weather conditions on the I-V characteristics. Learn to apply dc-dc converter in controlling the PV terminal voltage
In this lab session of ELEC5451 you will be guided to do the following: PART 1: PART 2: PART 3: To investigate and implement PWM signal generation using dsPIC microcontroller. To analyse and build the switching circuit consisting of an opto-coupler and a driver for MOSFET. To investigate the converter output voltage response when the switching duty cycle is being varied. In this lab session, the converter is used to regulate the voltage across two serially-connected 24W electric bulbs. Any differences from your expected results should be commented and explained. To derive the relationship between PV voltage and current under various weather conditions. Apply the dc-dc converter in controlling the PV operating voltages.

PART 4:

There will be two sets of notes; First Set is on Part 1 (Programming a Microcontroller) and Second Set is on Parts 2, 3 and 4 (Testing and Applying the Converter for a Stand-Alone PV System) Two symbols are used in the notes have different meanings A step you need to follow but is not necessary to be included in your report

A step you need to implement and is necessary to be included/commented in your report


Page | 1 ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Note Set No. 1: PROGRAMMING A DSPIC30F MICROCONTROLLER


Overview We are going to use one device from the dsPIC30F family which is dsPIC30F4011. The full details on device features can be found in VLE under datasheets but those that are essential for PWM generation are listed below (Note that different dsPIC30F device may be needed if more memory, more PWM modules, more ADC channels or higher processing speed is needed) [1]. (Refer to page 2 of datasheet) No. of Pins: 40/44 Program Memory: 48 Kilobytes / 16 000 instructions SRAM: 2048 bytes Timer 16-bit: 5 counters Motor Control PWM Module: 6 channels Output Comp/Std PWM: 4 peripherals A/D 10-bit (up to 200 ksps conversion rate): 9 Channels Internal Oscillator: 7.37 MHz We are going to use MPLAB IDE to build the machine codes which are mainly used to program your device to generate the appropriate PWM signals. The complete process is summarized in Figure 1. Start

Configuring the settings in MPLAB IDE

Writing a C program in Notepad / in C editor

Building the program in MPLAB IDE

Debugging the program in MPLAB IDE

Updating the program

Is the program correct?

Download the code to the device

End Figure 1. Flowchart for programming a microcontroller device Page | 2 ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Section 1: Setting up MPLAB IDE for Programming a dsPIC30F4011 device


To start the process, open MPLAB IDE (it should be located on the desktop or Microchip folder under Start>Programs) 1. First choose the PIC device you want by clicking on Configure then Select Device as shown in Figure 2:

Figure 2. Setting configuration for device

2. Pull down the Device Menu as shown below and select dsPIC30F4011. If your desktop already has selected this, then skip this step.

Figure 3. Selecting the correct device

Page | 3

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

3. Note the red and green lights. These indicate which MPLAB components support this device. A green light indicates support and you can proceed with your design. Sometimes you might see a yellow light; this would indicate minimal support for an upcoming part that might not yet be fully supported by MPLAB component. Usage of components with a yellow light instead of a green light is often intended for early adopters of new parts who need quick support and understand that some operations of functions may not be available. If the light is red then choose another PIC device or another programmer etc.

Figure 4. Summary of Microchip Tool Support 4. Next, create a project using the Project Wizard. Choose Project Wizard from the project menu as shown in Figure 5. A project is the way your files are organized to be compiled and assembled. In this case we will use a single C file for this project.

Figure 5. Creating Project with the Wizard

Page | 4

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

5. The Project Wizard should start and show the following opening screen, press Next to continue.

Figure 6. Welcome Dialogue Box 6. The first step allows you to select the device, which you have already done. If the device shown is not the dsPIC30F4011, change it on this dialog. This seems like a redundant step, but the project wizard bases most of the information related to the assembler, linker etc on the device selected.

Figure 7. Setting the Correct Device

Page | 5

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

7. This step is used to set up the language tools suite. All projects will be in C, so from the Active Toolsuite select the MPLAB C30 C Compiler which is for dsPIC30F series devices. The locations for these toolsuites may have been changed. The correct locations are shown in Figure 8.

Locations of C30 toolsuite contents (for our Schools PCs) C:\Program Files\Microchip\MPLAB C30\bin\pic30-as.exe C:\Program Files\Microchip\MPLAB C30\bin\pic30-gcc.exe C:\Program Files\Microchip\MPLAB C30\bin\pic30-ld.exe C:\Program Files\Microchip\MPLAB C30\bin\pic30-ar.exe (If the above are not available, i.e. when Browse to locate these files.) is indicated, use

Figure 8. Selecting the relevant toolsuite for dsPIC30F devices 8. Step three asks for the project name; for this first exercise, simply call it as PWM_1. Note that you have to enter the drive and the directories (if applicable) you are saving into as shown in Figure 9.

Figure 9. Giving a Project Name

Page | 6

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

9. Click Next to continue and the next step asks you to add files into the project. You can skip this step by clicking Next. This brings you to a Summary. As soon as you click Finish, Project and Output windows as shown in Figure 10. If you do not get them go to View menu and select Project and Output.

Figure 10. Summary of the Project Created The project window shows the workspace file name (PWM_1.mcw) in the title bar, and the project file (PWM_1.mcw) at the top of the file tree view. A workspace file keeps track of what files and windows are open, where the windows are located in the MPLAB IDE workspace, what programmer or debugger tools are selected and how they are configured and other information on how the MPLAB environment is set up. A project file keeps track of all necessary files to build a project including source and header files, library files, linker scripts and other files. 10. All programs require the linker script and header file relating to the device you use. A linker script is required to build the project. It is a command file for the linker, and defines options that describe the available memories on the target microcontroller. The linker script for dsPIC30F4011 can be added by right-clicking Linker Script, select Add Files and search for p30f4011.gld under C:\Program Files\Microchip\MPLAB C30\support\dsPIC30F\gld. Header file initializes or assigns any variables and functions needed for programming the selected device. For dsPIC30F2011, the header file needed is p30f4011.h under C:\Program Files\Microchip\MPLAB C30\support\dsPIC30F\h. Save your project by selecting Save Project from the Project menu. The Project and Output windows will be updated as shown in Figure 11. 11. (Only when you know what Oscillator your device is based on) It is quite important to first set the oscillator you want your microcontroller to be based on. For this PWM generation circuit, we will use the highest PLL factor of the internal clock. To set this up properly, go to Configure menu, and then Configuration Bits. Untick Configuration Bits set in code.

Page | 7

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Along Oscillator category and under the Setting tab select FRC w/PLL 16x. More information will be provided in Section 2.1 later.

Figure 11. Project with Header File and Linker Script Added

Section 2: Writing a C program for PWM generation


This section guides you to write the C program which can be 1) converted to machine code and then 2) uploaded to the device we use so that it can generate PWM signal as required. Copy the following program codes from the electronic copy of this notes onto a new NOTEPAD window (Though can be omitted, the comments indicated by //.. are useful to explain the codes).
#include <p30f4011.h> //This line is required to program any PIC device _FOSC(CSW_FSCM_OFF & FRC_PLL16); //FRC w/ PLL 16x instruction cycle clock rate 7.37MHz //REFER to the Part 2.1 for explanation //For more details read Pg 140 and Pg 143 of //datasheet #define MILLISEC 29480 // 1 mSec delay constant //***** function declarations******// int main(void); void init(void); void DelayNmSec(unsigned int N); //*****Variable declarations******// int ADCValue; //*****Main Program starts here******// int main() { 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? //If not the program will be trapped ADCValue = ADCBUF0; // If yes then get ADC value 10-bit PDC1 = ADCValue; } return 0; }

Page | 8

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

// ***** INITIALISATION ***** *// void init(void) { //****SETUP of ADC****// //REFER to the Section 2.3 for explanation // For more details see Page 131 - 138 of data sheet for reference //***ADC in Manual Sampling Mode*****// 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 //**SETUP OF PWM MODULE****// PWMCON1 PTCON PTPER PDC1 PTMR = = = = = 0x0011; //Enable PWM Pins and enable complementary mode 0x8002; //Read Section 2.2 736; //Read Section 2.2 736; //Read Section 2.2 0;

} void DelayNmSec(unsigned int N) //Redundant N loop { unsigned int j; while(N--) for(j=0;j < MILLISEC;j++); }

Save the code under PWM_1.c and add this into Source Files of your project. You are expected to understand the codes above. The following 3 sub-sections will guide you through but the full details and explanation of the device operation can be found in the datasheets.

Page | 9

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Section 2.1 Setting up and Understanding the Device Clock

Table 1: Frequency and Clock settings for dsPIC30F4011 device [1] There is a variety of clock types/oscillator you can use as the basis for the processing unit of your device. Using the setting we have done in Step 11 of Section 1, we are using the fastest clock rate the device can offer which is 16 x the internal clock frequency (7.37 MHz) (i.e. using FRC internal w/16x PLL).

Page | 10

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Task 1

Calculate the Instruction Execution Frequency using the formula stated in Note 3 under Table 24-15: Internal Clock Timing Examples in Table 1 (of previous page). You should remember in microcontroller, there are 4 clock cycles required to carry out each instruction. This value corresponds to the variable FCY in your program. In addition, the datasheet may use MIPS (millions instructions per second) which is equal to FCY/(106). The reciprocal of FCY is TCY which indicates the amount of time required to carry out one instruction. In your report you should demonstrate and explain how you determine FCY, MIPS and TCY based on the settings we have in the codes. In your program, there is a variable called MILISEC. Assume there is another variable, (j for example) is to be iteratively incremented to the value of MILISEC, and one increment takes one instruction cycle. Based on the understanding of the clock type you selected, mathematically show that with MILISEC equals to 29480 so that a 1 milisecond delay can be achieved. Show and explain your calculation. (In later section, you will find that practically this cannot be achieved because the one increment step may take longer than one instruction cycle.)

Section 2.2 Setting up the PWM operation


dsPIC30F devices have two main approaches using either Output Compare or Motor Control PWM modules. The former is sufficient for single or isolated PWM while the latter is useful for H-bridge type of converter (i.e. containing switches that require floating ground). However not all devices have both modules (dsPIC30F2011, dsPIC30F4013, dsPIC30F3014 etc only have Output Compare while dsPIC30F4011 for example has both). We are going to drive one dc-dc boost converter and actually Output Compare module will be sufficient but this lab is designed to help you in future projects which may require Motor Control PWM module; before the settings are done, please follow the brief explanation about Motor Control PWM module: Using this module, there are several approaches for generating PWM; one is by comparing a saw-tooth waveform (which is actually the value of PTMR) with a constant value which is defined by PDCx (x could be 1, 2, or 3) as shown in the Figure 12. We call this as edged aligned PWM The PWM1H is based on PDC1 and PWM2H is based on PDC2.

Figure 12: Edged-aligned PWM

Page | 11

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Another approach is by comparing a triangular waveform (which is actually the value of PTMR again) with a constant value of PDCx (x could be 1, 2, or 3) as shown in the Figure 13. We call this as Centre-Aligned PWM PWM1H is based on PDC1 and PWM2H on PDC2.

Figure 13: Centre-aligned PWM Can you tell the relationship between the duty ratio of PWM1H and the value of PDC1? Actually there is another output (which is not shown in the figures) which has the complementary (or the inverted) form of PWM1H and it is found at PWM1L. And so can you predict the relationship between the duty ratio of PWM1L and the value of PDC1? If so, what does the value of PDC1 represent? You will find that the second PWM method is more useful especially for more complicated PWM schemes (which will be covered in ELEC5525). So please read carefully the operation below: In the center aligned mode, the time base is an up-down counter: it starts from zero, counts up to the value in the period register and then counts down to zero and so on. Bit 15 in the timer register (PTMR) indicates the counting direction. In this mode, the PWM period is twice the time base period. The duty cycle buffers are updated when the timer is reset to zero; the interrupt flag is set at the same time. As you can see from Figure 13, the PWM waveform is centered around the instant in time when the timer reaches its maximum value and start counting back: from this the name center aligned. Although the PWM frequency is half the edge aligned PWM, this mode is very useful because it avoids the simultaneous switching of all the PWM outputs at the same time. This is not a problem for the device itself, but since power switches are connected to these outputs, the switching current can be quite high and may require an oversized power supply. a) Configure the PWM module
To use the centre-aligned PWM mode, we set PTMOD<1:0> in PTCON register to be 2 or 10(binary). The clock that governs the PWM should be the internal clock hence bit 15 is 1 and it is the same scale of the clock frequency hence bits 3 2 is 00. Hence PTCON is set to be 0x8002 (in hex).

b) Set the PWM period by writing an appropriate value for (PTPER)


We will need 20kHz PWM which has a period of 50 s (Tpwm).

Page | 12

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds


-

School of Electronic & Electrical Engineering

Task 2

From Figure 13, the time for PTMR to reach PTPER is half of Tpwm. Hence the following formula can be used to calculate the right value for PTPER to give the desired PWM frequency: TPWM = (2TCY PTCKPS pre scale value (PTPER + 1) ) Since we have set the pre-scale value 1, TPWM = (2TCY (PTPER + 1) ). Now can you verify the code given.

c) Set the PWM duty ratio by writing to the PDC1 register (assuming PDC1 has a direct relationship with the duty ratio)
From (a), we know PTPER represents half of the total period and we will know how long (or by what proportion) the pulse has to be high; thus the duty ratio can be set. The amount of the proportion is stored under PDC1 register. Note that the duty ratio of 100% will correspond to 2 PTPER using Centre-aligned PWM. What is the duty ratio value if PDC1 = 736?

d) Reset PTMR
It is worthwhile to reset the PTMR at the beginning of the coding and hence PTMR is et to 0.

Section 2.3 Setting up ADC Channels and Retrieving Data from the ADC Buffers
Figure 14 shows a basic conversion sequence and the terms that are used. Sample and Hold (S/H) amplifier first of all is connected to the (designated) analogue input for sampling. The time taken when S/H amplifier is called the sample time (Note that this may not necessary to be equal to the overall sampling time you will need to know for Modelling, Control etc.). Sample time may be set through the code (hence automatic sampling) or set manually through setting and clearing the SAMP bit (manual sampling). For both modes, the S/H amplifier is disconnected from the input at the end of sampling time. The conversion to the digital value may start automatically or by some externally triggered source. The conversion time for dsPIC30F devices are 12 TAD where TAD is one ADC clock cycle (the explanation for TAD will be covered below). At the end of the conversion, the result is loaded into ADC buffer. If a series of conversions (and hence few buffers will be used) are required, an interrupt can be used (will be explained later). For our dsPIC30F device (i.e. 4011 series), we have a minimum sample time which is about 154 ns so that the desired accuracy for the ADC conversion can be obtained.

Figure 14: General timing diagram of ADC process [2] Page | 13 ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

In general to configure the ADC for dsPIC30F device, the following steps must be performed: a) Setting the analogue input pins This can be done by clearing (i.e. 0) the relevant bits from bit 0 bit 7 of ADPCFG register (bit 0 corresponds to AN0, bit 1 to AN1 etc); pins which are not used should be defined as 1. b) Setting the voltage reference We want the full range of the 5V supply and hence ADC-ref-H should be set to AVDD and ADC-ref-L should be set to AVSS. Hence VCFG in ADCON2 is 000. If you need other voltage range, you can then use External Vref+/- and set the VCFG accordingly (refer to the datasheet [1]). c) Setting the type of sampling and conversion There are a few types of sampling and conversion you can do with the ADC for dsPIC30F devices. In the code above, only manual sampling is required; hence, ASAM in ADCON1 is set to 0. However, the sampling should not be done immediately (during the initialization stage) so SAMP is initially 0. SAMP is only set to 1 when the code is ready to perform sampling. We also want a manual conversion (i.e. the starting of conversion is done by the code), hence SSRC = 000. You can store the output in the buffer in various forms (integer, fractional, signed integer etc.) but in our case we will use integer type hence FORM = 00. ADON is turned off initially but has to be turned on at the end of the initialization stage. We also need to connect the channel of ADC conversion correctly to the ADC input. This can be done by setting CH0SA bits in ADCHS properly. CH0SB is used when multiplexing function is applied. c) Setting the Sampling time (TAD)
This is important especially auto sampling and conversion mode is applied. First most of our applications employ internal clock, hence ADRC in ADCON3 is equal to 0. Two other registers which are important for ADC settings; SAMC defines the number of TAD between the start of the sampling and the start of the conversion (this is important for auto conversion in this case the minimum SAMC is 1 TAD). For manual conversion, SAMC can be set to 0. The other register is ADCS and the formula to calculate this is shown below: ADCS = 2 (TAD/TCY) -1

d) Other features (like channel scanning and interrupts) Optional


When you have more than one ADC inputs or when you want to have more accurate measurements (by taking the average of a number of noisy signals), Channel scanning with ADC interrupts can be used. The register relating to this is ADCON2. CSCNA = 0 when scanning is not needed and 1 when it is needed. For the latter, the relevant bits in ADCSSL have to be set (1) so that the inputs are selected for the scanning (bit 0 corresponds to AN0, bit 1 to AN1 etc). BUFM is set to 0 if the data from ADC are not split into two words. (This mode is normally what we are going to use.) ALTS = 0 when only MUX A is used. (This mode is normally what we are going to use.)

Page | 14

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds


-

School of Electronic & Electrical Engineering

SMPI determines the number of samples being converted before an interrupt occurs. (0000 means an interrupt occurs after conversion of one sample, 0001 two samples, 0002 three samples and so on.)

e) Turn on the ADC when all the initialization has been done (i.e. ADCON1bits.ADON = 1)

Task 3

Study the codes above and with the help of the datasheet, describe how the ADC has been set.

Three other ADC approaches (auto sampling-conversion, and auto sampling-conversion with averaging and auto sampling conversion for two inputs) are listed in the Appendix. (YOU ARE EXPECTED TO TRY THEM OUT BUT ONLY WHEN YOU HAVE BUILT, DEBUGGED and PROGRAMMED YOUR DEVICE).

Section 3: Building and Debugging your Program


In this section, we will build the program and before uploading the machine language to the microcontroller, we will debug the program to 1) ensure the results (by observing the relevant registers) obtained are the expected ones and 2) measure the real time taken to execute one or a group of codes this can then help us to calculate the overall sampling time later for example. First select Build All under Project menu, as shown in Figure below:

Figure 15: Building a Project Almost immediately in the Output window under Build tab, there appears the summary of the programs built this includes the overall data and program memory that has been used. At

Page | 15

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

this stage, you are actually free to upload the machine codes (i.e. programming your device through a programmer like PICKIT2). For our exercise, we will firstly need to debug the program. To do this, select MPLAB SIM under Debugger menu. (This step is essential to confirm your code is working like how it was designed for.)

Figure 16: Turn on the MPLAB SIM Debugging Mode You can observe additional tab called MPLAB SIM appearing in the Output window and there are more options in Debugger menu as shown in Figure 17.

Figure 17: Debugging Functions Before we Run the program, let us turn on the following: a) Watch window under View menu. b) Stopwatch window under Debugger menu. c) Stimulus window under Debugger menu. You need to create a new one for the first time. There are a few stimulus features you can use but the most useful one is the Register Injection. Select that tab, click on the first empty row, automatically some texts as shown in Figure 18 will appear. The program detects that you can have registry entry for ADCBUF0. (NOTE: If the window you have opened does not appear on the screen, you may need to go back to Window menu to select it.)

Page | 16

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Figure 18: Stimulus Window Register Injection You need to upload a .txt file under Data Filename. Using NOTEPAD, type out the following list and save it under the name of PWM_value.txt. 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008 0x0009 0x000A 0x00AA 0x00FF 0x0055 0x03FF 0x0300 0x0388 Go back to the Register Injection window, browse and select PWM_value.txt. Click Apply and minimise this window. We also intend to observe any change of data stored in ADCBUF0, PDC1 and ADCValue this can be done under Watch window. Add these variables from the list. ADCBUF0 and OC1RS (inherent for any dsPIC30F) are obtained from SFR list while ADCValue (userdefined) isfrom Symbol. When you have added them all, the window should look like Figure 19. (Do not worry if the addresses look different from yours)

Page | 17

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Figure 19: Observing Various Registers in Watch Window If we were to run the program now, the registers will continue changing and we hardly can view the change. We can indicate breakpoints within the code so that we can observe and verify our codes. Let us have one breakpoint along ADCValue = ADCBUF0; line by just double-clicking on it. A B will appear beside the line of interest.

Figure 20: Breakpoint indication Now you can run your program by selecting Run from Debugger or pressing F9. If your codes have been correctly entered, the execution of your code should stop at the breakpoint you indicated. Monitor the Watch Window each time you press F9. (Note you can press F7 once to run only one program line at a time). Using the above debugging approach, explain how you can verify that the above codes have been correctly entered (i.e. manual sampling of ADC has been set and if a 5V is applied to the ADC input, a duty ratio of 50% of PWM signal will be generated.

Task 4

Now let us check the time between two consecutive updates of PDC1 (i.e. the duty ratio of PWM signal). To do this, let us Run the code and let the program point (the green arrow) stop at the breakpoint indicated in Figure 20. From the Stopwatch window, reset the time by clicking the zero tab. Press F9 once and you should check the time along Time row under Stopwatch. It should gives you 491 ms. Can you explain why you could not obtain 100 ms? (How can you improve this? HINT: The function DelayNmSec may take more than 1 ms to be executed in practice. So should 100 be still passed as the input?)

Task 5

Page | 18

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Figure 21: Stopwatch window when zero is pressed.

Section 4: Uploading the Program to the Device using PICKIT 2

Figure 22. Pin diagrams for dsPIC30F4011 Referring to the Pin Diagrams in Figure 22, the following indicates some of important pins you need to know for this lab: AN0 AN8: Analogue input channels (0 5 Volt) AVDD : Positive supply for analogue module AVSS : Negative supply for analogue module MCLR: Master clear input. (An active LOW to this will reset the device) OC1 OC2: Output Compare 1 through 2 PWM1L PWM3H: PWM Module Output 1 Low to PWM Module Output 3 High PGD: In-circuit serial programming data input/output pin PGC: In-circuit serial programming clock input/output pin VDD: Positive supply for logic and I/O pins VSS: Ground for logic and I/O pins.

External connections need to be done in order for the microcontroller to be programmed properly the device. The circuit diagram is given to you as shown in Figure 23.

Page | 19

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

4011

10k
40 39

32 31 26 25 PWM1H 37

Figure 23. External Connection for Programming dsPIC30F4011 using PICKIT2 To program your device, select PICKIT2 from the Select Programmer under Programmer. When your PICKIT2 has been connected to the circuit as shown in Figure 23, click on the following button (which can be normally shown at the top right corner): Click on this to establish connection to the device

Once it is clicked, the following messages appear in the Output window:

Figure 24. Establishing a link between dsPIC30F4011 and PICKIT2 To start uploading your code to the device, select Program from Programmer Menu as shown below:

Page | 20

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Once it is uploaded, the following messages should appear in your Output window.

Figure 25. Uploading machine code to dsPIC30F4011 through PICKIT2 and MPLAB IDE

Your microcontroller can now function as PWM generator! BUT You need to know if it is functioning as it should. Section 5: Testing the Microcontroller Device
Note that when the device is used as to generate PWM, MCLR has to be pulled-up to HIGH (5V supply) through a 10 k resistor as shown in Figure 22. This is to prevent the device from being reset while operating as a PWM generator. For this lab, the analogue input signal to AN2 is taken from a potentiometer which can be implemented using the circuit shown in Page | 21 ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Figure 26. (The only different between this circuit diagram and that in Figure 21 is that the ADC is applied with an analogue signal and the device is powered by an external 5 V supply.
4011 40 39

32 31

PWM1H

Figure 26. External connections for dsPIC30F4011 when it is used as PWM generation Circuit AVDD

AN2 Duty cycle value

AVSS Figure 27. Duty cycle adjustment circuit Note that the voltage across AN2 varies from 0 5V since AVDD and AVSS respectively are connected to 5 and 0 V. We are using this to determine the duty cycle value for the PWM signal we are generating. Theoretically you can now derive the relationship between the duty cycle value and the voltage level across AN2, referring to the full range of duty ratio provided by the code.

Task 6

Task 7

Obtain three different PWM signals with duty ratios of your own choice. Measure the voltage levels across the input potentiometer. For all cases, use oscilloscope to measure the voltage when the output PWM1H is HIGH and LOW. How about the frequency value? Confirm the observed frequency with the given code.

Page | 22

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Section 6: Testing Other ADC Approaches


Briefly describe the ADC process for the three subsequent programs (A, B, C). You are encouraged to refer to the datasheet. Determine the overall sampling time for each of them. Note that DutyCycle is updated using ADCValue>>2; this means 2 bits shifted to the right or a division of 4 in operated. Debug the program and check how much faster the shifting operator is than the multiplication operation. (Note that the shift operation can only be used because our scale factor is around 1/4. For other multiplication, fixed point arithmetic is required.)

Task 8
-

Program A: Auto Sampling-Conversion for the ADC


#include <p30f4011.h> //This line is required to program any PIC device _FOSC(CSW_FSCM_OFF & FRC_PLL16); #define MILLISEC 29480 // 1 mSec delay constant //***** function declarations******// int main(void); void init(void); void DelayNmSec(unsigned int N); //*****Variable declarations******// int ADCValue; int DutyCycle; //*****Main Program starts here******// int main() { init(); //****Program initialization - Jump to this function while (1) { ADCValue = 0; ADCON1bits.ASAM = 1; //auto start sampling while (!ADCON1bits.DONE); // conversion done? ADCON1bits.ASAM = 0; ADCValue = ADCBUF0; // yes then get ADC value 12-bit PDC1 = ADCValue; } return 0; } // ***** INITIALISATION ***** *// void init(void) { //****SETUP of ADC****// // Setup of ADC - Read Page 131 - 138 of data sheet for reference //***ADC in Automatic Sampling Mode*****// ADPCFG = 0xFFFB; ADCON1 = 0x00E0; ADCHS = 0x0002; ADCSSL = 0; ADCON3 = 0x0127; ADCON2 = 0; ADCON1bits.ADON = 1;

Page | 23

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds


//**SETUP OF PWM MODULE****// PWMCON1 = 0x0011; PTCON = 0x8002; PTPER = 736; PDC1 = 737; PTMR = 0;

School of Electronic & Electrical Engineering

} void DelayNmSec(unsigned int N) //Redundant N loop { unsigned int j; while(N--) for(j=0;j < MILLISEC;j++); }

Program B: Auto Sampling-Conversion with Averaging


#include <p30f4011.h> //This line is required to program any PIC device _FOSC(CSW_FSCM_OFF & FRC_PLL16); #define MILLISEC 29480 // 1 mSec delay constant //***** function declarations******// int main(void); void init(void); void DelayNmSec(unsigned int N); //*****Variable declarations******// int ADCValue; int DutyCycle; //*****Main Program starts here******// int main() { init(); //****Program initialization - Jump to this function while (1) { ADCValue = 0; IFS0bits.ADIF = 0; ADCON1bits.ASAM = 1; while(!IFS0bits.ADIF); // conversion done? ADCON1bits.ASAM = 0; ADCValue = ADCBUF0 + ADCBUF1; ADCValue = ADCValue>>1; PDC1 = ADCValue; } return 0; } // ***** INITIALISATION ***** *// void init(void) { //****SETUP of ADC****// // Setup of ADC - Read Page 131 - 138 of data sheet for reference //***ADC in Manual Sampling Mode*****// ADPCFG = 0xFFFB; ADCON1 = 0x00E0; ADCHS = 0x0002; ADCSSL = 0; ADCON3 = 0x0127; ADCON2 = 0x0004; ADCON1bits.ADON = 1; //**SETUP OF PWM MODULE****// PWMCON1 = 0x0011; //Enable PWM Pins and enable complementary mode

Page | 24

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

PTCON = 0x8002; PTPER = 736; PDC1 = 736; PTMR = 0; } void DelayNmSec(unsigned int N) //Redundant N loop { unsigned int j; while(N--) for(j=0;j < MILLISEC;j++); }

Program C: Auto Sampling-Conversion for Two outputs


#include <p30f4011.h> //This line is required to program any PIC device _FOSC(CSW_FSCM_OFF & FRC_PLL16); #define MILLISEC 29480 // 1 mSec delay constant //***** function declarations******// int main(void); void init(void); void DelayNmSec(unsigned int N); //*****Variable declarations******// int ADCValue; int DutyCycle; //*****Main Program starts here******// int main() { init(); //****Program initialization - Jump to this function while (1) { ADCValue = 0; IFS0bits.ADIF = 0; ADCON1bits.ASAM = 1; while (!IFS0bits.ADIF); // conversion done? ADCON1bits.ASAM = 0; ADCValue = ADCBUF0 + ADCBUF1; ADCValue = ADCValue>>1; PDC1 = ADCValue; } return 0; } // ***** INITIALISATION ***** *// void init(void) { //****SETUP of ADC****// // Setup of ADC - Read Page 131 - 138 of data sheet for reference //***ADC in Manual Sampling Mode*****// ADPCFG = 0xFFF3; ADCON1 = 0x00E0; ADCHS = 0x0002; ADCSSL = 0x000C; ADCON3 = 0x0127; ADCON2 = 0x0404; ADCON1bits.ADON = 1; //**SETUP OF PWM MODULE****// PWMCON1 = 0x0011; PTCON = 0x8002; PTPER = 736; PDC1 = 736; PTMR = 0;

Page | 25

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

} void DelayNmSec(unsigned int N) //Redundant N loop { unsigned int j; while(N--) for(j=0;j < MILLISEC;j++); }

Note Set No. 2: TESTING AND APPLYING THE CONVERTER FOR A STAND-ALONE PV SYSTEM

Section 1 INVESTIGATION OF THE STEP-UP CONVERTER MOUNTED ON A PCB BOARD


In this laboratory, the boost converter circuit we are working on is as shown in Figure 1. As observed, input and output terminals are left unconnected. You have the freedom to choose any supply or load AS LONG AS the voltage and current ratings are not exceeded. Therefore, it is part of your task to analyze the limitations (voltage, current, internal resistances, response time etc..) each component has. To do this, you must refer to the datasheet of the components and following explanation may provide you some helpful guides. NOTE: All component datasheets are in PDF version and can be obtained from ELEC5510 VLE.

L
+

D
+

L = 1 mH C1 = 82 F C2 = 82 F

C1
Input Socket

S
_

C2

_
Output Socket

Gate Socket

Figure 1. A Boost converter circuit Capacitors C1 and C2 The most important attribute you should check for is the maximum rating for the voltage across the capacitor. This value should not be exceeded at any operating conditions, whether during the transient or steady state. For our application, it is one of our precaution measures

Page | 26

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

to further restrict the peak voltage across the capacitor to be within 70% of the maximum rated value.

Inductor L The inductor we are using is the powdered-iron core type and the major advantage using this is the high saturation flux density. With this inductor, the current flowing through it will hardly saturate even at a higher DC value. There are a number of features you have to be aware of and these include the following: Rated current, IR (Maximum current value) Rated inductance, LR DC resistance Inductance at rated current, IR

All the above features should be considered when designing low-ripple converter whilst the operating current at any condition (transient or steady state) should NOT exceed the rated value. Power Switch S In this laboratory, we are using MOSFET type of switch. The maximum ratings including drain-source voltage, gate-source voltage and drain current impose a limitation on our converter operation. This means the supply voltage should not be above the maximum drainsource voltage, the gate signal should be below the maximum gate-source voltage and the switch current should be always lower than the maximum rated value. All semiconductor power losses including those in MOSFET are dissipated in the form of heat. It is therefore important to know these characteristics of our device. They are expressed as thermal resistances in the datasheet. This information will lead you to the design of adequate cooling system for MOSFET (or any other power devices in your future work). There are other important features which also determine your converter design limitation. These include: Gate threshold voltage (This determines the gate voltage at which the switch is properly turned on or off refer to Figures 4 in the datasheet). Static drain-source on resistance (This determines the losses and voltage drop across a switch during turn-on time refer to Figure 6 in the datasheet). Switching times; turn-on and turn-off delay times, rise and fall times (This determines the maximum switching frequency this converter can operate refer to Figure 17 in the datasheet)

Page | 27

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds Power Diode D

School of Electronic & Electrical Engineering

A fast recovery diode is used for our converter circuit. Check that the reverse recovery time is much faster than switching period. Also similar to MOSFET, it has maximum limits for operating forward current and its thermal characteristics are represented by the thermal resistance. Also it has a threshold voltage above which the diode is properly turned on (forward-biased). In this laboratory session you are assigned to populate the above described components on a PCB board which has been fabricated in advance for you. The layout is illustrated in Figure 2. You are expected to do the following tasks for this part of exercise. Task 1: Analyze the given PCB board and identify the locations for all the components. Remember that some components have polarity for the pins and they have to be connected appropriately. Remember also to attach the heat sinks to the devices properly. The input, output and gate sockets are the Weidmuller type (orange-coloured).

IMPORTANT: Check with the lab supervisor or demonstrator if you are unsure before you perform any test on the converter. (SAFETY COMES FIRST)

Figure 2. PCB Layout for boost converter

Page | 28

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Section 2: DEVELOPING SWITCHING CIRCUIT FOR MOSFET


A general block diagram for the switching circuit is illustrated in Figure 3. This consists of following components: a) Microcontroller b) Opto-coupler c) MOSFET DRIVER.

Gating signal for MOSFET

Opto-coupler Microcontroller

Driver

Figure 3. Block diagram for the switching circuit Opto-coupler The switching circuit is actually the interface between the low power digital circuit (microcontroller) and the high power converter circuit. Hence, it is crucial to have an adequate isolation between the two networks so that any high current fault in power circuit will not damage more expensive microcontroller and other control circuits. As observed in Figure 7 for opto-coupler internal structure, there is an electrical isolation between a LED and a photo-detector. The electrical signal from the input (ANODE-CATHODE) is firstly converted to light energy in the LED. The nearby photo-detector then senses the light beam and converts it back to electrical signal through an amplification circuit at the output. As given by the corresponding truth table, we know that the output from this opto-coupler is being inverted (A HIGH signal in the input means a LOW in the output). Can you explain this? Do we need any additional circuit connection? Refer to Figure 8. An input resistor, Rin connected at the input is used to limit the current flowing through the LED. Its value can be determined by the relationship between the desired forward current and the typical input forward voltage across the LED. This is given by
Rin 5 VF IF

Please read Page 2 and Page 9 of the datasheet for VF and IF values. At the output, a pull-up resistor of 4.7k is added (chosen according to driver specification). Can you predict what the output voltage value is when the input is HIGH (or LED is illuminated) and vice versa? You may worry about the ACTIVE LOW operation but you may find the solution for this when you go through the driver.

Page | 29

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Notice that there are two power supplies connected to this component. A ceramic capacitor 100nF is also connected across VCC and GND. To reduce noise interference, the capacitor should be physically as close to the opto-coupler as possible. IMPORTANT: You MUST make sure that the two power supplies are properly isolated. The grounds of both supplies MUST NOT be common. The 5V supply is the same as the one which powers the microcontroller whilst the 15V supply will also supplies energy to the driver circuit.

Figure 7. Pin diagram for HCPL 4503 Optocoupler

Figure 8. External connection for opto-coupler circuit

Driver This component provides the driving signal (with sufficient current and voltage level) to appropriately turn on and turn off the device which in our case is the power MOSFET (VH = 15V and VL = 0V). We shall use TD351which is advanced driver for MOSFET or IGBT. Its general pins diagram is shown in Figure 9.

Page | 30

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Figure 9. Pins diagram for TD351 driver There are various attractive features for this driver. These include the innovative active Miller clamp function which obviates negative gate drive in most applications and two-level turn-off feature with adjustable level and delay. Whilst the latter can protects against excessive overvoltage at turn off in the case of overcurrent/short-circuit condition, the former can reduce voltage spike on the device gate. In this laboratory, one level turn-off is sufficient for our application but we still include the Miller clamp function in our gating circuit. External circuit has to be connected to this component and this is illustrated in circuit diagram shown in Figure 10.

Figure 10. Gate driver circuit with TD351 Notice that the input has been pulled up to positive supply of 15V through a 4.7K. When the input is driven low, the output is high cancelling the inverted effect by the opto-coupler. Since one-level turn off is used, VREFand CD are connected with 4.7K. (If you embark on twolevel turn off in future, you have to connect a capacitor to each of these two pins as explained in Page 5/12 of the datasheet.). The gating resistor (47) is sufficient to turn on the MOSFET quickly but not too small that the current capability limit is exceeded. As can be observed, the gating voltage is constantly connected back to CLAMP pin for Miller Active Clamp function. Please read Page 5/12 of datasheet and Page 5/15 of TD351 Application Note for more details. Similar to opto-coupler, a capacitor is connected closely to the VH and VL pins of the driver. In order for the device to operate properly, another connection is needed between the MOSFET source and the negative terminal of 15V supply. Page | 31 ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

Again, remember that all power sources (5V, 15V and your power circuit supplies) have to be isolated. Make sure their grounds are NOT on the common point! In this part, you are expected to make necessary connections for all the above described circuits. DO NOT connect these circuits to the power converter you have built. You are expected to conduct some tests and make some observations on each of the circuit. You have to bear the following points in mind before you do the tests. Confirm with lab supervisor or demonstrator before you turn on your supplies. Build and test the circuit independently. (Microcontroller, Opto-coupler and Driver). Get ready with paper and pen as you need to make some observations (sketches and voltage measurements) and when you are ready, perform the following task. Test on Opto-coupler Task 1: Make all necessary connections and test on the circuit by just using TTL input (5V HIGH, 0V - LOW) from a signal generator. Make sure you have the same frequency as that obtained from microcontroller output. Make sure you apply the CORRECT power supply! Confirm your connection with the lab supervisor or demonstrator. Observe the input and output waveforms. Task 2: Replace the signal generator with the microcontroller. Check if you obtain expected outputs when you vary the duty cycle. Now can you see any difference in the output from the input of opto-coupler? Confirm your finding by repeating with a set of duty cycle values. Test on Driver Task 1: Make all necessary connections linking the driver to the opto-coupler and microcontroller. Make sure you apply the CORRECT power supply! Confirm your connection with the lab supervisor or demonstrator. Observe the input and output waveforms when you repeat again with a set of duty cycle values. You are also expected to sketch all the following waveforms when the input potentiometer is respectively measured at 1.5V, 2.5V, 3.5V and 4.5V a) Output from Microcontroller b) Output from Optocoupler c) Output from Driver Provide supportive explanation for your obtained results.

Section 3: TESTING THE BOOST CONVERTER

Page | 32

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds

School of Electronic & Electrical Engineering

L
+

D
+

5V

C1

S
_

C2

Figure 11. Application of converter to electric bulbs REMEMBER: CONFIRM WITH LAB SUPERVISOR OR DEMONSTRATOR BEFORE YOU TURN ON THE SUPPLY. DANGER: YOU ARE NOW DEALING WITH POWER CIRCUIT MAKE DISCONNECTION ONLY WHEN YOU HAVE TURNED OFF ALL SUPPLIES Task 1: Use your built converter to regulate the voltage across two-serially connected electric bulbs as shown in Figure 11. Connect a 5V 2A to the input and the gate is driven by the switching circuit you have built. Task 2: Vary the duty cycle of the PWM signal and observe the bulb brightness. Also observe the voltage across both the bulbs. Does it satisfy the input-output voltage relationship for boost converter? If not, why? (HINTS: Analyse boost converter operation when switch is turn on and turn off respectively. When you do this, reflect on the losses/voltage drops which may occur in any components/devices. Confirm your results with any relevant datasheets) You are expected to observe the output voltage values corresponding to input potentiometer of 1.5V, 3.5V and 4.5V. What are the applied duty cycles? List all the obtained measurement in a table. Discuss on the results.

Section 4: IDENTIFYING CURRENT-VOLTAGE CHARACTERISTICS OF A PRACTICAL PV PANEL


In this laboratory, a PV panel with a sun simulator setup has been prepared for you. The latter is used to practically simulate various weather conditions under which the PV system is experiencing. For this part, you are given a rheostat, which is sufficiently large to consume the energy generated by PV. You are expected to connect this component across the PV out as shown in Figure 1. Note that an ammeter and a voltmeter are included in the connection.

Page | 33

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds A

School of Electronic & Electrical Engineering

To derive the I-V characteristics of the PV panel, the rheostat has to be adjusted accordingly. Task 1: Can you explain the relationship between the resistance values of the rheostat and the I-V characteristics you will obtain later? Task 2: You should obtain two sets of I-V characteristics. The first is when the ambient temperatures (T1, T2, T3) are low under three solar irradiation levels (G1, G2, G3). The second one is when the ambient temperatures are high (T4, T5, T6) under the same irradiation levels (G1, G2, G3). For this task, you should be able to fill up the following table

Page | 34

ELEC5451 B.Chong (2012/2013 Session)

The University of Leeds Low Temperature (23 26oC) T1 G1 VPV T2 G2 VPV T3 G3 VPV

School of Electronic & Electrical Engineering

IPV

IPV

IPV

(You should obtain enough I-V pairs so that your I-V graphs can be accurately sketched.)

High Temperature (31 35oC) T4 T5 T6 G1 VPV IPV G2 VPV IPV G3 VPV IPV

Task 3: Note that you are not expected to measure the resistance of the rheostat. Therefore for each weather condition in task 2, derive the variation of the rheostat resistance. Does it conform to the relationship you state in Task 1? How would your measured I-V characteristics be different from the theoretical ones? Also observe the values for open circuit voltage and short circuit current when the weather changes. Task 4: Under each weather condition, deduce the maximum power point and list them all under a table. Can you see how the points vary when the weather conditions are changing?

Section 5: INVESTIGATION OF DC-DC CONVERTER USED FOR CONTROLLING PV TERMINAL VOLTAGE


For this part, the lab supervisor will demonstrate how the PV voltage can be controlled by the DC-DC converter. The output of the converter is connected with a CONSTANT load. Task 1: Perform various tests with four different values of duty cycles under a fixed weather condition. Observe the PV voltage and current. What does the DC-DC converter function as? What happens when duty cycle is regulated? Referring to the answers you provided for the previous two questions, can you explain the action in duty cycle regulation by relating the I-V characteristics you obtained. (Hint: You need the fundamental Ohms law for this.)

Page | 35

ELEC 5451 B. Chong (2012/2013 Session)

The University of Leeds References and Bibliography

School of Electronic & Electrical Engineering

[1] Datasheet of dsPIC302011 High Performance Digital Signal Controllers, Microchip Technology Inc. DS70065D, 2005. [2] Datasheet of 12-bit A/D Converter from dsPIC30F Family Reference Manual DS70065D, Microchip Technology Inc., 2005, pp 18.1 18.38. [3] Tom Markvart and Luis Castaner, Practical handbook of photovoltaics: fundamentals and applications, Elsevier Advanced Technology, New York, 2003. (Available online from library) [4] Tomas Markvart, Solar electricity, 2nd Edition, Wiley, Chichester, 2000. [5] Tore M. Undeland, William P. Robbins, Power electronics: converters, applications, and design, John Wiley & Sons, Hoboken, NJ, 2003. (Available online from library) and other references can be found in library or online journals by typing keywords, Power Electronics, dc-dc converter, Photovoltaic, digital implementation of PWM generator, gate drive circuit, MOSFET etc.

Page | 36

ELEC 5451 B. Chong (2012/2013 Session)

You might also like