0% found this document useful (0 votes)
10 views

lab1_statement

lab1_statement

Uploaded by

Pippo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

lab1_statement

lab1_statement

Uploaded by

Pippo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ELEC0055

Elements of Power Electronics


Teacher: FREBEL Fabrice ([email protected])

Teaching assistants: PURNODE Florent ([email protected])


EWBANK Bastien ([email protected])
Faculty of Applied Sciences
Academic year 2024/2025 Lab. technician: ARBAN Jean ([email protected])

Laboratory 1: Control Board


This laboratory offers a general introduction about your control board. You will learn how to program the
PIC18F26K22 microcontroller and how to use some of its modules. Pay attention to this laboratory as the concepts
introduced are of prime importance for your project.

Introduction

The figure below depicts a schematic of your control board.

5V 5V A7 B0
B4
Pickit connector
A3 5V
𝑉𝑔
1 28 A1
MCLR/RE RB7
Reset jumper

2 27 A0
A0 RA0 RB6
3 26 A2 Connector
A1 RA1 RB5 B5
4 25 B5 PWM
A2 RA2 RB4 B4
5V 5 24 A4 A6
PIC18F26K22

A3 RA3 RB3 PWM


6 23
Potentiometer

A4 RA4 RB2 𝑉𝑔 5V
7 22
RA5 RB1
Power in

V. regulator
8 21 on/off
Vss RB0 B0 5V
9 20
A7 RA7 VDD
10 19 LED bar
A6 RA6 Vss R network
11 18 9
RC0 RC7
12 17 8
RC1 RC6 A6
13 16 7
RC2 RC5 A7
14 15 6
RC3 RC4 PWM
5
4
3
2
1
0

Lab 1 - page 1
Your control board takes an input voltage Vg , that is the same as for your power board. However, this voltage is too
high for the PIC18F26K22 and a voltage regulator is therefore used to step it down to 5 V. Additionally, a switch
is provided to turn the board on and off, and a diode is included as a safety measure in case the Vg and ground
terminals are accidentally connected incorrectly. Two capacitors also surrounds the voltage regulator to smooth
the voltage.

The PIC18F26K22 is equipped with 28 pins and a wide range of modules. Among them is a 10-bit resolution Analog
to Digital Converter (ADC) which, for this laboratory, will be configured to read the voltage on the 7th pin. This
pin is connected to a potentiometer, whose output voltage can be manually increased (resp. decreased) by turning
clockwise (resp. counterclockwise) the screw on top of it. The 10-bit digital value read by the ADC can latter be
displayed on a bar of 10 LEDs, using pin 22, 23 and 11 to 18.

Another important module of the PIC18F26K22 is the Pulse Width Modulation (PWM) module. To get familiar
with it, you will be asked to send a PWM signal on pin 24, so as to control the brightness of an additional LED. For
added interest, you will control the duty cycle of the PWM signal with the voltage of the potentiometer. At the end
of this laboratory, by turning the screw on top of the potentiometer, you should therefore be able to accordingly
change the 10 LED display and to accordingly change the brightness of the PWM LED.

To program the PIC18F26K22, you will need to connect the Pickit3 (see figure below) to the pickit connector
of your control board. All the connections from the pickit connector to the PIC18F26K22 are already handled.
However, pay particular attention to respect the terminal order when connecting the Pickit3. To help you, a little
white triangle depicts the pin 1 both on the Pickit3 and on the control board.

Pickit3:

The remaining pins of the PIC18F26K22 can be used as general purpose inputs or outputs for your project. These
pins connect to a 14-pin connector, which will allow easy connection with your control board and with the HIL
device (see laboratory 2). Note however that two of these pins (pin 9 and 10), are also connected to LEDs to help
you debug your project.

Software

