อันนี้ลง code ดูลำดับการทำงานตามโปรแกรมได้
เป็น code ล่าสุด ที่แสดงผลแบบใหม่ไฟค่อยๆติดสว่างนะครับ ไม่กระพริบๆ แล้ว
/*
SoftStart Delay + Speaker Protection control for 120W.
Processor : PIC16F684
Compiler : CCS Lite (Built-in on MPLAB from Microchip)
internal RC Oscillator
Code written : [audiomania] Worapoht K.,
Update: 3 Feb 2011
*/
#include <16F684.h>
#include <stdio.h>
#FUSES NOWDT,INTRC_IO,PROTECT,NOMCLR,PUT,BROWNOUT,NOIESO,NOFCMEN
#use delay(clock=4000000)
#define MUTE_OUT PIN_A0
//#define GP1 PIN_A1
//#define GP2 PIN_A2
//#define GP3 PIN_A3
#define STATUS1_LED PIN_A4
#define AC_TRIG PIN_A5
//#define GP0 PIN_C0
#define SOFT_RELAY PIN_C1
#define FULL_RELAY PIN_C2
#define STATUS2_LED PIN_C3
//#define GP4 PIN_C4
#define WARM_LED PIN_C5
#define NO_BLINK 1
static volatile unsigned int AC_OK;
static volatile unsigned int sequence;
void STATUS_GREEN(void)
{
output_low (STATUS1_LED);
output_high (STATUS2_LED);
}
void STATUS_RED(void)
{
output_high (STATUS1_LED);
output_low (STATUS2_LED);
}
void STATUS_OFF(void)
{
output_low (STATUS1_LED);
output_low (STATUS2_LED);
}
void WARM_ON(void)
{
output_low (WARM_LED);
}
void WARM_OFF(void)
{
output_high (WARM_LED);
}
void SOFT_ON(void)
{
output_high (SOFT_RELAY);
}
void SOFT_OFF(void)
{
output_LOW (SOFT_RELAY);
}
void FULL_ON(void)
{
output_high (FULL_RELAY);
}
void FULL_OFF(void)
{
output_low (FULL_RELAY);
}
void MUTE_ON(void)
{
output_low (MUTE_OUT);
}
void MUTE_OFF(void)
{
input(MUTE_OUT);
}
unsigned int pause_ms(unsigned int time)
{
unsigned int i;
i=0;
AC_OK=0;
while(time-->0)
{
delay_ms(2);
if ((input(AC_TRIG))==0)
{
AC_OK=1;
i=0;
}
else
{
if (i++>50)
{
MUTE_ON();
SOFT_OFF();
FULL_OFF();
WARM_OFF();
STATUS_OFF();
AC_OK=0;
time=0;
sequence=0;
}
}
}
return AC_OK;
}
void main()
{
static volatile unsigned long n;
static volatile unsigned char first_run=0;
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
MUTE_ON();
SOFT_OFF();
FULL_OFF();
WARM_OFF();
STATUS_OFF();
pause_ms(500);
sequence=0;
while (TRUE)
{
if(sequence == 0) // WARM LED + Mute SPK
{
STATUS_OFF();
MUTE_ON();
SOFT_OFF();
FULL_OFF();
WARM_OFF();
pause_ms(1000);
if (AC_OK==1)
{
#ifdef NO_BLINK
for (n=0;n<=2000;n++)
{
WARM_OFF();
delay_us(2000-n);
WARM_ON();
delay_us(n);
}
#endif
pause_ms(1000);
if (AC_OK==1)
{
if (first_run==0)
{
sequence=1;
first_run=1;
}
else
{
for (n=0;n<=2000;n++)
{
WARM_ON();
delay_us(2000-n);
WARM_OFF();
delay_us(n);
}
STATUS_OFF();
sequence=0;
first_run=0;
}
}
}
}
else if (sequence == 1) // SOFT Relay + Red blink
{
WARM_ON();
SOFT_ON();
for (n=0;n<=3;n++)
{
STATUS_RED();
if ((pause_ms(500))==0)
{
break;
}
#ifndef NO_BLINK
STATUS_OFF();
#endif
if ((pause_ms(500))==0)
{
break;
}
}
if (AC_OK==1)
{
sequence=2;
}
}
else if (sequence == 2) // FULL Relay + Green blink
{
FULL_ON();
for (n=0;n<=7;n++)
{
STATUS_GREEN();
if ((pause_ms(500))==0)
{
break;
}
#ifndef NO_BLINK
STATUS_OFF();
#endif
if ((pause_ms(500))==0)
{
break;
}
SOFT_OFF();
}
if (AC_OK==1)
{
sequence=3;
}
}
else if (sequence == 3) // - WARM LED - SOFT Relay + solid Green
{
WARM_OFF();
SOFT_OFF();
STATUS_GREEN();
MUTE_OFF();
if ((pause_ms(1000))==0)
{
break;
}
else
{
sequence=4;
}
}
else if (sequence == 4)
{
pause_ms(100);
}
else
{
sequence=0;
}
}
}