Experiment No 4
Experiment No 4
AIM
Design and set up a microcontroller based 50Hz sine PWM half-bridge inverter to power a resistive load of
33 ohm. The switching frequency is to be 20kHz. The DC supply voltage is 200V. Also design a low pass
LC filter with a corner frequency of 1kHz to filter the harmonics, ensuring that the voltage drop across the
inductor does not exceed 10% of the inverter output voltage. Plot relevant waveforms.
SOFTWARE USED
DESIGN
Bit2 is set to be 1 to turn on the TIMER2 and bit 1-0 will determine the prescale value.
We need a prescale value as 1. Hence bit1-0 =00
T2CONbits. TMR2ON=1;
T2CONbits. T2CKPSX=00;
We don’t need capture and compare module. We need the PWM module only.
CCPxX, CCPxY are bits used to get 10 bit resolution. But here we use only 8 bit resolution. Hence these
2 bits (bit5-4) will be 0.
CCP1CONbits. CCP1X=0;
CCP1CONbits. CCP1Y=0;
CCP1CONbits. CCP1M=0b1111;
ADDRESS 12h
bit 6-3 TOUTPS3:TOUTPS0: Timer2 Output Postscale Select bits 0000 = 1:1 postscale
0001 = 1:2 postscale
0010 = 1:3 postscale
•
1111 = 1:16 postscale
1 = Timer2 is on
0 = Timer2 is off
Prescaler is 1
01 = Prescaler is 4
1x = Prescaler is 16
ADDRESS 17h/1Dh
PWM mode: These bits are the two LSbs of the PWM duty cycle. The eight MSbs are found in CCPRxL.
bit 3-0 CCPxM3:CCPxM0: CCPx Mode Select bits
▪ 0000 = Capture/Compare/PWM disabled (resets CCPx module) ▪ 0100 =
Capture mode, every falling edge
▪ 0101 = Capture mode, every rising edge
▪ 0110 = Capture mode, every 4th rising edge
▪ 0111 = Capture mode, every 16th rising edge
▪ 1000 = Compare mode, set output on match (CCPxIF bit is set) ▪ 1001 = Compare
mode, clear output on match (CCPxIF bit is set) ▪ 1010 = Compare mode, generate
software interrupt on match (CCPxIF bit is set, CCPx pin is unaffected)
▪ 1011 = Compare mode, trigger special event (CCPxIF bit is set, CCPx pin is
unaffected); CCP1 resets TMR1; CCP2 resets TMR1 and starts an A/D conversion
(if A/D module is enabled)
▪ 11xx = PWM mode
CAPTURE/COMPARE/PWM MODULES
Each Capture/Compare/PWM (CCP) module contains a 16-bit register which can operate as
a:
Both the CCP1 and CCP2 modules are identical in operation, with the exception being the
operation of the special event trigger. Table 8-1 and Table 8-2 show the resources and
interactions of the CCP module(s). In the following sections, the operation of a CCP module is
described with respect to CCP1. CCP2 operates the same as CCP1 except where noted.
CCP1 Module: Capture/Compare/PWM Register 1 (CCPR1) is comprised of two 8-bit
registers: CCPR1L (low byte) and CCPR1H (high byte). The CCP1CON register controls the
operation of CCP1. The special event trigger is generated by a compare match and will reset
Timer1.
PWM OPERATION
▪ In Pulse Width Modulation mode, the CCPx pin produces up to a 10-bit resolution
PWM output. Since the CCP1 pin is multiplexed with the PORTC data latch, the
TRISC bit must be cleared to make the CCP1 pin an output.
▪ Clearing the CCP1CON register will force the CCP1 PWM output latch to the default
low level. This is not the PORTC I/O data latch.
PWM OUTPUT
A PWM output has a time base (period) and a time that the output stays high (duty cycle).
The frequency of the PWM is the inverse of the period (1/period).
▪ The PWM period is specified by writing to the PR2 register. ▪ The PWM period can be
calculated using the following formula: PWM Period = [(PR2) + 1] • 4 • TOSC •
(TMR2 Prescale Value) ▪ PWM frequency is defined as 1/[PWM period].
▪ When TMR2 is equal to PR2, the following three events occur on the next increment
cycle:
• TMR2 is cleared
• The CCP1 pin is set (exception: if PWM duty cycle = 0%, the CCP1 pin will not be
set)
• The PWM duty cycle is latched from CCPR1L into
INTERRUPT REGISTERS
• To enable interrupt PIE1 (Peripheral Interrupt enable) register is used. TMR2IE bit of PIE1 register
will be 1 to enable interrupt.
• PIR1 is the peripheral interrupt flag register. When TMR2 overflows TMR2IF bit of PIR1 become 1
• General interrupt and peripheral interrupt enable is done by making PEIE=1 & GIE=1 • PEIE, GIE
are the bits of interrupt control register INTCON
MATRIX GENERATION
20000
Total no. of pulses per cycle of sine wave = = 400 50
Following C program is used to generate the duty ratio of these 400 pulses.
#include <stdio.h>
#include <math.h>
#include <conio.h>
#define PI 3.141592653589793
int main()
int period=1000;
int OnTimer=500;
int Nsamples=100;
int n=1;
double duty=0.5;
printf("####################################################
########\n");
printf("## ##\n");
printf("## ##\n");
printf("####################################################
########\n\n");
printf("Enter the modulation frequency : ");
scanf("%f",&modFreq);
printf("Enter the carrier frequency : ");
scanf("%f",&carrierFreq);
scanf("%f",&modIndex);
FILE* file;
file = fopen("matrix.c","w");
printf("%d };",OnTimer);
fprintf(file,"%d };",OnTimer);
}
else
printf("%d ,",OnTimer);
fprintf(file,"%d ,",OnTimer);
}
if(n%10 == 0)
printf("\n\t");
fprintf(file,"\n\t");
getch();
fclose(file);
return 0;
7 ,7 ,7 ,6 ,6 ,6 ,6 ,6 ,6 ,6 ,
6 ,6 ,6 ,6 ,6 ,6 ,6 ,7 ,7 ,7 ,
PROGRAM
Main File :
#include "configuration.h"
#include <xc.h>
#include "matrix.c"
if(PIR1bits.TMR2IF &&
PIE1bits.TMR2IE) {
PIR1bits.TMR2IF = 0; //timer 2 interrupt cleared
buffer = lookup[n++];
if(n>=LKUP_SIZE) n=0;
return;
void main()
CCPR1L = 0;
CCP1CONbits.CCP1X = 0; // LSB = 0
CCP1CONbits.CCP1Y = 0; // LSB = 0
while(1)
CCPR1L = buffer;
}
Header File:
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit
(RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code
protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all
program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#include <xc.h>
OUTPUT
RESULT
A microcontroller based 50Hz sine PWM half bridge inverter is designed and simulated using Proteus.
Relevant waveforms are plotted.
Submitted by:
Amala M
S2 M.Tech
Roll No:01