📄 timeruseex.c
字号:
//C51 一个产生占空比变化的脉冲信号的程序。它产生的脉宽调制信号用于电机变速控制
//motor.c
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar time,status,percent,period;
bit one_round;
uint oldcount,target=500;
void main()
{
IP=0x04;
TMOD=0x11;
TCON=0x54;
TH1=0;
TL1=0;
IE=0x86;
for(;;)
{if(one_round)
{
if(period<target)
{
if(percent<100)
++percent;
}
else if(percent>0)
--percent;
one_round=0;
}
}
}
void pulse(void)interrupt 1 using 1 //T/C中断服务程序
{
TH0=(65536-833)/256; //1 ms(10Mhz)
TL0=(65536-833)%256;
ET0=1;
if(++time==percent)P1=0;
else if(time==100)
{time=0;P1=1;}
}
void tachmeter(void) interrupt 2 using 2//外部中断1服务程序
{
union{
uint word;
struct
{
uchar hi;
uchar lo;
} byte;
}newcount;
newcount.byte.hi=Th1;
newcount.byte.lo=TL1;
period=newcount.word-oldcount;//测得周期
oldcount=newcount.word; //更新计数值
one_round=1; //每转一周,引起中断,设置标志
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -