急求用51单片机(stc89c52)利用pwm控制led灯光亮度变化的c程序,电路图如下: 5
急求用51单片机(stc89c52)利用pwm控制led灯光亮度变化的c程序,电路图如下:电路图可以忽略示波器和电压表...
急求用51单片机(stc89c52)利用pwm控制led灯光亮度变化的c程序,电路图如下:电路图可以忽略示波器和电压表
展开
1个回答
展开全部
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar count ,z;
sbit key_1 = P3^7;
sbit key_2 = P3^6;
sbit PWM = P2^0;
void key_control();
void init ();
void create_PWM(uchar);
void delay(uint);
void main ()
{
init ();
while(1);
{
key_control();
create_PWM(count);
}
}
void delay(uint z)
{
uint x,y;
for(x = z;x>0;x--)
for(y = 110;y>0;y--);
}
void init()
{
TMOD = 0x10;
TH1 = (65536-4608)/256;//50sm
TL1 = (65536-4608)%256;
TR1 = 1;
}
void create_PWM(uchar count)
{
static i;
if(TF1 == 1)
{
TF1 = 0;
TH1 = (65536-4608)/256;//50sm
TL1 = (65536-4608)%256;
i++;
if(i == 100) i=0;
if(i <= count) PWM = 1;
else PWM = 0;
}
}
void key_control()
{
if(key_1 ==0)
{
delay(10);
if(key_1 == 0)
{
count+= 5;
if(count >= 100)count = 0;
}
}
if(key_2 ==0)
{
delay(10);
if(key_2 ==0)
{
count-= 5;
if(count <= 0)count =100;
}
}
}
#define uint unsigned int
#define uchar unsigned char
uchar count ,z;
sbit key_1 = P3^7;
sbit key_2 = P3^6;
sbit PWM = P2^0;
void key_control();
void init ();
void create_PWM(uchar);
void delay(uint);
void main ()
{
init ();
while(1);
{
key_control();
create_PWM(count);
}
}
void delay(uint z)
{
uint x,y;
for(x = z;x>0;x--)
for(y = 110;y>0;y--);
}
void init()
{
TMOD = 0x10;
TH1 = (65536-4608)/256;//50sm
TL1 = (65536-4608)%256;
TR1 = 1;
}
void create_PWM(uchar count)
{
static i;
if(TF1 == 1)
{
TF1 = 0;
TH1 = (65536-4608)/256;//50sm
TL1 = (65536-4608)%256;
i++;
if(i == 100) i=0;
if(i <= count) PWM = 1;
else PWM = 0;
}
}
void key_control()
{
if(key_1 ==0)
{
delay(10);
if(key_1 == 0)
{
count+= 5;
if(count >= 100)count = 0;
}
}
if(key_2 ==0)
{
delay(10);
if(key_2 ==0)
{
count-= 5;
if(count <= 0)count =100;
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询