danyel
Junior Member level 1
- Joined
- Jan 29, 2010
- Messages
- 16
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Location
- dar es salaam
- Activity points
- 1,371
what addition circuit do I need,
unsigned short current_duty, old_duty, current_duty1, old_duty1;
void InitMain() {
ANSEL = 0; // Configure AN pins as digital
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
PORTA = 255;
TRISA = 255; // configure PORTA pins as input
PORTB = 0; // set PORTB to 0
TRISB = 0; // designate PORTB pins as output
PORTC = 0; // set PORTC to 0
TRISC = 0; // designate PORTC pins as output
PWM1_Init(5000); // Initialize PWM1 module at 5KHz
PWM2_Init(5000); // Initialize PWM2 module at 5KHz
}
void main() {
InitMain();
current_duty = 16; // initial value for current_duty
current_duty1 = 16; // initial value for current_duty1
PWM1_Start(); // start PWM1
PWM2_Start(); // start PWM2
PWM1_Set_Duty(current_duty); // Set current duty for PWM1
PWM2_Set_Duty(current_duty1); // Set current duty for PWM2
while (1) { // endless loop
if (RA0_bit) { // button on RA0 pressed
Delay_ms(40);
current_duty++; // increment current_duty
PWM1_Set_Duty(current_duty);
}
if (RA1_bit) { // button on RA1 pressed
Delay_ms(40);
current_duty--; // decrement current_duty
PWM1_Set_Duty(current_duty);
}
if (RA2_bit) { // button on RA2 pressed
Delay_ms(40);
current_duty1++; // increment current_duty1
PWM2_Set_Duty(current_duty1);
}
if (RA3_bit) { // button on RA3 pressed
Delay_ms(40);
current_duty1--; // decrement current_duty1
PWM2_Set_Duty(current_duty1);
}
Delay_ms(5); // slow down change pace a little
}
}
Hi,
I don't get what you're doing here. Why are you using PWM? You have to use phase angle control or pulse skipping. If you use phase angle control, you can't use the opto-triac that you used, you have to use a regular opto-triac such as MOC3021.
Why are you using 5kHz? You have to fire the triac with respect to the line frequency (50 Hz or 60 Hz). You have to sense the zero-crossing, and then decide how much time to wait before firing the triac and you repeat this the next cycle.
Assuming you're using an MOC3021 or something similar, when you set PWM duty cycle (the traditional DC-DC controlling PWM) at 20%, the output is high for the first 2ms and low for 8ms. But as soon as the output goes high, the MOC3021 fires the triac, which gets latched (on) until the next zero crossing.
Take any duty cycle and you'll see that the lamp will always light upto full brightness.
What you have to do is, if you need 20% brightness:
Half period is 10ms. So, your output needs to be on for 20% first half, then 20% second half. So, that means 80% off, this 80% is "cut" through phase control. Wait for 80% of the half-time, ie, 8ms and THEN send a high pulse to the MOC for about 100us and then turn the pulse low. Repeat this for the second half as well, and THEN you will get 20% brightness.
Take a look at this:
Phase fired controllers - Wikipedia, the free encyclopedia
Hope this helps.
Tahmid.
---------- Post added at 17:00 ---------- Previous post was at 16:57 ----------
Plus, you really need to use a resistor between the LED and mains. Add a 100k resistor. Add a resistor between the opto-triac output and the triac gate.
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?