已经用adc测得光敏数值,然后想用pwm控制led,有占空比的那种,但是led不亮,想问问什么原因,下面是程序
#include "STC15F2K60S2.h"
#include "main.h"
#include "LCD1602.h"
#include "ADC.h"
#include "led.h"
unsigned char str[5];
unsigned int backup=0;
unsigned char aa[]={'L','I','G','H','T' ,':'};
unsigned int result;
unsigned int result1;
sbit light_pwm=P2^1;
unsigned char duty_cycle=0;
bit flag=0;
void delay_nms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<125;j++);
}
}
void main()
{
// zd_count=0;
// update_count=0;
// pwm_value=0;
// flag=0;
//
Timer0Init();
ET0=1; //打开定时器0中断
EA=1; //打开总中断
light_pwm=1;
init();
Init_ADC();
LCD_Write_String(0,1,aa);
while (1)
{
adc_light_display();
if(!flag)
{
duty_cycle++;
if(duty_cycle==255)
{
flag=1;
}
}
else
{
duty_cycle--;
if(duty_cycle==0)
{
flag=0;
}
}
light_pwm= (duty_cycle == 0 || duty_cycle == 255) ? 0 : 1;
delay_nms(10);
}
}
//ADC数值读取转化
void adc_light_display()
{
result1=ADC_Chang(2);
result=result1*4.75;
while (result!=backup)
{
str[0]=result/1000%100+'0';
str[1]=result/100%10+'0';
str[2]='.';
str[3]=result/10%10+'0';
str[4]=result%10+'0';
str[5]='\0';
LCD_Write_String(6,1,str);
backup=result;
}
}
//定时器0初始化函数,可由ISP软件生成
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器为16位自动重装载模式,较常使用
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
//定时器0中断服务函数
void Time0_Int(void) interrupt 1 //定时器1为3
{
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
light_pwm=duty_cycle;
} |