主题:有个程序请你们看看
这是个显示程序.清高手近来看看!
#include <reg51.h>
unsigned char code BCD_code[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned char code Bit_code[8] ={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char buffer[8]={0xc0,0xc0,0xc0,0xc0,0xc0,0xa4,0xc0,0xc0}; //显示缓存数组;
//unsigned char phase1[4]={0x1,0x4,0x2,0x8};
//unsigned char phase1[4]={0xf1,0xf8,0xf2,0x0f4}; //clockwise phase;
//unsigned char phase2[4]={0xf1,0xf4,0xf2,0xf8}; //anticlockwise phase;
unsigned char phase1[4]={0xf3,0xf6,0xfc,0xf9};
unsigned char phase2[4]={0xf3,0xf9,0xfc,0xf6};
unsigned int count1,count2;
unsigned char speed1,disp_bit,n_phase;
unsigned int speed2;
unsigned int totalcycle; //累计直流电机转的圈数;
unsigned char keyflag;
unsigned char speed1_bit,speed2_bit;
bit action,direction;
sbit P14=P1^4;
sbit P32=P3^2;
sbit P33=P3^3;
sbit P34=P3^4;
void scankey();
void display();
void keyfunc(void);
void update_buffer();
void initial_EA(void)
{
TMOD=0x51; //T0定时,方式1,T1计数,方式1;
TH0=0xF6;TL0=0x3C;
TH1=0x0;TL1=0x0;
EA=1;ET0=1;
TR1=1;TR0=1; //启动T0,T1;
}
main()
{
//unsigned int m;
P1=0x0; //驱动电机
P2=0x0; //位码
P0=0x80; //段码
action=1; //启动停止控制变量
direction=1; //方向控制变量
speed2=20;
disp_bit=0;
speed1_bit=0;
speed2_bit=5;
n_phase=0;
count1=0;
count2=0;
totalcycle=0;
initial_EA();
////////////
while(1)
{
scankey();
keyfunc();
}
}
void scankey() //键盘扫描
{
if(P32==0) //启动停止
{
while(P32==0);
keyflag=0x1; //action
}
else if(P33==0)
{
while(P33==0);
keyflag=0x2; //speed2加档
}
else if(P34==0)
{
while(P34==0);
keyflag=0x4; //speed2减档
}
else if(P14==0)
{
while(P14==0);
keyflag=0x8;
}
else
keyflag=0xff;
}
void keyfunc(void) //键盘处理程序
{
unsigned char temp;
switch(keyflag)
{
case 0x1: //启动跟停止
if(action)
{
action=0;
}
else
{
action=1;
}
count2=0;
break;
case 0x2: //加速
{
switch(speed2)
{
case 1:
speed2=10;
break;
case 10:
speed2=20;
break;
case 20:
speed2=30;
break;
case 30:
speed2=40;
break;
case 40:
speed2=60;
break;
default:
break;
}
/*temp=speed2; //更新之前对temp和speed2_bit重新 赋值
speed2_bit=4;
while(temp/10!=0) //更新速度缓冲区的值(speed2)
{
buffer[speed2_bit++]=BCD_code[temp%10];
temp=temp/10;
}
buffer[speed2_bit]=BCD_code[temp];
count2=0; */
update_buffer();
break;
}
case 0x4:
{
switch(speed2)
{
case 10:
speed2=1;
break;
case 20:
speed2=10;
break;
case 30:
speed2=20;
break;
case 40:
speed2=30;
break;
case 60:
speed2=40;
break;
default:
break;
}
/*temp=speed2;
speed2_bit=4;
while(temp/10!=0)
{
buffer[speed2_bit++]=BCD_code[temp%10];
temp=temp/10;
}
buffer[speed2_bit]=BCD_code[temp];
count2=0; */
update_buffer();
break;
}
case 0x8:
if(direction)
{
direction=0;
}
else
{
direction=1;
}
count2=0;
break;
default:
break;
} //switch();
}
void test_speed()
{
unsigned char temp1;
speed1_bit=0;
speed1=TH1*256+TL1;
totalcycle+=speed1; //累加圈数;
TH1=0x0;TL1=0x0;
temp1=speed1;
if(temp1==0)temp1=totalcycle; //直流电机停止转动后就显示总的圈数;
while(temp1/10!=0)
{
buffer[speed1_bit++]=BCD_code[temp1%10];
temp1=temp1/10;
}
buffer[speed1_bit]=BCD_code[temp1];
count1=0x0;
}
void drive_step() //驱动电机
{
if(action==0)
P1=0xf0;
else
{
if(direction==1)
P1=phase1[n_phase++];
else
P1=phase2[n_phase++];
if(n_phase>3)
n_phase=0;
count2=0;
}
}
void display() //显示速度
{
if(speed1_bit+1==disp_bit)
disp_bit=4;
if(speed2_bit+1==disp_bit)
disp_bit=0;
P2=Bit_code[disp_bit];
P0=buffer[disp_bit];
disp_bit++;
if(disp_bit>7)disp_bit=0;
}
void update_buffer()
{
unsigned int temp;
temp=speed2;
speed2_bit=4;
while(temp/10!=0)
{
buffer[speed2_bit++]=BCD_code[temp%10];
temp=temp/10;
}
buffer[speed2_bit]=BCD_code[temp];
count2=0;
}
timer0() interrupt 1 using 0 //定时2.5ms中断
{
unsigned int drv_speed;
TH0=0xF6;TL0=0x3C;
count1++;
count2++;
display();
if(400==count1)
test_speed();
drv_speed=120/speed2; //60000/(speed*48*2.5);
if(count2==drv_speed)
drive_step();
}
此帖转自:[url]http://www.programfan.com/team/team.asp?team_id=1364[/url]
#include <reg51.h>
unsigned char code BCD_code[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned char code Bit_code[8] ={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char buffer[8]={0xc0,0xc0,0xc0,0xc0,0xc0,0xa4,0xc0,0xc0}; //显示缓存数组;
//unsigned char phase1[4]={0x1,0x4,0x2,0x8};
//unsigned char phase1[4]={0xf1,0xf8,0xf2,0x0f4}; //clockwise phase;
//unsigned char phase2[4]={0xf1,0xf4,0xf2,0xf8}; //anticlockwise phase;
unsigned char phase1[4]={0xf3,0xf6,0xfc,0xf9};
unsigned char phase2[4]={0xf3,0xf9,0xfc,0xf6};
unsigned int count1,count2;
unsigned char speed1,disp_bit,n_phase;
unsigned int speed2;
unsigned int totalcycle; //累计直流电机转的圈数;
unsigned char keyflag;
unsigned char speed1_bit,speed2_bit;
bit action,direction;
sbit P14=P1^4;
sbit P32=P3^2;
sbit P33=P3^3;
sbit P34=P3^4;
void scankey();
void display();
void keyfunc(void);
void update_buffer();
void initial_EA(void)
{
TMOD=0x51; //T0定时,方式1,T1计数,方式1;
TH0=0xF6;TL0=0x3C;
TH1=0x0;TL1=0x0;
EA=1;ET0=1;
TR1=1;TR0=1; //启动T0,T1;
}
main()
{
//unsigned int m;
P1=0x0; //驱动电机
P2=0x0; //位码
P0=0x80; //段码
action=1; //启动停止控制变量
direction=1; //方向控制变量
speed2=20;
disp_bit=0;
speed1_bit=0;
speed2_bit=5;
n_phase=0;
count1=0;
count2=0;
totalcycle=0;
initial_EA();
////////////
while(1)
{
scankey();
keyfunc();
}
}
void scankey() //键盘扫描
{
if(P32==0) //启动停止
{
while(P32==0);
keyflag=0x1; //action
}
else if(P33==0)
{
while(P33==0);
keyflag=0x2; //speed2加档
}
else if(P34==0)
{
while(P34==0);
keyflag=0x4; //speed2减档
}
else if(P14==0)
{
while(P14==0);
keyflag=0x8;
}
else
keyflag=0xff;
}
void keyfunc(void) //键盘处理程序
{
unsigned char temp;
switch(keyflag)
{
case 0x1: //启动跟停止
if(action)
{
action=0;
}
else
{
action=1;
}
count2=0;
break;
case 0x2: //加速
{
switch(speed2)
{
case 1:
speed2=10;
break;
case 10:
speed2=20;
break;
case 20:
speed2=30;
break;
case 30:
speed2=40;
break;
case 40:
speed2=60;
break;
default:
break;
}
/*temp=speed2; //更新之前对temp和speed2_bit重新 赋值
speed2_bit=4;
while(temp/10!=0) //更新速度缓冲区的值(speed2)
{
buffer[speed2_bit++]=BCD_code[temp%10];
temp=temp/10;
}
buffer[speed2_bit]=BCD_code[temp];
count2=0; */
update_buffer();
break;
}
case 0x4:
{
switch(speed2)
{
case 10:
speed2=1;
break;
case 20:
speed2=10;
break;
case 30:
speed2=20;
break;
case 40:
speed2=30;
break;
case 60:
speed2=40;
break;
default:
break;
}
/*temp=speed2;
speed2_bit=4;
while(temp/10!=0)
{
buffer[speed2_bit++]=BCD_code[temp%10];
temp=temp/10;
}
buffer[speed2_bit]=BCD_code[temp];
count2=0; */
update_buffer();
break;
}
case 0x8:
if(direction)
{
direction=0;
}
else
{
direction=1;
}
count2=0;
break;
default:
break;
} //switch();
}
void test_speed()
{
unsigned char temp1;
speed1_bit=0;
speed1=TH1*256+TL1;
totalcycle+=speed1; //累加圈数;
TH1=0x0;TL1=0x0;
temp1=speed1;
if(temp1==0)temp1=totalcycle; //直流电机停止转动后就显示总的圈数;
while(temp1/10!=0)
{
buffer[speed1_bit++]=BCD_code[temp1%10];
temp1=temp1/10;
}
buffer[speed1_bit]=BCD_code[temp1];
count1=0x0;
}
void drive_step() //驱动电机
{
if(action==0)
P1=0xf0;
else
{
if(direction==1)
P1=phase1[n_phase++];
else
P1=phase2[n_phase++];
if(n_phase>3)
n_phase=0;
count2=0;
}
}
void display() //显示速度
{
if(speed1_bit+1==disp_bit)
disp_bit=4;
if(speed2_bit+1==disp_bit)
disp_bit=0;
P2=Bit_code[disp_bit];
P0=buffer[disp_bit];
disp_bit++;
if(disp_bit>7)disp_bit=0;
}
void update_buffer()
{
unsigned int temp;
temp=speed2;
speed2_bit=4;
while(temp/10!=0)
{
buffer[speed2_bit++]=BCD_code[temp%10];
temp=temp/10;
}
buffer[speed2_bit]=BCD_code[temp];
count2=0;
}
timer0() interrupt 1 using 0 //定时2.5ms中断
{
unsigned int drv_speed;
TH0=0xF6;TL0=0x3C;
count1++;
count2++;
display();
if(400==count1)
test_speed();
drv_speed=120/speed2; //60000/(speed*48*2.5);
if(count2==drv_speed)
drive_step();
}
此帖转自:[url]http://www.programfan.com/team/team.asp?team_id=1364[/url]