我用的是定时器写的,没用中断。用两个按键控制PWM的占空比变大变小。。。PWM连接P2^0输出的。但是用示波器没有出现PWM波形。望有大神解惑。初学多问,无怪!
#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); //这是用IF语句写的,生成0,1信号的函数
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)
{
sta
tic 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;
}
}
}
一周热门 更多>