L298N 电机驱动模块
#include <reg52.h>
#include "..\lib\delay.h"
typedef unsigned char uchar;
typedef unsigned int uint;
#define S1 0x7e
#define S2 0x7d
#define S3 0x7b
#define S4 0x77
#define S5 0xbe
#define S6 0xbd
uchar PLAY;
sbit IN1 = P1^0;
sbit IN2 = P1^1;
sbit IN3 = P1^2;
sbit IN4 = P1^3;
void Go(void)
{
IN1 = 1;
IN2 = 0;
IN3 = 1;
IN4 = 0;
PLAY = 1;
}
void Back(void)
{
IN1 = 0;
IN2 = 1;
IN3 = 0;
IN4 = 1;
PLAY = 1;
}
void Stop(void)
{
IN1 = 0;
IN2 = 0;
IN3 = 0;
IN4 = 0;
PLAY = 0;
}
void Trun_Left(void)
{
IN1 = 1;
IN2 = 0;
IN3 = 0;
IN4 = 1;
PLAY = 1;
}
void Trun_Right(void)
{
IN1 = 0;
IN2 = 1;
IN3 = 1;
IN4 = 0;
PLAY = 1;
}
uchar keyscan(void) //使用行列反转扫描法
{
uchar cord_h, cord_l; //行列值中间变量
P3 = 0x0f; //行线输出全为0
cord_h = P3 & 0x0f; //读入列线值
if(cord_h != 0x0f) //先检测有无按键按下
{
delay_us(100); //去抖
if(cord_h != 0x0f)
{
cord_h = P3 & 0x0f; //读入列线值
P3 = cord_h | 0xf0; //输出当前列线值
cord_l = P3 & 0xf0; //读入行线值
return(cord_h + cord_l); //键盘最后组合码值
}
}
return (0xff);
}
void main()
{
uchar key;
P2 = 0x00;
Stop();
while(1)
{
key = keyscan();
switch(key)
{
case S1:Go();break;
case S2:Back();break;
case S3:Trun_Left();break;
case S4:Trun_Right();break;
case S5:Stop();break;
}
}
}