/***************网红补光灯@PMS150G******************/
/***************ID:共同学习stm8********************/
/***************QQ:774145445***********************/
/***************拼音Pinyin*************************/
/***************IDE_0.97C9**v0.2*******************/
/***************20230422***************************/
// _______ _______
// 5v-|1 vdd \_/ gnd 8|-gnd 5v---------l+(灯+)
// +---led_l1-|2 pa7pms150gpa0 7|-anjian_1
// | +-led_l2-|3 pa6 pa4 6|-anjian_2 |------l1(暖色)
// | | +led-|4_pa5_______pa3_5|-pwm-+-|4.7k|----+----||←
// | | | | | |--gnd
// | | /_\ 蓝 | | |--------l2(冷色)
// | | | +-|4.7k|-+--|--||←
// | | 1k | | |--gnd
// | |_________________________________________| |
// |______________________________________________|
/**************************************************/
//v0.20 框架
//v0.21 开关功能+按键闪烁效果
//v0.22 切换功能亮度功能
//v0.23 定时功能+倒计时闪烁
//v0.233
//.Mult_Program_7 0
// .Mult_Program_7 1, 537
//PMS150G : Last use Code : 0x219 , Remain Free-Code : 0x1D7 [+37] , Remain Free-RAM : 51
/**************************************************/
#include "extern.h"
/**************************************************/
led_l1 bit pa.7;
led_l2 bit pa.6;
led bit pa.5;
anjian_1 bit pa.0;
anjian_2 bit pa.4;
/**************************************************/
void anjian(); //按键
void zhixing(); //执行
/**************************************************/ //全局变量
byte kaiguan=0; //开关状态(0关机1开机2定时)
//byte dangwei=10; //挡位(1-10个挡位)
byte qiehuan=3; //颜色切换(1暖色2冷色3双色)
byte liangdu=255; //亮度(0-255)
byte dingshi=0; //定时60秒
/**************************************************/
void FPPA0 (void)
{
//.ADJUST_IC SYSCLK=IHRC/4 // SYSCLK=IHRC/4
.ADJUST_IC SYSCLK=IHRC/16; //系统时钟1m
kaiguan=0;
qiehuan=3;
liangdu=255;
//liangdu=155;
dingshi=0;
$ led out,low;
byte a1;
zhixing();
while (1)
{
anjian();
if(dingshi >0)
{
while(dingshi >0)
{
dingshi --;
if(led ==1){led =0;} else {led =1;}
anjian();
.delay 500000;
}
kaiguan =0;
//led =0; //指示灯亮起
zhixing();
}
.delay 1000
}
}
/**************************************************/
void anjian() //按键
{
byte anjian_a1; //识别按键长短按键用
/**************************************************/
if(kaiguan ==1) ///*开机状态才检测的按键
{
/**************************************************/ //第一次加功能
$ anjian_1 in,ph,x_pl; //输入,开上拉电阻,关闭下拉电阻
$ anjian_2 in,ph,x_pl;
nop; nop; nop;
anjian_a1 =0;
if(anjian_2 ==0) //加功能
{
.delay 1000; //延时一下去抖
if(anjian_2 ==0)
{
while(anjian_2 ==0)
{
.delay 50000; //每秒十次
//.delay 50000;
//if(led ==1){led =0;} else {led =1;} //达到闪烁效果
//.delay 50000;
//if(led ==1){led =0;} else {led =1;}
anjian_a1 ++;
if(anjian_a1 >10) //长按
{
if(liangdu >252){liangdu=255; return;}
liangdu +=2;
zhixing();
}
}
//if((anjian_a1 >2)&&(anjian_a1 <11)) //短按加档位加(10个档)
if((anjian_a1 >0)&&(anjian_a1 <11)) //短按加档位加(10个档)
{
if(liangdu >230) //防止加冒了
{
liangdu =255;
zhixing();
}
else
{
liangdu +=25;
zhixing();
}
}
return; //不继续检测直接返回
}
}
/**************************************************/ //第二次切换功能
$ anjian_1 in,ph,x_pl; //输入,开上拉电阻,关闭下拉电阻
$ anjian_2 in,ph,x_pl;
nop; nop; nop;
anjian_a1 =0;
/**************************************************/ //切换功能
if(anjian_1 ==0)
{
.delay 1000; //延时一下去抖
if(anjian_1 ==0)
{
while(anjian_1 ==0) //记录按键时常
{
.delay 50000; //每秒十次
//.delay 50000;
//if(led ==1){led =0;} else {led =1;} //达到闪烁效果
///.delay 50000;
//if(led ==1){led =0;} else {led =1;}
anjian_a1 ++;
if(anjian_a1 >100){return;} //大于10秒直接返回
}
//if(anjian_a1 >2)
if(anjian_a1 >0)
{
qiehuan ++;
if(qiehuan ==4){qiehuan =1;}
zhixing();
.delay 1000000; //阻塞1秒防止过快开关机
}
return; //不继续检测直接返回
}
}
/**************************************************/
/**************************************************/
/**************************************************/
} //*/开机状态才检测的按键
/**************************************************/ //第三次开关机功能
$ anjian_1 in,x_ph,pl; //输入,关上拉电阻,开下拉电阻
$ anjian_2 in,x_ph,pl;
nop; nop; nop;
anjian_a1 =0;
if(anjian_1 ==1) //开关
{
.delay 1000; //延时一下去抖
if(anjian_1 ==1)
{
while(anjian_1 ==1) //记录按键时常
{
.delay 50000; //每秒十次
//.delay 50000;
//if(led ==1){led =0;} else {led =1;} //达到闪烁效果
///.delay 50000;
///if(led ==1){led =0;} else {led =1;}
anjian_a1 ++;
//if(anjian_a1 >200){return;} //大于10秒直接返回
}
if(kaiguan==1) //开机状态
{
if(anjian_a1 >10) //长按定时
{
//dingshi =60; //定时陆拾秒
dingshi =120;
led =0; //指示灯亮起
return;
}
else if(anjian_a1 >2) //短按关机
{
kaiguan =0;
led =0; //指示灯亮起
dingshi =0; //定时清零
zhixing();
.delay 1000000; //阻塞1秒防止过快开关机
return;
}
}
if(kaiguan==0) //关机状态
{
//if(anjian_a1 >2) //开机
if(anjian_a1 >0)
{
kaiguan =1;
led =1; //关闭指示灯
zhixing();
.delay 1000000; //阻塞1秒防止过快开关机
}
}
}
return;
}
/**************************************************/ //第四次减功能
if(kaiguan ==1) //开机状态才检测的按键
{
$ anjian_1 out,high; //输出高
$ anjian_2 in,x_ph,pl; //输入开下拉电阻
nop; nop; nop;
anjian_a1 =0;
if(anjian_2 ==1) //减功能
{
.delay 1000; //延时一下去抖
while(anjian_2 ==1)
{
.delay 50000; //每秒十次
//.delay 50000;
//if(led ==1){led =0;} else {led =1;} //达到闪烁效果
//.delay 50000;
//if(led ==1){led =0;} else {led =1;}
anjian_a1 ++;
if(anjian_a1 >10) //长按
{
if(liangdu <26){return;}
liangdu -=2;
zhixing();
}
}
//if((anjian_a1 >2)&&(anjian_a1 <11)) //短按档位减(10个档)
if((anjian_a1 >0)&&(anjian_a1 <11)) //短按档位减(10个档)
{
//if(liangdu <26){return;}
if(liangdu <30){return;}
else{
liangdu -=23;
zhixing();
return;
}
}
}
}
/**************************************************/
}
/**************************************************/
void zhixing() //执行
{
if(kaiguan ==1) //开状态
{
if(liangdu ==255) //全亮状态
{
$ tm2c STOP;
$ pa.3 in;
nop; nop; nop;
if(qiehuan==1) //暖色
{
$ led_l1 out,high; //输出1
$ led_l2 out,low;
}
if(qiehuan==2) //冷色
{
$ led_l1 out,low;
$ led_l2 out,high; //输出1
}
if(qiehuan==3) //双色
{
$ led_l1 out,high; //输出1
$ led_l2 out,high;
}
return;
}
else
{
if(qiehuan==1)
{
$ led_l1 in,x_ph,x_pl; //暖色
$ led_l2 out,low; //输0
}
if(qiehuan==2)
{
$ led_l1 out,low; //输0
$ led_l2 in,x_ph,x_pl; //冷色
}
if(qiehuan==3)
{
$ led_l1 in,x_ph,x_pl; //双色
$ led_l2 in,x_ph,x_pl;
}
//$ tm2c STOP; //刷新亮度值
$ TM2C IHRC,PA3,PWM;
$ TM2S 8BIT,/1,/2;
tm2b =liangdu; //亮度是默认值或者上次值
return;
}
}
/**************************************************/
if(kaiguan ==0) //关状态
{
$ tm2c STOP;
$ pa.3 in,x_ph,x_pl;
nop; nop; nop;
$ led_l1 out,low; //输0
$ led_l2 out,low; //输0
led =0; //指示灯亮起
}
}
/*
void Interrupt (void)
{
pushaf;
if (Intrq.T16)
{ // T16 Trig
// User can add code
Intrq.T16 = 0;
//...
}
popaf;
}
*/
|