自写小车寻迹.docx

上传人:b****8 文档编号:9683802 上传时间:2023-05-20 格式:DOCX 页数:12 大小:17.03KB
下载 相关 举报
自写小车寻迹.docx_第1页
第1页 / 共12页
自写小车寻迹.docx_第2页
第2页 / 共12页
自写小车寻迹.docx_第3页
第3页 / 共12页
自写小车寻迹.docx_第4页
第4页 / 共12页
自写小车寻迹.docx_第5页
第5页 / 共12页
自写小车寻迹.docx_第6页
第6页 / 共12页
自写小车寻迹.docx_第7页
第7页 / 共12页
自写小车寻迹.docx_第8页
第8页 / 共12页
自写小车寻迹.docx_第9页
第9页 / 共12页
自写小车寻迹.docx_第10页
第10页 / 共12页
自写小车寻迹.docx_第11页
第11页 / 共12页
自写小车寻迹.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

自写小车寻迹.docx

《自写小车寻迹.docx》由会员分享,可在线阅读,更多相关《自写小车寻迹.docx(12页珍藏版)》请在冰点文库上搜索。

自写小车寻迹.docx

自写小车寻迹

下面的这个程序片段,是循迹的功能。

硬件小车 用的犀拓开发网的智能小车,小车采用L293电机驱动芯片,传感器用的红外对管 返回的是高低电平信号

传感器一共用了3个,采用夹着线走的方式

控制器用的Mega16控制器  传感器接PA口,当小车沿着线走, 那么小车不做动作

当小车偏离线时,做相应的调整

//======================================================================

//函    数:

void car_follow(void)

//实现功能:

循迹

//入口参数:

//返回  值:

//======================================================================

void car_follow(void)

{

unsigned int temp;

temp = PINA&0x07;

switch(temp)

{

case 0x00:

// 全部压线,原地右转

car_right();

while((PINA&0x07) !

= 0x05);                 // 当中间的传感器没寻到线时,一直保持原地右转

car_ahead();                                // 当中间的传感器寻到线时,小车向前直走

break;

case 0x01:

// 右中压线,右转

car_right();

while((PINA&0x07) !

= 0x05);

car_ahead();

break;

case 0x02:

// 左右压线,直走

//car_ahead();

break;

case 0x03:

// 右压线,  右转

car_right();

while((PINA&0x07) !

= 0x05);

car_ahead();

break;

case 0x04:

// 左中压线,左转

car_left();

while((PINA&0x07) !

= 0x05);

car_ahead();

break;

case 0x05:

// 中间压线,直行

//car_ahead();

break;

case 0x06:

// 左压线,  左转

car_left();

while((PINA&0x07) !

= 0x05);

car_ahead();

break;

case 0x07:

// 全不压线,直行

//car_ahead();

break;

}

}

以上程序片段用的函数,完整程序见下面

////////////////////////////////////主文件

//======================================================================

//The information contained herein is the exclusive property of

//Beijing Bynatec Co., Ltd. And shall not be distributed, reproduced,

//or disclosed in whole in part without prior written permission.

//(C) COPYRIGHT 2009 Beijing Bynatec Co., Ltd.

//ALL RIGHTS RESERVED

//The entire notice above must be reproduced on all authorized copies.

//======================================================================

//============================================================

//犀拓开发网 

//

//工程名称:

car_follow.aps

//功能描述:

智能小车循迹

//IDE环境:

AVR Studio v4.14 WinAVR-20081205

//操作系统:

windows xp sp3

//涉及的库:

libc.a

//组成文件:

main.c

//硬件连接:

左---中---右

//PA0--PA1--PA2

//压线:

PAn = 0

//离线:

PAn = 1

//key4  == PA3按键启动小车

//

//晶振7.3728MHz

//维护记录:

2009年3月2日v1.0

//======================================================================

//======================================================================

//文件名称:

main.c

//功能描述:

智能小车循迹

//维护记录:

2009年3月2日v1.0

//======================================================================

#include "avr/io.h"

#include "util/delay.h"

#include "avr/interrupt.h"

#include "car.h"

#include "key.h"

//======================================================================

//函    数:

void sensor_init(void)

//实现功能:

循迹传感器初始化

//入口参数:

//返回  值:

//======================================================================

void sensor_init(void)

{

DDRA &= ~0x07;// PA0~PA2 设置为输入

}

//======================================================================

//函    数:

void car_follow(void)

//实现功能:

循迹

//入口参数:

//返回  值:

//======================================================================

void car_follow(void)

{

unsigned int temp;

temp = PINA&0x07;

switch(temp)

{

case 0x00:

// 全部压线,原转

car_right();

while((PINA&0x07) !

= 0x05);

car_ahead();

break;

case 0x01:

// 右中压线,右转

car_right();

while((PINA&0x07) !

= 0x05);

car_ahead();

break;

case 0x02:

// 左右压线,直走

//car_ahead();

break;

case 0x03:

// 右压线,  右转

car_right();

while((PINA&0x07) !

= 0x05);

car_ahead();

break;

case 0x04:

// 左中压线,左转

car_left();

while((PINA&0x07) !

= 0x05);

car_ahead();

break;

case 0x05:

// 中间压线,直行

//car_ahead();

break;

case 0x06:

// 左压线,  左转

car_left();

while((PINA&0x07) !

= 0x05);

car_ahead();

break;

case 0x07:

// 全不压线,直行

//car_ahead();

break;

}

}

//======================================================================

//函    数:

int main(void)

//实现功能:

主函数 循迹

//入口参数:

