|
/*********************************************************************************************
DB1-010 感應(yīng)式閱讀燈
STC11L08XE單片機(jī):
PxM0=0x00; //準(zhǔn)雙向
PxM1=0x00;
PxM0=0x11;//強(qiáng)推挽
PxM1=0x00;
PxM0=0x00;//高阻
PxM1=0x11;
PxM0=0x11;//開漏
PxM1=0x11;
程序功能:
1.首次開燈時(shí),為最高亮度.
2.手壓在外殼上方時(shí),LED會(huì)逐漸變暗至最低亮度.
3.手放開時(shí),LED會(huì)保持在當(dāng)前亮度.
4.再次壓住外殼,LED將會(huì)逐漸變亮至最大亮度.
5.DYS412紅外傳感器,平時(shí)輸出高電平,有反射信號(hào)輸出低電平.
6.可開關(guān)和無極調(diào)光.
/*********************************************************************************************
說明:
/*********************************************************************************************/
#include <DB1.H> //STC11Fxx或STC11Lxx系列單片機(jī)頭文件 #include<reg52.h>
#include <intrins.h> //51基本運(yùn)算(包括_nop_空函數(shù))
#define LED P20 //燈組1
#define DYS412 P45 //DYS412傳感器輸入
unsigned char MENU;
/*********************************************************************************************
函數(shù)名:毫秒級(jí)CPU延時(shí)函數(shù)
調(diào) 用:DELAY_MS (?);
參 數(shù):1~65535(參數(shù)不可為0)
返回值:無
結(jié) 果:占用CPU方式延時(shí)與參數(shù)數(shù)值相同的毫秒時(shí)間
備 注:應(yīng)用于1T單片機(jī)時(shí)i<600,應(yīng)用于12T單片機(jī)時(shí)i<125
/*********************************************************************************************/
void DELAY_MS (unsigned int a){
unsigned int i;
while( a-- != 0){
for(i = 0; i < 600; i++);
}
}
/*********************************************************************************************/
void DELAY (unsigned char a){//顏色PWM用延時(shí)
unsigned char i;
while( a-- != 0){
for(i = 0; i < 2; i++);
}
}
/*********************************************************************************************/
void LEDPWM (unsigned char i,unsigned char a){//顯示程序(需不斷調(diào)用)
unsigned char x;
for(x=0;x<a;x++){//設(shè)置循環(huán)顯示次數(shù)
LED = 0;
DELAY (i);
LED = 1;
DELAY (255-i);
}
}
/*********************************************************************************************/
void init (void){ //初始IO程序
P4SW = 0xff; //啟動(dòng)P4接口
P0M0 = 0x00; //設(shè)置I/O口工作方式(推挽)
P0M1 = 0x00; //設(shè)置I/O口工作方式(高阻)
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
}
/**********************************************************************************************/
void main (void){
unsigned char DY_PWM;
unsigned int c;
bit ONOFF;//開/關(guān)燈標(biāo)志位
bit aad;//調(diào)光加減標(biāo)志位
DELAY_MS (500);
init();//初始程序
ONOFF = 0;c = 0;DY_PWM = 255;
while(1){
if(ONOFF==1){//燈開關(guān)控制位
LEDPWM(DY_PWM,1);//調(diào)光程序
}
if(DYS412==0){//按鍵/感應(yīng) 處理程序部分
while(DYS412==0&&c<800){//長按到時(shí)跳轉(zhuǎn)
c++;LEDPWM(DY_PWM,1);
}
if(c>700){//長鍵處理
while(DYS412==0){
if(aad){
DY_PWM++;
if(DY_PWM>254){
LEDPWM(210,30);
DY_PWM=254;
}
LEDPWM(DY_PWM,20);
}else{
DY_PWM--;
if(DY_PWM<2){
LEDPWM(3,30);
DY_PWM=2;
}
LEDPWM(DY_PWM,20);
}
}
aad=~aad;//下次調(diào)光時(shí)是反向調(diào)整的
ONOFF = 1;//調(diào)光后必然是開燈狀態(tài)的
}else{//短鍵處理
ONOFF=~ONOFF;
while(DYS412==0)LEDPWM(DY_PWM,1);
}
c=0;
}
}
}
|
|