1、使用T0和T1外部脉冲计数,控制两个电机,到1700个脉冲电机停止,
--统计脉冲个数,用外部中断更好。
--T0和T1,留着定时用。
用四个按键控制,按键1控制电机正转,按键2控制电机反转,按键3控制电机1正转,按键4控制电机2正转。
2、例程:
#include<reg51.h>sbit k1 = ? //自己定义
sbit k2 = ?
sbit k3 = ?
sbit k4 = ?
sbit DIAN1 = ?
sbit DIAN2 = ?
sbit DIAN3 = ?
sbit DIAN4 = ?
sbit SEN1 = ?
sbit SEN2 = ?
void delay(unsigned int x)
{
unsigned int m, n
for(m = x m > 0 m--) for(n = 115 n > 0 n--)
}
void main(void)
{
TMOD = 0x55
IE = 0x8a
while(1) {
if (!k1) {
delay(10)
if (!k1) {
while(!k1)
TH0 = (65536 - 1700) / 256
TL0 = (65536 - 1700) % 256
SEN1 = 0
DIAN1 = 0 DIAN2 = 1
SEN1 = 1
TR0 = 1
}
}
if (!k2) {
delay(10)
if (!k2) {
while(!k2)
TH0 = (65536 - 1700) / 256
TL0 = (65536 - 1700) % 256
SEN1 = 0
DIAN1 = 1 DIAN2 = 0
SEN1 = 1
TR0 = 1
}
}
if (!k3) {
delay(10)
if (!k3) {
while(!k3)
TH1 = (65536 - 1700) / 256
TL1 = (65536 - 1700) % 256
SEN2 = 0
DIAN3 = 0 DIAN4 = 1
SEN2 = 1
TR1 = 1
}
}
if (!k4) {
delay(10)
if (!k4) {
while(!k4)
TH1 = (65536 - 1700) / 256
TL1 = (65536 - 1700) % 256
SEN2 = 0
DIAN3 = 1 DIAN4 = 0
SEN2 = 1
TR1 = 1
}
}
}
}
TMOD=0x05;TH0=0;TL0=0;
TR0=1;
while(1)
{
a=TH0*256+TL0;
display(a);
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)