Ehab Abdalla
Newbie level 6
I'm using PIC18F4431 to generate 6 PWM inorder to control the speed of a three phase induction motor.
I'm also using IR2112 for gate driving of MOSFET (IRFP460).
The problem is that the motor dosen't rotate at all and I can't figure out why
Here's the MikroC Prog. and the schematic
Hope anyone can help...
#define up porte.f0
#define down porte.f1
#define rev porte.f2
#define startstop portd.f0
#define onoff portd.f2
#define Lo(param) ((char *)¶m)[0]
#define Hi(param) ((char *)¶m)[1]
unsigned char mask(unsigned char num);
unsigned int sintable[64]={437 , 474 , 510, 545, 579 , 611, 641 , 669 , 694, 716, 735, 751 , 764 , 773 , 778,780 , 778 , 773 , 764, 751 , 735 , 716 , 694 , 669, 641 , 611 , 579 , 545 , 510, 474, 437 ,400, 363 ,326 , 290, 255 , 221 , 189, 159 , 131, 106 , 84 , 65 , 49 , 36, 27, 22 , 20 , 22 ,27, 36 , 49 , 65, 84 , 106, 131 , 159 , 189 , 221 , 255 ,290 , 326 , 363 , 400 };
unsigned int sintable_new[64];
unsigned int dutycycle;
unsigned int xx=0,yy=0,zz=0,setfreq=0;
unsigned char x=1,freq,i,frequency,digit,digit10, digit1;
float nn,mi, mm=15.384615e-4;
void interrupt()
{
if (ptif_bit==1)
{
yy=xx+setfreq;
zz=yy>>10;
xx=yy;
PDC0L=Lo(sintable_new[zz]);
PDC0H=Hi(sintable_new[zz]);
if(rev==0)
{
if (zz>20)
{
PDC1L=Lo(sintable_new[zz-21]);
PDC1H=Hi(sintable_new[zz-21]);
}
else
{
PDC1L=Lo(sintable_new[zz+43]);
PDC1H=Hi(sintable_new[zz+43]);
}
if (zz>42)
{
PDC2L=Lo(sintable_new[zz-43]);
PDC2H=Hi(sintable_new[zz-43]);
}
else
{
PDC2L=Lo(sintable_new[zz+21]);
PDC2H=Hi(sintable_new[zz+21]);
}
}
if(rev==1)
{
if (zz>20)
{
PDC2L=Lo(sintable_new[zz-21]);
PDC2H=Hi(sintable_new[zz-21]);
}
else
{
PDC2L=Lo(sintable_new[zz+43]);
PDC2H=Hi(sintable_new[zz+43]);
}
if (zz>42)
{
PDC1L=Lo(sintable_new[zz-43]);
PDC1H=Hi(sintable_new[zz-43]);
}
else
{
PDC1L=Lo(sintable_new[zz+21]);
PDC1H=Hi(sintable_new[zz+21]);
}
}
ptif_bit=0;
}
}
void main()
{
PWMCON0=0 ;
PWMCON1=1; //PWM timer sync configuration
freq=50;
setfreq=13*freq;
GIE_BIT=1;
PEIE_BIT=1;
PTIE_BIT=1;
trisc=0;
trisb=0;
trisa=0;
TRISD.F0=1;
TRISD.F1=0;
PORTD.F1=0;
TRISD.F2=1;
trise=0b00001111;
PORTB=0;
porta=0;
ANSEL0=0;
ANSEL1=0;
frequency= setfreq/13;
digit = frequency % 10;
digit1 = mask(digit); // Prepare mask for displaying ones
digit = frequency/10;
digit10 = mask(digit); // Prepare mask for displaying tens
PORTA=digit10;
RB6_BIT=digit10.f6;
portc.f0=digit1.f0;
portc.f1=digit1.f1;
portc.f2=digit1.f2;
portc.f3=digit1.f3;
portc.f4=digit1.f4;
portc.f5=digit1.f5;
portc.f6=digit1.f6;
if(setfreq<650)
{
i=0;
mi=mm*setfreq;
nn=400-400*mi;
for(i=0;i<64;i++)
{
sintable_new=mi*sintable+nn ;
}
i=0;
}
else
{
for (i=0;i<64;i++)
{
sintable_new=sintable;
}
i=0;
}
PTPERL=199; //16 MHZ OSCILLATOR PTPER=16 MHZ/(4*FPWM*TMRPS) -1, FPWM=5 KHZ
PTPERH=0;
PTCON0=0b00000100;
PTIP_BIT=1;
DTCON=0b11000011;
dutycycle=400;
PDC0L=Lo(dutycycle);
PDC0H=Hi(dutycycle);
PTCON1=0b10000000;
while(1)
{
if (startstop==1)
{
PWMCON0=0; //onll pwm0 ,pwm1,2,3,4,5 disabled
x=1;
}
if (startstop==0)
{
if(x==1)
{
PWMCON0=0b01000000;
x=0;
}
}
if (onoff==0)
{
portd.f1=1; //supply relay energized
}
else
{
portd.f1=0; //supply relay off
}
if (up==0)
{
if (setfreq < 1300)
{
setfreq=setfreq+13;
frequency= setfreq/13;
digit = frequency % 10;
digit1 = mask(digit); // Prepare mask for displaying ones
digit = frequency/10;
digit10 = mask(digit); // Prepare mask for displaying tens
PORTA=digit10;
RB6_BIT=digit10.f6;
portc.f0=digit1.f0;
portc.f1=digit1.f1;
portc.f2=digit1.f2;
portc.f3=digit1.f3;
portc.f4=digit1.f4;
portc.f5=digit1.f5;
portc.f6=digit1.f6;
delay_ms(300);
if(setfreq<650)
{
i=0;
mi=mm*setfreq;
nn=400-400*mi;
for(i=0;i<64;i++)
{
sintable_new=mi*sintable+nn ;
delay_us(50);
}
i=0;
}
else
{
for (i=0;i<64;i++)
{
sintable_new=sintable;
delay_us(50);
}
i=0;
}
}
}
if (down==0)
{
if (setfreq > 26)
{
setfreq=setfreq-13;
frequency= setfreq/13;
digit = frequency % 10;
digit1 = mask(digit); // Prepare mask for displaying ones
digit = frequency/10;
digit10 = mask(digit); // Prepare mask for displaying tens
PORTA=digit10;
RB6_BIT=digit10.f6;
portc.f0=digit1.f0;
portc.f1=digit1.f1;
portc.f2=digit1.f2;
portc.f3=digit1.f3;
portc.f4=digit1.f4;
portc.f5=digit1.f5;
portc.f6=digit1.f6;
delay_ms(300);
if(setfreq<650)
{
i=0;
mi=mm*setfreq;
nn=400-400*mi;
for(i=0;i<64;i++)
{
sintable_new=mi*sintable+nn ;
delay_us(50);
}
i=0;
}
else
{
for (i=0;i<64;i++)
{
sintable_new=sintable;
delay_us(50);
}
i=0;
}
}
}
}
}
unsigned char mask(unsigned char num) {
switch (num) {
case 0 : return 0xC0;
case 1 : return 0xF9;
case 2 : return 0xA4;
case 3 : return 0xB0;
case 4 : return 0x99; //
case 5 : return 0x92;
case 6 : return 0x82;
case 7 : return 0xF8;
case 8 : return 0x80;
case 9 : return 0x90;
}
}
View attachment edaboard.jpg
I'm also using IR2112 for gate driving of MOSFET (IRFP460).
The problem is that the motor dosen't rotate at all and I can't figure out why
Here's the MikroC Prog. and the schematic
Hope anyone can help...
#define up porte.f0
#define down porte.f1
#define rev porte.f2
#define startstop portd.f0
#define onoff portd.f2
#define Lo(param) ((char *)¶m)[0]
#define Hi(param) ((char *)¶m)[1]
unsigned char mask(unsigned char num);
unsigned int sintable[64]={437 , 474 , 510, 545, 579 , 611, 641 , 669 , 694, 716, 735, 751 , 764 , 773 , 778,780 , 778 , 773 , 764, 751 , 735 , 716 , 694 , 669, 641 , 611 , 579 , 545 , 510, 474, 437 ,400, 363 ,326 , 290, 255 , 221 , 189, 159 , 131, 106 , 84 , 65 , 49 , 36, 27, 22 , 20 , 22 ,27, 36 , 49 , 65, 84 , 106, 131 , 159 , 189 , 221 , 255 ,290 , 326 , 363 , 400 };
unsigned int sintable_new[64];
unsigned int dutycycle;
unsigned int xx=0,yy=0,zz=0,setfreq=0;
unsigned char x=1,freq,i,frequency,digit,digit10, digit1;
float nn,mi, mm=15.384615e-4;
void interrupt()
{
if (ptif_bit==1)
{
yy=xx+setfreq;
zz=yy>>10;
xx=yy;
PDC0L=Lo(sintable_new[zz]);
PDC0H=Hi(sintable_new[zz]);
if(rev==0)
{
if (zz>20)
{
PDC1L=Lo(sintable_new[zz-21]);
PDC1H=Hi(sintable_new[zz-21]);
}
else
{
PDC1L=Lo(sintable_new[zz+43]);
PDC1H=Hi(sintable_new[zz+43]);
}
if (zz>42)
{
PDC2L=Lo(sintable_new[zz-43]);
PDC2H=Hi(sintable_new[zz-43]);
}
else
{
PDC2L=Lo(sintable_new[zz+21]);
PDC2H=Hi(sintable_new[zz+21]);
}
}
if(rev==1)
{
if (zz>20)
{
PDC2L=Lo(sintable_new[zz-21]);
PDC2H=Hi(sintable_new[zz-21]);
}
else
{
PDC2L=Lo(sintable_new[zz+43]);
PDC2H=Hi(sintable_new[zz+43]);
}
if (zz>42)
{
PDC1L=Lo(sintable_new[zz-43]);
PDC1H=Hi(sintable_new[zz-43]);
}
else
{
PDC1L=Lo(sintable_new[zz+21]);
PDC1H=Hi(sintable_new[zz+21]);
}
}
ptif_bit=0;
}
}
void main()
{
PWMCON0=0 ;
PWMCON1=1; //PWM timer sync configuration
freq=50;
setfreq=13*freq;
GIE_BIT=1;
PEIE_BIT=1;
PTIE_BIT=1;
trisc=0;
trisb=0;
trisa=0;
TRISD.F0=1;
TRISD.F1=0;
PORTD.F1=0;
TRISD.F2=1;
trise=0b00001111;
PORTB=0;
porta=0;
ANSEL0=0;
ANSEL1=0;
frequency= setfreq/13;
digit = frequency % 10;
digit1 = mask(digit); // Prepare mask for displaying ones
digit = frequency/10;
digit10 = mask(digit); // Prepare mask for displaying tens
PORTA=digit10;
RB6_BIT=digit10.f6;
portc.f0=digit1.f0;
portc.f1=digit1.f1;
portc.f2=digit1.f2;
portc.f3=digit1.f3;
portc.f4=digit1.f4;
portc.f5=digit1.f5;
portc.f6=digit1.f6;
if(setfreq<650)
{
i=0;
mi=mm*setfreq;
nn=400-400*mi;
for(i=0;i<64;i++)
{
sintable_new=mi*sintable+nn ;
}
i=0;
}
else
{
for (i=0;i<64;i++)
{
sintable_new=sintable;
}
i=0;
}
PTPERL=199; //16 MHZ OSCILLATOR PTPER=16 MHZ/(4*FPWM*TMRPS) -1, FPWM=5 KHZ
PTPERH=0;
PTCON0=0b00000100;
PTIP_BIT=1;
DTCON=0b11000011;
dutycycle=400;
PDC0L=Lo(dutycycle);
PDC0H=Hi(dutycycle);
PTCON1=0b10000000;
while(1)
{
if (startstop==1)
{
PWMCON0=0; //onll pwm0 ,pwm1,2,3,4,5 disabled
x=1;
}
if (startstop==0)
{
if(x==1)
{
PWMCON0=0b01000000;
x=0;
}
}
if (onoff==0)
{
portd.f1=1; //supply relay energized
}
else
{
portd.f1=0; //supply relay off
}
if (up==0)
{
if (setfreq < 1300)
{
setfreq=setfreq+13;
frequency= setfreq/13;
digit = frequency % 10;
digit1 = mask(digit); // Prepare mask for displaying ones
digit = frequency/10;
digit10 = mask(digit); // Prepare mask for displaying tens
PORTA=digit10;
RB6_BIT=digit10.f6;
portc.f0=digit1.f0;
portc.f1=digit1.f1;
portc.f2=digit1.f2;
portc.f3=digit1.f3;
portc.f4=digit1.f4;
portc.f5=digit1.f5;
portc.f6=digit1.f6;
delay_ms(300);
if(setfreq<650)
{
i=0;
mi=mm*setfreq;
nn=400-400*mi;
for(i=0;i<64;i++)
{
sintable_new=mi*sintable+nn ;
delay_us(50);
}
i=0;
}
else
{
for (i=0;i<64;i++)
{
sintable_new=sintable;
delay_us(50);
}
i=0;
}
}
}
if (down==0)
{
if (setfreq > 26)
{
setfreq=setfreq-13;
frequency= setfreq/13;
digit = frequency % 10;
digit1 = mask(digit); // Prepare mask for displaying ones
digit = frequency/10;
digit10 = mask(digit); // Prepare mask for displaying tens
PORTA=digit10;
RB6_BIT=digit10.f6;
portc.f0=digit1.f0;
portc.f1=digit1.f1;
portc.f2=digit1.f2;
portc.f3=digit1.f3;
portc.f4=digit1.f4;
portc.f5=digit1.f5;
portc.f6=digit1.f6;
delay_ms(300);
if(setfreq<650)
{
i=0;
mi=mm*setfreq;
nn=400-400*mi;
for(i=0;i<64;i++)
{
sintable_new=mi*sintable+nn ;
delay_us(50);
}
i=0;
}
else
{
for (i=0;i<64;i++)
{
sintable_new=sintable;
delay_us(50);
}
i=0;
}
}
}
}
}
unsigned char mask(unsigned char num) {
switch (num) {
case 0 : return 0xC0;
case 1 : return 0xF9;
case 2 : return 0xA4;
case 3 : return 0xB0;
case 4 : return 0x99; //
case 5 : return 0x92;
case 6 : return 0x82;
case 7 : return 0xF8;
case 8 : return 0x80;
case 9 : return 0x90;
}
}
View attachment edaboard.jpg