|
1金钱
#include "reg52.h"
typedef unsigned int u16;
typedef unsigned char u8;
sbit key=P3^2;
sbit mot=P1^0;
u8 duty=20;
u8 cycle=0;
u8 tim=0;
u8 th=0xFF;
u8 tl=0xF6;
u8 b=0;
u8 rece=0;
void delay_10us(u8 D)
{
while(D--);
}
void init()
{
EA=1;
ES=1;
TR1=1;
ET0=1;
TR0=1;
TMOD=0x21;
TH0=th;
TL0=tl;
TH1=0xFA;
TL1=0xFA;
SCON=0x50;
PCON=0x80;
}
void uart()interrupt 4
{
while(!RI);
RI=0;
rece=SBUF;
}
void tim0()interrupt 1
{
TH0=th;
TL0=tl;
tim++;
if(tim>=cycle) tim=0;
if(tim<=duty)
mot=1;
else
mot=0;
}
void pro1(u8 D,u8 i)
{
static u8 dir=0;
cycle=D;
if(dir==0)
{
duty++;
if(duty==cycle) dir=1;
}
else
{
duty--;
if(duty==20) dir=0;
}
delay_10us(i);
}
main()
{
init();
while(1)
{
switch(rece)
{
case '0': pro1(100,1); break;
case '1': pro1(200,1); break;
}
}
}
我知道答案
本帖寻求最佳答案回答被采纳后将获得系统奖励 10 金钱 , 目前已有 0人回答
|
|