Okada
Banned
- Joined
- Jun 16, 2016
- Messages
- 1,159
- Helped
- 129
- Reputation
- 252
- Reaction score
- 129
- Trophy points
- 63
- Activity points
- 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 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 /* * Project name: Three Phase - Controlled Half Wave Rectifier * Revision History: 20170406: - initial release (A); * Description: Three Phase - Controlled Half Wave Rectifier Using SCRs * Test configuration: MCU: PIC18F46K22 **broken link removed** Dev.board: Custom Board Oscillator: XT, 4.0000 MHz Crystal Ext. Modules: None. SW: mikroC PRO for PIC [url]https://www.mikroe.com/mikroc/pic/[/url] * Author(s): Okada * Project Start Date: 04/04/2017 * Project Completion Date: * NOTES: - Three Phase - Controlled Half Wave Rectifier */ // LCD module connections sbit LCD_RS at LATE0_bit; sbit LCD_EN at LATD0_bit; sbit LCD_D4 at LATC3_bit; sbit LCD_D5 at LATC2_bit; sbit LCD_D6 at LATC1_bit; sbit LCD_D7 at LATC0_bit; sbit LCD_RS_Direction at TRISE0_bit; sbit LCD_EN_Direction at TRISD0_bit; sbit LCD_D4_Direction at TRISC3_bit; sbit LCD_D5_Direction at TRISC2_bit; sbit LCD_D6_Direction at TRISC1_bit; sbit LCD_D7_Direction at TRISC0_bit; // End LCD module connections sbit R_Phase_SCR_Gate_Trigger at LATD5_bit; sbit Y_Phase_SCR_Gate_Trigger at LATD6_bit; sbit B_Phase_SCR_Gate_Trigger at LATD7_bit; #define PHASE_R 0 #define PHASE_Y 1 #define PHASE_B 2 const code char txt1[] = "F Angle: "; const code char txt2[] = "C Angle: "; const char character[] = {6,9,9,6,0,0,0,0}; char txt[32], i = 0; unsigned char scr_select_counter = 0; unsigned int timer_reload_value = 0x0000; unsigned int raw_adc_value = 0, previous_raw_adc_value = 2048; double scr_firing_delay_in_seconds = 0.0; double previous_scr_firing_delay_in_seconds = 20.0; double fosc = 4000000.0, timer_prescalar = 1.0; double mains_frequency = 50.0; double period = 0.0; double duration_for_half_wave_of_ac = 0.0; double duration_for_120_degrees = 0.006467; double minimum_gate_trigger_delay = 0.0003; double adc_factor = 0.0; double timer_resolution = 65536.0; double timer_tick_period = 0.0; double scr_firing_angle = 0.0; double scr_conduction_angle = 0.0; //Timer1 //Prescaler 1:1; TMR1 void InitTimer1() { T1CON = 0x01; TMR1IF_bit = 0; TMR1H = timer_reload_value >> 8; TMR1L = timer_reload_value; TMR1IE_bit = 1; } void Interrupt() { if((INT0IE_bit) && (INT0IF_bit)) { INT0IF_bit = 0; //INT0IE_bit = 0; scr_select_counter = 0; InitTimer1(); } else if((INT1IE_bit) && (INT1IF_bit)) { INT1IF_bit = 0; //INT1IE_bit = 0; scr_select_counter = 1; InitTimer1(); } else if((INT2IE_bit) && (INT2IF_bit)) { INT2IF_bit = 0; //INT2IE_bit = 0; scr_select_counter = 2; InitTimer1(); } if((TMR1IE_bit) && (TMR1IF_bit)) { TMR1IE_bit = 0; TMR1IF_bit = 0; //Enter your code here switch(scr_select_counter) { case PHASE_R: R_Phase_SCR_Gate_Trigger = 1; Delay_us(100); R_Phase_SCR_Gate_Trigger = 0; //INT1IE_bit = 1; break; case PHASE_Y: Y_Phase_SCR_Gate_Trigger = 1; Delay_us(100); Y_Phase_SCR_Gate_Trigger = 0; //INT2IE_bit = 1; break; case PHASE_B: B_Phase_SCR_Gate_Trigger = 1; Delay_us(100); B_Phase_SCR_Gate_Trigger = 0; //INT0IE_bit = 1; break; }; } } void CustomChar(char pos_row, char pos_char) { char i; Lcd_Cmd(64); for(i = 0; i <= 7; i++) Lcd_Chr_CP(character[i]); Lcd_Chr(pos_row, pos_char, 0); } char *CopyConst2Ram(char * dest, const code char *src) { char *d; d = dest; for(;*dest++ = *src++;); return d; } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ADCON1 = 0x80; ADCON2 = 0b10110101; ANSELA = 0x01; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0x01; TRISB = 0x07; TRISC = 0x00; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LATE = 0x00; Delay_ms(200); LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,CopyConst2Ram(txt, txt1)); LCD_Out(2,1,CopyConst2Ram(txt, txt2)); timer_tick_period = 1.0 / (fosc / 4.0 / timer_prescalar); //mains ac period period = 1.0 / mains_frequency; //duration for 180 degrees duration_for_half_wave_of_ac = period / 2.0; //**** duration_for_120_degrees = period / 3.0; //for raw adc value = 0 we have to get scr firing delay = 0 sec and //for raw adc value 1023 we have to get scr firing delay of 9.7ms //we don't take 10ms because there is a 200us delay after zero cross //and before the INT pin goes low and another 100us delay after INT pin //goes low and SCR fires for scr firing delay = 0 //so, we subtract 300us from 10ms to get 0.0097 sec //if raw adc value is 1023 then we have to get scr firing delay of 0.0097 //sec and so we have to use equation, //scr_firing_delay_in_seconds = raw_adc_value * 0.0097 / 1023.0 //so, the factor for multiplying with raw adc value is 0.0097 /1023.0 // which is (duration_for_half_wave_of_ac - minimum_gate_trigger_delay) / 1023.0 //adc_factor = (duration_for_half_wave_of_ac - minimum_gate_trigger_delay) / 1023.0; ////adc_factor = (duration_for_120_degrees - minimum_gate_trigger_delay) / 1023.0; adc_factor = (duration_for_half_wave_of_ac - minimum_gate_trigger_delay) / 1023.0; INTEDG0_bit = 0; INT0IF_bit = 0; INT0IE_bit = 1; INTEDG1_bit = 0; INT1IF_bit = 0; INT1IE_bit = 1; INTEDG2_bit = 0; INT2IF_bit = 0; INT2IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while(1) { raw_adc_value = (unsigned int)ADC_Read(0); Delay_ms(1); if(previous_raw_adc_value != raw_adc_value) { //to get scr firing delay of 0 to 9.97ms (10ms - 300us, 180 degree) for 0 to //1023 raw adc //value //**** to get scr firing delay of 0 to 6.6467ms (6.67ms - 300us, 120 degrees) //1023 raw adc //value scr_firing_delay_in_seconds = (double)raw_adc_value * adc_factor; //only if adc value is greater than 0 then calculate the timer //reload value which is used in ISR to get the required scr //firing delay ////if(raw_adc_value > 0) { timer_reload_value = (unsigned int)(timer_resolution - (scr_firing_delay_in_seconds / timer_tick_period)); ////} //as mentioned above there is 300us delay and for half of AC //period that is for 10ms, angle is 180 degrees, so for 300us //it is 5.4 degrees //so, to get scr firing angle between 5.4 degrees and (180 - 5.4) //degrees we use below equation //eg: if scr_firing_delay_in_seconds = 0 we get scr_firing_angle //= 5.4 degrees //if scr_firing_angle_in_seconds = 0.0097 then we get //scr_firing_angle = 174.6 degrees //due to initial 300us delay the scr can only conduct for max //174.6 degrees scr_firing_angle = 18000 * scr_firing_delay_in_seconds + 5.4; //finally scr conduction angle is calculated from below equation //eg: if scr firing angle is 5.4 degrees then scr conduction angle //is 180 - 5.4 = 174.6 degrees scr_conduction_angle = 180.0 - scr_firing_angle; //the scr firing angle floating point value is converted to //fixed length string with fixed lenght = 5 FloatToStr_FixLen(scr_firing_angle, txt, 5); //the string is printed on LCD line 1 LCD_Out(1,10,txt); //the degree symbol is printed on LCD at required position CustomChar(1,15); //the scr conduction angle floating point value is converted to //fixed length string with fixed lenght = 5 FloatToStr_FixLen(scr_conduction_angle, txt, 5); //the string is printed on LCD line 2 LCD_Out(2,10,txt); //the degree symbol is printed on LCD at required position CustomChar(2,15); //this is to make sure that this block of code is executed only //if adc value has changed previous_raw_adc_value = raw_adc_value; } } }
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 /* * Project name: Three Phase - Controlled Half Wave Rectifier * Revision History: 20170406: - initial release (A); * Description: Three Phase - Controlled Half Wave Rectifier Using SCRs * Test configuration: MCU: PIC18F46K22 **broken link removed** Dev.board: Custom Board Oscillator: XT, 4.0000 MHz Crystal Ext. Modules: None. SW: mikroC PRO for PIC [url]https://www.mikroe.com/mikroc/pic/[/url] * Author(s): Okada * Project Start Date: 04/04/2017 * Project Completion Date: * NOTES: - Three Phase - Controlled Half Wave Rectifier */ // LCD module connections sbit LCD_RS at LATE0_bit; sbit LCD_EN at LATD0_bit; sbit LCD_D4 at LATC3_bit; sbit LCD_D5 at LATC2_bit; sbit LCD_D6 at LATC1_bit; sbit LCD_D7 at LATC0_bit; sbit LCD_RS_Direction at TRISE0_bit; sbit LCD_EN_Direction at TRISD0_bit; sbit LCD_D4_Direction at TRISC3_bit; sbit LCD_D5_Direction at TRISC2_bit; sbit LCD_D6_Direction at TRISC1_bit; sbit LCD_D7_Direction at TRISC0_bit; // End LCD module connections sbit R_Phase_SCR_Gate_Trigger at LATD5_bit; sbit Y_Phase_SCR_Gate_Trigger at LATD6_bit; sbit B_Phase_SCR_Gate_Trigger at LATD7_bit; #define PHASE_R 0 #define PHASE_Y 1 #define PHASE_B 2 const code char txt1[] = "F Angle: "; const code char txt2[] = "C Angle: "; const char character[] = {6,9,9,6,0,0,0,0}; char txt[32], i = 0; unsigned char scr_select_counter = 0; unsigned char was_not_triggered[3] = {0, 0, 0}; unsigned char firing_angle_delay_greater_then_or_equal_to_120_degrees = 0; unsigned int timer_reload_value = 0x0000; unsigned int raw_adc_value = 0, previous_raw_adc_value = 2048; double scr_firing_delay_in_seconds = 0.0; double previous_scr_firing_delay_in_seconds = 20.0; double fosc = 4000000.0, timer_prescalar = 1.0; double mains_frequency = 50.0; double period = 0.0; double duration_for_half_wave_of_ac = 0.0; double duration_for_120_degrees = 0.006467; double minimum_gate_trigger_delay = 0.0003; double end_limit_delay = 0.0003; double adc_factor = 0.0; double timer_resolution = 65536.0; double timer_tick_period = 0.0; double scr_firing_angle = 0.0; double scr_conduction_angle = 0.0; //Timer1 //Prescaler 1:1; TMR1 void InitTimer1() { T1CON = 0x01; TMR1IF_bit = 0; TMR1H = timer_reload_value >> 8; TMR1L = timer_reload_value; TMR1IE_bit = 1; } void Interrupt() { R_Phase_SCR_Gate_Trigger = 0; Y_Phase_SCR_Gate_Trigger = 0; B_Phase_SCR_Gate_Trigger = 0; if((INT0IE_bit) && (INT0IF_bit)) { INT0IF_bit = 0; //INT0IE_bit = 0; scr_select_counter = 0; InitTimer1(); if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) { B_Phase_SCR_Gate_Trigger = 1; Delay_us(200); B_Phase_SCR_Gate_Trigger = 0; } } if((INT1IE_bit) && (INT1IF_bit)) { INT1IF_bit = 0; //INT1IE_bit = 0; scr_select_counter = 1; InitTimer1(); if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) { R_Phase_SCR_Gate_Trigger = 1; Delay_us(200); R_Phase_SCR_Gate_Trigger = 0; } } if((INT2IE_bit) && (INT2IF_bit)) { INT2IF_bit = 0; //INT2IE_bit = 0; scr_select_counter = 2; InitTimer1(); if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) { Y_Phase_SCR_Gate_Trigger = 1; Delay_us(200); Y_Phase_SCR_Gate_Trigger = 0; } } if((TMR1IE_bit) && (TMR1IF_bit)) { TMR1IE_bit = 0; TMR1IF_bit = 0; //Enter your code here switch(scr_select_counter) { case PHASE_R: R_Phase_SCR_Gate_Trigger = 1; Delay_us(200); R_Phase_SCR_Gate_Trigger = 0; /* if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 0) { R_Phase_SCR_Gate_Trigger = 1; } else { B_Phase_SCR_Gate_Trigger = 1; R_Phase_SCR_Gate_Trigger = 1; } Delay_us(200); B_Phase_SCR_Gate_Trigger = 0; R_Phase_SCR_Gate_Trigger = 0; //INT1IE_bit = 1; */ break; case PHASE_Y: Y_Phase_SCR_Gate_Trigger = 1; Delay_us(200); Y_Phase_SCR_Gate_Trigger = 0; /* if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 0) { Y_Phase_SCR_Gate_Trigger = 1; } else { R_Phase_SCR_Gate_Trigger = 1; Y_Phase_SCR_Gate_Trigger = 1; } Delay_us(200); R_Phase_SCR_Gate_Trigger = 0; Y_Phase_SCR_Gate_Trigger = 0; //INT2IE_bit = 1; */ break; case PHASE_B: B_Phase_SCR_Gate_Trigger = 1; Delay_us(200); B_Phase_SCR_Gate_Trigger = 0; /* if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 0) { B_Phase_SCR_Gate_Trigger = 1; } else { Y_Phase_SCR_Gate_Trigger = 1; B_Phase_SCR_Gate_Trigger = 1; } Delay_us(200); Y_Phase_SCR_Gate_Trigger = 0; B_Phase_SCR_Gate_Trigger = 0; //INT0IE_bit = 1; */ break; }; } } void CustomChar(char pos_row, char pos_char) { char i; Lcd_Cmd(64); for(i = 0; i <= 7; i++) Lcd_Chr_CP(character[i]); Lcd_Chr(pos_row, pos_char, 0); } char *CopyConst2Ram(char * dest, const code char *src) { char *d; d = dest; for(;*dest++ = *src++;); return d; } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ADCON1 = 0x80; ADCON2 = 0b10110001; ANSELA = 0x01; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0x01; TRISB = 0x07; TRISC = 0x00; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LATE = 0x00; Delay_ms(200); LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,CopyConst2Ram(txt, txt1)); LCD_Out(2,1,CopyConst2Ram(txt, txt2)); timer_tick_period = 1.0 / (fosc / 4.0 / timer_prescalar); //mains ac period period = 1.0 / mains_frequency; //duration for 180 degrees duration_for_half_wave_of_ac = period / 2.0; //**** duration_for_120_degrees = period / 3.0; //for raw adc value = 0 we have to get scr firing delay = 0 sec and //for raw adc value 1023 we have to get scr firing delay of 9.7ms //we don't take 10ms because there is a 200us delay after zero cross //and before the INT pin goes low and another 100us delay after INT pin //goes low and SCR fires for scr firing delay = 0 //so, we subtract 300us from 10ms to get 0.0097 sec //if raw adc value is 1023 then we have to get scr firing delay of 0.0097 //sec and so we have to use equation, //scr_firing_delay_in_seconds = raw_adc_value * 0.0097 / 1023.0 //so, the factor for multiplying with raw adc value is 0.0097 /1023.0 // which is (duration_for_half_wave_of_ac - minimum_gate_trigger_delay) / 1023.0 //adc_factor = (duration_for_half_wave_of_ac - minimum_gate_trigger_delay) / 1023.0; ////adc_factor = (duration_for_120_degrees - minimum_gate_trigger_delay) / 1023.0; adc_factor = (duration_for_half_wave_of_ac - minimum_gate_trigger_delay - end_limit_delay) / 1023.0; INTEDG0_bit = 0; INT0IF_bit = 0; INT0IE_bit = 1; INTEDG1_bit = 0; INT1IF_bit = 0; INT1IE_bit = 1; INTEDG2_bit = 0; INT2IF_bit = 0; INT2IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while(1) { raw_adc_value = (unsigned int)ADC_Read(0); Delay_us(50); if(previous_raw_adc_value != raw_adc_value) { //to get scr firing delay of 0 to 9.97ms (10ms - 300us, 180 degree) for 0 to //1023 raw adc //value //**** to get scr firing delay of 0 to 6.6467ms (6.67ms - 300us, 120 degrees) //1023 raw adc //value scr_firing_delay_in_seconds = (double)raw_adc_value * adc_factor; //only if adc value is greater than 0 then calculate the timer //reload value which is used in ISR to get the required scr //firing delay ////if(raw_adc_value > 0) { timer_reload_value = (unsigned int)(timer_resolution - (scr_firing_delay_in_seconds / timer_tick_period)); ////} if(scr_firing_delay_in_seconds >= 6.67) { firing_angle_delay_greater_then_or_equal_to_120_degrees = 1; } else { firing_angle_delay_greater_then_or_equal_to_120_degrees = 0; } //as mentioned above there is 300us delay and for half of AC //period that is for 10ms, angle is 180 degrees, so for 300us //it is 5.4 degrees //so, to get scr firing angle between 5.4 degrees and (180 - 5.4) //degrees we use below equation //eg: if scr_firing_delay_in_seconds = 0 we get scr_firing_angle //= 5.4 degrees //if scr_firing_angle_in_seconds = 0.0097 then we get //scr_firing_angle = 174.6 degrees //due to initial 300us delay the scr can only conduct for max //174.6 degrees scr_firing_angle = 18000 * scr_firing_delay_in_seconds + 5.4; //finally scr conduction angle is calculated from below equation //eg: if scr firing angle is 5.4 degrees then scr conduction angle //is 180 - 5.4 = 174.6 degrees scr_conduction_angle = 180.0 - scr_firing_angle; //the scr firing angle floating point value is converted to //fixed length string with fixed lenght = 5 FloatToStr_FixLen(scr_firing_angle, txt, 5); //the string is printed on LCD line 1 LCD_Out(1,10,txt); //the degree symbol is printed on LCD at required position CustomChar(1,15); //the scr conduction angle floating point value is converted to //fixed length string with fixed lenght = 5 FloatToStr_FixLen(scr_conduction_angle, txt, 5); //the string is printed on LCD line 2 LCD_Out(2,10,txt); //the degree symbol is printed on LCD at required position CustomChar(2,15); //this is to make sure that this block of code is executed only //if adc value has changed previous_raw_adc_value = raw_adc_value; } } }
void Interrupt() {
R_Phase_SCR_Gate_Trigger = 0;
Y_Phase_SCR_Gate_Trigger = 0;
B_Phase_SCR_Gate_Trigger = 0;
if((INT0IE_bit) && (INT0IF_bit)) {
INT0IF_bit = 0;
//INT0IE_bit = 0;
scr_select_counter = 0;
InitTimer1();
if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) {
B_Phase_SCR_Gate_Trigger = 1;
[COLOR="#FF0000"]Delay_us(200);[/COLOR]
B_Phase_SCR_Gate_Trigger = 0;
}
}
if((INT1IE_bit) && (INT1IF_bit)) {
INT1IF_bit = 0;
//INT1IE_bit = 0;
scr_select_counter = 1;
InitTimer1();
if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) {
R_Phase_SCR_Gate_Trigger = 1;
[COLOR="#FF0000"]Delay_us(200);[/COLOR]
R_Phase_SCR_Gate_Trigger = 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 //Timer0 //Prescaler 1:2; TMR0 Preload = 106; Actual Interrupt Time : 300 us //Place/Copy this part in declaration section void InitTimer0() { T0CON = 0xC0; TMR0L = 0x6A; TMR0IE_bit = 1; } //Timer1 //Prescaler 1:1; TMR1 void InitTimer1() { T1CON = 0x01; TMR1IF_bit = 0; TMR1H = timer_reload_value >> 8; TMR1L = timer_reload_value; TMR1IE_bit = 1; } void Interrupt() { R_Phase_SCR_Gate_Trigger = 0; Y_Phase_SCR_Gate_Trigger = 0; B_Phase_SCR_Gate_Trigger = 0; if((INT0IE_bit) && (INT0IF_bit)) { INT0IF_bit = 0; scr_select_counter = 0; InitTimer1(); if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) { B_Phase_SCR_Gate_Trigger = 1; InitTimer0(); } } if((INT1IE_bit) && (INT1IF_bit)) { INT1IF_bit = 0; scr_select_counter = 1; InitTimer1(); if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) { R_Phase_SCR_Gate_Trigger = 1; InitTimer0(); } } if((INT2IE_bit) && (INT2IF_bit)) { INT2IF_bit = 0; scr_select_counter = 2; InitTimer1(); if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) { Y_Phase_SCR_Gate_Trigger = 1; InitTimer0(); } } if((TMR1IE_bit) && (TMR1IF_bit)) { TMR1IE_bit = 0; TMR1IF_bit = 0; //Enter your code here switch(scr_select_counter) { case PHASE_R: R_Phase_SCR_Gate_Trigger = 1; break; case PHASE_Y: Y_Phase_SCR_Gate_Trigger = 1; break; case PHASE_B: B_Phase_SCR_Gate_Trigger = 1; break; }; } if((TMR0IE_bit) && (TMR0IF_bit)) { R_Phase_SCR_Gate_Trigger = 0; Y_Phase_SCR_Gate_Trigger = 0; B_Phase_SCR_Gate_Trigger = 0; TMR0IF_bit = 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 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 /* * Project name: Three Phase - Controlled Half Wave Rectifier * Revision History: 20170406: - initial release (A); * Description: Three Phase - Controlled Half Wave Rectifier Using SCRs * Test configuration: MCU: PIC18F46K22 **broken link removed** Dev.board: Custom Board Oscillator: XT, 4.0000 MHz Crystal Ext. Modules: None. SW: mikroC PRO for PIC [url]https://www.mikroe.com/mikroc/pic/[/url] * Author(s): Okada * Project Start Date: 04/04/2017 * Project Completion Date: * NOTES: - Three Phase - Controlled Half Wave Rectifier */ // LCD module connections sbit LCD_RS at LATE0_bit; sbit LCD_EN at LATD0_bit; sbit LCD_D4 at LATC3_bit; sbit LCD_D5 at LATC2_bit; sbit LCD_D6 at LATC1_bit; sbit LCD_D7 at LATC0_bit; sbit LCD_RS_Direction at TRISE0_bit; sbit LCD_EN_Direction at TRISD0_bit; sbit LCD_D4_Direction at TRISC3_bit; sbit LCD_D5_Direction at TRISC2_bit; sbit LCD_D6_Direction at TRISC1_bit; sbit LCD_D7_Direction at TRISC0_bit; // End LCD module connections sbit R_Phase_SCR_Gate_Trigger at LATD5_bit; sbit Y_Phase_SCR_Gate_Trigger at LATD6_bit; sbit B_Phase_SCR_Gate_Trigger at LATD7_bit; #define PHASE_R 0 #define PHASE_Y 1 #define PHASE_B 2 const code char txt1[] = "F Angle: "; const code char txt2[] = "C Angle: "; const char character[] = {6,9,9,6,0,0,0,0}; char txt[32], i = 0; unsigned char scr_select_counter = 0; unsigned char was_not_triggered[3] = {0, 0, 0}; unsigned char firing_angle_delay_greater_then_or_equal_to_120_degrees = 0; unsigned int timer_reload_value = 0x0000; unsigned int raw_adc_value = 0, previous_raw_adc_value = 2048; double scr_firing_delay_in_seconds = 0.0; double previous_scr_firing_delay_in_seconds = 20.0; double fosc = 4000000.0, timer_prescalar = 1.0; double mains_frequency = 50.0; double period = 0.0; double duration_for_half_wave_of_ac = 0.0; double duration_for_120_degrees = 0.006467; double minimum_gate_trigger_delay = 0.0003; double end_limit_delay = 0.0003; double adc_factor = 0.0; double timer_resolution = 65536.0; double timer_tick_period = 0.0; double scr_firing_angle = 0.0; double scr_conduction_angle = 0.0; //Timer0 //Prescaler 1:2; TMR0 Preload = 106; Actual Interrupt Time : 300 us //Place/Copy this part in declaration section void InitTimer0() { T0CON = 0xC0; TMR0L = 0x6A; TMR0IE_bit = 1; } //Timer1 //Prescaler 1:1; TMR1 void InitTimer1() { T1CON = 0x01; TMR1IF_bit = 0; TMR1H = timer_reload_value >> 8; TMR1L = timer_reload_value; TMR1IE_bit = 1; } void Interrupt() { R_Phase_SCR_Gate_Trigger = 0; Y_Phase_SCR_Gate_Trigger = 0; B_Phase_SCR_Gate_Trigger = 0; if((INT0IE_bit) && (INT0IF_bit)) { INT0IF_bit = 0; scr_select_counter = 0; InitTimer1(); if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) { B_Phase_SCR_Gate_Trigger = 1; } TMR0L = 0x6A; } if((INT1IE_bit) && (INT1IF_bit)) { INT1IF_bit = 0; scr_select_counter = 1; InitTimer1(); if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) { R_Phase_SCR_Gate_Trigger = 1; } TMR0L = 0x6A; } if((INT2IE_bit) && (INT2IF_bit)) { INT2IF_bit = 0; scr_select_counter = 2; InitTimer1(); if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) { Y_Phase_SCR_Gate_Trigger = 1; } TMR0L = 0x6A; } if((TMR1IE_bit) && (TMR1IF_bit)) { TMR1IE_bit = 0; TMR1IF_bit = 0; //Enter your code here switch(scr_select_counter) { case PHASE_R: R_Phase_SCR_Gate_Trigger = 1; break; case PHASE_Y: Y_Phase_SCR_Gate_Trigger = 1; break; case PHASE_B: B_Phase_SCR_Gate_Trigger = 1; break; }; TMR0L = 0x6A; } if((TMR0IE_bit) && (TMR0IF_bit)) { R_Phase_SCR_Gate_Trigger = 0; Y_Phase_SCR_Gate_Trigger = 0; B_Phase_SCR_Gate_Trigger = 0; TMR0IF_bit = 0; } } void CustomChar(char pos_row, char pos_char) { char i; Lcd_Cmd(64); for(i = 0; i <= 7; i++) Lcd_Chr_CP(character[i]); Lcd_Chr(pos_row, pos_char, 0); } char *CopyConst2Ram(char * dest, const code char *src) { char *d; d = dest; for(;*dest++ = *src++;); return d; } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ADCON1 = 0x80; ADCON2 = 0b10110001; ANSELA = 0x01; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0x01; TRISB = 0x07; TRISC = 0x00; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LATE = 0x00; Delay_ms(200); LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,CopyConst2Ram(txt, txt1)); LCD_Out(2,1,CopyConst2Ram(txt, txt2)); timer_tick_period = 1.0 / (fosc / 4.0 / timer_prescalar); //mains ac period period = 1.0 / mains_frequency; //duration for 180 degrees duration_for_half_wave_of_ac = period / 2.0; //**** duration_for_120_degrees = period / 3.0; //for raw adc value = 0 we have to get scr firing delay = 0 sec and //for raw adc value 1023 we have to get scr firing delay of 9.7ms //we don't take 10ms because there is a 200us delay after zero cross //and before the INT pin goes low and another 100us delay after INT pin //goes low and SCR fires for scr firing delay = 0 //so, we subtract 300us from 10ms to get 0.0097 sec //if raw adc value is 1023 then we have to get scr firing delay of 0.0097 //sec and so we have to use equation, //scr_firing_delay_in_seconds = raw_adc_value * 0.0097 / 1023.0 //so, the factor for multiplying with raw adc value is 0.0097 /1023.0 // which is (duration_for_half_wave_of_ac - minimum_gate_trigger_delay) / 1023.0 //adc_factor = (duration_for_half_wave_of_ac - minimum_gate_trigger_delay) / 1023.0; ////adc_factor = (duration_for_120_degrees - minimum_gate_trigger_delay) / 1023.0; adc_factor = (duration_for_half_wave_of_ac - minimum_gate_trigger_delay - end_limit_delay) / 1023.0; InitTimer0(); INTEDG0_bit = 0; INT0IF_bit = 0; INT0IE_bit = 1; INTEDG1_bit = 0; INT1IF_bit = 0; INT1IE_bit = 1; INTEDG2_bit = 0; INT2IF_bit = 0; INT2IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while(1) { raw_adc_value = (unsigned int)ADC_Read(0); Delay_us(50); if(previous_raw_adc_value != raw_adc_value) { //to get scr firing delay of 0 to 9.97ms (10ms - 300us, 180 degree) for 0 to //1023 raw adc //value //**** to get scr firing delay of 0 to 6.6467ms (6.67ms - 300us, 120 degrees) //1023 raw adc //value scr_firing_delay_in_seconds = (double)raw_adc_value * adc_factor; //only if adc value is greater than 0 then calculate the timer //reload value which is used in ISR to get the required scr //firing delay ////if(raw_adc_value > 0) { timer_reload_value = (unsigned int)(timer_resolution - (scr_firing_delay_in_seconds / timer_tick_period)); ////} if(scr_firing_delay_in_seconds >= 6.67) { firing_angle_delay_greater_then_or_equal_to_120_degrees = 1; } else { firing_angle_delay_greater_then_or_equal_to_120_degrees = 0; } //as mentioned above there is 300us delay and for half of AC //period that is for 10ms, angle is 180 degrees, so for 300us //it is 5.4 degrees //so, to get scr firing angle between 5.4 degrees and (180 - 5.4) //degrees we use below equation //eg: if scr_firing_delay_in_seconds = 0 we get scr_firing_angle //= 5.4 degrees //if scr_firing_angle_in_seconds = 0.0097 then we get //scr_firing_angle = 174.6 degrees //due to initial 300us delay the scr can only conduct for max //174.6 degrees scr_firing_angle = 18000 * scr_firing_delay_in_seconds + 5.4; //finally scr conduction angle is calculated from below equation //eg: if scr firing angle is 5.4 degrees then scr conduction angle //is 180 - 5.4 = 174.6 degrees scr_conduction_angle = 180.0 - scr_firing_angle; //the scr firing angle floating point value is converted to //fixed length string with fixed lenght = 5 FloatToStr_FixLen(scr_firing_angle, txt, 5); //the string is printed on LCD line 1 LCD_Out(1,10,txt); //the degree symbol is printed on LCD at required position CustomChar(1,15); //the scr conduction angle floating point value is converted to //fixed length string with fixed lenght = 5 FloatToStr_FixLen(scr_conduction_angle, txt, 5); //the string is printed on LCD line 2 LCD_Out(2,10,txt); //the degree symbol is printed on LCD at required position CustomChar(2,15); //this is to make sure that this block of code is executed only //if adc value has changed previous_raw_adc_value = raw_adc_value; } } }
Use the hardware to generate the delay and maybe even the firing signal. (don´t use busy_wait (=software) delay in an ISR)Then what is the solution to remove the Delays from ISR ? I can't put the SCR firing code in main loop. The firing pulse width varies if I set flag in ISR and then fire the SCR in main loop after testing the flag in main loop.
Does the PIC have a wave generation mode (like PWM?).
Beyond "not liking" PWM mode, which could be dismissed either as snobbism or ignorance
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 /* * Project name: Three Phase - Controlled Half Wave Rectifier * Revision History: 20170406: - initial release (A); * Description: Three Phase - Controlled Half Wave Rectifier Using SCRs * Test configuration: MCU: PIC18F46K22 **broken link removed** Dev.board: Custom Board Oscillator: XT, 4.0000 MHz Crystal Ext. Modules: None. SW: mikroC PRO for PIC [url]https://www.mikroe.com/mikroc/pic/[/url] * Author(s): Okada * Project Start Date: 04/04/2017 * Project Completion Date: * NOTES: - Three Phase - Controlled Half Wave Rectifier */ // LCD module connections sbit LCD_RS at LATE0_bit; sbit LCD_EN at LATD0_bit; sbit LCD_D4 at LATC3_bit; sbit LCD_D5 at LATC2_bit; sbit LCD_D6 at LATC1_bit; sbit LCD_D7 at LATC0_bit; sbit LCD_RS_Direction at TRISE0_bit; sbit LCD_EN_Direction at TRISD0_bit; sbit LCD_D4_Direction at TRISC3_bit; sbit LCD_D5_Direction at TRISC2_bit; sbit LCD_D6_Direction at TRISC1_bit; sbit LCD_D7_Direction at TRISC0_bit; // End LCD module connections sbit R_Phase_SCR_Gate_Trigger at LATD5_bit; sbit Y_Phase_SCR_Gate_Trigger at LATD6_bit; sbit B_Phase_SCR_Gate_Trigger at LATD7_bit; #define PHASE_R 0 #define PHASE_Y 1 #define PHASE_B 2 const code char txt1[] = "F Angle: "; const code char txt2[] = "C Angle: "; const char character[] = {6,9,9,6,0,0,0,0}; char txt[32], i = 0; unsigned char scr_select_counter = 0; unsigned char was_not_triggered[3] = {0, 0, 0}; unsigned char firing_angle_delay_greater_then_or_equal_to_120_degrees = 0; unsigned int timer_reload_value = 0x0000; unsigned int raw_adc_value = 0, previous_raw_adc_value = 2048; double scr_firing_delay_in_seconds = 0.0; double previous_scr_firing_delay_in_seconds = 20.0; double fosc = 4000000.0, timer_prescalar = 1.0; double mains_frequency = 50.0; double period = 0.0; double duration_for_half_wave_of_ac = 0.0; double duration_for_120_degrees = 0.006467; double minimum_gate_trigger_delay = 0.0003; double end_limit_delay = 0.0003; double adc_factor = 0.0; double timer_resolution = 65536.0; double timer_tick_period = 0.0; double scr_firing_angle = 0.0; double scr_conduction_angle = 0.0; //Timer0 //Prescaler 1:2; TMR0 Preload = 106; Actual Interrupt Time : 300 us //Place/Copy this part in declaration section void InitTimer0() { T0CON = 0xC0; TMR0L = 0x6A; TMR0IE_bit = 1; } //Timer1 //Prescaler 1:1; TMR1 void InitTimer1() { T1CON = 0x01; TMR1IF_bit = 0; TMR1H = timer_reload_value >> 8; TMR1L = timer_reload_value; TMR1IE_bit = 1; } void Interrupt() { if((TMR0IE_bit) && (TMR0IF_bit)) { TMR0IE_bit = 0; TMR0IF_bit = 0; R_Phase_SCR_Gate_Trigger = 0; Y_Phase_SCR_Gate_Trigger = 0; B_Phase_SCR_Gate_Trigger = 0; } if((INT0IE_bit) && (INT0IF_bit)) { INT0IF_bit = 0; scr_select_counter = 0; InitTimer1(); /* if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) { B_Phase_SCR_Gate_Trigger = 1; InitTimer0(); } */ } if((INT1IE_bit) && (INT1IF_bit)) { INT1IF_bit = 0; scr_select_counter = 1; InitTimer1(); /* if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) { R_Phase_SCR_Gate_Trigger = 1; InitTimer0(); } */ } if((INT2IE_bit) && (INT2IF_bit)) { INT2IF_bit = 0; scr_select_counter = 2; InitTimer1(); /* if(firing_angle_delay_greater_then_or_equal_to_120_degrees == 1) { Y_Phase_SCR_Gate_Trigger = 1; InitTimer0(); } */ } if((TMR1IE_bit) && (TMR1IF_bit)) { TMR1IE_bit = 0; TMR1IF_bit = 0; //Enter your code here switch(scr_select_counter) { case PHASE_R: R_Phase_SCR_Gate_Trigger = 1; break; case PHASE_Y: Y_Phase_SCR_Gate_Trigger = 1; break; case PHASE_B: B_Phase_SCR_Gate_Trigger = 1; break; }; InitTimer0(); } } void CustomChar(char pos_row, char pos_char) { char i; Lcd_Cmd(64); for(i = 0; i <= 7; i++) Lcd_Chr_CP(character[i]); Lcd_Chr(pos_row, pos_char, 0); } char *CopyConst2Ram(char * dest, const code char *src) { char *d; d = dest; for(;*dest++ = *src++;); return d; } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ADCON1 = 0x80; ADCON2 = 0b10110001; ANSELA = 0x01; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0x01; TRISB = 0x07; TRISC = 0x00; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LATE = 0x00; Delay_ms(200); LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,CopyConst2Ram(txt, txt1)); LCD_Out(2,1,CopyConst2Ram(txt, txt2)); timer_tick_period = 1.0 / (fosc / 4.0 / timer_prescalar); //mains ac period period = 1.0 / mains_frequency; //duration for 180 degrees duration_for_half_wave_of_ac = period / 2.0; //**** duration_for_120_degrees = period / 3.0; //for raw adc value = 0 we have to get scr firing delay = 0 sec and //for raw adc value 1023 we have to get scr firing delay of 9.7ms //we don't take 10ms because there is a 200us delay after zero cross //and before the INT pin goes low and another 100us delay after INT pin //goes low and SCR fires for scr firing delay = 0 //so, we subtract 300us from 10ms to get 0.0097 sec //if raw adc value is 1023 then we have to get scr firing delay of 0.0097 //sec and so we have to use equation, //scr_firing_delay_in_seconds = raw_adc_value * 0.0097 / 1023.0 //so, the factor for multiplying with raw adc value is 0.0097 /1023.0 // which is (duration_for_half_wave_of_ac - minimum_gate_trigger_delay) / 1023.0 //adc_factor = (duration_for_half_wave_of_ac - minimum_gate_trigger_delay) / 1023.0; ////adc_factor = (duration_for_120_degrees - minimum_gate_trigger_delay) / 1023.0; adc_factor = (duration_for_half_wave_of_ac - minimum_gate_trigger_delay - end_limit_delay) / 1023.0; INTEDG0_bit = 0; INT0IF_bit = 0; INT0IE_bit = 1; INTEDG1_bit = 0; INT1IF_bit = 0; INT1IE_bit = 1; INTEDG2_bit = 0; INT2IF_bit = 0; INT2IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while(1) { raw_adc_value = (unsigned int)ADC_Read(0); Delay_us(50); if(previous_raw_adc_value != raw_adc_value) { //to get scr firing delay of 0 to 9.97ms (10ms - 300us, 180 degree) for 0 to //1023 raw adc //value //**** to get scr firing delay of 0 to 6.6467ms (6.67ms - 300us, 120 degrees) //1023 raw adc //value scr_firing_delay_in_seconds = (double)raw_adc_value * adc_factor; //only if adc value is greater than 0 then calculate the timer //reload value which is used in ISR to get the required scr //firing delay if(raw_adc_value > 0) { timer_reload_value = (unsigned int)(timer_resolution - (scr_firing_delay_in_seconds / timer_tick_period)); } /* if(scr_firing_delay_in_seconds >= 6.37) { firing_angle_delay_greater_then_or_equal_to_120_degrees = 1; } else { firing_angle_delay_greater_then_or_equal_to_120_degrees = 0; } */ //as mentioned above there is 300us delay and for half of AC //period that is for 10ms, angle is 180 degrees, so for 300us //it is 5.4 degrees //so, to get scr firing angle between 5.4 degrees and (180 - 5.4) //degrees we use below equation //eg: if scr_firing_delay_in_seconds = 0 we get scr_firing_angle //= 5.4 degrees //if scr_firing_angle_in_seconds = 0.0097 then we get //scr_firing_angle = 174.6 degrees //due to initial 300us delay the scr can only conduct for max //174.6 degrees scr_firing_angle = 18000 * scr_firing_delay_in_seconds + 5.4; //finally scr conduction angle is calculated from below equation //eg: if scr firing angle is 5.4 degrees then scr conduction angle //is 180 - 5.4 = 174.6 degrees scr_conduction_angle = 180.0 - scr_firing_angle; //the scr firing angle floating point value is converted to //fixed length string with fixed lenght = 5 FloatToStr_FixLen(scr_firing_angle, txt, 5); //the string is printed on LCD line 1 LCD_Out(1,10,txt); //the degree symbol is printed on LCD at required position CustomChar(1,15); //the scr conduction angle floating point value is converted to //fixed length string with fixed lenght = 5 FloatToStr_FixLen(scr_conduction_angle, txt, 5); //the string is printed on LCD line 2 LCD_Out(2,10,txt); //the degree symbol is printed on LCD at required position CustomChar(2,15); //this is to make sure that this block of code is executed only //if adc value has changed previous_raw_adc_value = raw_adc_value; } } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 //Timer0 //Prescaler 1:1; TMR0 Preload = 58846; Actual Interrupt Time : 6.69 ms //Place/Copy this part in declaration section void InitTimer0() { T0CON = 0x88; TMR0H = 0xE5; TMR0L = 0xDE; TMR0IE_bit = 1; }
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?