|
|
View previous topic :: View next topic |
Author |
Message |
eng/ IBRAHIM
Joined: 14 Aug 2007 Posts: 31
|
DIMMER |
Posted: Sat Oct 25, 2008 6:24 am |
|
|
hi
pls
i want to mak a lamp dimmer by use 12c508 .i find this code in microchip site but it not written by CCS how can convert to CCS compiler
#pragma option v;
#include <12C508.h>
/********************************************************/
/* DIMMER.C
/*
/* Lamp dimmer for the 12C508.
/* This program uses the internal 4MHz oscillator
/* To drive TRIAC, the output is taken high
/* or put in high-impeadance(open drain) to release it
/*
/* NOTE: This program is designed to work with a 60Hz
/* line frequency, it must be modified if used
/* on a 50Hz AC line.
/*
/* GPIO<0> = Dim button
/* GPIO<1> = No Connect
/* GPIO<2> = Output to TRIAC
/* GPIO<3> = Bright Button
/* GPIO<4> = Zero Crossing sense input
/* GPIO<5> = No Connect
/********************************************************/
#defineBrtbut GPIO.0 //Brighten button
#define Output GPIO.2 //Output to TRIAC
#define Dimbut GPIO.3 //Dim button
#define LineInput GPIO.4 //AC line zero crossing sense
void Buttoncheck(void); //Button check routine
unsigned int PercentOn, Maxdim; //Global variables
unsigned int TestCheck, Outcount, TestCount;
unsigned int DelayCnt;
unsigned int LastBoth, FirstPass;
unsigned int Count;
const Maxbrt = 0xFD, NotInTest = 3;
void main()
{
PercentOn = 0xD0; //On Period
Maxdim = 0x70; //Value of Maximum dimming
TestCheck = 0; //Test mode check counter
Outcount = 0; //Counter for test mode exit
TestCount = 0; //Test mode counter
DelayCnt = NotInTest; //Delay count
LastBoth = 0; //Both buttons pressed last time flag
FirstPass = 1; //Indicate power-up
Count = 0; //General counter
for(Count = 0; Count < 60; Count++) //Allow power supply to stabilize
{
while(LineInput == 1);
while(LineInput == 0);
CLRWDT;
}
WREG = 0x85;
#asm ( OPTION); //1:64 tmr0 prescaler, pullups enabled
WREG = 0x1D;
#asm ( TRIS GPIO); //Set up I/O
GPIO = 0x04; //Set TRIAC output latch high
while(LineInput == 1) //Synch to line phase
CLRWDT;
TMR0 = PercentOn; //Get Delay time
while(TMR0 >= 3 && LineInput == 0) //Delay to enter main at proper point
CLRWDT;
while(1) //Stay in this loop
{
Count = 0;
while (Count++ < DelayCnt) //Check for button press every
DelayCnt zero crossings
{
if(LineInput == 1) //Check for AC line already high
{
Maxdim += 5; //If so, increment Maxdim
while(LineInput == 1); // and re-sync with line
while(LineInput == 0)
CLRWDT;
}
else
{
while(LineInput == 0) //Wait for zero crossing
CLRWDT;
Maxdim = PercentOn - TMR0 + 2; //Compensate full dim value for line
// frequency vs osc. speed
}
if(FirstPass == 1) //If first pass, go to full dim
{
FirstPass = 0;
PercentOn = Maxdim;
}
if(PercentOn < Maxdim) //If maxdim moved, fix brightness
PercentOn = Maxdim;
TMR0 = PercentOn; //Get delay time
while(TMR0 >= 3 && LineInput == 1) //Delay TRIAC turn on (wait for Counter rollover)
CLRWDT;
GPIO = 0x04; //Set TRIAC output latch high
WREG = 0x19;
#asm ( TRIS GPIO); //Fire TRIAC
NOP; //Delay for TRIAC fire pulse
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
WREG = 0x1D;
#asm ( TRIS GPIO); //Release TRIAC fire Signal
CLRWDT;
if(LineInput == 0) //Check for AC line already low
{
Maxdim += 5; //If so, increment Maxdim
while(LineInput == 0); // and re-sync with line
while(LineInput == 1)
CLRWDT;
}
else
{
while(LineInput==1) //Wait for zero crossing
CLRWDT;
Maxdim = PercentOn - TMR0 + 2; //Compensate full dim value for line
// frequency vs osc. speed
}
if(PercentOn < Maxdim) //If maxdim moved, fix brightness
PercentOn = Maxdim;
TMR0 = PercentOn; //Get Delay time
while(TMR0 >= 3 && LineInput == 0) //Delay TRIAC turn on
CLRWDT;
GPIO = 0x04; //Set TRIAC output latch high
WREG = 0x19;
#asm ( TRIS GPIO); //Fire TRIAC
NOP; //Delay for TRIAC fire pulse
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
WREG = 0x1D;
#asm ( TRIS GPIO); //Release TRIAC fire signal
CLRWDT;
}
Buttoncheck(); //Check for button press
}
}
/******************************************************** */
/* ButtonCheck */
/* */
/* This subroutine checks for presses on the BRT and DIM */
/* buttons and increments or decrements PercentOn. */
/* */
/* If both buttons are pressed and the lamp */
/* is not off, it is turned off, if off, it is set to */
/* to max bright. */
/* */
/* In addition, a test function is built in. If both */
/* buttons are pressed, the dim let go and then pressed */
/* again, test mode is entered. If dim is pressed */
/* (alone), the program goes to normal operation at max */
/* dim. The test mode brightens to full bright, dims to */
/* full dim, flashes full bright twice, and repeats. */
/******************************************************** */
void Buttoncheck()
{
NOP; //Bugfix for MPLABC V1.10
if(TestCheck == 3) //Check test mode flag
{
DelayCnt = 2; //Reset the delay count
if(Brtbut && !Dimbut) //If Dimbutton pressed, exit test mode
{
TestCheck = 0; //Clear Test mode flag
DelayCnt = 5;
return;
}
if(TestCount == 0) //Ramp up to full dim
{
if(++PercentOn > Maxbrt) //Check for full bright
{
PercentOn = Maxbrt;
++TestCount;
return;
}
else
return;
}
if(TestCount == 1) //Ramp down to full dim
{
if(--PercentOn <= Maxdim) //Check for full dim
{
PercentOn = Maxbrt;
++TestCount;
return;
}
else
return;
}
while(TestCount++ < 5) //Delay
return;
while(TestCount++ < 10) //Turn off for a short period
{
PercentOn = Maxdim;
return;
}
while(TestCount++ < 15) //Turn On for a short period
{
PercentOn = Maxbrt;
return;
}
while(TestCount++ < 20) //Turn off for a short period
{
PercentOn = Maxdim;
return;
}
while(TestCount++ < 25) //Turn on for a short period
{
PercentOn = Maxbrt;
return;
}
while(TestCount++ < 30) //Turn off for a short period
{
PercentOn = Maxdim;
return;
}
PercentOn = Maxdim;
TestCount = 0; //Reset to beggining of test sequence
if(++Outcount == 255) //Run 255 cycles of test mode
{
TestCheck = 0; //Clear Test mode flag
DelayCnt = NotInTest;
Outcount = 0;
}
return;
}
if(TestCheck) //If Test mode not entered quickly,
if(++Outcount == 0x60) // quit checking
{
DelayCnt = NotInTest;
Outcount = 0;
TestCheck = 0;
}
if(!TestCheck && !Brtbut && !Dimbut) //Check bright & dim at same time
TestCheck = 1; //If both pressed, set to look for next combo
if(TestCheck == 1 && !Brtbut && Dimbut) //Check for only bright button pressed
TestCheck = 2; //If pressed, set to look for next combo
if(TestCheck == 2 && !Brtbut && !Dimbut) //Check for both pressed again
{
TestCheck = 3; //Enable test mode
TestCount = 0;
PercentOn = Maxdim;
Outcount = 0;
}
if(!Dimbut && !Brtbut) //If both pressed
{
if(LastBoth == 0) //Don't flash if held
{
LastBoth = 1;
if(PercentOn == Maxdim) //If full off...
PercentOn = Maxbrt; // turn full on...
else
PercentOn = Maxdim; // otherwise turn off
}
}
else
LastBoth = 0;
if(!Brtbut && Dimbut) //Check for brighten cmd
PercentOn ++;
if(Brtbut && !Dimbut) //Check for dim cmd
PercentOn --;
if(PercentOn > Maxbrt) //If greater than full bright
PercentOn = Maxbrt;
if(PercentOn < Maxdim) //If less than full dim
PercentOn = Maxdim;
} |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: DIMMER |
Posted: Sat Oct 25, 2008 6:31 am |
|
|
eng/ IBRAHIM wrote: | hi
pls
i want to mak a lamp dimmer by use 12c508 .i find this code in microchip site but it not written by CCS how can convert to CCS compiler
|
By understanding all the features of this code that are unique to that other compiler and then finding the corresponding functions in the CCS compiler. If you are too lazy to at least give it a try, then why should anyone else bother? _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sat Oct 25, 2008 7:57 am |
|
|
This is proprietary Microchip code from an application note and they may not be happy with it being copied and reproduced here. |
|
|
eng/ IBRAHIM
Joined: 14 Aug 2007 Posts: 31
|
|
Posted: Sat Oct 25, 2008 8:06 am |
|
|
Hi
Thanks RLScott
I'm not lazy but I try to make dimmer by PIC to control in AC motor since long time.
I try you said and I change a lot of function but i not understand more functions because i using CCS compiler only. After change function the compiler gives error and so on. And the code including assembly function. |
|
|
Guest
|
|
Posted: Sat Oct 25, 2008 8:36 am |
|
|
This is pretty simple code, only a few pages long. Reverse engineer it - that is make a flow chart of how the code works. Then code your flow chart in CCS C.
This is called: "Reverse Engineering" and it is done all the time by all of us when we are trying to understand nearly everything.
By spending your time learning how to do this you will be a better engineer. But it takes effort and time on your part. The time you spend learning this now will pay off for your entire life. So it is an investment in YOUR future.
HTH - Steve H. |
|
|
|
|
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
|