View previous topic :: View next topic |
Author |
Message |
bonhome
Joined: 04 Mar 2011 Posts: 4 Location: DZ
|
phase control light dimmer |
Posted: Fri Mar 04, 2011 4:56 am |
|
|
Hello, I have been working on a phase control light dimmer project with pic 16f877 microcontroller with ccs pic c compiler driving a MOC3021 optotriac controlling a lamp 220 v 50 Hz .
I can say that I know the principle well, so I wrote this c code, and for some reason it does not work.
Well in this case I am trying to control the lamp at 3 different phases 100, 75, 50%.
Code: |
int porcentage=100;
#int_EXT
EXT_isr()
{
if(porcentage==50)
{
delay_us(4990);
output_high(PIN_C0);
delay_us(100);
output_low(PIN_C0);
// set_timer1(5000);
}
else if(porcentage==75)
{
delay_us(2480);
output_high(PIN_C0);
delay_us(100);
output_low(PIN_C0);
// set_timer1(2500);
}
else if(porcentage==100)
{
output_high(PIN_C0);
delay_us(100);
output_low(PIN_C0);
}
}
void main()
{
port_b_pullups(FALSE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_2(T2_DISABLED,0,1);
enable_interrupts(INT_EXT);
ext_int_edge( H_TO_L );
enable_interrupts(GLOBAL);
set_tris_c(0b11111110);
while(1){
}
}
|
So if anyone could help me I would appreciate it. Thank you. |
|
|
Rosmaninho
Joined: 16 Jan 2011 Posts: 23
|
|
Posted: Fri Mar 04, 2011 7:04 am |
|
|
I'm only a beginner, but, your program will be arrested inside your While(1), and it won't do nothing more.
This is the error I find. |
|
|
bonhome
Joined: 04 Mar 2011 Posts: 4 Location: DZ
|
|
Posted: Fri Mar 04, 2011 7:26 am |
|
|
an external interruption caused by a zeros cross circuit will execute that ISr _________________ hi |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Mar 04, 2011 8:10 am |
|
|
Can you tell us what you code DOES do? Do you get any output at all? Is it erratic? What does it NOT do? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
bonhome
Joined: 04 Mar 2011 Posts: 4 Location: DZ
|
|
Posted: Fri Mar 04, 2011 1:26 pm |
|
|
The code trigger a optotriac with a small impulse of 100us. _________________ hi |
|
|
sseidman
Joined: 14 Mar 2005 Posts: 159
|
|
Posted: Fri Mar 04, 2011 1:46 pm |
|
|
What does "won't work" mean?? |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sun Mar 06, 2011 12:22 am |
|
|
One thing I would suggest if you have a spare pin is at the start of your ISR, set it high, wait 50 us or so then set it low. That would give you a test of your zero crossing detector as well as letting you view the delay between zero crossing and the trigger point if you have a dual channel scope. If I have any spare pins, I always try to do something with that pin during the ISR so I can tell that it is a) getting executed and b) gives me a reference timing signal.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
bonhome
Joined: 04 Mar 2011 Posts: 4 Location: DZ
|
|
Posted: Sun Mar 06, 2011 10:20 am |
|
|
thanks that's a good idea _________________ hi |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sun Mar 06, 2011 10:40 am |
|
|
As an alternative, just toggle a pin in the ISR. This way, if the detector is working perfectly, you should get a square wave in sync with the incoming signal, If a crossing is missed, the phase will shift, giving an instant 'diagnosis', and doing this removes the need for any delay in the ISR.
On a further comment, does the trigger really need a pulse as long as 100uSec. In most cases 1uSec is adequate. Are you in a 50Hz, or 100Hz country?. 50% power, on a full wave system, should imply triggering after 1/2 of 1/2 wave, so in a 60Hz country, after 4.16mSec, or in a 50Hz country, after 5mSec, neither of which correspond to the delay values you have, though possibly you are allowing 10uSec to get into the ISR, and the value then looks reasonable for a system on 50Hz. However if you are allowing 10uSec to get into the ISR, why does this increase to 20, for the 75% power timings?.
Are you sure you have got the chips crystal delays setup right - obviously nothing here will work if this is wrong.
Best Wishes |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sun Mar 06, 2011 6:20 pm |
|
|
Ttelmah wrote: | As an alternative, just toggle a pin in the ISR. This way, if the detector is working perfectly, you should get a square wave in sync with the incoming signal, If a crossing is missed, the phase will shift, giving an instant 'diagnosis', and doing this removes the need for any delay in the ISR.
[snip]
Best Wishes |
That's probably a better idea. What I usually do is set a pin at the start of the ISR then clear it again at the end of the ISR - lets me see just how much time is being spent in an ISR. I had not suggested that for this one because you had a variable delay in the ISR, but simply toggle the pin each time through the ISR as Ttelmah suggests is a good idea. Either way, one of the things you learn to do with embedded things like this is put some sort of "test points" in the system that allows you to see what is going on. Makes life much easier (usually).
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
|