milan.rajik
Banned
@FvM
I have fixed the opto circuit. Here is fixed circuit.
Edit:
Changed the code a little but still not working.
- - - Updated - - -
New code but the buttons not yet working.
I have fixed the opto circuit. Here is fixed circuit.
Edit:
Changed the code a little but still not working.
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 char milliSec[4] = {0, 0, 0, 0}, i = 0, max_ = 0, initial = 0, readKeys = 0; sbit Gate1_Trigger at LATD0_bit; sbit Gate1_Trigger_Direction at TRISD0_bit; sbit Gate2_Trigger at LATD1_bit; sbit Gate2_Trigger_Direction at TRISD1_bit; sbit Gate3_Trigger at LATD2_bit; sbit Gate3_Trigger_Direction at TRISD2_bit; sbit Gate4_Trigger at LATD3_bit; sbit Gate4_Trigger_Direction at TRISD3_bit; //Timer1 //Prescaler 1:1; TMR1 Preload = 63536; Actual Interrupt Time : 1 ms //Place/Copy this part in declaration section void InitTimer1() { T1CON = 0x01; TMR1IF_bit = 0; TMR1H = 0xF8; TMR1L = 0x30; TMR1IE_bit = 1; } void InitTimer1Initial() { T1CON = 0x01; TMR1IF_bit = 0; TMR1H = 0xFE; TMR1L = 0xFF; TMR1IE_bit = 1; } void interrupt() { if(INT0IF_bit) { i = 0; if(milliSec[0] == 0) //if any of milliSec[x] is 0 then fire the corresponding triac immediately after ZC. Gate1_Trigger = 1; if(milliSec[1] == 0) Gate2_Trigger = 1; if(milliSec[2] == 0) Gate3_Trigger = 1; if(milliSec[3] == 0) Gate4_Trigger = 1; InitTimer1(); //Start 1 ms timer interrupt Delay_us(100); Gate1_Trigger = 0; Gate2_Trigger = 0; Gate3_Trigger = 0; Gate4_Trigger = 0; INT0IF_bit = 0; } if(TMR1IF_bit) { if(i == (milliSec[0])) Gate1_Trigger = 1; if(i == (milliSec[1])) Gate2_Trigger = 1; if(i == (milliSec[2])) Gate3_Trigger = 1; if(i == (milliSec[3])) Gate4_Trigger = 1; Delay_us(100); Gate1_Trigger = 0; Gate2_Trigger = 0; Gate3_Trigger = 0; Gate4_Trigger = 0; if(i == max_)TMR1ON_bit = 0; ++i; TMR1IF_bit = 0; // 1ms Timer interrupt TMR1H = 0xF8; TMR1L = 0x30; } if(INT1IF_bit) { readKeys = 1; INT1IF_bit = 0; } } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xC0; TRISB = 0x03; TRISC = 0xFF; TRISD = 0x00; TRISE = 0x08; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x08; LATA = 0x00; LATB = 0x00; LATC = 0xFF; LATD = 0x00; LATE = 0x00; Gate1_Trigger_Direction = 0; Gate2_Trigger_Direction = 0; Gate3_Trigger_Direction = 0; Gate4_Trigger_Direction = 0; Gate1_Trigger = 0; Gate2_Trigger = 0; Gate3_Trigger = 0; Gate4_Trigger = 0; INTEDG0_bit = 0; INT0IF_bit = 0; INT0IE_bit = 1; INTEDG1_bit = 1; INT1IF_bit = 0; INT1IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while (1) { if(milliSec[0] > milliSec[1])max_ = milliSec[0]; else max_ = milliSec[1]; if(milliSec[1] > milliSec[2])max_ = milliSec[1]; else max_ = milliSec[2]; if(milliSec[2] > milliSec[3])max_ = milliSec[2]; else max_ = milliSec[3]; if(milliSec[3] > milliSec[4])max_ = milliSec[3]; else max_ = milliSec[4]; if(readKeys) { Delay_ms(50); if(PORTC == 0x01) if(milliSec[0] < 9)++milliSec[0]; else if(PORTC == 0x02) if(milliSec[0] > 0)--milliSec[0]; else if(PORTC == 0x04) if(milliSec[1] < 9)++milliSec[1]; else if(PORTC == 0x08) if(milliSec[1] > 0)--milliSec[1]; else if(PORTC == 0x10) if(milliSec[2] < 9)++milliSec[2]; else if(PORTC == 0x20) if(milliSec[2] > 0)--milliSec[2]; else if(PORTC == 0x40) if(milliSec[3] < 9)++milliSec[3]; else if(PORTC == 0x80) if(milliSec[3] > 0)--milliSec[3]; readKeys = 0; } } }
- - - Updated - - -
New code but the buttons not yet working.
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 char milliSec[4] = {0, 0, 0, 0}, i = 0, max_ = 0, readKeys = 0; sbit Gate1_Trigger at LATD0_bit; sbit Gate1_Trigger_Direction at TRISD0_bit; sbit Gate2_Trigger at LATD1_bit; sbit Gate2_Trigger_Direction at TRISD1_bit; sbit Gate3_Trigger at LATD2_bit; sbit Gate3_Trigger_Direction at TRISD2_bit; sbit Gate4_Trigger at LATD3_bit; sbit Gate4_Trigger_Direction at TRISD3_bit; //Timer1 //Prescaler 1:1; TMR1 Preload = 63536; Actual Interrupt Time : 1 ms //Place/Copy this part in declaration section void InitTimer1() { T1CON = 0x01; TMR1IF_bit = 0; TMR1H = 0xF8; TMR1L = 0x30; TMR1IE_bit = 1; } void interrupt() { if(INT0IF_bit) { i = 0; if(milliSec[0] == 0) //if any of milliSec[x] is 0 then fire the corresponding triac immediately after ZC. Gate1_Trigger = 1; //These 4 if statements will take some 15 us to execute if(milliSec[1] == 0) Gate2_Trigger = 1; if(milliSec[2] == 0) Gate3_Trigger = 1; if(milliSec[3] == 0) Gate4_Trigger = 1; InitTimer1(); //Start 1 ms timer interrupt Delay_us(100); //Timer will be running and before 1 ms expires the below code which requires some 15 us will execute Gate1_Trigger = 0; Gate2_Trigger = 0; Gate3_Trigger = 0; Gate4_Trigger = 0; INT0IF_bit = 0; } if(TMR1IF_bit) { if(i == (milliSec[0])) //These 4 if statements will take some 15 us to execute Gate1_Trigger = 1; if(i == (milliSec[1])) Gate2_Trigger = 1; if(i == (milliSec[2])) Gate3_Trigger = 1; if(i == (milliSec[3])) Gate4_Trigger = 1; Delay_us(100); Gate1_Trigger = 0; Gate2_Trigger = 0; Gate3_Trigger = 0; Gate4_Trigger = 0; if(i == max_)TMR1ON_bit = 0; ++i; TMR1IF_bit = 0; // 1ms Timer interrupt TMR1H = 0xF8; TMR1L = 0x30; } if(INT1IF_bit) { readKeys = 1; INT1IF_bit = 0; } } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xC0; TRISB = 0x03; TRISC = 0xFF; TRISD = 0x00; TRISE = 0x08; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x08; LATA = 0x00; LATB = 0x00; LATC = 0xFF; LATD = 0x00; LATE = 0x00; Gate1_Trigger_Direction = 0; Gate2_Trigger_Direction = 0; Gate3_Trigger_Direction = 0; Gate4_Trigger_Direction = 0; Gate1_Trigger = 0; Gate2_Trigger = 0; Gate3_Trigger = 0; Gate4_Trigger = 0; INTEDG0_bit = 0; INT0IF_bit = 0; INT0IE_bit = 1; INTEDG1_bit = 1; INT1IF_bit = 0; INT1IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while (1) { if(milliSec[0] > milliSec[1])max_ = milliSec[0]; //These 4 if statements take some 5 us to execute else max_ = milliSec[1]; if(milliSec[1] > milliSec[2])max_ = milliSec[1]; else max_ = milliSec[2]; if(milliSec[2] > milliSec[3])max_ = milliSec[2]; else max_ = milliSec[3]; if(milliSec[3] > milliSec[4])max_ = milliSec[3]; else max_ = milliSec[4]; if(readKeys) { Delay_ms(50); //Debounce delay for button press if(PORTC == 0x01) if(milliSec[0] < 9)++milliSec[0]; else if(PORTC == 0x02) if(milliSec[0] > 0)--milliSec[0]; else if(PORTC == 0x04) if(milliSec[1] < 9)++milliSec[1]; else if(PORTC == 0x08) if(milliSec[1] > 0)--milliSec[1]; else if(PORTC == 0x10) if(milliSec[2] < 9)++milliSec[2]; else if(PORTC == 0x20) if(milliSec[2] > 0)--milliSec[2]; else if(PORTC == 0x40) if(milliSec[3] < 9)++milliSec[3]; else if(PORTC == 0x80) if(milliSec[3] > 0)--milliSec[3]; readKeys = 0; } } }
Attachments
Last edited: