faiz khan
Member level 1
- Joined
- Apr 30, 2015
- Messages
- 35
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 6
- Activity points
- 263
unsigned char FlagReg;
sbit ZC at FlagReg.B0;
int x;
void interrupt()
{
int i;
if (INTCON.INTF)
{ //INTF flag raised, so external interrupt occured
// ZC = 1;
INTCON.INTF = 0;
// }
//if (ZC){ //zero crossing occurred
for(i=1;i<=x;i++)
{
delay_ms(1);
}
PORTD.B0 = 1; //Send a pulse
delay_us(250);
PORTD.B0 = 0;
//ZC = 0;
// }
}
}
void main() {
x=1;
PORTB = 0;
TRISB = 0x01; //RB0 input for interrupt
PORTA = 0;
ADCON1 = 7; //Disable ADC
TRISA = 0xFF; //Make all PORTA inputs
PORTD = 0;
TRISD = 0; //PORTD all output
OPTION_REG.INTEDG = 0; //interrupt on falling edge
INTCON.INTF = 0; //clear interrupt flag
INTCON.INTE = 1; //enable external interrupt
INTCON.GIE = 1; //enable global interrupt
while (1)
{
if((RC5==0))
{
x=x+1;
//delay_ms(1);
}
if((RC6==0))
{
x=x-1;
//delay_ms(1);
}
}
}
Did you see the link I posted in post #7. How many steps do you need. Is 10 steps of power control enough ? Are you simulating in Proteus or testing in hardware. Please provide the circuit.
Should the least power turn of the device or do you want another button to turn ON/OFF the device ?
What is your AC frequency ? 50 Hz or 60 Hz ?
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 unsigned char milliSec = 0, count = 0; //Timer0 //Actual Interrupt Time : 1 ms //Place/Copy this part in declaration section void InitTimer0() { OPTION_REG = 0x81; TMR0 = 6; TMR0IE_bit = 1; } void interrupt() { if(INTF_bit) { INTF_bit = 0; INTE_bit = 0; InitTimer0(); INTEDG_bit = ~INTEDG_bit; } if(TMR0IF_bit) { TMR0IF_bit = 0; TMR0 = 6; //Enter your code here if(++count == (milliSec + 1)) { TMR0IE_bit = 0; PORTD.F0 = 1; //Delay_us(200); PORTD.F0 = 0; count = 0; INTE_bit = 1; } } } void main() { CMCON = 0x07; ADCON1 = 0x87; TRISA = 0xC0; TRISB = 0x07; TRISC = 0x00; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x07; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; INTEDG_bit = 0; INTF_bit = 0; INTE_bit = 1; GIE_bit = 1; while (1){ if(RB1_bit) { Delay_ms(50); while(RB1_bit); if(milliSec < 8)++milliSec; } if(RB2_bit) { Delay_ms(50); while(RB2_bit); if(milliSec > 0)--milliSec; } } }
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 unsigned char milliSec = 0, count = 0; //Timer0 //Actual Interrupt Time : 1 ms //Place/Copy this part in declaration section void InitTimer0() { OPTION_REG = 0x81; TMR0 = 6; TMR0IE_bit = 1; } void interrupt() { if(INTF_bit) { INTF_bit = 0; INTE_bit = 0; InitTimer0(); INTEDG_bit = ~INTEDG_bit; } if(TMR0IF_bit) { TMR0IF_bit = 0; TMR0 = 6; //Enter your code here if(++count == (milliSec + 1)) { TMR0IE_bit = 0; PORTD.F0 = 1; Delay_us(200); PORTD.F0 = 0; count = 0; INTE_bit = 1; } } } void main() { CMCON = 0x07; ADCON1 = 0x87; TRISA = 0xC0; TRISB = 0x07; TRISC = 0x00; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; INTEDG_bit = 0; INTF_bit = 0; INTE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while (1){ if(RB1_bit) { Delay_ms(50); while(RB1_bit); if(milliSec < 9)++milliSec; } if(RB2_bit) { Delay_ms(50); while(RB2_bit); if(milliSec > 0)--milliSec; } } }
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 unsigned char milliSec = 0, count = 0; //Timer1 //Prescaler 1:1; TMR1 Preload = 64536; Actual Interrupt Time : 1 ms //Place/Copy this part in declaration section void InitTimer1(){ T1CON = 0x01; TMR1IF_bit = 0; TMR1H = 0xFC; TMR1L = 0x18; TMR1IE_bit = 1; } void interrupt() { if(INTF_bit) { INTF_bit = 0; INTEDG_bit = ~INTEDG_bit; //INTE_bit = 0; InitTimer1(); } if(TMR1IF_bit) { TMR1IF_bit = 0; TMR1H = 0xFC; TMR1L = 0x18; //Enter your code here if(++count == (milliSec + 1)) { TMR1ON_bit = 0; PORTD.F0 = 1; Delay_us(200); PORTD.F0 = 0; count = 0; //INTE_bit = 1; } } } void main() { CMCON = 0x07; ADCON1 = 0x87; TRISA = 0xC0; TRISB = 0x07; TRISC = 0x00; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; INTEDG_bit = 0; INTF_bit = 0; INTE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while (1){ if(RB1_bit) { Delay_ms(50); while(RB1_bit); if(milliSec < 9)++milliSec; } if(RB2_bit) { Delay_ms(50); while(RB2_bit); if(milliSec > 0)--milliSec; } } }
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 unsigned char milliSec = 0, count = 0; //Timer1 //Prescaler 1:1; TMR1 Preload = 64711; Actual Interrupt Time : 825 us //Place/Copy this part in declaration section void InitTimer1(){ T1CON = 0x01; TMR1IF_bit = 0; TMR1H = 0xFC; TMR1L = 0x95; TMR1IE_bit = 1; } void interrupt() { if(INTF_bit) { INTF_bit = 0; INTEDG_bit = ~INTEDG_bit; //INTE_bit = 0; InitTimer1(); } if(TMR1IF_bit) { TMR1IF_bit = 0; TMR1H = 0xFC; TMR1L = 0x95; //Enter your code here if(++count == (milliSec + 1)) { TMR1ON_bit = 0; PORTD.F0 = 1; Delay_us(100); PORTD.F0 = 0; count = 0; //INTE_bit = 1; } } } void main() { CMCON = 0x07; ADCON1 = 0x87; TRISA = 0xC0; TRISB = 0x07; TRISC = 0x00; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; INTEDG_bit = 0; INTF_bit = 0; INTE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while (1){ if(RB1_bit) { Delay_ms(50); while(RB1_bit); if(milliSec < 9)++milliSec; } if(RB2_bit) { Delay_ms(50); while(RB2_bit); if(milliSec > 0)--milliSec; } } }
@betwixt
Please check the code I have attached. If you have Proteus then please check why TRIAC signal is not showing.
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?