其次这个“TH0 = 0x0FFTL0 = 0x0FF”存在问题,应该是TH0 = 0x0FTL0 = 0x0F或TH0 = 0xFFTL0 = 0xFF,但这两种都不会是33KHz左右的频率
#include<reg52.h>#define uchar unsigned signed
sbit jia_key=P3^6
sbit jian_key=P3^7
sbit zf_key=P3^5
bit flag=0
uchar codetable[]={0x01,0x02,0x04,0x08,0x08,0x04,0x02,0x01}
uchar num,qidong,PWM,table_begin
void delay(uchar 1)
{
uchar j,k
for(j=ij>0j--)
for(k=125k>0k--)
}
void key()
{
if(jia_key==0)
{
delay(5)
if(jia_key==0)
{
num++
if(num==4)num=3
while(jia_key==0)
}
}
if(jian_key==0)
{
delay(5)
if(jian_key==0)
if(num!=0)
num--
else
num=0
while(jian_key==0)
}
}
if(zf_key==0)
{
delay(5)
if(zf_key==0)
{
flag=-flag
while(zf_key==0)
}
}
}
void qidong()
{
uchar i,j
switch(num)
{
case 0:PWM=5break
case 1:PWM=4break
case 2:PWM=3break
case 3:PWM=2break
}
if(flag==0)
table_begin=0
else
table_begin=4
for(i=table_begini<4+table_begini++)
{
P1=table[i]
for(j=0j<PWMj++)
delay(10)
}
}
void main()
{
while(1)
{
key()
qidong()
}
}
#include<stc.h>#define uchr unsigned char
#define uint unsigned int
sbit zh0 = P2^0
sbit zh1 = P2^1//定义两个端口
uint t //变量T
void main()
{
TMOD = 0x01
THO = (65536-50000)/256
TL0 = (65536-50000)%256
EA = 1
ET0 = 1
TR0 =1 //启动中断
while(1)
{
zh0=1
zh1=0 //程序近来后开始正传
if(t==200) //当中断进入200次,也就是10后电机反转,20次是一秒
{
t=0//把T清零
zh0=0
zh1=1
}
}
}
/*中断程序*/
void timer0 () interrupt 1
{
TH0 = (65536-50000)/256
TL0 = (65536-50000)%256
t++
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)