|
发表于 2009-7-28 18:23:29
|
显示全部楼层
下面的这个程序片段,是循迹的功能。
硬件小车 用的犀拓开发网的智能小车,小车采用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.
//======================================================================
//============================================================
// 犀拓开发网 http://www.mcutop.com
//
// 工程名称: 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_Debounce 1 // (4/112.5)秒=28.125ms
//================================================
// 持续按键时间间隔设定,单位1/128秒
//================================================
#define Key_TimeOut 16 // (64/112.5)秒=0.5688s
//================================================
// 按键使用端口设定
//================================================
#define Key_ALL 0x08
#define P_Key_Data PINA
#define P_Key_Dir DDRA
#define P_Key_Port PORTA
/*
#define P_Key_Data PINB
#define P_Key_Dir DDRB
#define P_Key_Port PORTB
#define P_Key_Data PINC
#define P_Key_Dir DDRC
#define P_Key_Port PORTC
#define P_Key_Data PIND
#define P_Key_Dir DDRD
#define P_Key_Port PORTD*/
#include "key.h"
#include <avr/io.h>
#include <avr/interrupt.h>
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; // 没有按键
}
//////////////////////////////////////小车驱动文件
//============================================================
// 犀拓开发网 http://www.mcutop.com
//
// 文件名称: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);
}
智能小车循迹完整程序ourdev_465283.rar(文件大小:22K) (原文件名:car_follow.rar) |
|