短按调色,1次白光,2次黄光,3次中性光每种光都可长按无级调光,互不影响。
中微SC8p8022已测稳定.
- #include <sc.h>
- #include "Touch_Kscan_Library.h"
- volatile unsigned int pwm_duty,pwm_duty1,pwm_duty2,num;
- volatile unsigned char MainTime,touch_count = 0;
- volatile bit B_MainLoop,state=0;
- volatile unsigned int Time;
- void delay(int count) {
- for (int i = 0; i < count; i++) {
- for (int j = 0; j < 100; j++) {
- }
- }
- }
- //系统初始化
- void Init_System()
- {
- asm("nop");
- asm("clrwdt");
- INTCON = 0; //禁止中断
- OSCCON = 0X72; //配置振荡为8M
- OPTION_REG = 0;
-
- PORTB = 0;
-
- PIE1 = 2;
-
- PR2 = 250; //8M下将TMR2设置为125us中断
- //TRISB=0B00010100;
- TRISB=0;
- PWMCON0=0;
- PWMTL=0XEF;
- PWMT4L=0XEF;
- PWMTH=0B00001111;
-
- PWMD23H=00;
- PWMD3L=0X0F;
- PWMD4L=0X0F;
-
-
- PWMIF=0;
- PWMCON0 &= ~(0b00011000);
-
- T2CON = 4; //使能定时器2
- INTCON = 0XC0; //使能中断
- }
- void Refurbish_Sfr()
- {
- //刷新中断相关控制寄存器
- PIE1 = 2;
- PR2 = 250;
- INTCON = 0XC0;
- if (4 != T2CON)
- T2CON = 4;
- }
- void set_pwm_duty_cycle(unsigned int pwm3_duty, unsigned int pwm4_duty)
- {
-
-
-
- pwm3_duty=pwm3_duty/4;
- pwm4_duty=pwm4_duty/4;
- // 写入PWM3占空比(8位)
-
- PWMD23H = ((pwm3_duty >> 8) & 0x03)<<4; // 高2位
- PWMD3L = pwm3_duty&0xFF ; // 低8位
- // 写入PWM4占空比(10位)
- PWMTH = ((pwm3_duty >> 8) & 0x03)<<4;
- PWMD4L = pwm4_duty&0xFF;
- PWMCON0=0B00011000;
-
-
-
-
-
- }
- /***********************************************************
- 键处理函数
- ***********************************************************/
- void KeyServer()
- {
- static unsigned char KeyOldFlag = 0;
- static bit Leave,state=0;
- unsigned int Tim;
-
- // 有键按下
- if (KeyFlag[0])
- {
- delay(6);//delay(8);
- if(KeyFlag[0]){
- if(Leave)Leave = 0; //有键按下 清 松手标志
-
- Time++;
- if(Time>200)
- {
- Tim++;
- if(Tim>=3000){
- Tim=0;
-
- if(state==0)
- {
- num++; //增加亮度
- if(num>=800)
- {
- num=800;
- }
- }
- if((state==1) && (num>8))
- {
- num--; //减少加亮度
- if(num<=12)
- {
- num=12;
- }
- }
-
- switch(touch_count)
- {
- case 1:
- pwm_duty1=num;
- set_pwm_duty_cycle(0,pwm_duty1);
- break;
- case 2:
- pwm_duty=num;
- set_pwm_duty_cycle(pwm_duty, 0);
- break;
- case 3:
- pwm_duty2=num;
- set_pwm_duty_cycle(pwm_duty2, pwm_duty2);//
- break;
- case 4:
- break;
- }
- //num=pwm_duty;
- }
- }
-
- KeyOldFlag = KeyFlag[0];
- }
- }
- else //没键按下
- {
- if(Leave == 0) //无键按下 置位 松手标志
- {
- Leave = 1;
- state = ~state; //松手后只执行一次
- }
-
- if((KeyOldFlag!=KeyFlag[0]) && Time<200) //短按处理
- {
- delay(4);
- touch_count++;
- switch (touch_count)
- {
- case 1:
- {set_pwm_duty_cycle(0, pwm_duty1); // PWM4 亮 黄
- num=pwm_duty1;
- break;}
- case 2:
- {set_pwm_duty_cycle(pwm_duty, 0); // PWM3 亮
- num=pwm_duty;
- break;}
- case 3:
- {set_pwm_duty_cycle(pwm_duty2,pwm_duty2); // 都 亮
- num=pwm_duty2;
- break;}
- case 4:
- {//set_pwm_duty_cycle(0, 0); // 关
- //PWMD3L=0;
- //PWMD4L=0;
- PWMCON0 &= ~(0b00011000);
- touch_count=0;
- break;}
- }
-
- }
-
- Time=0;
- KeyOldFlag = 0;
- }
- }
- /***********************************************************
- 中断服务函数
- ***********************************************************/
- void interrupt Isr_Timer()
- {
- if (TMR2IF)
- { //若只使能了一个中断源,可以略去判断
- TMR2IF = 0;
- if (++MainTime >= 20)
- {
- MainTime = 0;
- B_MainLoop = 1;
-
- }
- }
- else
- {
- PIR1 = 0;
- }
-
- }
- /***********************************************************
- 主循环
- ***********************************************************/
- void main()
- {
- Init_System();
- pwm_duty=12;
- pwm_duty1=12;
- pwm_duty2=12;
- while (1)
- {
- if (B_MainLoop)
- {
- B_MainLoop = 0;
- CLRWDT();
- CheckTouchKey();
- Refurbish_Sfr();
- KeyServer();
- }
- }
- }
复制代码
程序下载:
双色无级调光.rar
(136.47 KB, 下载次数: 0)
|