zoukankan      html  css  js  c++  java
  • 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;
    }
    }
    }
  • 相关阅读:
    1219 总结
    1206 冲刺三
    1130 冲刺2
    1128 主页面
    1123 冲刺3
    1121 冲刺2
    1118 冲刺1
    1117 新冲刺
    0622 软件工程总结
    0617 实验四 主存空间的分配和回收
  • 原文地址:https://www.cnblogs.com/LinFx/p/2372601.html
Copyright © 2011-2022 走看看