Sinewave Inverter When adding ADC read in program lowers the switching frequecy

Status
Not open for further replies.

lalgpt

Junior Member level 3
Joined
Jan 25, 2012
Messages
30
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Location
India
Activity points
1,474
Hi all


this the program that i have tried to generate sine wave PWM but has a problem when without adding the ADC
it works fine but when adding ADC read it lowers the switching frequency

const unsigned char sinetable[123]={0,6,13,19,25,32,38,44,51,57,63,69,75,81,88,93,99,105,111,117,122,128,
133,139,144,149,154,159,164,169,173,178,182,187,191,195,199,203,206,210,
213,217,220,223,225,228,231,233,235,237,239,241,243,244,245,247,248,248,
249,249,250,250,250,250,249,249,248,248,247,245,244,243,241,239,237,235,
233,231,228,225,223,220,217,213,210,206,203,199,195,191,187,182,178,173,
169,164,159,154,149,144,139,133,128,122,117,111,105,99,93,88,81,75,69,63,
57,51,44,38,32,25,19,13,6};
int i=0;

void main(void)
{
PORTB = 0;
TRISC=0xFB;
TRISB = 0;
LATC=0;
CCP1CON=0x0C;
PR2=249;
T2CON=0x00;
while(1)
{
i=0;
for(i=0; i <123; i++)
{
CCPR1L= sinetable;
TMR2=0x0;
PIR1.TMR2IF=0;
T2CON.TMR2ON=1;
while(PIR1.TMR2IF ==0);
}
PORTB.F1 = !PORTB.F1 ;//RB1
voltage = ADC_read(0); // when adding this switching frequency reduces
}
}
 

Read from ADC module takes a long time if you compare with the processor speed. Try to use the ADC interruption to avoid this problem.
 

Please help me

i am very weak in c programming......................................................

I am Using PIC18F452 with 20MHz crystal
 

Instead of this:
Code:
i=0;
for(i=0; i <123; i++)
{
CCPR1L= sinetable[i];
TMR2=0x0;
PIR1.TMR2IF=0;
T2CON.TMR2ON=1;
while(PIR1.TMR2IF ==0);
}
PORTB.F1 = !PORTB.F1 ;//RB1
voltage = ADC_read(0); // when adding this switching frequency reduces
}

try this:
Code:
i=0;
ADCON0 = 0x01 + (0 << 2);   // set the a/d converter to sample channel 0  
ADGO = 1;      // start the a/d converter
for(i=0; i <123; i++)
{
CCPR1L= sinetable[i];
TMR2=0x0;
PIR1.TMR2IF=0;
T2CON.TMR2ON=1;
while(PIR1.TMR2IF ==0)
if (!ADGO)   // is a conversion ready?
{
voltage = (((uint16)ADRESH)<<8)+ADRESL;  // get the 16 bit a/d value
ADGO = 1;    // restart the a/d converter to get another conversion
}
; // end of while(PIR1.TMR2IF ==0)
PORTB.F1 = !PORTB.F1 ;//RB1
}
 

Thank you steveelliott, i will try this. after that I will be back here.

one more problem is there with this program

while changing the above sine table in to float sintable [10]= {0.1,0.2,0.3,..............} etc it aslo reducing the switching frequency
 

Try don't use float math with this microcontroller. It takes a lot of processing (the PIC18 architeture has only 8 bits integer multiplication, addition and subtraction).
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…