PWM控制LED灯亮度程序

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#i nclude
#i nclude
#i nclude
#include
void ledxianshi();
void PWM()
sbit up=P1^5; //定义UP键
sbit down=P1^6; //定义DOWN键
static unsigned char pwm;
unsigned char t;
unsigned char xdata seg_add _at_ 0xEFFF;
unsigned char xdata light_add _at_ 0xF7FF;
#define base_Adr 0xDFFF
#define uchar unsigned char
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define uchar unsigned char
#define uint unsigned int
#define ADC0809 XBYTE[0x0B000] //P2.6选通
float sum=0,fjg;
uint i,j,*q,*p,sd=5;
sbit p1_0=P1^0;
void wait(uint n);
void main(void)
{ j=0;
TMOD=0X01;//设置定时器0
TH0=0XFC;//定时时间设为1ms
TL0=0X18;
EA=0;//先关中断
EA=1;//开总中断
ET0=1;
//TR0=1;
for(; ;)
{
if(j=20){
do{}while(!TF0);
q=0x30;
for(i=0;i<20;i++)
{sum=sum+(*q)*(*q);
q++;
}
sum=sum/20;
fjg=sqrt(sum);
if(fjg>sd)
p1_0=1;
j=0;
}}
}
void power(void)
{ uchar x;
for(;;)
{ADC0809=0x01;
wait(1000);
x=ADC0809;
P2=x;
}
}
void wait(uint n)
{ uint i,j;
for(i=0;ifor(j=0;j<100;j++);
}
timer0()interrupt 1 using 1
{*p=ADC0809;
p++;
TH0=0XFC;
TL0=0X18;
TR0=1;
j++;
}
//sbit EOC=P3^2;//为A/D转换结束标志
//sbit START= //启动A/D

uint data tvdata; //
unsigned char xdata CH_base_Adr _at_ base_Adr;
unsigned char CH_data;
//unsigned char float;

void delay(unsigned int N) //延时
{
int i;
for(i=0;i}
void main()using 0
{
long b=19;
//light_add=0X3f;

/* 采样AD0809通道的值 */
while(1){
//CH_base_Adr=3;

CH_data =CH_base_Adr;
P0=CH_data;
tvdata=P0; //显示口
tvdata=(tvdata*b);
CH_base_Adr=0;
ledxianshi(); //显示
delay(50);
}
}
void ledxianshi(void) //显示模块
{
uchar k=0,seg=0,i=0;
//int i=0;

led[3]=tvdata%10;
led[2]=tvdata/10%10;
led[1]=tvdata/100%10;
led[0]=tvdata/1000;
for(k=0;k<4;k++)
{
seg=tv[k]; //选择哪个LED口
seg_add=seg;
i=led[k]; //显示口
light_add=a; //显示
delay(10);
}
}

PWM ()
{
void delay (unsigned char ms);
TMOD=0X21;
ET0=1;
ET1=1;
EA=1;
TH0=0XFC; //定时器0,定时1ms
TL0=0X18;
TH1=0X9C; //定时器1,定时0.1ms
TL1=0X00;
TR0=1;
PT1=1; //定义定时器1优先中断
pwm=0x9c;
while (1)
{
do
{
pwm++;
delay (10);
if (pwm==0xff) beep();
}
while (pwm!=0xff);

do
{
pwm--;
delay (10);
if (pwm==0x01) beep ();
}
while (pwm!=0x01);

}
}

void timer0 (void) interrupt 1 //定义定时器0
{
TH0=0XFC;
TL0=0X18;
TR1=1;


TH1=pwm;
P0=0X00;

}

void timer1(void) interrupt 3 //定义定时器1
{
P0=0XFF;
TR1=0;
}

void delay (unsigned char ms) //延时
{
unsigned char i=120;
while (ms--)
{
while (i--)
{}
}
}



相关文档
最新文档