⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bianma.c

📁 单片机开发资料光盘-双龙-686M.zip
💻 C
字号:
// Target : 8515
//  Crystal:  8.0000Mhz
// 更新日期: 2003.03.18
// 接上遥控器,按动"A","B","C","D"会有LED灯指示
// 程序中每按键4次,认为一次输入,
//4个一组,根据按键的不同执行相应程序

//按四次"A",运行悬崖车程序
//按四次"B",运行遥控车程序
          //无键按下----------前行
		  //"A", 按住---------右转
		  //"B", 按住---------后退
		  //"C", 按住---------停止
		  //"D", 按住---------左转

//按四次"C",运行音乐车程序
//按四次"D",运行霓虹车程序

#include <io8515v.h>
#include <macros.h>
#include <sl3010.c>

//#define A 0x04      //0b0000,0100
//#define B 0x02      //0b0000,0010
//#define C 0x08      //0b0000,1000
//#define D 0x01      //0b0000,0001
#define delaytime  300

unsigned char bianma;
unsigned char pa,pb,pc,pd,sl,remote,bianma_count;

//call this routine to initialise all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();

 MCUCR = 0x00;
 GIMSK = 0x00;
 TIMSK = 0x00;
 SEI(); //re-enable interrupts
 //all peripherals are now initialised
}

void remote_car()
{
 unsigned char stop_rst,temp;
 DDRB = 0x00;PORTB = 0xFF;
 DDRA = 0xff;PORTA = 0xff;
 while(1)
     {
	  remote = pb = PINB & 0x0F;
	  PORTA = ~pb;
	  if(pb == 0x04)
	    {
		 PORTD = 0x60;   //右转//0110 0000   
		 PORTA = 0xCC;     //1100,1100
		 stop_rst=0x00;
		}
	  if(pb == 0x02)
	    {
		 PORTD = 0x50;   //后退//0101 0000   
		 PORTA = 0x3C;     //0011,1100
		 stop_rst=0x00;
		}
	  if(pb == 0x08)
	    {
		 PORTD = 0xff;   //stop   
		 PORTA = 0xff;     //
		 stop_rst++;
		}
	  if(pb == 0x01)
	    {
         PORTD = 0x90;   //左转//1001 0000   
		 PORTA = 0x33;     //0011,0011
		 stop_rst=0x00;
		}
	  if(pb == 0x00) 
	    {
		 PORTD = 0xA0;   //前行//1010 0000   
		 PORTA = 0xC3;     //1100,0011
		}
/*
      while(1)
	      {
		   temp = PINB & 0x0f;
		   if(temp != remote)
		   {break;}
		  }
	  if(stop_rst == 0x04)
	    {
		 PORTD = 0xff;   //stop   
		 PORTA=0xff;     //
		 break;
		}
*/
	  delay_ms(100);
	 }
}

void xuanya_car(void)
{
 DDRC=0x01;PORTC=0xFF;
 DDRA=0xFF;PORTA=0xFF;  
 DDRD=0xFF;PORTD=0xFF;
 while(1)
   {//左(pc7)  中(pc6)  右(pc5)
    pc = PINC & 0x10;
	if(pc==0x00)
	  {
	   PORTD = 0x50;
	   PORTA=0x3C;//后退
	   delay_ms(delaytime);
	   PORTD = 0x60;
	   PORTA=0xCC;//右转
	   delay_ms(delaytime/2);
	  }
    pc = PINC & 0xE0;
    if(pc==0x40)
	  {
	   PORTD = 0xFF;//刹车
	   PORTA = 0xFF;
	   //后退,右转
	   PORTD = 0x50;
	   PORTA=0x3C;//后退
	   delay_ms(delaytime);
	   PORTD = 0x60;
	   PORTA=0xCC;//右转
	   delay_ms(delaytime/2);
	  }
	if(pc==0x80)
	  {
	   stop();
	   //往右转弯
	   PORTD = 0x50;
	   PORTA=0x3C;//后退
	   delay_ms(delaytime);
	   PORTD = 0x60;//右转
	   PORTA=0xCC;
	   delay_ms(delaytime);
	  }
	if(pc==0x20)
	  {
	   stop();
	   //往右转弯
	   PORTD = 0x50;//后退
	   PORTA=0x3C;
	   delay_ms(delaytime);
	   PORTD = 0x60;//右转
	   PORTA=0xCC;
	   delay_ms(delaytime);
	  }
	if(pc==0xc0)
	  {
	   stop();
	   //后退,往右转弯
	   PORTD = 0x50;//后退
	   PORTA=0x3C;
	   delay_ms(delaytime);
	   PORTD = 0x60;//右转
	   PORTA=0xCC;
	   delay_ms(delaytime);
	  }
	if(pc==0x60)
	  {
	   stop();
	   //后退,往右转弯
	   PORTD = 0x50;
	   PORTA=0x3C;//后退
	   delay_ms(delaytime);
	   PORTD = 0x60;//右转
	   PORTA=0xCC;
	   delay_ms(delaytime);
	  }
	if(pc==0xE0)
	  {
       //刹车
       PORTD = 0xF0;
	   PORTA = 0xFF;
	  } 
	else
	  {
	    PORTD = 0xA0;//前行
		PORTA=0xC3;
      }
   }
}

void music_car()
{
 DDRA = 0xFF; PORTA = 0xFF;
 PORTA = 0xCC;
 while (1)
 	;
}

void led_car()
{
 DDRA = 0xFF; PORTA = 0xFF;
 PORTA = 0x0F;
 while (1)
 	;
}

void main(void)
{
 unsigned temp;
 
 port_init();         //PA,PB,PC,PD 初始化
 
 loop:

 PORTA = 0x00;delay_ms(500);
 PORTA = 0xff;delay_ms(500);
 bianma = 0x0000;
 
 while(1)
     {
	  remote = PINB & 0x0f;
	  PORTA = ~remote;
	  switch (remote)
      	{//按键检查
         case  0x04 : 
	          {
			   bianma=bianma | 0xA000;
			   bianma_count = bianma_count+1;
			  }
      	break;
         case  0x02 : 
	          {
			   bianma=bianma | 0x0B00;
			   bianma_count = bianma_count+1;
			  }
      	break;
         case  0x08 : 
	          {
			   bianma=bianma | 0x00C0;
			   bianma_count = bianma_count+1;
			  }
      	break;
         case 0x01 : 
	          {
			   bianma=bianma | 0x000D;
			   bianma_count = bianma_count+1;
			  }
      	break;
      	}
	  while(1)
	      {//释键检查
		   temp = PINB & 0x0f;
		   if(temp != remote)
		   {break;}
		  }
	  if(bianma_count == 4)
	    {//按键四次,认为 一次有效输入结束
		 bianma_count=0;break;
		}
 	 }
 delay_ms(200);
 PORTA = 0xaa;delay_ms(500);
 PORTA = 0x55;delay_ms(500);
 if(bianma == 0xa000)   
   {xuanya_car();}
 if(bianma == 0x0b00)
   {remote_car();}
 if(bianma == 0x00c0)
   {music_car();}
 if(bianma == 0x000d)
   {led_car();}
 else  goto  loop;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -