标题:
STC12单片机按键控制PWM程序
[打印本页]
作者:
2545010433
时间:
2021-11-10 14:42
标题:
STC12单片机按键控制PWM程序
#include <reg52.h>
typedef unsigned char uchar;
typedef unsigned int uint;
sbit Key1=P3^0;
sbit Key2=P3^1;
sbit PWM=P1^0;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar count,time,i;
void delayms(uint k)
{
uint i,j;
for(i=k;i>0;i--)
for(j=110;j>0;j--);
}
void Timer0Init() //100微秒@12.000MHz
{
TMOD |= 0x02; //设置定时器模式
TL0 = 0x9C; //设置定时初值
TH0 = 0x9C; //设置定时重载值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=1;
ET0=1;
}
void display()
{
static bit num=0;
P0 = 0x00;
if(num)
{
P2=~0xfe;
P0=table[i/10];
num=~num;
}
else
{
P2=~0xfd;
P0=table[i%10];
num=~num;
}
}
void main( )
{
Timer0Init();
count=99;
i=100-count;
while(1)
{
if(!Key1) //
{
delayms(10);
if(!Key1)
{
if(count>1)
count--;
while(!Key1);
i=100-count;
}
}
if(!Key2)
{
delayms(10);
if(!Key2)
{
if(count<99)
count++;
while(!Key2);
i=100-count;
}
}
}
}
void timer0() interrupt 1
{
time++;
if(count>=time)
PWM=1;
else PWM=0;
if(time>99)
time=0;
if(time%10==0)
display();
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1