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

📄 perfecte.c

📁 寻线小车源程序
💻 C
字号:
//ICC-AVR application builder : 2007-12-15 15:08:27
// Target : M16
// Crystal: 8.0000Mhz

#include <iom16v.h>
#include <macros.h>
typedef unsigned char uint8;                                   /* 定义可移植的无符号8位整数关键字            */
typedef unsigned int  uint16;          
//void Choose( unsigned char rem0);
void WriteOCR1A(unsigned int i); 
void WriteOCR1B(unsigned int i); 

	
void port_init(void)
 {
   PORTA = 0x00;
   DDRA  = 0x00;
   PORTB = 0x00;
   DDRB  = 0xFF;
   PORTC = 0x50;//m103 output only 
   DDRC  = 0xFF;
   PORTD = 0xCF;
   DDRD  = 0x30;
 }

//TIMER1 initialize - prescale:8
// WGM: 5) PWM 8bit fast, TOP=0x00FF
// desired value: 4KHz
// actual value:  3.906KHz (2.4%)
void timer1_init(void)
 {
   TCCR1B = 0x00; //stop
   TCNT1H = 0x00; //setup
   TCNT1L = 0x01;
   OCR1AH = 0x00;
   OCR1AL = 0xFF;
   OCR1BH = 0x00;
   OCR1BL = 0xFF;
   ICR1H  = 0x00;
   ICR1L  = 0xFF;
   TCCR1A = 0xA1;
   TCCR1B = 0x0A; //start Timer
 }

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

   MCUCR = 0x00;
   GICR  = 0x00;
   TIMSK = 0x00; //timer interrupt sources
    SEI(); //re-enable interrupts
    //all peripherals are now initialized
    }
void Delay100us(uint8 x)
{
	uint8 i;          //4clock                   
	for(i=147;x!=0;x--)	
	while(--i);      //5 * i clock
}
void Delay1ms(uint16 n)
{        
	for (;n!=0;n--)
	{
		Delay100us(10);
	}
}
	void forward1()
{    OCR1AL=0xff*2/3;
     OCR1BL=0x08;
}
	void backward1()
{    OCR1AL=0x08;
     OCR1BL=0xff*2/3;
}

void delay(void)//延时
    {
     char i=0;
	   while(i!=5)
	     { i++;}
	}
  
void Forward(void) //PWM调制,相应转向
   {
   
    WriteOCR1A(0xff*2/3);//65%
	 WriteOCR1B(0xff*2/3);//65%
    }
void TurnLL(void)
   {
   
    WriteOCR1A(0xff*2/3);//right PWM controle 55%
	 WriteOCR1B(0x2f);//10%
	}
void TurnLM(void)
   { 
    
     WriteOCR1A(0xff*2/3);//50
      WriteOCR1B(0x0f);//0.5%
	}
void TurnLB(void)
{
   WriteOCR1A(0xff*2/3);
   WriteOCR1B(0x00);
 }
void TurnRL(void)
   {
      
    WriteOCR1A(0x2f);//10%
      WriteOCR1B(0xff*2/3);//50%
	}
void TurnRM(void)
   {
    
    WriteOCR1A(0x0f);//0.5%
	  WriteOCR1B(0xff*2/3);//50%
	}
void TurnRB(void)
{
   WriteOCR1A(0x00);
   WriteOCR1B(0xff*2/3);
 }
void Stop(void)
{
      WriteOCR1A(0x00);
      WriteOCR1B(0x00);
}

//void Choose( char rem0)//出线检测
   //{ char compear=rem0;
     //switch(compear)
	  //{
	   //default:
	   //while(image=0x00)
	               //{
	                 //WriteOCR1A(0xCD);//80%
                      //WriteOCR1B(0x05);
					  //image=PINA;
					//}
	   //case 0x01:while(image=0x00)
	               //{
	                 //WriteOCR1A(0x05);
                      //WriteOCR1B(0xCD);//70%
					   //image=PINA;
					//} 
		//case 0x00:Stop();break;
	   //} 
    //}
	  
//为比较寄存器写值
void WriteOCR1A(unsigned int i) 
    {
     unsigned char sreg; 
       sreg=SREG;
         //_CLI();//禁止中断
          OCR1AL=i;
            SREG=sreg;
    }
void WriteOCR1B(unsigned int i) 
    {
       unsigned char sreg; 
         sreg=SREG;
         //_CLI();//
          OCR1BL=i;
            SREG=sreg;
    } 
   
void main(void)
{ 
  int i;
  unsigned char image;
  init_devices();//insert your functional code here
  delay();
  while(1)
  {  
    Delay1ms();
    image=PINA;
    PORTB=PINA;
	switch(image)
    {
           case 0x00:Forward();break;
	   case 0x04:TurnLL();break;
	   case 0x0C:TurnLM();break;
	   case 0x08:TurnLB();break;
	   case 0x02:TurnRL();break;
	   case 0x03:TurnRM();break;
           case 0x01:TurnRB();break;
           case 0x0e:Stop();break;
           case 0x07:Stop();break;
     default:
    forward1();
    backward1();
    break;
     }
   }   
}

⌨️ 快捷键说明

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