自制直流电机调速系统仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg52.h>
- /*********************************************
- * 定时器初值
- *********************************************/
- #define T0HIGH 0xec
- #define T0LOWW 0x8a
- /*********************************************
- *
- *********************************************/
- typedef unsigned char u8;
- typedef unsigned int u16;
- typedef unsigned long u32;
- typedef bit BOOL;
- /*********************************************
- * 单片机管脚定义
- *********************************************/
- sbit KEY_ON_PIN = P2^0;
- sbit KEY_INC_PIN = P2^1;
- sbit KEY_DEC_PIN = P2^2;
- sbit MOTO_LEFT = P3^6;
- sbit MOTO_RIGHT = P3^7;
- /*********************************************
- * 按键定义
- *********************************************/
- #define KEY_NULL 0xff
- #define KEY_LONG_PERIOD 25
- #define KEY_CONTINUE_PERIOD 20
- #define KEY_DOWN 0x80
- #define KEY_CONTINUE 0x40
- #define KEY_UP 0x20
- #define KEY_STATE_INIT 0
- #define KEY_STATE_WOBBLE 1
- #define KEY_STATE_PRESS 2
- #define KEY_STATE_LONG 3
- #define KEY_STATE_CONTINUE 4
- #define KEY_STATE_RELEASE 5
- #define KEY_ON_VALUE 1
- #define KEY_INC_VALUE 2
- #define KEY_DEC_VALUE 3
- #define KEY_ON (KEY_ON_VALUE | KEY_DOWN)
- #define KEY_INC (KEY_INC_VALUE | KEY_DOWN)
- #define KEY_INC_CON (KEY_INC_VALUE | KEY_CONTINUE)
- #define KEY_DEC (KEY_DEC_VALUE | KEY_DOWN)
- #define KEY_DEC_CON (KEY_DEC_VALUE | KEY_CONTINUE)
- /*********************************************
- * 常量定义
- *********************************************/
- #define true 1
- #define false 0
- #define MOTO_ON MOTO_LEFT = false;\
- MOTO_RIGHT = true
- #define MOTO_OFF MOTO_LEFT = true;\
- MOTO_RIGHT = true
- /*********************************************
- * 全局变量定义
- *********************************************/
- code u8 DisCode[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0xff};
- u8 PWMpercent = 0;
- u8 DisNum[4] = {0x0a, 0x0a, 0x0a, 0x00};
- u8 DisPosition = 0;
- u8 SecCount = 0;
- u8 u10msNum = 0;
- BOOL b10msEvent = false;
- BOOL b100msEvent = false;
- BOOL b1secEvent = false;
- BOOL MotoStart = false;
- BOOL MotoRun = false;
- void Key_Prosess(void);
- void Dynamic_Display(void);
- void Speed_Display(void);
- /*********************************************
- * 主函数
- *********************************************/
- main()
- {
- TMOD = 0x01;
- TH0 = T0HIGH;
- TL0 = T0LOWW;
- TR0 = 1;
- ET0 = 1;
- EA = 1;
- P1 = 0x00;
- while(1)
- {
- if(true == b10msEvent) //10毫秒定时标志
- {
- b10msEvent = false;
- Key_Prosess(); //按键扫描
- Dynamic_Display(); //动态显示
- if(true == MotoStart) //如果电机启动
- {
- if(true == MotoRun)
- {
- if(--u10msNum == 0) //如果电机转动时间到,停止电机
- {
- MotoRun = false;
- MOTO_OFF;
- }
- }
- if(true == b1secEvent) //PWM周期为1秒,1秒钟时间到,开始下一个周期
- {
- b1secEvent = false;
- MotoRun = true;
- MOTO_ON;
- u10msNum = PWMpercent;
- }
- }
- }
- }
- }
- /*********************************************
- * 清显示
- *********************************************/
- void Num_Clear(void)
- {
- u8 i;
- for(i=0; i<3; i++)
- {
- DisNum[i] = 0x0a;
- }
- DisNum[3] = 0x00;
- }
- /*********************************************
- * 读键值
- *********************************************/
- u8 Key_Scan(void)
- {
- if(KEY_ON_PIN == false)
- return KEY_ON_VALUE;
- if(KEY_INC_PIN == false)
- return KEY_INC_VALUE;
- if(KEY_DEC_PIN == false)
- return KEY_DEC_VALUE;
- return KEY_NULL;
- }
- /*********************************************
- * 按键状态扫描
- *********************************************/
- u8 GetKey(void)
- {
- static u8 KeyState = KEY_STATE_INIT;
- static u8 KeyTimeCount = 0;
- static u8 LastKey = KEY_NULL; //保存按键释放时候的键值
- u8 KeyTemp = KEY_NULL ;
- KeyTemp = Key_Scan();
- switch(KeyState)
- {
- case KEY_STATE_INIT: //初始状态
- if(KEY_NULL != KeyTemp) //有键按下
- {
- KeyState = KEY_STATE_WOBBLE; //进入消抖
- }
- break;
- case KEY_STATE_WOBBLE: //消抖
- KeyState = KEY_STATE_PRESS;
- break;
- case KEY_STATE_PRESS:
- if(KEY_NULL != KeyTemp)
- {
- LastKey = KeyTemp; //保存键值,以便在释放按键状态返回键值
- KeyTemp |= KEY_DOWN; //按键按下
- KeyState = KEY_STATE_LONG;
- }
- else
- {
- KeyState = KEY_STATE_INIT;
- }
- break;
- case KEY_STATE_LONG:
- if(KEY_NULL != KeyTemp)
- {
- if(++KeyTimeCount >= KEY_LONG_PERIOD)
- {
- KeyTimeCount = 0;
- KeyState = KEY_STATE_CONTINUE;
- }
- }
- else
- {
- KeyState = KEY_STATE_RELEASE;
- }
- break;
- case KEY_STATE_CONTINUE:
- if(KEY_NULL != (KeyTemp))
- {
- if(++KeyTimeCount >= KEY_CONTINUE_PERIOD)
- {
- KeyTimeCount = 0;
- KeyTemp |= KEY_CONTINUE; //连击按键事件发生
- }
- }
- else
- {
- KeyState = KEY_STATE_RELEASE;
- }
- break;
- case KEY_STATE_RELEASE:
- LastKey |= KEY_UP; //按键释放
- KeyTemp = LastKey;
- KeyState = KEY_STATE_INIT;
- break;
- default:
- break;
- }
- return KeyTemp;
- }
- /*********************************************
- * 按键功能处理
- *********************************************/
- void Key_Prosess(void)
- {
- u8 KeyData;
- KeyData = GetKey();
- switch(KeyData)
- {
- case KEY_ON: //开关电机
- if(true == MotoStart)
- {
- MotoStart = false;
- Num_Clear();
- MOTO_OFF;
- MotoRun = false;
- PWMpercent= 0;
- }
- else
- {
- MotoStart = true;
- MOTO_ON;
- MotoStart = true;
- MotoRun = true;
- u10msNum = PWMpercent = 1;
- }
- break;
- case KEY_INC: //单步增加
- case KEY_INC_CON: //连续增加
- if((PWMpercent < 100) && MotoStart) //PWM从1%~100%
- PWMpercent++;
- break;
- case KEY_DEC: //单步减小
- case KEY_DEC_CON: //连续减小
- if((PWMpercent > 1) && MotoStart) //PWM从1%~100%
- PWMpercent--;
- break;
- default:
- break;
- }
- Speed_Display();
- }
- /*********************************************
- * 速度转换成十进制数
- *********************************************/
- void Speed_Display(void)
- {
- u8 i;
- DisNum[1] = PWMpercent / 100;
- DisNum[2] = PWMpercent % 100 / 10;
- DisNum[3] = PWMpercent % 10;
- for(i=1; i<3; i++)
- {
- if(DisNum[i] != 0)
- break;
- DisNum[i] = 0x0a;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
moto.rar
(68.74 KB, 下载次数: 78)
|