car.c

来自「基于AT Mega16的多功能小车」· C语言 代码 · 共 77 行

C
77
字号
//============================================================
// 犀拓开发网 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(5);
	PORTD |= _BV(PD2) | _BV(PD4);
}

void car_backward(void)				// 后退
{
	PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));
	_delay_ms(5);
	PORTD |= _BV(PD3) | _BV(PD5);
}

void car_left_Circle(void)			// 原地左转
{
	PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));
	_delay_ms(5);
	PORTD |= _BV(PD3) | _BV(PD4);
}

void car_right_Circle(void)			// 原地右转
{
	PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));
	_delay_ms(5);
	PORTD |= _BV(PD2) | _BV(PD5);
}

void car_left_stop(void)			// 左转,左轮不动
{
	PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));
	_delay_ms(5);
	PORTD |= _BV(PD2) | _BV(PD3) | _BV(PD4);
}

void car_right_stop(void)			// 右转,右轮不动
{
	PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));
	_delay_ms(5);
	PORTD |= _BV(PD2) | _BV(PD4) | _BV(PD5);
}

void car_left(void)					// 左转
{
	PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));
	_delay_ms(5);
	PORTD |= _BV(PD4);
}

void car_right(void)				// 右转
{
	PORTD &= ~(_BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5));
	_delay_ms(5);
	PORTD |= _BV(PD2);
}

⌨️ 快捷键说明

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