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

📄 scar.c

📁 基于Robo-PICA robot kit的PIC 16F877a的单片机编程 Innovative Experiment Co.,Ltd.
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
 * File     :  Scar.c
 * Purpose  :  smartcar drive + sensor + wireless 
 * Author   :  smartcar team
 * E-mail   :  
 * Release  :  4.9.2007
 * Copyright (c) embedded lab school of software sjtu.
 *****************************************************************************/

#include <16F877.h>                     // Standard Header file for the PIC16F877 device
#define  TxD         PIN_C6             // Define Transmitted Data
#define  RxD         PIN_C7             // Define Received Data
#define  CLOCK_SP    20000000           // Clock Speed(Hz)


#device ADC=10                              // ADC 10 Bit  (1111111111=1,023
#use delay (clock=CLOCK_SP)                // Use built-in function: delay_ms() & delay_us()
#use rs232(baud=9600, xmit=TxD,rcv=RxD)    // Use serial I/O port (RS232)
#use fast_io(A)                            // programming of the direction register.
#fuses HS                                  // Oscillator mode HS
#fuses NOLVP, NOWDT                        // No Low Voltage Program, No Watchdog timer
#fuses NOPROTECT                           // Code no protection

/***********************************************************************
 **  Functions prototypes                                              *
 ***********************************************************************/

#define LED0 PIN_B3
#define LED1 PIN_B4
#define LED2 PIN_B5

#define LEFT  0
#define RIGHT 1

//#define DEEP PIN_A4               //Speaker

#define rs PIN_C0                 //LCD
#define en PIN_C5

int16 i;
char Car_Id[10] = "sword\0";

#define BUF_SIZE	40
char 	buf[BUF_SIZE];

#define TURN_LEFT	1
#define TURN_RIGHT	2
#define	GO		3
#define REVERSE		4

#define STOP		8
#define ARRIVE		10


char direction = 'N';      		//方向初赋值  0100 north
//            0010 east
//            0001 sorth
//            1000 west 
//	
int16 count;                            //路口计数
int16 cmd;

char starting = 'A';
char destination;

#define MAX_CROSS	10
char ur_path[MAX_CROSS];

int16 IrSensorValue=0x00;              //0 or 1

void LCD_en_write(void)        //EN端产生一个高电平脉冲,写LCD
{
	output_high(en);
	delay_us(2);
	output_low(en);
}


void LCD_write_char(unsigned command,unsigned data)       //command=0  data
{        
	unsigned data_temp; 
	data_temp = data;
	delay_us(1000);               //very very important!!

	if (command == 1)                 //data
	{
		OUTPUT_HIGH(rs);             //RS=1 
		//output_d(data <<4);          // LCD_DATA_PORT &= 0X0F;
		OUTPUT_D(data_temp & 0xf0);  //LCD_DATA_PORT |= data_temp&0xf0;   //send high 4bit 
		LCD_en_write();
		data_temp=data_temp << 4;
		OUTPUT_D(data_temp);
		LCD_en_write();
	}

	else                                             //command
	{
		OUTPUT_LOW(rs);                              //RS=0
		//OUTPUT_D(data <<4);                       // LCD_DATA_PORT &= 0X0F;
		OUTPUT_D(data_temp & 0xf0);                 //LCD_DATA_PORT |= command_temp&0xf0;//send high 4bit
		LCD_en_write();
		data_temp=data_temp << 4;
		OUTPUT_D(data_temp);
		LCD_en_write();
	}
}


void LCD_set_xy( unsigned char x, unsigned char y )
{
	unsigned char address;
	if (y == 0) address = 0x80 + x;
	else 
		address = 0xc0 + x;
	LCD_write_char( 0, address );
}


void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
	LCD_set_xy( X, Y );

	while (*s) 
	{
		LCD_write_char( 1, *s );
		s ++;
	}
}

/*void strcpy(char *s1, char *s2)         //used in LCD
  {
  while(*s1++ = *s2++)   ;
  }*/

void LCD_init()
{
	delay_ms(15);                    //init
	LCD_write_char(0,0x28);          //显示模式设置(不检测忙信号)//
	delay_ms(5);                     //共三次,确保可靠复位 //
	LCD_write_char(0,0x28);
	delay_ms(5);
	LCD_write_char(0,0x28);
	delay_ms(5);
	LCD_write_char(0,0x28); //显示模式设置(以后均检测忙信号)//
	LCD_write_char(0,0x08); // 显示关闭 //
	LCD_write_char(0,0x01); // 显示清屏 //
	LCD_write_char(0,0x06); // 显示光标移动设置 //
	LCD_write_char(0,0x0c); // 显示开及光标设置 //
	LCD_write_char(0,0x01);
}

void motorA_Forward() 
{
	output_high(PIN_C2);
	output_low(PIN_D0);
	output_high(PIN_D1);
}

void motorA_Backward() 
{
	output_high(PIN_C2);
	output_high(PIN_D0);
	output_low(PIN_D1);
}

void brakeA()
{
	output_high(PIN_C2);
	output_high(PIN_D0);
	output_high(PIN_D1);
}

