#include <STC12C2052AD.H>
#include <intrins.h>
//输入50hz交流电全波整流后的脉冲,为100hz,也就是一个周期为10ms,每个周期100级pwm,也就是pwm的处理精度为0.1ms。
#define IDL 0X01
#define POF 0X10
#define ON 1
#define OFF 0
unsigned char uc_temp; //临时变量
unsigned char uc_temp1;
unsigned char uc_temp2;
unsigned char bdata uc_bit_flag;
unsigned char uc_pwm0_set_val;
unsigned char uc_pwm1_set_val;
unsigned char uc_pwm2_set_val;
unsigned char uc_pwm_count;//pwm计数
unsigned char uc_pwm0_val;//pwm设定值
unsigned char uc_pwm1_val;//pwm设定值
unsigned char uc_pwm2_val;//pwm设定值
sbit sb_pwm0_out=P3^4;
sbit sb_pwm1_out=P1^7;
sbit sb_pwm2_out=P3^7;
unsigned char uc_time_count_10ms;
unsigned char uc_time_count_1s;
unsigned char uc_time_count_temp;
unsigned char uc_time_count_temp1;
unsigned char uc_change_i;
//////////////////////////////////////////////灯带控制
unsigned char uc_pwm_count_val; //0.1ms机器计数值
unsigned char uc_pwm_count_val_temp; //0.1ms机器计数值
sbit sb_pwm_count_init_flag=uc_bit_flag^2;//0.1ms机器计数值初始化标志,为1为正在初始化
sbit sb_pwm_count_init_fail_flag=uc_bit_flag^3;//0.1ms机器计数值初始化标志,为1为正在初始化
/////////////////////////////////////////////
typedef struct
{
unsigned char val_index;
unsigned char val_num;
unsigned char pwm_time;
unsigned char pwm_step;
unsigned char run_record_time;
unsigned char run_cycle_num;
}RUN_INDEX;
const unsigned char code setval[29][3]=
{
/////兰,绿,红
{100,100,100,}, //0
{0 ,100,0 ,}, //1
{100,0 ,100,}, //2
{100,0 ,0 ,}, //3
{0 ,100,100,}, //4
{0 ,0 ,100,}, //5
{100,100,0 ,}, //6
{100,100,0 ,}, //7
{100,100,100,}, //8
{0 ,0 ,100,}, //9
{0 ,100,100,}, //10
{100,100,0 ,}, //11
{100,0 ,100,}, //12
{0 ,0 ,100,}, //13
{0 ,100,100,}, //14
{100,100,100,}, //15
{100,100,0 ,}, //16
{100,0 ,0 ,}, //17
{100,100,0 ,}, //18
{100,0 ,100,}, //19
{0 ,100,100,}, //20
{100,0 ,0 ,}, //21
{0 ,0 ,100,}, //22
{0 ,0 ,0 ,}, //23
{0 ,100,0 ,}, //24
{0 ,0 ,0 ,}, //25
{100,0 ,0 ,}, //26
{0 ,0 ,0 ,}, //27
{100,100,100,}, //28
};
#define RUN_INDEX_NUM 14
const RUN_INDEX code run_index[RUN_INDEX_NUM]=
{
{0 ,6,0,10,1,15}, //全亮全灭
{0 ,3,0,10,1,15}, //2】兰绿红依次亮
{2 ,3,0,10,1,15}, //3】红绿兰依次亮
{0 ,7,5,1,100,15}, //8】红-》绿-》兰-》红绿-》红兰-》绿兰-》全亮
{8 ,5,1,1,70,15}, //9】渐变 全亮-》红-》红绿-》绿兰-》红兰
{0 ,4,0,10,1,15}, //4】红绿兰往复亮
{18 ,3,0,10,1,15}, //6】绿兰-》红兰-》红绿
{13 ,5,0,10,1,15}, //7】红-》红绿-》红绿兰-》绿兰-》兰
{22 ,2,0,10,1,2 }, //红闪
{24 ,2,0,10,1,2 }, //绿闪
{26 ,2,0,10,1,2 }, //兰闪
{2 ,2,0,10,1,2 }, //红绿闪
{0 ,2,0,10,1,2 }, //绿兰闪
{21 ,2,0,10,1,2 }, //兰红闪
};
const unsigned char code run_record_count_time[10]=
{
120,92,72,56,32,20,16,13,10,5
};
unsigned char uc_run_index_i; //记录号
unsigned char uc_add_val; //时间加的单位
unsigned char uc_add_val1; //时间加的单位
unsigned char uc_add_val1_temp;
unsigned char uc_run_cycle_i; //循环计数
unsigned char uc_boot_kind;
unsigned char uc_old_in_val;
unsigned char uc_run_index_base;
unsigned char uc_run_index_num;
sbit sb_run_flag=uc_bit_flag^0;
unsigned char uc_run_kind;
#define BOOT_KIND 7
void main(void)
{
//设置P3.3,P3.5,P3.7为强上拉
sb_pwm0_out=ON;
sb_pwm1_out=ON;
sb_pwm2_out=ON;
// P3M0=0;
// P3M1=0Xa8;
/* ISP_ADDRH=0X10;
ISP_ADDRL=255;
ISP_CONTR=0X8B;
ISP_CMD=1;
ISP_DATA=0;
uc_temp=0;
EA=0;
for(;ISP_DATA!=255&&ISP_ADDRL!=10;)
{
uc_temp=ISP_DATA;
ISP_ADDRL++;
ISP_TRIG=0X46;
ISP_TRIG=0XB9;
}
if(uc_temp>BOOT_KIND) uc_temp=0;
if(PCON&POF)//判断冷启标志
{//前一个值+1写入下一个地址,如果大于一页,擦除一页,写入地址0
uc_temp++;
if(uc_temp>BOOT_KIND) uc_temp=1;
if(ISP_ADDRL==10)
{
ISP_CMD=3;
ISP_ADDRL=0;
ISP_TRIG=0X46;
ISP_TRIG=0XB9;
}
ISP_CMD=2;
ISP_DATA=uc_temp;
ISP_TRIG=0X46;
ISP_TRIG=0XB9;
ISP_CONTR=0X0B;
PCON&=~POF;//清除冷启标志
}
uc_boot_kind=uc_temp;//*/
//初始化数据
PT0=1; //time0中断提高优先级
uc_pwm_count_val=0;
sb_pwm_count_init_flag=1;
sb_pwm_count_init_fail_flag=0;
uc_temp=0;//外部中断计数清零
//初始化定时器0为模式2。
//初始化定时器1为模式1。
TMOD=0X12;
ET1=1;//定时器1中断使能
IT1=1;//边沿触发外部中断0
EX1=1;//使能外部中断0
EA=1;
for(;sb_pwm_count_init_flag;)
{
WDT_CONTR=0X30;//喂狗
PCON|=IDL; //idle模式时
}
for(;sb_pwm_count_init_fail_flag;)
{
WDT_CONTR=0X30;//喂狗
PCON|=IDL; //idle模式时
}
uc_time_count_10ms=255;
uc_time_count_1s=255;
uc_time_count_temp=0;
uc_time_count_temp1=0;
uc_pwm_count=0;//自编pwm数据初始化
uc_pwm0_val=0;
uc_pwm1_val=0;
uc_pwm2_val=0;
uc_pwm0_set_val=0;
uc_pwm1_set_val=0;
uc_pwm2_set_val=0;
uc_change_i=255;
TH0=uc_pwm_count_val;
TL0=uc_pwm_count_val;
TR0=1;
//各种中断时使。
ET0=1;//定时器0中断使能
// IT0=1;//边沿触发外部中断0
// EX0=1;//使能外部中断0
EA=1;
// WDT_CONTR=0X30;//狗初始化
uc_add_val=120; //时间加的单位
uc_add_val1=1; //时间加的单位
uc_add_val1_temp=1;
uc_run_index_i=0; //记录号
uc_run_cycle_i=0; //循环计数
uc_old_in_val=0xff;
sb_run_flag=0;
uc_run_kind=0xff;
sb_run_flag=0;
// uc_time_count_10ms=255;
// uc_time_count_1s=255;
// uc_time_count_temp=0;
// uc_time_count_temp1=0;
// uc_change_i=255;
uc_run_index_base=3;
uc_run_index_num=1;
uc_run_index_i=uc_run_index_base; //记录号
uc_run_cycle_i=0; //循环计数
ET0=1;
sb_run_flag=1;
uc_add_val=run_record_count_time[7];
while(1)
{
/* uc_temp=~P1;
// uc_temp=0x99;
if(uc_old_in_val!=uc_temp)
{
uc_temp1=uc_temp&0x0f;
uc_temp2=uc_temp&0xf0;
uc_add_val1_temp=6-(uc_temp1/2);
if(uc_temp2==0x0)
{
ET0=0;
sb_run_flag=0;
uc_add_val=10;
switch(uc_temp1)
{
case 0:
sb_pwm0_out=OFF;
sb_pwm1_out=OFF;
sb_pwm2_out=ON;
break;
case 1:
sb_pwm0_out=OFF;
sb_pwm1_out=ON;
sb_pwm2_out=OFF;
break;
case 2:
sb_pwm0_out=ON;
sb_pwm1_out=OFF;
sb_pwm2_out=OFF;
break;
case 3:
sb_pwm0_out=OFF;
sb_pwm1_out=ON;
sb_pwm2_out=ON;
break;
case 4:
sb_pwm0_out=ON;
sb_pwm1_out=OFF;
sb_pwm2_out=ON;
break;
case 5:
sb_pwm0_out=ON;
sb_pwm1_out=ON;
sb_pwm2_out=OFF;
break;
case 6:
sb_pwm0_out=ON;
sb_pwm1_out=ON;
sb_pwm2_out=ON;
break;
case 7:
ET0=1;
sb_run_flag=1;
uc_run_index_base=11;
uc_run_index_num=1;
break;
case 8:
ET0=1;
sb_run_flag=1;
uc_run_index_base=12;
uc_run_index_num=1;
break;
case 9:
ET0=1;
sb_run_flag=1;
uc_run_index_base=13;
uc_run_index_num=1;
break;
}
}
else
{
ET0=1;
sb_run_flag=1;
uc_add_val=run_record_count_time[uc_temp1];
}
if(uc_run_kind!=uc_temp2)
{
// if(uc_temp2==0x10)
// {
// uc_run_kind=uc_temp2;
// uc_temp2=(uc_boot_kind+1)*16;
// }//
switch(uc_temp2)
{
case 0x10:
uc_run_index_base=0;
uc_run_index_num=7;
break;
case 0x20:
uc_run_index_base=1;
uc_run_index_num=1;
break;
case 0x30:
uc_run_index_base=2;
uc_run_index_num=1;
break;
case 0x40:
uc_run_index_base=5;
uc_run_index_num=1;
break;
case 0x50:
uc_run_index_base=8;
uc_run_index_num=3;
break;
case 0x60:
uc_run_index_base=6;
uc_run_index_num=1;
break;
case 0x70:
uc_run_index_base=7;
uc_run_index_num=1;
break;
case 0x80:
uc_run_index_base=3;
uc_run_index_num=1;
break;
case 0x90:
uc_run_index_base=4;
uc_run_index_num=1;
break;
}
uc_run_kind=uc_temp2;
}
uc_old_i