djc
Advanced Member level 1
- Joined
- Jan 27, 2013
- Messages
- 402
- Helped
- 3
- Reputation
- 6
- Reaction score
- 2
- Trophy points
- 1,298
- Location
- India
- Activity points
- 4,554
Hi all,
Here is my code for sine wave generation using PWM. Controller is PIC16F72, mikroc and crystal is 8MHz.
At the output i am using 1kOhm and 102 to filter. However i am not getting the sine wave. Can anybody please guide me as to where am i going wrong.
- - - Updated - - -
If i use PWM frequency as 5K instead of 20k, i get atlest some part of sine wave, means half of the positive half cycle. For 20Khz, wave shape is like saw tooth.
For 5K PWM frequency,wave goes up to half of the positive sine wave then comes back..and it goes on.I am not getting complete sine wave atlest for positive half cycle.
Here is my code for sine wave generation using PWM. Controller is PIC16F72, mikroc and crystal is 8MHz.
Code:
static unsigned char i = 0,flag=1;
//=============== SINE WAVE LOOK UP TABLE ================//
const unsigned char sine[50] ={ 52,57,62,66,70,74,77,80,82,84,85,86,86, 86,85,83,81,78,75,72,69,65,61,56,52, 48,44,39,35,31,28,25,22,19,17,15,14,14, 14,15,16,18,20,23,26,30,34,38,43,48};
///=============== SINE WAVE LOOK UP TABLE ================//
void Port_setting(){
TRISC = 0x00;
PORTC = 0x00;
}
void Timer_setting(){
//INTCON =0b11100000 ; /*//0xc0;0xa0*/
TMR0 = 100; /*//Stop timer 0 during set up*/
INTCON.GIE = 1;
INTCON.PEIE = 1;
INTCON.TMR0IE = 0;
T0CS_bit = 0; /*// use internal clock to trigger timer to count*/
PSA_bit = 0; /*// Use the prescaler to slow the timer down*/
/*// prescaler
// 111 = 1:256 Prescale value
// 110 = 1:128 Prescale value
// 101 = 1:64 Prescale value
// 100 = 1:32 Prescale value
// 011 = 1:16 Prescale value
// 010 = 1:8 Prescale value
// 001 = 1:4 Prescale value
// 000 = 1:2 Prescale value*/
PS0_bit = 1; //0 /*//for 16 bit timer 1/4 of clock freq 001*/
PS1_bit = 1; //1
PS2_bit = 1; //0
}
void interrupt(){
if(INTCON.TMR0IF){
PORTC.B3 = ~PORTC.B3;
if(flag==1){
i++;
PWM1_Set_Duty(sine[i]);
if ( i == 49 ) flag = 0;
}
if(flag==0){
i--;
PWM1_Set_Duty(sine[i]);
if ( i == 0 ) flag = 1;
}
TMR0 = 100; //Timer set for 20ms interrupt
INTCON.TMR0IF = 0;
}
}
void main() {
Port_setting();
Timer_setting();
PWM1_Init(20000); //PWM at 20KHz
PWM1_Start();
INTCON.TMR0IE = 1;
while(1)
{
}
}
- - - Updated - - -
If i use PWM frequency as 5K instead of 20k, i get atlest some part of sine wave, means half of the positive half cycle. For 20Khz, wave shape is like saw tooth.
For 5K PWM frequency,wave goes up to half of the positive sine wave then comes back..and it goes on.I am not getting complete sine wave atlest for positive half cycle.