CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

signal phase dimmer code
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
lindoudaM2



Joined: 09 Oct 2012
Posts: 13
Location: Tunisie

View user's profile Send private message AIM Address

signal phase dimmer code
PostPosted: Wed Oct 10, 2012 3:56 pm     Reply with quote

My code works well in ISIS simulation but in reality-show it's not same. I want to control dimming light with a triac.

Someone help me please to correct my code.
Code:

#include <16F877A.h>
#use delay(clock=20000000)
#fuses XT, NOWDT, NOPROTECT, PUT, NOLVP

byte x;

void init ()
{
setup_adc_ports(RA0_ANALOG);
setup_adc(adc_clock_div_2);
set_adc_Channel(0);
setup_ccp1(ccp_pwm) ;
setup_timer_2 (T2_Div_by_1,255,1);
setup_timer_1(T1_internal);

SET_TRIS_B(0xFD);
}

#int_ext
void ext_isr()
{
if(input (pin_B0)  )
  ext_int_edge(H_to_L);
}

void main()
{
init();

while (1)
  {
   enable_interrupts(INT_EXT);
   enable_interrupts(GLOBAL);

   delay_ms (10);
   x=read_adc();
   set_pwm1_Duty(x);
   delay_ms (10);
   disable_interrupts(INT_EXT);
   Clear_interrupt(INT_EXT);
  }
}
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Oct 10, 2012 4:14 pm     Reply with quote

why not use RB int instead of Ext int?

it trigger on change... so you dont have to change the direction of the trigger signal....



I would also move these:
Code:
   enable_interrupts(INT_EXT);
   enable_interrupts(GLOBAL);

out of the while loop and reverse the order...
Code:
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_EXT);



i believe... that once the ISR triggers it automaticaly clears the interrupt flag... so you dont have to doit manually....

your crystal fuse is XT... yet you are declaring a 20MHz clock...
Try HS better...


just my 2 cents..

G
_________________
CCS PCM 5.078 & CCS PCH 5.093
lindoudaM2



Joined: 09 Oct 2012
Posts: 13
Location: Tunisie

View user's profile Send private message AIM Address

PostPosted: Wed Oct 10, 2012 4:27 pm     Reply with quote

I don't understand what you mean by that, not use RB int is that i did wrong (int_ext).
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Oct 10, 2012 4:44 pm     Reply with quote

Quote:
My code works well in ISIS simulation but in reality-show it's not same. I want to control dimming light with a triac.

Three things.

1) Forget ISIS. It's so full of bugs you're wasting your time and ours.
2) Describe your hardware, or show us a schematic.
3) Tell us what's happening that's not what you expect.

Mike
lindoudaM2



Joined: 09 Oct 2012
Posts: 13
Location: Tunisie

View user's profile Send private message AIM Address

PostPosted: Wed Oct 10, 2012 5:13 pm     Reply with quote

Mike Walne wrote:
Quote:
My code works well in ISIS simulation but in reality-show it's not same. I want to control dimming light with a triac.

Three things.

1) Forget ISIS. It's so full of bugs you're wasting your time and ours.
2) Describe your hardware, or show us a schematic.
3) Tell us what's happening that's not what you expect.

Mike

I am trying to conceive a single-phase triac dimmer controlled by the PIC 16F877A. My problem is at the level of the load (lamp) there are no variation of brightness despite the PWM signal varies appropriately.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Oct 11, 2012 12:51 am     Reply with quote

First of all, don't start a new thread for the same project every time. This is already your 4th thread for this dimmer project.
Keeping it all together makes it easier for everyone to see what has already been discussed.

Quote:
I am trying to conceive a single-phase triac dimmer controlled by the PIC 16F877A. My problem is at the level of the load (lamp) there are no variation of brightness despite the PWM signal varies appropriately.
I think you made a major design flaw. A triac is not to be connected to a PWM output. PWM will generate a lot of trigger pulses but, by it's nature, the triac will stay enabled once it is triggered till the current flowing through it drops to zero. So, after the first PWM pulse, the triac will enable and then nothing changes until the next mains line zero crossing.

