#include <reg52.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar code ccw[]={0x08,0x04,0x02,0x01}; //逆时针旋转相序//uchar code cw[]={0x01,0x02,0x04,0x08}; //顺时针旋转相序//uchar codespeed[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d};//uchar codetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, //0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//共阴极数码管显示表uchar code table_anode[]={ //共阳极0~f数码管编码0xc0,0xf9,0xa4,0xb0,//0~30x99,0x92,0x82,0xf8,//4~70x80,0x90,0x88,0x83,//8~b0xc6,0xa1,0x86,0x8e //c~f};uchar codeccw8[]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09}; //8拍旋转相序表uint
time,show1,show2,sym,temp,t,a,flag,i,j,rpm,n;sbit inc=P3^2;//加速sbit dec=P3^3;//减速sbit keyccw=P3^4;//逆时针转sbit keycw=P3^5;//顺时针转sbit pause=P3^6;//暂停sbit fir=P2^0;//第一数码管sbit sec=P2^1;//第二数码管 void init();//初始化定时器0,1,串口void direc();//按键控制转向void speedctl();//按键控制转速void display();//速度显示函数void delaynms(uint aa);//1ms延时函数 void main(){ init(); while(1) { speedctl(); direc(); display(); }}void wind_init(){ TMOD=0X21;//定时器1为方式2,定时器0为方式1 TH1=0xFD; TL1=0xFD; TH0=(65536-1000)/256;//1毫秒定时中断 TL0=(65536-1000)%256; TR1=1;//启动定时器1 EA=1;//打开总中断 ET0=1;//允许定时器0中断 TR0=1;//启动定时器0}void speedctl() { if(inc==0) //加速 { n++; if(n==11) n=10; while(!inc) display(); time=0; } elseif(dec==0) //减速 { n--; if(n==0) n=1; while(!dec) display(); time=0; } switch(n) { case1: t=937; //60/64转速一分钟一圈 里面转一圈937ms,基准速度一分钟一圈 rpm=1; break; case2: t=312; rpm=3; break; case3: t=187; rpm=5; break; case4: t=133; rpm=7; break; case5: t=104; rpm=9; break; case6: t=85; rpm=11; break; case7: t=72; rpm=13; break; case8: t=62; rpm=15; break; case9: t=55; rpm=17; break; case10: t=49; rpm=19; break; } }void direc(){ if(keyccw==0) { flag=1; time=0; } elseif(keycw==0) { flag=2; time=0; } elseif(pause==0) flag=0;}void display(){ fir=1; sec=0; P0=table_anode[rpm/10]; delaynms(17); fir=0; sec=1; P0=table_anode[rpm%10]; delaynms(20);}void delaynms(uint aa){ ucharbb; while(aa--) { for(bb=0;bb<250;bb++); //1ms基准延时程序 }}void timer0() interrupt 1{ TH0=(65536-1000)/256; TL0=(65536-1000)%256; time++; if(flag==1)//逆时针转 { if(time==t) { time=0; P1=ccw
; i++; if(i==4) i=0; } } elseif(flag==2)//顺时针转 { if(time==t) { time=0; if(i>0) i--; elseif (i==0) i=3; P1=ccw; } }}
-
-
一周热门 更多>