//返回  值:

//======================================================================

int main(void)

{

uint8_t key;

Key_Init();

car_init();

sensor_init();

while

(1)// 等待按键启动 KEY4

{

key = Key_Get();

if(key == KEY_4)

break;

}

car_ahead();// 小车启动

while

(1)

{

car_follow();

}

}

// 定时器0

//ISR(SIG_OVERFLOW0)

ISR(TIMER0_OVF_vect)

{

Key_Scan();

}

//////////////////////////按键处理文件

//============================================================

// 文件名称:

Key.c

// 实现功能:

1*4按键扫描程序,适用于低电平有效的按键电路

// 使用定时器0中断(112.5Hz)

// 日期:

    2008/7/28

//============================================================

//================================================

//按键去抖动时间设定,单位为1/112.5秒

//================================================

#define Key_Debounce1// (4/112.5)秒=28.125ms

//================================================

//持续按键时间间隔设定,单位1/128秒

//================================================

#define Key_TimeOut16// (64/112.5)秒=0.5688s

//================================================

//按键使用端口设定

//================================================

#define Key_ALL0x08

#define P_Key_DataPINA

#defineP_Key_DirDDRA

#defineP_Key_PortPORTA

/*

#define P_Key_DataPINB

#defineP_Key_DirDDRB

#defineP_Key_PortPORTB

#define P_Key_DataPINC

#define P_Key_DirDDRC

#define P_Key_PortPORTC

#define P_Key_DataPIND

#define P_Key_DirDDRD

#define P_Key_PortPORTD*/

#include "key.h"

#include 

#include 

volatile uint8_t KeyCode;// 存储获得的键值

volatile uint8_t ScanCnt;// 该变量用来表示按键持续时间

volatile uint8_t KeyUp;// 按键是否处于抬起状态

//======================================================================

//函    数:

void Key_Init(void)

//实现功能:

初始化按键扫描程序,该函数将开启定时器0溢出中断

//入口参数:

//返回  值:

//======================================================================

void Key_Init(void)

{

P_Key_Dir &= ~(/*_BV(0) | _BV

(1) | _BV

(2) | */_BV(3));

P_Key_Port |= /*_BV(0) | _BV

(1) | _BV

(2) | */_BV(3);// 设置按键为上拉电阻

ScanCnt = 0;// 初始化变量

KeyCode = 0;

KeyUp = 1;

TCNT0 = 0;

TCCR0 = _BV(CS02) | _BV(CS00);// 预分频256

TIMSK |= _BV(TOIE0);

sei();// 使能中断

}

//======================================================================

//函    数:

void Key_Scan(void)

//实现功能:

按键扫描,该函数被定时器0中断服务函数程序调用

//入口参数:

//返回  值:

//======================================================================

void Key_Scan(void)

{

uint8_t key_t;

//PORTB^=_BV

(1);// 调试时用来指示

key_t = ~P_Key_Data;// 获取IO端口状态

key_t &= Key_ALL;// 判断当前是否有键按下

if(key_t !

= 0)

{

KeyUp = 0;// 按键没抬起

if(key_t == KeyCode)

{

ScanCnt++;

}

else

{

ScanCnt = 0;

KeyCode = key_t;

}

}

else

{

KeyUp = 1;// 按键抬起

}

}

//======================================================================

//函    数:

uint8_t Key_Get(void)

//实现功能:

获取键值

//入口参数:

//返回  值:

获得的键值

//======================================================================

uint8_t Key_Get(void)

{

uint8_t key_G;

cli();// 关中断

if((ScanCnt >= Key_Debounce) && (KeyUp == 1))

{

key_G = KeyCode;

KeyCode = 0;

ScanCnt = 0;

sei();// 开中断

return key_G;

}

if((ScanCnt >= Key_TimeOut) && (KeyUp==0))

{

key_G = KeyCode;

KeyCode = 0;

ScanCnt = 0;

KeyUp = 1;

sei();// 开中断

return key_G;

}

sei();

return 0;// 没有按键

}

//////////////////////////////////////小车驱动文件

//============================================================

// 犀拓开发网 

//

// 文件名称:

car.c

// 实现功能:

小车驱动

// 日期:

 2009年3月2日

//============================================================

#include "avr/io.h"

#include "util/delay.h"

void car_init(void)

{

DDRD |= _BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5);

}

void car_stop(void)// 刹车

{

PORTD |= _BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5);

}

void car_ahead(void)// 前进

{

PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));

_delay_ms(10);

PORTD |= _BV(PD2) | _BV(PD4);

}

void car_backward(void)// 后退

{

PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));

_delay_ms(10);

PORTD |= _BV(PD3) | _BV(PD5);

}

void car_left_Circle(void)// 原地左转

{

PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));

_delay_ms(10);

PORTD |= _BV(PD3) | _BV(PD4);

}

void car_right_Circle(void)// 原地右转

{

PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));

_delay_ms(10);

PORTD |= _BV(PD2) | _BV(PD5);

}

void car_left_stop(void)// 左转,左轮不动

{

PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));

_delay_ms(10);

PORTD |= _BV(PD2) | _BV(PD3) | _BV(PD4);

}

void car_right_stop(void)// 右转,右轮不动

{

PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));

_delay_ms(10);

PORTD |= _BV(PD2) | _BV(PD4) | _BV(PD5);

}

void car_left(void)// 左转

{

PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));

_delay_ms(10);

PORTD |= _BV(PD4);

}

void car_right(void)// 右转

{

PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));

_delay_ms(10);

PORTD |= _BV(PD2);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2