I suggest you do some more reading into the subject. Microchip has some really nice Application Notes on their website for this.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Oct 11, 2012 8:25 am     Reply with quote

Like ckielstra says, you need to think about how you're going to trigger the triac.

So far you've not shown us anything about your hardware.

When your hardware is sorted, the triac part of the software goes:-

1) Wait for zero crossing.
2) Wait for a time set by ADC output (0 - 10ms for UK mains).
3) Trigger triac.
4) Loop back to 1, assuming triac will turn off close to next zero crossing.

Mike
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Thu Oct 11, 2012 2:37 pm     Reply with quote

All of this is assuming we are talking a resistive (tungsten) light or something similar for dimming. If it is an inductive (fluorescent or something else), then it opens a whole new can of worms. Usually in this kind of project, it is a resistive load, but every now and then, after many posts, the OP happens to mention the fact it is an inductive load or something else.

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
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Oct 12, 2012 10:11 am     Reply with quote

Quote:

triac dimmer


1- PWM is NOT the way to trigger a triac
2- you need to sense each zero crossing of the AC power waveform
and trigger at an appropriate phase angle offset from zero - to get phase control
3-study triac phase control , then design a circuit and ONLY THEN create your control code.
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Fri Oct 12, 2012 12:21 pm     Reply with quote

Also you need to look at some real AC waveform.
Real mains, especially when triacs are involved anywhere nearby, does not resemble a sinusoid at all closely. This is a typical example of where Proteus will give very erroneous results.
This is why there are quite complex IC;s around to make zero crossing detectors, since it is not a simple thing to do...

Best Wishes
lindoudaM2



Joined: 09 Oct 2012
Posts: 13
Location: Tunisie

View user's profile Send private message AIM Address

PostPosted: Fri Oct 12, 2012 4:19 pm     Reply with quote

Hello
I ameliorated my code and here is it
wish that everything will be fixed Smile
Code:

#include<16F877A.h>
#use delay (clock =20000000)
#fuses HS,NOWDT, NOPROTECT,  PUT, NOLVP

byte x;
void init ()
{
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_2);
Set_adc_channel(0);
setup_CCP1(CCP_PWM);
setup_timer_2 (T2_Div_by_1,255,1);
setup_timer_1(T1_internal);
set_tris_b(0xFE);
}

#int_ext
void ext_isr()
{

if (input(pin_B0))
  {
   ext_int_edge(H_to_L);
  }
}

void main()
{
init();

enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);

while (1)
  {
   delay_ms (10);

   x=read_adc();
   set_pwm1_Duty(x);
 
   disable_interrupts(INT_EXT);
   clear_interrupt(INT_EXT);
   delay_ms(10);
  }

}
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Fri Oct 12, 2012 6:59 pm     Reply with quote

"ameliorated" - good grief - i'd never heard that word before so had to look it up. Obviously others have not heard of it before either - they claim "Ameliorate is currently in the top 1% of lookups and is the 104th most popular word on Merriam-Webster.com." - my learning for today is complete - time to relax :-)

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
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Oct 13, 2012 3:41 am     Reply with quote

Quote:
I ameliorated my code and here is it
wish that everything will be fixed

So, does it now work?

Mike
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Oct 13, 2012 7:31 am     Reply with quote

Mike Walne wrote:
Quote:
I ameliorated my code and here is it
wish that everything will be fixed

So, does it now work?

Mike
I guess not because his interrupt is doing nothing, he is not using any synchronization to the AC waveform and the PWM output that was discouraged is still there...

I don't mind to help, but what is the question?
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Sat Oct 13, 2012 9:09 am     Reply with quote

I'd be very hesitant to use the word ameliorated, since it implies 'better'.
It's like posting saying "I've fixed my code, but it still doesn't work". One part or the other of the statement is untrue....

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group