void brakeB() 
{
	output_high(PIN_C1);
	output_high(PIN_D2);
	output_high(PIN_D3);
}

void motorB_Forward() 
{
	output_high(PIN_C1);
	output_low(PIN_D2);
	output_high(PIN_D3);
}

void motorB_Backward()
{
	output_high(PIN_C1);
	output_high(PIN_D2);
	output_low(PIN_D3);
}

void GoStraight()                        
{
	motorA_Forward();
	motorB_Forward();
}

void GoBack()
{
	motorA_Backward();
	motorB_Backward();
}

void TurnLeft()
{
	motorA_Backward();
	motorB_Forward();   
}
void TurnRight()
{
	motorA_Forward();
	motorB_Backward();
}
void brake()
{
	brakeA();
	brakeB();
}	

void TurnLittle(int16 dir)
{	
	int16 i;
	for(i=0; i<2000; i++)
	{
		//direction == 1(LETURN_RIGHTFT) ||  direction == 0(RIGHT)
		dir?motorA_Forward():motorB_Forward();
	}
}

void TurnAngle(int16 dir, int16 angle) // angle must be multiple 1 degree
{
	for(i=0; i<angle; i++)
	{
		TurnLittle(dir);
	}
}

int16 Read_IrSensor(int16 channel)
{
	int16 value;
	setup_adc_ports(A_ANALOG );
	setup_adc(ADC_CLOCK_INTERNAL );
	set_adc_channel(channel);
	delay_us(10);
	value = read_adc();
	setup_adc( ADC_OFF );
	return value;
}

void ReverseCar()
{
	int16 j;
	for(i=0;i<135;i++)           //135000 = 90 degree
	{for(j=0;j<1000;j++)
		{
			motorA_Backward();
			motorB_Forward();
		} 
	} 
}

/*
   void BackBySensor()
   {
   int16 value0,value1,value2,value3;
   int16 IrSensorValue=0x00;              //0 or 1

   while(true)
   {      
   IrSensorValue=0x00;              
   value0 = read_IrSensor(0);
   value1 = read_IrSensor(1);
   value2 = read_IrSensor(2);
   value3 = read_IrSensor(3);
   IrSensorValue = ((value0<200 ? 0x08:0) | IrSensorValue);   //sensor0   1   2    3
   IrSensorValue = ((value1<200 ? 0x04:0) | IrSensorValue);   //      白  黑  黑   白 
   IrSensorValue = ((value2<200 ? 0x02:0) | IrSensorValue);   //      0   1   1    0
   IrSensorValue = ((value3<200 ? 0x01:0) | IrSensorValue);   //


   if( IrSensorValue == 0x06 )	       //0000 0110
   {
   GoBack();
   } 
   if( IrSensorValue == 0x02 )          //0000 0010
   {
   brakeA();
   motorB_Backward();
   }
   if (IrSensorValue == 0x04 )  	        //0000 0100
   {
   motorA_Backward();
   brakeB();
   }
   if( IrSensorValue == 0x0f)         //0000 1111
   {
   return;
   }
   }
   }
   */

#include <string.h>
#include <stdlib.h>

	int
get_cmd(char *dir)
{
	strcpy( buf, "E\0" );
	if( strncmp( dir, buf, 1) == 0 )
	{
		switch( direction )
		{
			case 'E':
				return GO;
			case 'S':
				direction = 'E';
				return TURN_LEFT;
			case 'W':
				direction = 'E';
				return REVERSE;
			case 'N':
				direction = 'E';
				return TURN_RIGHT;
		}
	}

	strcpy( buf, "S\0" );
	if( strncmp( dir, buf, 1) == 0 )
	{
		switch( direction )
		{
			case 'E':
				direction = 'S';
				return TURN_RIGHT;
			case 'S':
				return GO;
			case 'W':
				direction = 'S';
				return TURN_LEFT;
			case 'N':
				direction = 'S';
				return REVERSE;
		}
	}

	strcpy( buf, "W\0" );
	if( strncmp( dir, buf, 1) == 0 )
	{
		switch( direction )
		{
			case 'E':
				direction = 'W';
				return REVERSE;
			case 'S':
				direction = 'W';
				return TURN_RIGHT;
			case 'W':
				return GO;
			case 'N':
				direction = 'W';
				return TURN_LEFT;
		}
	}

	strcpy( buf, "N\0" );
	if( strncmp( dir, buf, 1) == 0 )
	{
		switch( direction )
		{
			case 'E':
				direction = 'N';
				return TURN_LEFT;
			case 'S':
				direction = 'N';
				return REVERSE;
			case 'W':
				direction = 'N';
				return TURN_RIGHT;
			case 'N':
				return GO;
		}
	}

	strcpy( buf, "#\0" );
	if( strncmp( dir, buf, 6) == 0 )
	{
		switch( direction )
		{
			case 'E':
				direction = 'W';
				break;
			case 'S':
				direction = 'N';
				break;
			case 'W':
				direction = 'E';

⌨️ 快捷键说明

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