xpress_embedo
Advanced Member level 4
- Joined
- Jul 5, 2011
- Messages
- 1,154
- Helped
- 161
- Reputation
- 396
- Reaction score
- 189
- Trophy points
- 1,353
- Location
- India
- Activity points
- 10,591
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 fSampling = 10000; %10Khz Sampling Rate F = 200; %200hz Frequency t = 0:1/fSampling:1-1/fSampling; xSignal = sin(2*pi*F*t); % plot(t(1:100),xSignal(1:100)); xSignalPWM = xSignal(1:100); xSignalPWM = xSignalPWM * 250; xSignalPWM = xSignalPWM + 249.5067; xSignalPWM = round(xSignalPWM); plot(xSignalPWM);
Code C - [expand] 1 2 3 4 5 6 value = 250 + (int)250*sin((float)i*314 * 2/10000); CCPR1L = value>>2; value = value & 0x03; CCP1CON = CCP1CON | (value<<4); if(++i > 99) i=0;
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 #include <htc.h> #include <math.h> #define _XTAL_FREQ 20000000UL __CONFIG(FOSC_HS & WDTE_OFF & LVP_OFF); void PWM_Init(float frequency,unsigned char timer_prescalar); void PWM_Set_Duty(unsigned char duty_cycle,unsigned char timer_presclar); void PWM_Stop(); const unsigned int table[100] = {250 , 281 , 312 , 342 , 370 , 396 , 421 , 442 , 461 , 476 , 487 , 495 , 499 , 499 , 495 , 487 , 476 , 461 , 442 , 421 , 396 , 370 , 342 , 312 , 281 , 250 , 218 , 187 , 157 , 129 , 103 , 78 , 57 , 38 , 23 , 12 , 4 , 0 , 0 , 4 , 12 , 23 , 38 , 57 , 78 , 103 , 129 , 157 , 187 , 218 , 250 , 281 , 312 , 342 , 370 , 396 , 421 , 442 , 461 , 476 , 487 , 495 , 499 , 499 , 495 , 487 , 476 , 461 , 442 , 421 , 396 , 370 , 342 , 312 , 281 , 250 , 218 , 187 , 157 , 129 , 103 , 78 , 57 , 38 , 23 , 12 , 4 , 0 , 0 , 4 , 12 , 23 , 38 , 57 , 78 , 103 , 129 , 157 , 187 , 218}; unsigned char i=0; unsigned char flag = 0; unsigned int value; void interrupt Timer_ISR(void) { TMR1H = 0xFE; TMR1L = 0x09; i++; flag = 1; TMR1IF = 0; } void main() { TRISC2 = 0; T2CON = 0b00000101; PWM_Init(10000,4); T1CON = 0x01; TMR1IF = 0; TMR1H = 0xFE; TMR1L = 0x09; TMR1IE = 1; INTCON = 0xC0; while(1) { if(flag == 1) { if(i>=99) i=0; value = table[i]; CCPR1L = (value>>2); value = value & 0x03; CCP1CON = CCP1CON | (value<<4); flag = 0; } } } void PWM_Init(float frequency,unsigned char timer_prescalar) { float temp; TRISC2 = 0; /* PR2 = (FOSC/(4*PWMFreq*TMR2 Prescalar)-1) */ temp = _XTAL_FREQ/frequency; temp = temp/4; temp = temp/timer_prescalar; PR2 = (unsigned char)(temp); PR2 = PR2-1; CCPR1L = 0x00; CCP1CON = 0x0C; //Set Zero Percent Duty Cycle Initially } void PWM_Set_Duty(unsigned char duty_cycle,unsigned char timer_prescalar) { unsigned int register_value; float frequency; frequency = (_XTAL_FREQ/4); frequency = (frequency/timer_prescalar); frequency = (frequency/(PR2+1)); //Frequency of PWM Signal frequency = (frequency*100)/(duty_cycle); register_value = (_XTAL_FREQ)/(unsigned int)frequency; register_value = (register_value/timer_prescalar); CCPR1L = (register_value>>2); register_value = register_value & 0x03; CCP1CON = CCP1CON | (register_value<<4); }
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 108 109 110 111 112 113 114 115 116 #include <htc.h> #define _XTAL_FREQ 20000000UL __CONFIG(FOSC_HS & WDTE_OFF & LVP_OFF); void PWM_Init(float frequency,unsigned char timer_prescalar); void PWM_Set_Duty(unsigned char duty_cycle,unsigned char timer_presclar); void PWM_Stop(); //#define SINE_WAVE #define SAWTOOTH_WAVE //#define SQUARE_WAVE #if defined(SINE_WAVE) const unsigned int table[100] = {250 , 281 , 312 , 342 , 370 , 396 , 421 , 442 , 461 , 476 , 487 , 495 , 499 , 499 , 495 , 487 , 476 , 461 , 442 , 421 , 396 , 370 , 342 , 312 , 281 , 250 , 218 , 187 , 157 , 129 , 103 , 78 , 57 , 38 , 23 , 12 , 4 , 0 , 0 , 4 , 12 , 23 , 38 , 57 , 78 , 103 , 129 , 157 , 187 , 218 , 250 , 281 , 312 , 342 , 370 , 396 , 421 , 442 , 461 , 476 , 487 , 495 , 499 , 499 , 495 , 487 , 476 , 461 , 442 , 421 , 396 , 370 , 342 , 312 , 281 , 250 , 218 , 187 , 157 , 129 , 103 , 78 , 57 , 38 , 23 , 12 , 4 , 0 , 0 , 4 , 12 , 23 , 38 , 57 , 78 , 103 , 129 , 157 , 187 , 218 }; #elif defined(SAWTOOTH_WAVE) const unsigned int table[100] = {0 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 , 110 , 120 , 130 , 140 , 150 , 160 , 170 , 180 , 190 , 200 , 210 , 220 , 230 , 240 , 250 , 260 , 270 , 280 , 290 , 300 , 310 , 320 , 330 , 340 , 350 , 360 , 370 , 380 , 390 , 400 , 410 , 420 , 430 , 440 , 450 , 460 , 470 , 480 , 490 , 0 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 , 110 , 120 , 130 , 140 , 150 , 160 , 170 , 180 , 190 , 200 , 210 , 220 , 230 , 240 , 250 , 260 , 270 , 280 , 290 , 300 , 310 , 320 , 330 , 340 , 350 , 360 , 370 , 380 , 390 , 400 , 410 , 420 , 430 , 440 , 450 , 460 , 470 , 480 , 490 }; #elif defined(SQUARE_WAVE) const unsigned int table[100] = {500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; #endif unsigned char i=0; unsigned char flag = 0; unsigned int value; void interrupt Timer_ISR(void) { TMR1H = 0xFE; TMR1L = 0x09; i++; flag = 1; TMR1IF = 0; } void main() { TRISC2 = 0; T2CON = 0b00000101; PWM_Init(10000,4); T1CON = 0x01; TMR1IF = 0; TMR1H = 0xFE; TMR1L = 0x09; TMR1IE = 1; INTCON = 0xC0; while(1) { if(flag == 1) { if(i>=99) i=0; value = table[i]; CCPR1L = (value>>2); value = value & 0x03; CCP1CON = CCP1CON | (value<<4); flag = 0; } } } void PWM_Init(float frequency,unsigned char timer_prescalar) { float temp; TRISC2 = 0; /* PR2 = (FOSC/(4*PWMFreq*TMR2 Prescalar)-1) */ temp = _XTAL_FREQ/frequency; temp = temp/4; temp = temp/timer_prescalar; PR2 = (unsigned char)(temp); PR2 = PR2-1; CCPR1L = 0x00; CCP1CON = 0x0C; //Set Zero Percent Duty Cycle Initially } void PWM_Set_Duty(unsigned char duty_cycle,unsigned char timer_prescalar) { unsigned int register_value; float frequency; frequency = (_XTAL_FREQ/4); frequency = (frequency/timer_prescalar); frequency = (frequency/(PR2+1)); //Frequency of PWM Signal frequency = (frequency*100)/(duty_cycle); register_value = (_XTAL_FREQ)/(unsigned int)frequency; register_value = (register_value/timer_prescalar); CCPR1L = (register_value>>2); register_value = register_value & 0x03; CCP1CON = CCP1CON | (register_value<<4); }
#include <htc.h>
#define _XTAL_FREQ 20000000UL
__CONFIG(FOSC_HS & WDTE_OFF & LVP_OFF);
void PWM_Init(float frequency,unsigned char timer_prescalar);
void PWM_Set_Duty(unsigned char duty_cycle,unsigned char timer_presclar);
void PWM_Stop();
#define SINE_WAVE
//#define SAWTOOTH_WAVE
//#define SQUARE_WAVE
#if defined(SINE_WAVE)
const unsigned int table[100] =
{250 , 281 , 312 , 342 , 370 , 396 , 421 , 442 , 461 , 476 , 487 , 495 , 499 , 499 , 495 , 487 ,
476 , 461 , 442 , 421 , 396 , 370 , 342 , 312 , 281 , 250 , 218 , 187 , 157 , 129 , 103 , 78 ,
57 , 38 , 23 , 12 , 4 , 0 , 0 , 4 , 12 , 23 , 38 , 57 , 78 , 103 , 129 , 157 , 187 , 218 , 250 ,
281 , 312 , 342 , 370 , 396 , 421 , 442 , 461 , 476 , 487 , 495 , 499 , 499 , 495 , 487 , 476 ,
461 , 442 , 421 , 396 , 370 , 342 , 312 , 281 , 250 , 218 , 187 , 157 , 129 , 103 , 78 , 57 ,
38 , 23 , 12 , 4 , 0 , 0 , 4 , 12 , 23 , 38 , 57 , 78 , 103 , 129 , 157 , 187 , 218
};
#elif defined(SAWTOOTH_WAVE)
const unsigned int table[100] =
{0 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 , 110 , 120 , 130 , 140 , 150 , 160 , 170 ,
180 , 190 , 200 , 210 , 220 , 230 , 240 , 250 , 260 , 270 , 280 , 290 , 300 , 310 , 320 , 330 ,
340 , 350 , 360 , 370 , 380 , 390 , 400 , 410 , 420 , 430 , 440 , 450 , 460 , 470 , 480 , 490 ,
0 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 , 110 , 120 , 130 , 140 , 150 , 160 , 170 ,
180 , 190 , 200 , 210 , 220 , 230 , 240 , 250 , 260 , 270 , 280 , 290 , 300 , 310 , 320 , 330 ,
340 , 350 , 360 , 370 , 380 , 390 , 400 , 410 , 420 , 430 , 440 , 450 , 460 , 470 , 480 , 490
};
#elif defined(SQUARE_WAVE)
const unsigned int table[100] =
{500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 ,
500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 500 , 500 , 500 , 500 , 500 , 500 , 500 ,
500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 ,
500 , 500 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
0 , 0 , 0 , 0
};
#endif
unsigned char i=0;
unsigned char flag = 0;
unsigned int value;
void interrupt Timer_ISR(void)
{
TMR1IF = 0;
value = table[i];
CCPR1L = (value>>2);
value = value & 0x03;
CCP1CON = CCP1CON | (value<<4);
if(++i > 99)
i=0;
TMR1H = 0xFE;
TMR1L = 0x09;
}
void main()
{
TRISC2 = 0;
T2CON = 0b00000101;
PWM_Init(10000,4);
T1CON = 0x01;
TMR1IF = 0;
TMR1H = 0xFE;
TMR1L = 0x09;
TMR1IE = 1;
INTCON = 0xC0;
while(1)
{
}
}
void PWM_Init(float frequency,unsigned char timer_prescalar)
{
float temp;
TRISC2 = 0;
/*
PR2 = (FOSC/(4*PWMFreq*TMR2 Prescalar)-1)
*/
temp = _XTAL_FREQ/frequency;
temp = temp/4;
temp = temp/timer_prescalar;
PR2 = (unsigned char)(temp);
PR2 = PR2-1;
CCPR1L = 0x00;
CCP1CON = 0x0C; //Set Zero Percent Duty Cycle Initially
}
void PWM_Set_Duty(unsigned char duty_cycle,unsigned char timer_prescalar)
{
unsigned int register_value;
float frequency;
frequency = (_XTAL_FREQ/4);
frequency = (frequency/timer_prescalar);
frequency = (frequency/(PR2+1)); //Frequency of PWM Signal
frequency = (frequency*100)/(duty_cycle);
register_value = (_XTAL_FREQ)/(unsigned int)frequency;
register_value = (register_value/timer_prescalar);
CCPR1L = (register_value>>2);
register_value = register_value & 0x03;
CCP1CON = CCP1CON | (register_value<<4);
}
I used Hitech C compiler put _XTAL_FREQ definition in my code and you can run it in any IDE with hitech tool suite...Code:#include <htc.h> #define _XTAL_FREQ 20000000UL __CONFIG(FOSC_HS & WDTE_OFF & LVP_OFF); void PWM_Init(float frequency,unsigned char timer_prescalar); void PWM_Set_Duty(unsigned char duty_cycle,unsigned char timer_presclar); void PWM_Stop(); #define SINE_WAVE //#define SAWTOOTH_WAVE //#define SQUARE_WAVE #if defined(SINE_WAVE) const unsigned int table[100] = {250 , 281 , 312 , 342 , 370 , 396 , 421 , 442 , 461 , 476 , 487 , 495 , 499 , 499 , 495 , 487 , 476 , 461 , 442 , 421 , 396 , 370 , 342 , 312 , 281 , 250 , 218 , 187 , 157 , 129 , 103 , 78 , 57 , 38 , 23 , 12 , 4 , 0 , 0 , 4 , 12 , 23 , 38 , 57 , 78 , 103 , 129 , 157 , 187 , 218 , 250 , 281 , 312 , 342 , 370 , 396 , 421 , 442 , 461 , 476 , 487 , 495 , 499 , 499 , 495 , 487 , 476 , 461 , 442 , 421 , 396 , 370 , 342 , 312 , 281 , 250 , 218 , 187 , 157 , 129 , 103 , 78 , 57 , 38 , 23 , 12 , 4 , 0 , 0 , 4 , 12 , 23 , 38 , 57 , 78 , 103 , 129 , 157 , 187 , 218 }; #elif defined(SAWTOOTH_WAVE) const unsigned int table[100] = {0 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 , 110 , 120 , 130 , 140 , 150 , 160 , 170 , 180 , 190 , 200 , 210 , 220 , 230 , 240 , 250 , 260 , 270 , 280 , 290 , 300 , 310 , 320 , 330 , 340 , 350 , 360 , 370 , 380 , 390 , 400 , 410 , 420 , 430 , 440 , 450 , 460 , 470 , 480 , 490 , 0 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 , 110 , 120 , 130 , 140 , 150 , 160 , 170 , 180 , 190 , 200 , 210 , 220 , 230 , 240 , 250 , 260 , 270 , 280 , 290 , 300 , 310 , 320 , 330 , 340 , 350 , 360 , 370 , 380 , 390 , 400 , 410 , 420 , 430 , 440 , 450 , 460 , 470 , 480 , 490 }; #elif defined(SQUARE_WAVE) const unsigned int table[100] = {500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 500 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; #endif unsigned char i=0; unsigned char flag = 0; unsigned int value; void interrupt Timer_ISR(void) { TMR1IF = 0; value = table[i]; CCPR1L = (value>>2); value = value & 0x03; CCP1CON = CCP1CON | (value<<4); if(++i > 99) i=0; TMR1H = 0xFE; TMR1L = 0x09; } void main() { TRISC2 = 0; T2CON = 0b00000101; PWM_Init(10000,4); T1CON = 0x01; TMR1IF = 0; TMR1H = 0xFE; TMR1L = 0x09; TMR1IE = 1; INTCON = 0xC0; while(1) { } } void PWM_Init(float frequency,unsigned char timer_prescalar) { float temp; TRISC2 = 0; /* PR2 = (FOSC/(4*PWMFreq*TMR2 Prescalar)-1) */ temp = _XTAL_FREQ/frequency; temp = temp/4; temp = temp/timer_prescalar; PR2 = (unsigned char)(temp); PR2 = PR2-1; CCPR1L = 0x00; CCP1CON = 0x0C; //Set Zero Percent Duty Cycle Initially } void PWM_Set_Duty(unsigned char duty_cycle,unsigned char timer_prescalar) { unsigned int register_value; float frequency; frequency = (_XTAL_FREQ/4); frequency = (frequency/timer_prescalar); frequency = (frequency/(PR2+1)); //Frequency of PWM Signal frequency = (frequency*100)/(duty_cycle); register_value = (_XTAL_FREQ)/(unsigned int)frequency; register_value = (register_value/timer_prescalar); CCPR1L = (register_value>>2); register_value = register_value & 0x03; CCP1CON = CCP1CON | (register_value<<4); }
I am getting distorted wave.
I saw your code, my code is almost like that.
I dont know which ide you used for your project.
So i only saw you code but not able to compiler it.
One think to have in mind, you can change the output waveform frequency by timer values only not by PWM values....
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?