The MPLAB X Integrated Development Environment (MPLAB X IDE) is essential for programming your
PIC18F26K22. It offers a rich array of tools and capabilities but is relatively heavy in memory space require-
ment (around 8GB), so be sure your computer has space enough to support the installation. To install it, go to the
website “https://www.microchip.com > Tools and resources > Develop > MPLAB X IDE”, download the version
suitable for your computer (Windows, Linux or macOS), launch the installation and follow the installer instructions.
At one point, you will be asked to choose which applications and which microchip device support to install. Make
sure to select at least ”MPLAB X IDE” and ”8 bit MCUs”, you can disregard the other options as they will not
be used for the project. The installer may ask you to install some device drivers, install them all. Finally, at the
end of the installation, it should ask you to install some compilers. Select the XC8 compiler and process to its
installation1 . During the installation procedure, you can select the free license. Later, make sure to add XC8 to
the PATH environment variable.

Once the installation is over, launch MPLAB X IDE. To write code and program the PIC18F26K22, you need
to create a new project (File > New Project...). In the window that pops up, select “Microship Embedded” and
“Application Project(s)”. You later need to select the right device. To do so, select “PIC18F26K22”2 in the “Device”
box and left the “Tool” box with “No Tool”. You are later required to select a compiler, that is the XC8 compiler
1 If the installer does not ask you for the installation of XC8, you should download it from the website “https://www.microchip.com

> Tools and resources > Develop > MPLAB XC compilers”.


2 You can speed up the search by writing in the “Device” box.

Lab 1 - page 2
previously installed. Finally, you will need to choose a project name and location, you can left unchanged the
checkboxes and the encoding. When pushing “Finish”, you project will be created and a “MCC” and “MPLAB
home” windows may open. These additional windows are of no use for the laboratory and for the project, you can
simply close them.

Finally, you need to create a file in which to write your code (File > New File...). In the window that pops up,
make sure to select your project and select “C” in “Categories” and “C Main File” in “File Types”. In the next
window, simply choose for a file name and click “Finish”.

Let’s now make sure everything is working properly. Start by connecting the Pickit3 to your con-
trol board. Make sure to connect the terminals of the Pickit3 and of the control board in the right
order! The small white triangles on the Pickit3 and on the control board indicate the location of pin number 1.
You can later connect the Pickit3 to your computer and we will configure your project to use it. In the top
left of your window, in the project section, right click on your project and, in the drop-down menu, select “Prop-
erties”. You can now select the Pickit3 tool in “Connected Hardware Tool” and push “Apply”. Do not close
the window yet. Instead, in the left menu, push the “Pickit 3” option that should have appeared. Your control
board can be powered with a power supply (for instance a 9 V battery), but it is also possible to power on the
PIC18F26K22 using directly the Pickit3. For this laboratory, we will choose this last option. Select “Power” in
“Option categories”, check “Power target circuit from PICKit3” and choose a voltage level of 5 V. In future lab-
oratories, make sure to uncheck the “Power target circuit from PICKit3” box when the control board is powered
by another device. Eventually, press “Apply” and “Ok”. You can now program the PIC18F26K22 by pressing the
green play button (“Run Main Project”) on top of the window. A caution message should pop up, make sure to
respect it before pressing OK.

At this point, your PIC18F26K22 should have been programmed successfully3 . However, nothing should happen
with your control board as your c file is doing nothing. You can copy and paste the code below and launch again
the programming. If everything works fine, 8 of the 10 LEDs of the LED bar will turn on.

# include < xc .h >


# include < stdint .h >
# pragma config FOSC = INTIO67

void main ( void ) {

TRISC = 0 x00 ; // RC registers ( pin 11 to 18) are outputs


ANSELC = 0 x00 ; // set RC outputs as digital
LATC = 0 xFF ; // set all RC outputs to 1
}

Hands-on

Programming a microcontroller is significantly different from high-level programming. You have to manually con-
figure peripherals and input/output (I/O) pins. Additionally, you need to handle low-level hardware interactions,
carefully manage memory, and ensure real-time responsiveness. The programming environment is often more con-
strained, with limited debugging tools and fewer built-in libraries. As a result, mastering the microcontroller’s
architecture is crucial, and the datasheet becomes your go-to guide for successful programming. You will find the
datasheet of the PIC18F26K22 on the class website. For the laboratory and for the project, you are not required
to read the whole datasheet. You should rather understand how information is given, and be able to pick the
information you are looking for efficiently.

