milan.rajik
Banned
- Joined
- Apr 1, 2013
- Messages
- 2,524
- Helped
- 540
- Reputation
- 1,078
- Reaction score
- 524
- Trophy points
- 1,393
- Activity points
- 0
Code C - [expand] 1 VDelay(2 + x, 4000)
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 unsigned char myFlags; unsigned char milliSec = 0; sbit ZC at myFlags.B0; void interrupt() { if (INT0IF_bit){ ZC = 1; INT0IF_bit = 0; } } void main() { ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xFF; TRISB = 0x07; TRISD = 0x00; PORTA = 0x00; PORTB = 0x00; PORTD = 0x00; LATA = 0x00; LATB = 0x00; LATD = 0x00; INTEDG0_bit = 0; INT0IF_bit = 0; INT0IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while (1){ if(RB1_bit) { Delay_ms(100); if(RB1_Bit) if(milliSec < 10)milliSec += 1; Delay_ms(1000); } if(RB2_bit) { Delay_ms(100); if(RB2_Bit) if(milliSec > 0)milliSec -= 1; Delay_ms(1000); } while(!ZC); VDelay_Advanced_ms(milliSec, 4000); //Fire TRIAC after 1, 2, 3, 4, 5, 6, 7, 8, 9, msec for RB1 button press and release //Fire TRIAC after 9, 8, 7, 6, 5, 4, 3, 2, 1 msec for RB2 button press and release PORTD.F0 = 1; Delay_us(250); PORTD.F0 = 0; ZC = 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 unsigned char myFlags; unsigned char milliSec = 0, counter = 0; sbit ZC at myFlags.B0; //Timer1 //Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 500 ms //Place/Copy this part in declaration section void InitTimer1() { T1CON = 0x31; TMR1IF_bit = 0; TMR1H = 0x0B; TMR1L = 0xDC; TMR1IE_bit = 1; INTCON = 0xC0; } void interrupt() { if (INT0IF_bit){ ZC = 1; INT0IF_bit = 0; } if(TMR1IF_bit) { if(++counter == 2) { counter = 0; TMR1ON_bit = 0; } TMR1IF_bit = 0; TMR1H = 0x0B; TMR1L = 0xDC; } } void main() { ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xFF; TRISB = 0x07; TRISD = 0x00; PORTA = 0x00; PORTB = 0x00; PORTD = 0x00; LATA = 0x00; LATB = 0x00; LATD = 0x00; INTEDG0_bit = 0; INT0IF_bit = 0; INT0IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while (1){ if((RB1_bit) && (!TMR1ON_bit)) { Delay_ms(100); if((RB1_bit) && (!TMR1ON_bit)) if(milliSec < 10)milliSec += 1; InitTimer1(); } if((RB2_bit) && (!TMR1ON_bit)) { Delay_ms(100); if((RB2_bit) && (!TMR1ON_bit)) if(milliSec > 0)milliSec -= 1; InitTimer1(); } while(!ZC); VDelay_Advanced_ms(milliSec, 4000); //Fire TRIAC after 1, 2, 3, 4, 5, 6, 7, 8, 9, msec for RB1 button press and release //Fire TRIAC after 9, 8, 7, 6, 5, 4, 3, 2, 1 msec for RB2 button press and release PORTD.F0 = 1; Delay_us(250); PORTD.F0 = 0; ZC = 0; } }
For quick check of correct phase angle code operation, I would expect an oscilloscope waveform showing the AC, zero-crossing and gate control signal. The waveforms in post #1 don't reveal much information.
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 unsigned char myFlags; unsigned char milliSec = 9, counter = 0; char mask[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F}; sbit ZC at myFlags.B0; //Timer1 //Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 500 ms //Place/Copy this part in declaration section void InitTimer1() { T1CON = 0x31; TMR1IF_bit = 0; TMR1H = 0x0B; TMR1L = 0xDC; TMR1IE_bit = 1; INTCON = 0xC0; } void interrupt() { if (INT0IF_bit){ ZC = 1; INT0IF_bit = 0; } if(TMR1IF_bit) { if(++counter == 2) { counter = 0; TMR1ON_bit = 0; } TMR1IF_bit = 0; TMR1H = 0x0B; TMR1L = 0xDC; } } void main() { ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xFF; TRISB = 0x07; TRISC = 0x00; TRISD = 0x00; PORTA = 0x00; PORTB = 0x00; PORTD = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0xFF; LATD = 0x00; INTEDG0_bit = 0; INT0IF_bit = 0; INT0IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while (1){ if((RB1_bit) && (!TMR1ON_bit)) { Delay_ms(100); if((RB1_bit) && (!TMR1ON_bit)) if(milliSec > 0)milliSec -= 1; LATC = mask[9 - milliSec]; //display fan speed value on 7 segment display (SSD) InitTimer1(); //To create 1 sec delay between button presses //When timer is running and less than 1 sec TMR0ON_bit will be 1 and hence button press detect code will be disabled } if((RB2_bit) && (!TMR1ON_bit)) { Delay_ms(100); if((RB2_bit) && (!TMR1ON_bit)) if(milliSec < 9)milliSec += 1; LATC = mask[9 - milliSec]; //display fan speed value on 7 segment display (SSD) InitTimer1(); //To create 1 sec delay between button presses //When timer is running and less than 1 sec TMR0ON_bit will be 1 and hence button press detect code will be disabled } if(ZC) { VDelay_Advanced_ms(milliSec, 4000); //Fire TRIAC after 1, 2, 3, 4, 5, 6, 7, 8, 9, msec for RB1 button press and release //Fire TRIAC after 9, 8, 7, 6, 5, 4, 3, 2, 1 msec for RB2 button press and release LATD.F0 = 1; Delay_us(250); LATD.F0 = 0; ZC = 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 unsigned char milliSec = 9, counter = 0, i = 0; char mask[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F}; //Timer1 //Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 500 ms //Place/Copy this part in declaration section void InitTimer1() { T1CON = 0x31; TMR1IF_bit = 0; TMR1H = 0x0B; TMR1L = 0xDC; TMR1IE_bit = 1; INTCON = 0xC0; } void interrupt() { if(INT0IF_bit){ //Fire TRIAC after 1, 2, 3, 4, 5, 6, 7, 8, 9, msec for RB1 button press and release //Fire TRIAC after 9, 8, 7, 6, 5, 4, 3, 2, 1 msec for RB2 button press and release while(i != milliSec) { Delay_ms(1); i++; } i = 0; LATD.F0 = 1; Delay_us(250); LATD.F0 = 0; INT0IF_bit = 0; } if(TMR1IF_bit) { if(++counter == 2) { counter = 0; TMR1ON_bit = 0; } TMR1IF_bit = 0; TMR1H = 0x0B; TMR1L = 0xDC; } } void main() { ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xFF; TRISB = 0x07; TRISC = 0x00; TRISD = 0x00; PORTA = 0x00; PORTB = 0x00; PORTD = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0xFF; LATD = 0x00; INTEDG0_bit = 0; INT0IF_bit = 0; INT0IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while (1){ if((RB1_bit) && (!TMR1ON_bit)) { Delay_ms(100); if((RB1_bit) && (!TMR1ON_bit)) if(milliSec > 0)milliSec -= 1; InitTimer1(); //To create 1 sec delay between button presses //When timer is running and less than 1 sec TMR0ON_bit will be 1 and hence button press detect code will be disabled } if((RB2_bit) && (!TMR1ON_bit)) { Delay_ms(100); if((RB2_bit) && (!TMR1ON_bit)) if(milliSec < 9)milliSec += 1; InitTimer1(); //To create 1 sec delay between button presses //When timer is running and less than 1 sec TMR0ON_bit will be 1 and hence button press detect code will be disabled } LATC = mask[9 - milliSec]; //display fan speed value on 7 segment display (SSD) } }
I cannot understand why the top two traces show a negative part of the first cycle (and perhaps the last?) but not for the other cycles - what changed?
static int lastButtonStates;
// Define the number of times a pin must match before it is debounced
#define MAX_CHECKS 10
static volatile char states[MAX_CHECKS];
static volatile int index;
static volatile int buttonStates;
void Interrupt(void)
{
char buttonBits;
int i, j;
if( TMR1IF)
{
TMR1IF = 0; // Clear the interrupt flag
TMR1 = 61536; // (65536 - 4000) reset the timer
// Read in the current button values
buttonBits = PORTB & 0x07; // Read in Port B bits RB0 to RB2
states[index++] = buttonBits;
j = 0xff;
for( i = 0; i <MAX_CHECKS; i++)
{ j &= states[i]; }
buttonStates = j;
if( index >= MAX_CHECKS)
{ index = 0; }
}
}
// Within the main program
char previousTrigger = 0; // Last used state of the RB0 input
index = 0;
// Part of the initialisation section
// Create a 1mSec clock
T1CON = 0;
TMR1 = 61536; // (65536 - 4000)
TMR1IF = 0;
TMR1IE = 1;
T1CONbits.TMR1ON = 1;
// In the main loop - check for a chage in the buttons states
if( buttonStates != lastButtonStates)
{
// Work out which was different and in which direction
// and take appropriate action
if( (buttonStates && 0x02) == 0x02 then)
{
// Button RB1 is high so increment the counter
if( milliSec < 10) milliSec++;
}
// Etc
lastButtonStates = buttonStates
}
// Check to see if we need to trigger
if( PORTBbits.RB0 != previousTrigger)
{
// Capture the new state
previousTrigger = PORTBbits.RB0;
// Do whatever we need to as we have just seen an edge
// If you need to know if it was rising or falling then
// it will be rising if PORTBbits.RB0 is not 1
}
Yes, that's expectable because only the positive cycle is triggered.New proteus file attached. Use .hex file from post #15. I changed TRIAC model in Proteus. Now positive half cycle is chopping properly with button press but negative half cycle has vanished.
I'm not using Proteus 8 and can't check your simulation setup. But obviously the 4n37 output waveform (about 50 Hz square wave with 50 percent duty cycle) is different from the expectable 100 Hz pulses. It may be something trivial like interchanged pins in the simulation model. You can find out by e.g. reviewing a SPICE netlist of the design.Why 4N37 is not giving correct pulses ? AC freq is 50 Hz and output of bridge rectifier should be double the input AC freq that is 100 Hz. So, the 4N37 output should go high to low twice per cycle of AC. Why it is not happening ? Or is it working fine at your side ?
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?