您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > 小车程序 采用C8051f020芯片
小车程序采用C8051f020芯片#includec8051f020.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedint#defineSYSCLK22118400sfrPH0=0xFA;sfrPH1=0xFB;sbitIN1=P3^0;sbitIN2=P3^1;sbitIN3=P3^2;sbitIN4=P3^3;sbitS1=P2^0;sbitS2=P2^1;sbitS3=P2^2;sbitS4=P2^3;sbitS5=P2^4;sbitS6=P2^5;ucharcodetable[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};uintnum,temp,left,right,t,r,ge,shi,bai,qian;voidSYSCLK_init()//时钟初始化{inti;OSCXCN=0x67;for(i=0;i256;i++);while(!(OSCXCN&0x80));OSCICN=0x88;}voidIO_init()//IO口初始化{XBR0=0x10;XBR1=0x00;XBR2=0x40;P0MDOUT=0xff;P3MDOUT=0xff;}voidPCA_int_init()//PCA0初始化{PCA0MD=0x08;//使用系统时钟禁止中断PCA0CPM0=0xc2;PCA0CPM1=0xc2;PCA0CN=0x40;PCA0L=0x00;PCA0H=0x00;PCA0CPL0=0x00;//设置PCA0CP0脉冲宽度PCA0CPH0=240;PCA0CPL1=0x00;//设置PCA0CP1脉冲宽度PCA0CPH1=240;}voidT_init()//定时器初始化{IE=0x82;TMOD=0x01;TH0=(65536-18432)/256;TL0=(65536-18432)%256;TR0=0;}voiddelay1ms(uintz)//延时函数{uintx,y,i;for(y=z;y0;y--){for(x=0;x10;x++)for(i=0;i221;i++);}}voidcontrol()//方向控制{num=P2;num=num&0x3f;switch(num){case0x0c://001100011110111111case0x1e:case0x3f:{PH0=0xa0;//左路电机PH1=0xa0;//右路电机}break;case0x0e://001110001111011111case0x0f:case0x1f:{PH0=0xa0;PH1=0x00;}break;case0x06://000110000111case0x07:{PH0=0xa0;PH1=0x00;}break;case0x01://000011000001case0x03:{PH0=0xb8;PH1=0x00;}break;case0x1c://011100111100111110case0x3c:case0x3e:{PH0=0x90;PH1=0xa0;}break;case0x18://011000111000case0x38:{PH0=0x80;PH1=0xa0;}break;case0x30://110000100000case0x20:{PH0=0x60;PH1=0xb8;}break;case0x00://000000{if(PH0PH1){PH0=0x00;PH1=0x70;}if(PH0PH1){PH0=0x70;PH1=0x00;}}break;}}voiddisplay(ucharqian,ucharbai,ucharshi,ucharge)//数码管显示函数{if(qian!=0){P4=0xef;P7=table[qian];delay1ms(2);}if(qian!=0||bai!=0){P4=0xdf;P7=table[bai];delay1ms(2);}if(qian!=0||bai!=0||shi!=0){P4=0xbf;P7=table[shi];delay1ms(2);}P4=0x7f;P7=table[ge];delay1ms(2);}voidmain()//主函数{WDTCN=0xde;//关闭看门狗WDTCN=0xad;SYSCLK_init();IO_init();PCA_int_init();T_init();IN1=0;IN2=1;IN3=0;IN4=1;TR0=1;//打开定时器while(1){control();display(qian,bai,shi,ge);if(temp==1){temp=0;ge=shi=bai=qian=0;display(qian,bai,shi,ge);for(r=0;r5;r++){P6=0x00;delay1ms(1000);P6=0xff;delay1ms(1000);}IN2=1;IN4=1;TR0=1;}}}voidtimer0()interrupt1{TH0=(65536-18432)/256;TL0=(65536-18432)%256;t++;if(t==100){t=0;ge++;if(ge==10)//秒计数{ge=0;shi++;}if(shi==10){shi=0;bai++;}if(bai==10){bai=0;qian++;}if(qian==10)ge=shi=bai=qian=0;}if(shi==5){IN2=0;IN4=0;TR0=0;temp=1;}}
本文标题:小车程序 采用C8051f020芯片
链接地址:https://www.777doc.com/doc-361360 .html