int main (void)
{
volatile u8 t=0;
volatile u8 i=0;
volatile u8 key; //按键
volatile u16 led0pwmval = 499;//LED灯亮度值 0为最亮,499为最弱
volatile u8 SD_Mode = 0;//手动模式
volatile u16 adcx0;
volatile u16 adcx1;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //初始化延时函数
uart_init(115200); //初始化串口波特率为115200
LCD_Init(); //LCD初始化
LED_Init(); //初始化LED
KEY_Init(); //按键初始化
Adc_Init(); //adc初始化
TIM13_PWM_Init(500-1,84-1); //84M/84=1Mhz的计数频率,重装载值500,所以PWM频率为 1M/500=2Khz.
TIM_SetCompare1(TIM13,led0pwmval); //修改比较值,修改占空比
POINT_COLOR=RED;
LCD_ShowString(30,50,200,16,16,"CANOE [url=home.php?mod=space&uid=72445]@[/url] Light");
delay_ms(5000);
while(1)
{
while (SD_Mode == 0)//自动模式
{
LCD_ShowString(30,70,200,16,16,"zidongmoshi");
LCD_ShowString(30,150,200,16,16,"LIGHT_VAL:");
key=KEY_Scan(0);
if(key==KEY0_PRES||key==WKUP_PRES) //在自动模式下,如果检测到有按键按下,则退出自动模式进入手动模式
{
SD_Mode = 1;
}
TIM_SetCompare1(TIM13,499);
adcx0=Get_Adc_Average(ADC_Channel_2,10); //通过ADC1探测得到红外探测模块的输出值,当有人活动时会监测得到高电平
if (adcx0 > 1500) //当探测得到高电平时,根据ADC通道3采集得到光敏电阻的值来调节亮度值
{
for(i=0;i<100;i++)
{
adcx1=Get_Adc_Average(ADC_Channel_3,10);//采集得到光敏电阻的值,环境亮度越暗得到的值越小,亮度越高得到的值越大
//adcx1的值处于1200~4000范围内
led0pwmval = (adcx1 - 1200)/6;
TIM_SetCompare1(TIM13,led0pwmval); //修改比较值,修改占空比
POINT_COLOR=BLUE;//设置字体为蓝色
LCD_ShowxNum(110,150,(500-led0pwmval),4,16,0); //显示 亮度值
delay_ms(100);
key=KEY_Scan(0);
if(key==KEY0_PRES||key==WKUP_PRES) //在自动模式下,如果检测到有按键按下,则退出自动模式进入手动模式
{
SD_Mode = 1;
i=100;
}
}
}
}
led0pwmval = 300;
while(SD_Mode == 1) //手动模式下 按键增减LED亮度
{
LCD_ShowString(30,70,200,16,16,"shoudongmoshi");
LCD_ShowString(30,90,200,16,16,"WK_UP:+ KEY0:-");
POINT_COLOR=BLUE;//设置字体为蓝色
LCD_ShowString(30,150,200,16,16,"LIGHT_VAL:");
t++;
key=KEY_Scan(0);
if(key==WKUP_PRES)
{
if(led0pwmval<499)led0pwmval+=40;
TIM_SetCompare1(TIM13,led0pwmval); //修改比较值,修改占空比
}
else if(key==KEY0_PRES)
{
if(led0pwmval>45)led0pwmval-=40;
else led0pwmval=0;
TIM_SetCompare1(TIM13,led0pwmval); //修改比较值,修改占空比
}
if(t==10||key==KEY0_PRES||key==WKUP_PRES) //WKUP/KEY1按下了,或者定时时间到了
{
LCD_ShowxNum(110,150,(500-led0pwmval),4,16,0); //显示 亮度值
LED0=!LED0;
t=0;
}
delay_ms(10);
}
}
}