|
#include <reg52.h>
#define ufloat unsigned float
#define uchar unsigned char
#define uint unsigned int
uchar zkb, t0;
bit flag1, flag2; //flag1占空比标志位, flag2占空比方向标志位(0自增, 1自减);
sbit led = P1^1; //输出口;
void init ();
void display ();
/**********----------**********/
int main (void)
{
init();
while (1)
{
display ();
}
return 0;
}
/**********----------**********/
void init () //初始化子函数
{
TMOD = 0x01; //定时器0模式;
TH0 = (65536-10)/256; //附高8位初值(0.01ms);
TL0 = (65536-10)%256; //附底8位初值(0.01ms);
EA = 1; //开总中断;
ET0 = 1; //开定时器0中断;
TR0 = 1; //开启定时器0;
}
/**********----------**********/
void display () //占空比子函数;
{
if (flag1 == 1) //占空比标志起始;
{
flag1 = 0; //复位占空比标志位;
if (flag2 == 0) //占空比自增标志;
{
zkb++;
if (zkb == 200) //占空比最大值;
{
flag2 = 1; //占空比进入自减标志位开启;
}
}
else //占空比自减开始;
{
zkb--;
if (zkb == 0) //占空比最小值;
{
flag2 = 0; //占空比进入自增标志位开启;
}
}
}
}
/**********----------**********/
void timer0() interrupt 1
{
TH0 = (65536-10)/256;
TL0 = (65536-10)%256;
t0++;
if (t0 <= zkb) //未到占空比数值输出为0;
{
led = 0;
}
else
{
led = 1; //到达占空比数值输出为1;
}
if (t0 == 200) //总周期为200*0.01ms 一旦到达总周期,动态计数器t0累加值清零,进入下个循环;
{
t0 = 0;
led = 1;
flag1 = 1; //占空比标志再次开启;
}
}
|
评分
-
查看全部评分
|