附录
以下为电机PWM调速程序详细代码
#include
unsigned char Bright_Level[8]={0,40,80,120,150,180,210,240}; unsigned char counter=0;
unsigned char code a[8]={0x81,0x42,0x24,0x18,0x18,0x24,0x42,0x81}; unsigned char code b[8]={0xc3,0x66,0x3c,0x18,0x18,0x3c,0x66,0xc3}; unsigned char code c[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; unsigned char j=0x01;
/****初始化******************** Timer 0 定时中断处理
**************************/ //timer init void initTimer0(void) {
TMOD=0x51; TH0=0xff; TL0=0xff-46; TH1=0x00; TL1=0x00; TR0=1; ET0=1; IT0=0; EA=1;
} //timer0/counter0 interrupt
void main() { /*int i,j;
unsigned char temp;
for(j=0;j<5;j++) {
temp=0x01; for(i=0;i<8;i++)
26
{
temp<<=1; P1=~temp; delay(20000); }*/
unsigned char i; initTimer0();
Init_Max7219(); while(1)
{//for(i=0;i<8;i++)
// 计算转速;
/*提取转速的位值;
speed; speed1;
display[3]=spead/1000; spead1=[spead/1000]; spead=spead1; display[2]=spead/100; spead=spead2; display[1]=spead/10; spead=spead3;
for(i=0;i<4;i++)
{ Write_Max7219(i,1);
delay(1000); } } }
////////////////////////////////////////////中断程序////////////////// void timer0(void) interrupt 3 {
unsigned char i;
static unsigned char counter; static unsigned int counter1;
27
TH0=0xff; TL0=0xff-46; counter++; counter1++;
if(counter>=80) P2=0; else
P2=1;
if (counter>100) counter=0; if(counter1==2) {启动定时器 1; }
if(counter1>=1002) {停止定时器 1;
x=TH1*256+TL1;//x为全局变量
TH1=0x00; TL1=0x00;
counter1=0; } TF0=0; } }
/****************按键扫描**************/
sbit P0^0=key1; //加速
x=TH1*256+TL1; //t1方式1计数,读入计数值 TH1=0x00; TL1=0x00; x++; display(); }
sbit P0^1=key2; //减速 key() {
28
if(key1==1) //加速 {
while (key1==1); t++; } if(t>=5) t=5; if(key2==1) //减速 {
while(key2==1); t--; } if(t<1)
t=1;
}
TR0=0;//清中断 }
29