The rest of this document is decomposed in three parts, designed to gradually improve your understanding. At
first, you will configure the PIC18F26K22 clock and a timer to make a LED blinks at 1 Hz. Later, you will configure
the ADC module to read an analog voltage. Finally, from the ADC measurement, you will control the duty cycle
of a PWM signal, so as to control the brightness of a LED.
3 If the programming failed, a message should appear in red in the bottom of your window. You may get a message similar to

“PICkit3 is trying to supply 5 volts from the USB port, but the target VDD is measured to be ... volts.”. You can simply try again to
program the PIC18F26K22 or, if the problem remains, update the voltage level of the Pickit3 in the properties of your project. It may
also be helpful to disconnect the Pickit3 from your computer, and to connect it again.

Lab 1 - page 3
1. Clock and Timer

The initial step in using the PIC18F26K22 is to choose for its clock. You can use an internal oscillator or opt for
different external oscillators like quartz crystal resonators, resistor-capacitor circuits, etc. In this lab and for the
project, we will use a 64 MHz clock, from the internal oscillator block. To select it, you need to write the SCS and
IRCF bits of the OSCCON register, as well as the PLLEN bit of the OSCTUNE register. What should be their values?

The PWM signal that will be sent to the power board of your DC-DC converter operates at a switching frequency
much lower than the clock frequency, typically a few tens of kHz. A first step to generate this PWM signal is to
use a timer module. What should you do to configure the Timer2 module to work at 20 kHz?

The Timer2 module can also generate interrupts at the selected frequency (20 kHz). Whenever an interrupt is
issued, the program momentarily pauses its current operations to execute the designated interrupt service routine.
In the hereunder code (“blink.c” available on the class website), an interrupt is used to generate a square signal on
pin 24, which is connected to the PWM LED of your control board. Read, understand and complete this code so
that the LED blinks at a frequency of 1 Hz. Eventually, load it into your PIC18F26K22 to check your developments.

1 # include < xc .h >


Comments on the code:
2 # include < stdint .h >
3 # pragma config FOSC = INTIO67 , LVP = OFF
Lines 1 to 4 are basic includes and configurations, 4 # pragma config DEBUG = OFF , WDTEN = OFF
lines 6 to 17 define the interrupt routine, and 5
lines 19 to 42 define the main block. 6 unsigned int counter = 1;
7 unsigned int counter_max = ?;
Starting with the main block, lines 22 to 24 write 8 void __interrupt () isr ( void ) {
the IRCF, PLLEN and SCS bits which configure the 9 if ( PIR1bits . TMR2IF ) { // Timer2 interrupt
clock source and frequencya . Similarly, lines 27 10 PIR1bits . TMR2IF = 0;
to 30 configure the Timer2 module. On the other 11 if ( counter >= counter_max ) {
hand, line 33 and 34 activate the use of interrupts 12 counter = 1;
13 LATB3 = ! LATB3 ; // Switches RB3 status
and line 35 specifically enables the interrupts
14 }
from the Timer2 module. The PWM LED will 15 else counter += 1;
be used as output and the corresponding register 16 }
(RB3) has to be configured so. Henceforth, the 17 }
instruction “TRISB3=0” set RB3 as an output 18

and “ANSB3=0” configures it as digital. Running 19 void main ( void ) {


the instruction “LATB3 = 1” will turn on the 20

PWM LED while running the instruction “LATB3 21 // Clock config


= 0” will turn it off. At line 41, we eventually 22 OSCCONbits . IRCF = 0 b ???;
enter a infinite loop, which will only be broken 23 OSCTUNEbits . PLLEN =0 b ?;
when an interrupt occurs. 24 OSCCONbits . SCS = 0 b ??;
25

26 // Config Timer2 resource


When the Timer2 module issues an interrupt, 27 T2CONbits . T2OUTPS = 0 b ????;
the program execution jumps to the interrupt 28 T2CONbits . T2CKPS = 0 b ??;
function and pass the condition on line 9. At 29 PR2 = ?;
first, the interrupt flag is cleared (line 10). We 30 T2CONbits . TMR2ON = 0 b ?;
then proceed to the comparison of two unsigned 31

integers, counter and counter max, defined 32 // Enable interrupts


at lines 6 and 7. Whenever counter reaches 33 INTCONbits . PEIE =1; // Peripheral interrupts
counter max, counter is reinitialized and the 34 INTCONbits . GIE =1; // Global interrupts
state of RB3 is flipped so that the PWM LED 35 PIE1bits . TMR2IE =1;
36
blinks, otherwise counter is incremented.
37 // Config RB3 ( pin 24 -> PWM LED )
38 TRISB3 = 0; // RB3 set as output
a Note that the prefix “0b” is used to indicate that the
39 ANSB3 = 0; // RB3 set as digital
number is binary. To write a number in hexadecimal, the 40
prefix “0x” should be used instead. Finally, in decimal,
41 while (1) {}
no prefix is used at all.
42 }

