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)
{
}
}
102 means 1 nF? 1k*1nF = 1µs time constant = low pass cut off frequency 160 kHz. How should it filter 20 kHz pwm?At the output i am using 1kOhm and 102 to filter.
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;
i++;
PWM1_Set_Duty(sine[i]);
if ( i == 49 ) i = 0;
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)
{
}
Yes and no. Yes, because a first order filter (simple RC) will always produce an exponential waveform, not a sine. You'll need a higher order, preferably LC or active RC filter for a steeper characteristic that only passes the fundamental wave. No because a first order filter with a suitable cut-off frequency, e.g. 1-2 kHz, will give you a waveform nearer to a sine wave.If i use higher values of capacitor, wave shape will change, it may be sawtooth but never be sine.
For a frequency of 50Hz, one complete cycle will take 20mS. During this 20mS your sine value should be updated
right from start to end. Since the sine table consists 50 values, each sine value will continue for 20mS/50 = 400uS.
In the general case (for example with a variable frequency sine), there is no relation between the number of values
in your table and the timer update rate.
That depends on the formula you are using, the ones in the data sheet refer to actual crystal frequency.Do we have to divide clock frequency by 4 in the PIC for timer related calculations?
That depends on the formula you are using, the ones in the data sheet refer to actual crystal frequency.Do we have to divide clock frequency by 4 in the PIC for timer related calculations?
' Timer0 Registers:
' Prescaler=1:16; TMR0 Preset=56; Freq=2,475.24752Hz; Period=404.00 µs
OPTION_REG.T0CS = 0 ' bit 5 TMR0 Clock Source Select bit:0=Internal Clock (CLKO) / 1=Transition on T0CKI pin
OPTION_REG.T0SE = 0 ' bit 4 TMR0 Source Edge Select bit: 0=low/high / 1=high/low
OPTION_REG.PSA = 0 ' bit 3 Prescaler Assignment bit: 0=Prescaler is assigned to the Timer0
OPTION_REG.PS2 = 0 ' bits 2-0 PS2:PS0: Prescaler Rate Select bits
OPTION_REG.PS1 = 1
OPTION_REG.PS0 = 1
TMR0 = 56 ' preset for timer register(2 cycle adjusted)
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?