# pic18f4431continues pwm generation without delays.

Status
Not open for further replies.

#### bilal_sheikh

##### Junior Member level 1
hello all, recently i am working on a project that is variable frequency drive. i have to control the speed of a single phase AC induction motor. inverter design which i am following in a full H-bridge circuit containing power mosfets. now i have to generate variable frequency PWM signles using MCU, for that purpose i am using pic18f4431. here is the result of simulation which i am also getting on hardware.

now, have a look at the 10ms delay, during which both the channels are off (which i cant bear when actually driving a motor). the 10ms delay is due to the fact that i am checking adc value every now and than to varry the frequency, if the user wants.
any solutions ? so that i am unable to check the adc value as well as both the pwm channels operate without delay (continuesly) . comments are appreciated

#### ernpao

Even if you were polling the adc channels, why would the pwm channels turn off? Are you disabling them? If it was me, I would use a timer interrupt to periodically reconfigure the pwm module and sample the adc in the main loop. Post your code here so others will have a better idea of what is causing the 10ms delay.

#### bilal_sheikh

##### Junior Member level 1
ok sir, i will post the code after inserting comments in the code so that others can understand.

- - - Updated - - -

sir, i am trying to obtain sine pwm using sine look up table. for each and every frequency, sine look up table is generated during runtime. my code is given. i have checked it on hardware, its not continuously running.

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include<p18f4431.h>
#include <math.h>
#pragma config LVP =OFF
#pragma config WDTEN = OFF
#pragma config OSC=HS
#include <math.h>
unsigned char p=0;
unsigned char q=0;
unsigned char r=0;
unsigned char x=0;
float f1=96.66;
unsigned char sinee [64];
float factor;
float frequency,val,pr2;
unsigned char sine [64]={0, 4, 8, 13, 17, 21, 25, 29, 33, 37, 41, 44, 48, 51, 55, 58, 61, 64, 66, 69, 72, 74, 76, 78, 79, 81, 82, 83, 84, 85, 86, 86, 86, 86, 86, 85, 84, 83, 82, 81, 79, 78, 76, 74, 72, 69, 66, 64, 61, 58, 55, 51, 48, 44, 41, 37, 33, 29, 25, 21, 17, 13, 8, 4};
float factor;
{
unsigned char t=0;
while(t<d)
{
t=t+1;
}
}
void pwm (void)
{
for( p=0; p<64; p=p+1) //loop for implementing look-up table, writing duty cycle value to CCPR1L register which is copied in CCPR1H on each interupt
//i.e when a match between TMR2 & PR2 occura
{
CCPR1L=sinee[p];
PIR1bits.TMR2IF=0;
while(PIR1bits.TMR2IF==0); //when interupt occurs TMR2IF bit sets to 1, showing that inerupt has occured
}
//CCPR1L=0; //after the above loop executed 64 times i.e for 10ms, square waves are inverted

for( q=0; q<64; q=q+1) //loop for implementing look-up table, writing duty cycle value to CCPR1L register which is copied in CCPR1H on each interupt
{ //i.e when a match between TMR2 & PR2 occur
CCPR2L=sinee[q];
PIR1bits.TMR2IF=0;
while( PIR1bits.TMR2IF==0);
}

}
void setup(void) //function for adc setup
{

//ADCON0bits.ACMOD0=0; //contineous mode single channel mode
ANSEL0=1;

TRISC=0x00; //pwm setup
CCP1CON=0x00;
CCP2CON=0x00;
CCPR1L=0;
// CCPR2L=0; /
CCP1CON=0xC;
CCP2CON=0xC;
T2CON=6;
}
{

while(ADCON0bits.GO); //wait for the conversion to finish

pr2 =( 2456 *(pow(frequency,-1.002)))-0.888; // period register calculation formula

factor=pr2/f1; //contant factor that should be multiplied with basic sine lookup table to get the table for desired frequency
PR2=pr2;
for (r=0;r<64;r=r+1)
{
sinee[r]=factor*sine[r];

}
return PR2;
}

void main()
{
setup();
pwm();

}

kindly help if u can. thanks

Last edited by a moderator:

#### ernpao

While you may be using the overflow flag for the timer, it doesn't seem like you are using ISRs and the interrupt vector. I suggest you read up on those and use them. Your mcu seems to have deadtime features and is geared for motor control (perfect for your h bridge). You shouldn't have to write code for deadtime (but looking at you code, are you only using deadtime as a delay function?). Btw, how are you polling your adc pin when it seems like you don't have an infinite loop in your main() function? Those are some things I'd like to point out.

#### abc_de

##### Full Member level 4
Your motor is running without any problem

Status
Not open for further replies.

Replies
4
Views
1K
Replies
9
Views
6K
Replies
8
Views
7K
Replies
14
Views
6K
Replies
6
Views
2K