Lab 1 - page 4
2. Analog to Digital Converter

An Analog-to-Digital Converter (ADC) is a device that converts analog signals, like voltage levels, into digital values
that can be read and processed by digital systems. The PIC18F26K22 is equipped with one such ADC module,
converting analog signals to a 10-bit binary representation of it. This tool will be of prime importance for your
project, as you will use it to read the input and output voltages of your converter.

For the laboratory purpose, a potentiometer has been added on your control board. By turning the screw on top
of it, you can vary the voltage applied on pin 7 (Register A5) from 0 V (Vss ) to 5 V (VDD ). Carefully read the
ADC part of the datasheet and modify the previous code to read the voltage on pin 7 and display the digital result
on the bar of 10 LEDs. Launch an ADC conversion at each Timer2 interrupt, update the LED bar status at each
ADC interrupt, and consider an acquisition time of 8 TAD .

Note 1: You will find a pin summary on page 6 in the PIC18F26K22 datasheet. It specifies which analog
channel is connected to pin 7.
Note 2: Section 17.2.10 provides a step-by-step procedure to use the ADC.
Note 3: Rather than to configure one pin at a time as in lines 38 and 39 on the previous code, you can
write a whole byte. For instance, running the instructions “TRISC = 0x00;” and “ANSELC =
0x00;” will configure all the “C” registers as digital outputs.
Note 4: The instruction “val << i” shifts the bits of val by i position(s) to the left and inserts zero(s)
at the i least significant bit(s). Similarly, “val >> i” shifts the bits by i position(s) to the
right and inserts zero(s) at the i most significant bit(s).

3. Pulse Width Modulation

The PIC18F26K22 also offers modules to generate Pulse-Width-Modulated (PWM) signals. In your project, you
will use such a PWM signal to control the switch of your converter. To get used with it, modify your previous code
to send a PWM signal on pin 24 (RB3) so to control the brightness of the PWM LED. Use a switching frequency
of 20 kHz, update the duty-cycle value after each ADC conversion, and make it vary linearly with the voltage
measured from the potentiometer. Make sure the PWM LED is“fully on” only when the ADC output is maximum.

Note 1: The RB3 register is associated with the Enhanced Capture/Compare/PWM module “CCP2”.
This module has an half-bridge mode which can generate two complementary PWM signals on
two different ports, noted P2A and P2B. By default, P2A links to RC1 and P2B links to RB5.
By adding the instruction “#pragma config CCP2MX=PORTB3” at the beginning of your code,
you can make P2A link to RB3. Note that you should configure the CCP2 module to operate
in single output mode, to drive only the P2A port.
Note 2: To configure the PWM module, make sure to follow the steps presented in Section 14.4.8 of the
datasheet. Note that there is a slight mistake at the last point of step 6, you should read “Set
the CCP1ASE bit of the ECCP1AS register.”.
Note 3: You can use an unsigned integer “v” to store the voltage read by the ADC using the instruction
“v = (unsigned int)((ADRESH << 2) + (ADRESL >> 6))” if the ADC result is left justified,
“v = (unsigned int)((ADRESH << 8) + ADRESL)” if it is right justified. The value of v will
then vary between 0 and 210 − 1.
Note 4: The duty cycle value can also be represented using an unsigned integer, denoted as D. To convert
the value of v to D, a scaling factor is necessary when the maximum values of these quantities
are not represented by the same unsigned integers. For instance, if the maximum value of v
is 4 and the maximum value of D is 5, you may consider doing “D=v*5/4”. Microcontrollers
are however bad at handling divisions. A better way to handle the scaling factor is to use the
instruction“D=(5*v)>>2”, as shifting by 2 bits to the right indeed gives the floored value of a
division by 4. You should however make sure that “(5*v)” do not overpass the maximum value
of an unsigned int (215 − 1), it would result in an integer overflow.

