void interrupt() {
if((TMR2IE_bit) && (TMR2IF_bit)) {
//Enter your code here
//SET_FREQ = 65536 / (64 * 2) = 512
//Required AC Frequency = 50 Hz
//PWM Freq = 12821 Hz
//Tpwm = 1/12.821 KHz = 78 us (Timer2 Interrupt Period)
//78 us * 64 * 2 = 9984 = 9.984 ms (for 180 degrees)
//9.984 * 2 = 19.968 ms
//AC freq = 1/19.968 ms = 50.08 Hz
TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
if(TBL_POINTER_NEW < TBL_POINTER_OLD) {
CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge
[COLOR="#FF0000"]TBL_POINTER_NEW = 0;
TBL_POINTER_OLD = 0;[/COLOR]
}
TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 10;
DUTY_CYCLE = TBL_POINTER_SHIFT + adder;
//Assign PWM duty 10 bit
CCPR1L = (sine_table[DUTY_CYCLE] & 0x3FC) >> 2;
CCP1CON.DC1B1 = (sine_table[DUTY_CYCLE] & 0x02) >> 1;
CCP1CON.DC1B0 = (sine_table[DUTY_CYCLE] & 0x01);
TBL_POINTER_OLD = TBL_POINTER_NEW;
TMR2IF_bit = 0;
I doubt this. This are just 2 clock cycles of 4MHz.I debugged ISR code and it is taking 0.5 us.
No, otherwise you'll loose the feature of varying the output frequency continuously. Overflow is intentional in a DDS generator.I think TBL_POINTER_OLD and TBL_POINTER_NEW should be reset after every 10mS i.e. on overflow of 16 bit register like this.
adder = FB_Step << 6
Or clean up interrupt code. Table operations are long winded in 8-bit PIC. You shouldn't have three where one does it.Yes, Problem is with Oscillator frequency. Will see which frequency works fine.
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 // LCD module connections sbit LCD_RS at LATB4_bit; sbit LCD_EN at LATB5_bit; sbit LCD_D4 at LATB0_bit; sbit LCD_D5 at LATB1_bit; sbit LCD_D6 at LATB2_bit; sbit LCD_D7 at LATB3_bit; sbit LCD_RS_Direction at TRISB4_bit; sbit LCD_EN_Direction at TRISB5_bit; sbit LCD_D4_Direction at TRISB0_bit; sbit LCD_D5_Direction at TRISB1_bit; sbit LCD_D6_Direction at TRISB2_bit; sbit LCD_D7_Direction at TRISB3_bit; // End LCD module connections #define MAX_DEMO_LIMIT 20 #define MAX_DEMO_LIMIT_ADDRESS 0x20 #define FBCh 0 unsigned char demo_limit = 0; char msg[17]; unsigned char FB_Step = 0; unsigned int FBV = 0, adder = 0; const char msg1[] = " Stabilizer "; const char msg2[] = " Inverter "; const char msg3[] = ""; const char msg4[] = ""; const unsigned char sine_table[960] = { 0,0,2,4,6,10,14,18, 24,30,36,43,50,58,66,74, 82,89,97,105,113,120,127,133, 139,145,149,153,157,159,161,163, 163,163,161,159,157,153,149,145, 139,133,127,120,113,105,97,89, 82,74,66,58,50,43,36,30, 24,18,14,10,6,4,2,0, 0,0,2,4,6,10,14,19, 25,31,38,45,52,60,68,76, 85,93,101,109,117,124,131,138, 144,150,155,159,163,165,167,169, 169,169,167,165,163,159,155,150, 144,138,131,124,117,109,101,93, 85,76,68,60,52,45,38,31, 25,19,14,10,6,4,2,0, 0,0,2,4,7,10,15,20, 26,32,39,46,54,62,70,79, 88,96,105,113,121,129,136,143, 149,155,160,165,168,171,173,175, 175,175,173,171,168,165,160,155, 149,143,136,129,121,113,105,96, 88,79,70,62,54,46,39,32, 26,20,15,10,7,4,2,0, 0,0,2,4,7,11,15,21, 27,33,40,48,56,64,73,82, 91,99,108,117,125,133,141,148, 154,160,166,170,174,177,179,181, 181,181,179,177,174,170,166,160, 154,148,141,133,125,117,108,99, 91,82,73,64,56,48,40,33, 27,21,15,11,7,4,2,0, 0,0,2,4,7,11,16,21, 28,34,42,50,58,67,76,85, 94,103,112,121,130,138,146,154, 160,167,172,177,181,184,186,188, 188,188,186,184,181,177,172,167, 160,154,146,138,130,121,112,103, 94,85,76,67,58,50,42,34, 28,21,16,11,7,4,2,0, 0,0,2,4,7,11,16,22, 28,35,43,51,60,69,78,87, 97,107,116,125,134,143,151,159, 166,172,178,183,187,190,192,194, 194,194,192,190,187,183,178,172, 166,159,151,143,134,125,116,107, 97,87,78,69,60,51,43,35, 28,22,16,11,7,4,2,0, 0,0,2,4,8,12,17,23, 29,37,44,53,62,71,80,90, 100,110,120,129,138,147,156,163, 171,177,183,188,192,196,198,200, 200,200,198,196,192,188,183,177, 171,163,156,147,138,129,120,110, 100,90,80,71,62,53,44,37, 29,23,17,12,8,4,2,0, 0,0,2,4,8,12,17,23, 30,38,46,54,64,73,83,93, 103,113,123,133,142,152,160,168, 176,183,189,194,198,202,204,206, 206,206,204,202,198,194,189,183, 176,168,160,152,142,133,123,113, 103,93,83,73,64,54,46,38, 30,23,17,12,8,4,2,0, 0,1,2,5,8,13,18,24, 31,39,47,56,66,76,86,96, 107,117,127,137,147,157,166,174, 182,189,195,200,205,208,211,212, 213,212,211,208,205,200,195,189, 182,174,166,157,147,137,127,117, 107,96,86,76,66,56,47,39, 31,24,18,13,8,5,2,1, 0,1,2,5,8,13,18,25, 32,40,49,58,68,78,88,99, 110,120,131,141,151,161,170,179, 187,194,201,206,211,214,217,218, 219,218,217,214,211,206,201,194, 187,179,170,161,151,141,131,120, 110,99,88,78,68,58,49,40, 32,25,18,13,8,5,2,1, 0,1,2,5,9,13,19,26, 33,41,50,59,69,80,91,101, 113,124,134,145,156,166,175,184, 192,199,206,212,216,220,223,224, 225,224,223,220,216,212,206,199, 192,184,175,166,156,145,134,124, 113,101,91,80,69,59,50,41, 33,26,19,13,9,5,2,1, 0,1,2,5,9,14,19,26, 34,42,51,61,71,82,93,104, 116,127,138,149,160,170,180,189, 197,205,212,217,222,226,229,230, 231,230,229,226,222,217,212,205, 197,189,180,170,160,149,138,127, 116,104,93,82,71,61,51,42, 34,26,19,14,9,5,2,1, 0,1,2,5,9,14,20,27, 35,44,53,63,73,84,96,107, 119,131,142,154,165,175,185,194, 203,211,218,224,229,233,236,237, 238,237,236,233,229,224,218,211, 203,194,185,175,165,154,142,131, 119,107,96,84,73,63,53,44, 35,27,20,14,9,5,2,1, 0,1,2,5,9,14,21,28, 36,45,54,64,75,87,98,110, 122,134,146,157,169,180,190,199, 208,216,223,230,235,239,242,243, 244,243,242,239,235,230,223,216, 208,199,190,180,169,157,146,134, 122,110,98,87,75,64,54,45, 36,28,21,14,9,5,2,1, 0,1,2,5,10,15,21,28, 37,46,56,66,77,89,101,113, 125,137,149,161,173,184,194,204, 213,222,229,235,240,245,248,249, 250,249,248,245,240,235,229,222, 213,204,194,184,173,161,149,137, 125,113,101,89,77,66,56,46, 37,28,21,15,10,5,2,1 }; unsigned int TBL_POINTER_NEW = 0, TBL_POINTER_OLD = 0, TBL_POINTER_SHIFT = 0, DUTY_CYCLE = 0, SET_FREQ = 512; //Timer2 //Prescaler 1:1; Postscaler 1:2; TMR2 Preload = 155; Actual Interrupt Time : 78 us //Place/Copy this part in declaration section void InitTimer2() { T2CON = 0x0C; TMR2IE_bit = 1; PR2 = 155; INTCON = 0xC0; } void interrupt() { if((TMR2IE_bit) && (TMR2IF_bit)) { //Enter your code here //SET_FREQ = 65536 / (64 * 2) = 512 //Required AC Frequency = 50 Hz //PWM Freq = 12821 Hz //Tpwm = 1/12.821 KHz = 78 us (Timer2 Interrupt Period) //78 us * 64 * 2 = 9984 = 9.984 ms (for 180 degrees) //9.984 * 2 = 19.968 ms //AC freq = 1/19.968 ms = 50.08 Hz TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ; if(TBL_POINTER_NEW < TBL_POINTER_OLD) { CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge TBL_POINTER_NEW = 0; TBL_POINTER_OLD = 0; } TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 10; DUTY_CYCLE = TBL_POINTER_SHIFT + adder; //Assign PWM duty 10 bit CCPR1L = (sine_table[DUTY_CYCLE] & 0x3FC) >> 2; CCP1CON.DC1B1 = (sine_table[DUTY_CYCLE] & 0x02) >> 1; CCP1CON.DC1B0 = (sine_table[DUTY_CYCLE] & 0x01); TBL_POINTER_OLD = TBL_POINTER_NEW; TMR2IF_bit = 0; } } char *CopyConst2Ram(char *dest, const code char *src) { char *d; d = dest; for(;*dest++ = *src++;)asm clrwdt; return d; } void main() { asm clrwdt OSCCON = 0x77; OSCTUNE = 0x00; /* demo_limit = EEPROM_Read(MAX_DEMO_LIMIT_ADDRESS); Delay_ms(20); if(demo_limit <= MAX_DEMO_LIMIT) { demo_limit++; EEPROM_Write(MAX_DEMO_LIMIT_ADDRESS, demo_limit); Delay_ms(20); } */ CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ADCON1 = 0x80; ADCON2 = 0b10110101; ANSELA = 0x01; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xC1; TRISB = 0x00; TRISC = 0x06; TRISD = 0xE0; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LATE = 0x00; //PWM4_Init(12821); //PWM4_Set_Duty(127); //FAN //PWM4_Stop(); //PWM4_Start(); PWM3_Init(12821); PWM3_Set_Duty(0); CCP1CON.CCP1M3 = 1; CCP1CON.CCP1M2 = 1; CCP1CON.CCP1M1 = 0; CCP1CON.CCP1M0 = 0; CCP1CON.P1M0 = 1; CCP1CON.P1M1 = 0; Delay_ms(200); // Lcd code doesn't work if called after InitTimer2() // as ISR will be called every 78 us LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,CopyConst2Ram(msg, msg1)); InitTimer2(); TRISC = 0x00; TRISD = 0x00; PWM3_Start(); while(1) { asm clrwdt //if(demo_limit < MAX_DEMO_LIMIT) { //} FBV = ADC_Read(FBCh); if(FBV < 512) { if(++FB_Step > 14) { // eg: 14 = (0 to 14) no. of pwm duty sets in the array FB_Step = 14; // example if FB_Step = 15 then (see last line) } } else{ if(FB_Step > 0) { FB_Step--; } } adder = FB_Step << 6; // << 6 because I have 2^6 = 64 entries in each pwm duty set // 14 << 6 = 896 /* ********** code from ISR *********** TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ; if(TBL_POINTER_NEW < TBL_POINTER_OLD) { CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge } TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 10; DUTY_CYCLE = TBL_POINTER_SHIFT + adder; /* ************************************* Eg: TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ; TBL_POINTER_NEW = 0 + 512; //512 TBL_POINTER_SHIFT = 512 >> 10; // 0 DUTY_CYCLE = TBL_POINTER_SHIFT + adder; //adder = 896, FB_Step = 14 DUTY_CYCLE = 0 + 896; // In the 64 pwm duties (of each pwm duty set), each duty is called twice // to get 50 Hz TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ; // So, (SET_FREQ * 64 * 2) = 512 * 64 * 2 = 65536 //So, Eg: TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ; TBL_POINTER_NEW = (65536 - 512) + 512; //65536 TBL_POINTER_SHIFT = 65536 >> 10; // 64 DUTY_CYCLE = TBL_POINTER_SHIFT + adder; //adder = 896, FB_Step = 14 DUTY_CYCLE = 64 + 896; //960 (last entry of the array that is 1) */ } }
switch(DUTY_CYCLE) {
case 63*0x01:
case 63*0x02:
case 63*0x03:
case 63*0x04:
case 63*0x05:
case 63*0x06:
case 63*0x07:
case 63*0x08:
case 63*0x09:
case 63*0x0A:
case 63*0x0B:
case 63*0x0C:
case 63*0x0D:
case 63*0x0E:
case 63*0x0F:
...
case 63*0x40:
adder = FB_Step << 6;
break;
};
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 // LCD module connections sbit LCD_RS at LATB4_bit; sbit LCD_EN at LATB5_bit; sbit LCD_D4 at LATB0_bit; sbit LCD_D5 at LATB1_bit; sbit LCD_D6 at LATB2_bit; sbit LCD_D7 at LATB3_bit; sbit LCD_RS_Direction at TRISB4_bit; sbit LCD_EN_Direction at TRISB5_bit; sbit LCD_D4_Direction at TRISB0_bit; sbit LCD_D5_Direction at TRISB1_bit; sbit LCD_D6_Direction at TRISB2_bit; sbit LCD_D7_Direction at TRISB3_bit; // End LCD module connections #define MAX_DEMO_LIMIT 20 #define MAX_DEMO_LIMIT_ADDRESS 0x20 #define FBCh 0 unsigned char demo_limit = 0; char msg[17]; unsigned char FB_Step = 0; unsigned int FBV = 0, adder = 0, previous_adder = 0; const char msg1[] = " Stabilizer "; const char msg2[] = " Inverter "; const char msg3[] = ""; const char msg4[] = ""; const unsigned char sine_table[960] = { 0,0,2,4,6,10,14,18, 24,30,36,43,50,58,66,74, 82,89,97,105,113,120,127,133, 139,145,149,153,157,159,161,163, 163,163,161,159,157,153,149,145, 139,133,127,120,113,105,97,89, 82,74,66,58,50,43,36,30, 24,18,14,10,6,4,2,0, 0,0,2,4,6,10,14,19, 25,31,38,45,52,60,68,76, 85,93,101,109,117,124,131,138, 144,150,155,159,163,165,167,169, 169,169,167,165,163,159,155,150, 144,138,131,124,117,109,101,93, 85,76,68,60,52,45,38,31, 25,19,14,10,6,4,2,0, 0,0,2,4,7,10,15,20, 26,32,39,46,54,62,70,79, 88,96,105,113,121,129,136,143, 149,155,160,165,168,171,173,175, 175,175,173,171,168,165,160,155, 149,143,136,129,121,113,105,96, 88,79,70,62,54,46,39,32, 26,20,15,10,7,4,2,0, 0,0,2,4,7,11,15,21, 27,33,40,48,56,64,73,82, 91,99,108,117,125,133,141,148, 154,160,166,170,174,177,179,181, 181,181,179,177,174,170,166,160, 154,148,141,133,125,117,108,99, 91,82,73,64,56,48,40,33, 27,21,15,11,7,4,2,0, 0,0,2,4,7,11,16,21, 28,34,42,50,58,67,76,85, 94,103,112,121,130,138,146,154, 160,167,172,177,181,184,186,188, 188,188,186,184,181,177,172,167, 160,154,146,138,130,121,112,103, 94,85,76,67,58,50,42,34, 28,21,16,11,7,4,2,0, 0,0,2,4,7,11,16,22, 28,35,43,51,60,69,78,87, 97,107,116,125,134,143,151,159, 166,172,178,183,187,190,192,194, 194,194,192,190,187,183,178,172, 166,159,151,143,134,125,116,107, 97,87,78,69,60,51,43,35, 28,22,16,11,7,4,2,0, 0,0,2,4,8,12,17,23, 29,37,44,53,62,71,80,90, 100,110,120,129,138,147,156,163, 171,177,183,188,192,196,198,200, 200,200,198,196,192,188,183,177, 171,163,156,147,138,129,120,110, 100,90,80,71,62,53,44,37, 29,23,17,12,8,4,2,0, 0,0,2,4,8,12,17,23, 30,38,46,54,64,73,83,93, 103,113,123,133,142,152,160,168, 176,183,189,194,198,202,204,206, 206,206,204,202,198,194,189,183, 176,168,160,152,142,133,123,113, 103,93,83,73,64,54,46,38, 30,23,17,12,8,4,2,0, 0,1,2,5,8,13,18,24, 31,39,47,56,66,76,86,96, 107,117,127,137,147,157,166,174, 182,189,195,200,205,208,211,212, 213,212,211,208,205,200,195,189, 182,174,166,157,147,137,127,117, 107,96,86,76,66,56,47,39, 31,24,18,13,8,5,2,1, 0,1,2,5,8,13,18,25, 32,40,49,58,68,78,88,99, 110,120,131,141,151,161,170,179, 187,194,201,206,211,214,217,218, 219,218,217,214,211,206,201,194, 187,179,170,161,151,141,131,120, 110,99,88,78,68,58,49,40, 32,25,18,13,8,5,2,1, 0,1,2,5,9,13,19,26, 33,41,50,59,69,80,91,101, 113,124,134,145,156,166,175,184, 192,199,206,212,216,220,223,224, 225,224,223,220,216,212,206,199, 192,184,175,166,156,145,134,124, 113,101,91,80,69,59,50,41, 33,26,19,13,9,5,2,1, 0,1,2,5,9,14,19,26, 34,42,51,61,71,82,93,104, 116,127,138,149,160,170,180,189, 197,205,212,217,222,226,229,230, 231,230,229,226,222,217,212,205, 197,189,180,170,160,149,138,127, 116,104,93,82,71,61,51,42, 34,26,19,14,9,5,2,1, 0,1,2,5,9,14,20,27, 35,44,53,63,73,84,96,107, 119,131,142,154,165,175,185,194, 203,211,218,224,229,233,236,237, 238,237,236,233,229,224,218,211, 203,194,185,175,165,154,142,131, 119,107,96,84,73,63,53,44, 35,27,20,14,9,5,2,1, 0,1,2,5,9,14,21,28, 36,45,54,64,75,87,98,110, 122,134,146,157,169,180,190,199, 208,216,223,230,235,239,242,243, 244,243,242,239,235,230,223,216, 208,199,190,180,169,157,146,134, 122,110,98,87,75,64,54,45, 36,28,21,14,9,5,2,1, 0,1,2,5,10,15,21,28, 37,46,56,66,77,89,101,113, 125,137,149,161,173,184,194,204, 213,222,229,235,240,245,248,249, 250,249,248,245,240,235,229,222, 213,204,194,184,173,161,149,137, 125,113,101,89,77,66,56,46, 37,28,21,15,10,5,2,1 }; unsigned int TBL_POINTER_NEW = 0, TBL_POINTER_OLD = 0, TBL_POINTER_SHIFT = 0, DUTY_CYCLE = 0, SET_FREQ = 512; //Timer2 //Prescaler 1:1; Postscaler 1:2; TMR2 Preload = 155; Actual Interrupt Time : 78 us //Place/Copy this part in declaration section void InitTimer2() { T2CON = 0x0C; TMR2IE_bit = 1; PR2 = 155; INTCON = 0xC0; } void interrupt() { if((TMR2IE_bit) && (TMR2IF_bit)) { //Enter your code here //SET_FREQ = 65536 / (64 * 2) = 512 //Required AC Frequency = 50 Hz //PWM Freq = 12821 Hz //Tpwm = 1/12.821 KHz = 78 us (Timer2 Interrupt Period) //78 us * 64 * 2 = 9984 = 9.984 ms (for 180 degrees) //9.984 * 2 = 19.968 ms //AC freq = 1/19.968 ms = 50.08 Hz TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ; if(TBL_POINTER_NEW < TBL_POINTER_OLD) { CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge TBL_POINTER_NEW = 0; TBL_POINTER_OLD = 0; } TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 10; DUTY_CYCLE = TBL_POINTER_SHIFT + adder; //Assign PWM duty 10 bit CCPR1L = (sine_table[DUTY_CYCLE] & 0x3FC) >> 2; CCP1CON.DC1B1 = (sine_table[DUTY_CYCLE] & 0x02) >> 1; CCP1CON.DC1B0 = (sine_table[DUTY_CYCLE] & 0x01); TBL_POINTER_OLD = TBL_POINTER_NEW; TMR2IF_bit = 0; } } char *CopyConst2Ram(char *dest, const code char *src) { char *d; d = dest; for(;*dest++ = *src++;)asm clrwdt; return d; } void main() { asm clrwdt OSCCON = 0x77; OSCTUNE = 0x00; /* demo_limit = EEPROM_Read(MAX_DEMO_LIMIT_ADDRESS); Delay_ms(20); if(demo_limit <= MAX_DEMO_LIMIT) { demo_limit++; EEPROM_Write(MAX_DEMO_LIMIT_ADDRESS, demo_limit); Delay_ms(20); } */ CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ADCON1 = 0x80; ADCON2 = 0b10110101; ANSELA = 0x01; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xC1; TRISB = 0x00; TRISC = 0x06; TRISD = 0xE0; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LATE = 0x00; //PWM4_Init(12821); //PWM4_Set_Duty(127); //FAN //PWM4_Stop(); //PWM4_Start(); PWM3_Init(12821); PWM3_Set_Duty(0); CCP1CON.CCP1M3 = 1; CCP1CON.CCP1M2 = 1; CCP1CON.CCP1M1 = 0; CCP1CON.CCP1M0 = 0; CCP1CON.P1M0 = 1; CCP1CON.P1M1 = 0; Delay_ms(200); // Lcd code doesn't work if called after InitTimer2() // as ISR will be called every 78 us LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,CopyConst2Ram(msg, msg1)); InitTimer2(); TRISC = 0x00; TRISD = 0x00; PWM3_Start(); while(1) { asm clrwdt //if(demo_limit < MAX_DEMO_LIMIT) { //} FBV = ADC_Read(FBCh); if(FBV < 512) { if(++FB_Step > 14) { // eg: 14 = (0 to 14) no. of pwm duty sets in the array FB_Step = 14; // example if FB_Step = 15 then (see last line) } } else{ if(FB_Step > 0) { FB_Step--; } } switch(DUTY_CYCLE) { case 63*0x01: case 63*0x02: case 63*0x03: case 63*0x04: case 63*0x05: case 63*0x06: case 63*0x07: case 63*0x08: case 63*0x09: case 63*0x0A: case 63*0x0B: case 63*0x0C: case 63*0x0D: case 63*0x0E: case 63*0x0F: case 63*0x11: case 63*0x12: case 63*0x13: case 63*0x14: case 63*0x15: case 63*0x16: case 63*0x17: case 63*0x18: case 63*0x19: case 63*0x1A: case 63*0x1B: case 63*0x1C: case 63*0x1D: case 63*0x1E: case 63*0x1F: case 63*0x21: case 63*0x22: case 63*0x23: case 63*0x24: case 63*0x25: case 63*0x26: case 63*0x27: case 63*0x28: case 63*0x29: case 63*0x2A: case 63*0x2B: case 63*0x2C: case 63*0x2D: case 63*0x2E: case 63*0x2F: case 63*0x31: case 63*0x32: case 63*0x33: case 63*0x34: case 63*0x35: case 63*0x36: case 63*0x37: case 63*0x38: case 63*0x39: case 63*0x3A: case 63*0x3B: case 63*0x3C: case 63*0x3D: case 63*0x3E: case 63*0x3F: adder = FB_Step << 6; break; } // << 6 because I have 2^6 = 64 entries in each pwm duty set // 14 << 6 = 896 /* ********** code from ISR *********** TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ; if(TBL_POINTER_NEW < TBL_POINTER_OLD) { CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge } TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 10; DUTY_CYCLE = TBL_POINTER_SHIFT + adder; /* ************************************* Eg: TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ; TBL_POINTER_NEW = 0 + 512; //512 TBL_POINTER_SHIFT = 512 >> 10; // 0 DUTY_CYCLE = TBL_POINTER_SHIFT + adder; //adder = 896, FB_Step = 14 DUTY_CYCLE = 0 + 896; // In the 64 pwm duties (of each pwm duty set), each duty is called twice // to get 50 Hz TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ; // So, (SET_FREQ * 64 * 2) = 512 * 64 * 2 = 65536 //So, Eg: TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ; TBL_POINTER_NEW = (65536 - 512) + 512; //65536 TBL_POINTER_SHIFT = 65536 >> 10; // 64 DUTY_CYCLE = TBL_POINTER_SHIFT + adder; //adder = 896, FB_Step = 14 DUTY_CYCLE = 64 + 896; //960 (last entry of the array that is 1) */ } }
SET_FREQ = 256;
Or clean up interrupt code. Table operations are long winded in 8-bit PIC. You shouldn't have three where one does it.
CCPR1L = (sine_table[DUTY_CYCLE] & 0x3FC) >> 2;
CCP1CON.DC1B1 = (sine_table[DUTY_CYCLE] & 0x02) >> 1;
CCP1CON.DC1B0 = (sine_table[DUTY_CYCLE] & 0x01);
I was particularly referring to this code lines.
Code C - [expand] 1 2 3 4 5 6 const code unsigned int sine_table[64] = { 0,2,10,22,38,59,85,114,147,183,223,265,310,356,404,452, 502,551,599,647,693,738,780,820,856,889,918,944,965,981,993,1001, 1003,1001,993,981,965,944,918,889,856,820,780,738,693,647,599,551, 502,452,404,356,310,265,223,183,147,114,85,59,38,22,10,2 };
What is the value of 2.16W in degree C ?
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?