程序如下:#include
#define uchar unsigned char
#define uint unsigned int
const uchar tab_hi[] = {0x81,0xf7,0xf7,0x81,0xff,0xbd,0x81,0xbd}; //HI default
const uchar tab_zheng[] = {0x7e,0x7e,0x06,0x7e,0x00,0x76,0x76,0x76}; //zheng
const uchar tab_fang[] = {0x7f,0x9f,0x6f,0xb1,0xea,0xea,0xb2,0x7a}; //fang
const uchar tab_stop[] = {0xff,0xff,0x00,0xee,0xee,0xee,0xf5,0xfb}; //P
uchar *p;
uchar PWM1=0xfe; //PWM greater ,speed faster,default 0.5 ms
uchar PWM2=0x34;
uchar ENA;
uchar count0=0;
uchar count1=0;
const uchar tab[] = {0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; //P2
***it dir = P1^0; //direction
***it faster = P1^1; //faster
***it lower = P1^2; //lower
***it st = P1^3; //start or stop
***it P3_6 = P3^6;
***it P3_7 = P3^7;
void delay(uint n) //delay n ms
{
uchar i,j;
for(i=n;i>0;i--)
for(j=110;j>0;j--); //delay 1ms
}
void init(void)
{
TMOD = 0x11; // t1 work on mode 1,t0 work on mode 1
TH0 =0xfc; //initial value fc66, means 1 ms
TL0 =0x66;
TH1 =PWM1;
TL1 =PWM2;
EA = 1;
ET0=1;
ET1=1;
TR0 =1; //T0 START
}
void timer0() interrupt 1 //timer 0 interrupt
{
TH0=0xfc; //reload the 1 ms value to the timer 0
TL0=0x66;
if(count0<100)
count0++;
else
{
count0=0;
ENA=1;
TH1=PWM1;
TL1=PWM2;
TR1=1; //T1 START
}
}
void timer1() interrupt 3 //timer 1 interrupt 3
{
TH1=PWM1; // reload the value
TL1=PWM2;
if(count1<100)
count1++;
else
{
TR1=0; //T1 stop
ENA=0;
count1=0;
}
}
void main(void)
{
uchar i,j,change=0,direction=0;
P1 = 0xff; // SET 1 prepare to read data from P1 port
P3_6 = 1; // the default state of motor is stop
P3_7 = 1;
p = tab_hi; //initiated the 8*8 led, show HI
init();
while(1)
{
if(dir==0) //dir = change the direction of motor
{
delay(10); //delay for debouncing
if(dir==0)
{
direction = ~direction;
if(direction==0)
{
P3_6=1;
P3_7=ENA;
p =tab_zheng;
}
else
{
P3_6=ENA;
P3_7=1;
p=tab_fang;
}
}
while(!dir);
delay(10);
while(!dir);
}
if(faster==0) // speed up
{
delay(10);
if(faster==0)
{
PWM1=0xff; //0.05ms
PWM2=0xd2;
}
while(!faster);
delay(10);
while(!faster);
}
if(lower==0) //lower = speed down
{
delay(10);
if(lower==0)
{
PWM1=0xfc; //0.95 ms
PWM2=0x94;
}
while(!lower);
delay(10);
while(!lower);
}
if(st==0) //st = start the motor or stop the motor
{
delay(10);
if(st==0)
{
change = ~change;
if(change==0) //stop t0he motor
{
P3_6 = 1;
P3_7 = 1;
PWM1 = 0xfe; //0.5 ms
PWM2 = 0x34;
p = tab_stop;
}
else // start the motor
{
P3_6 = 1;
P3_7 = ENA;
p = tab_zheng;
}
}
while(!st);
delay(10);
while(!st);
}
for(i=0,j=0;i<8;i++)
{
P2 = tab[j++]; // enable
P0 = *p++; //zi xing ma
delay(2);
if(j==8)
{
j=0;
p=p-8;
//P0=0xff;
//P2=0x00;
}
}
}
}
实际调试的时候,电机速度不变,不知道是什么原因,求大家分析一下
|