Lab 1 - page 5
The MPLAB X IDE offers a debugging tool particularly useful for troubleshooting your code. It notably allows you
to visualize in real-time the values of your variables and registers. To use this tool, change the beginning of your
code to set the “DEBUG” configuration to “ON” (do not forget to put it back to “OFF” once you are over using
it) and press “Debug Main Project” on the top menu. Within a few seconds, this will program your PIC18F26K22
and launch it in debug mode.

At any time, you can pause the execution of your code by pressing the orange pause button, and visualize the values
of your variables in the “Variables” toolbar, down on your screen. You can notably check and display in binary
the voltage read by the ADC. If everything works properly, its ten least significant bits should correspond to the
display of the LED bar. To launch the program execution again, press the green “Continue” button.

You can also define some breakpoints, which allow you to pause the execution of your program when it reaches a
specific line of your code. To define such a breakpoint, simply push on the line number you want your program to
pause on. However, prevent using breakpoints in interrupt routines, as their behavior may vary while debugging.

Deliverables and Scoring Guidelines

For this laboratory, you are asked to send to Florent Purnode the c code you obtained for the PWM part, and
you are asked to write a very brief report (maximum one page) detailing the resolution of the PWM signal. In
particular, you should propose a graph that clearly illustrates the relationship between the analog voltage of the
potentiometer, the ADC digital output and the output duty cycle value. Ensure that your graph highlights the
discrete steps of the duty cycle value and demonstrates the impact of applying a formula like “D=(5*v)>>2”. Include
annotations or descriptions to explain your graph. The hereunder scoring board will be used to grade your work.

Topic Weight Score Criterion

The code solves the problem efficiently with no errors. It is easy to read, follows best practices, and includes clear and helpful comments
3
when needed.

Functionality The code solves the problem accurately with no major errors. There are some redundant lines or areas that lack helpful comments, but the
2 logic remains clear overall.
and style of 30 %
the code The code solves the problem but could be optimized. It contains minor errors or unclear/missing comments, making parts of the code harder
1
to understand.
0 The code fails to solve the problem.

1 The clock operates at 64 MHz.


Right FOSC 5%
0 The clock does not operate at 64 MHz.

1 The switching frequency is 20 kHz.


Right 𝑓𝑠 5%
0 The switching frequency is not 20 kHz.

1 An ADC conversion is launched at each Timer2 interrupt and the duty cycle value is updated as soon as the ADC conversion finishes.
Interrupts 10 %
0 Wrong timing between events.
Damage to the 1 The PWM module is configured following instructions of Section 14.4.8, which ensure no damage to the application circuits.
application cir- 10 %
cuit 0 Wrong set of instructions when configuring the PWM module, which could result in damage to the application circuit.

2 Demonstrates a strong understanding of PWM signal generation and control.


PWM under- Demonstrates a basic understanding, with some significant errors or misunderstandings that affect the interpretation or application of PWM
standing 15 % 1
control.
0 Lacks understanding of PWM principles, leading to incorrect or incoherent results.
The graph is clear, well-labeled, and accurately reflects the data. Annotations or descriptions effectively explain the discrete steps and their
2 implications.
Graph Quality
and Clarity 15 % The graph is mostly clear and labeled correctly, with some issues in representation or explanation.
1
0 The graph is missing, unclear, or does not adequately represent the data or task requirements.
The student’s work fully meets all the laboratory specifications and requirements. The code and report adhere strictly to the guidelines
2 provided
Compliance
with report 10 %
1 The student’s work meets most of the laboratory specifications but contains minor deviations or omissions.
Specifications
0 The student’s work does not comply with the laboratory specifications. This includes a wrong number of pages for the report.

Lab 1 - page 6

You might also like