下面是仿真图:
/****************************************Copyright (c)**************************************************
**
** -----------------------------------------------------------------------------------------------------
** File Name :
** Version : V1.0.0
** Description :
* 【外部晶振】: 11.0592mhz
* 【主控芯片】: STC89C52
* 【编译环境】: Keil μVisio4
* 【程序功能】:
* 【使用说明】:
** -----------------------------------------------------------------------------------------------------
** Author : Creavalia
** Date : 2016年10月19日
** -----------------------------------------------------------------------------------------------------
** Modify Person:
** Modify Date :
** Description :
*********************************************************************************************************/
// Includes --------------------------------------------------------------------------------------------
#include
/********************************************************************************************************
** Marcos define
*********************************************************************************************************/
#define MotorTabNum 5
unsigned char T0_NUM;
//IO设置
sbit K1 = P3^5; // 启动
sbit K2 = P3^4; // 反向
sbit K3 = P3^3; // 加速
sbit K4 = P3^2; // 减速
sbit FX = P2^4; // 方向
sbit MotorEn = P2^5; // 使能
sbit CLK = P2^6; // 脉冲
//----数码管---------------
sbit SMG1 = P2^0; //数码管第一位定义
sbit SMG2 = P2^1; //数码管第二位定义
sbit SMG3 = P2^2; //数码管第三位定义
sbit SMG4 = P2^3; //数码管第四位定义
int table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //共阴极数码管段值
//int table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,}; //共阳极数码管段值
unsigned char g_MotorSt = 0; // 启停
unsigned char g_MotorDir = 0; // 正反
//unsigned char MotorTab[6] = {200, 160, 120, 80, 40, 20};
unsigned char MotorTab[6] = {12, 10, 8, 6, 4, 2};
//unsigned char MotorTab[6] = {6, 5, 4, 3, 2, 1};
signed char g_MotorNum = 0;
//--------------
void delayms(xms);
void mDelay(unsigned int DelayTime); //延时函数
void T0_Init();
void KeyScan(void);
void Display(void);
// 主程序 main()
void main(void)
{
unsigned char i;
T0_Init(); // 定时器0 初始化
MotorEn = 0; // L297 使能(只有智能,才能正常工作,否则停止)
FX = 0;
while(1)
{
KeyScan(); // 按键
for(i=0; i<10; i++)
Display();
}
}
/********定时器初始化***********************************************************/
void T0_Init()
{
TMOD = 0x01;
TH0 = (65535-100)/256; // 1ms
TL0 = (65535-100)%256;
EA = 1;
ET0 = 1;
// TR0 = 1;
}
/********定时器中断*****************/
void T0_time() interrupt 1
{
// TR0 = 0;
TH0 = (65535-100)/256;
TL0 = (65535-100)%256;
T0_NUM++;
if(T0_NUM >= MotorTab[g_MotorNum]) // 增加按键g_MotorNum++ 减小按键g_MotorNum--
{
T0_NUM = 0;
CLK=CLK^0x01; // 输出脉冲
}
// TR0 = 1;
}
//-----按键控制---------------------
void KeyScan(void)
{
if(K1 == 0)
{
delayms(5); // 软件延时消抖
if(K1 == 0)
{
while(!K1){Display();};
g_MotorSt = g_MotorSt ^ 0x01;
MotorEn ^= 1;
TR0 = 1;
}
}
if(K2 == 0)
{
delayms(10); // 软件延时消抖
if(K2 == 0)
{
// while(!K2){Display();};
g_MotorDir = g_MotorDir ^ 0x01;
FX ^= 1;
while(!K2){Display();};
}
}
if(K3 == 0) // 加速
{
delayms(5); // 软件延时消抖
if(K3 == 0)
{
while(!K3){Display();};
g_MotorNum++;
if(g_MotorNum > MotorTabNum)
g_MotorNum = MotorTabNum;
}
}
if(K4 == 0) // 减速
{
delayms(5); // 软件延时消抖
if(K4 == 0)
{
while(!K4){Display();};
g_MotorNum--;
if(g_MotorNum < 0)
g_MotorNum = 0;
}
}
}
/******************************************************************************
* 名称: Set_Display()
* 功能: 数码管显示函数
* 输入: 无
* 输出: 无
* 说明:
******************************************************************************/
void Display(void)
{
unsigned char b1, b2;
b1=g_MotorNum/10;
b2=g_MotorNum%10;
if(FX == 0)
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』