功能:
控制步進(jìn)電機實現(xiàn)加速、減速、正轉(zhuǎn),反轉(zhuǎn),停止,并用LCD12864顯示相關(guān)信息。
#define KEY_GLOBAL? 1
#include "Key.h"
static uchar keyValueBuff;???????????? //讀取鍵值的緩沖區(qū)
static uchar keyValueOld;????????????? //前一次的鍵值
static uchar keyValueTemp;???????????????????? ?? //鍵值中間交換變量
static uint? stillTimes;?????????????? //鍵按下保持次數(shù)
static uint? stillTimesMax;
static uchar get_data_1_count(uchar number);
static void read_all_key(uchar *buff);
/**********************************************************
*函數(shù)名稱:keyDelay(uint us)
*函數(shù)功能:毫秒級延時
*函數(shù)入口:延時計數(shù)變量us
*函數(shù)出口:無
***********************************************************/
void keyDelay(uint us)
{
uchar delayi;
while(--us)
{
for(delayi=0;delayi<10;delayi++);
}
}
/*******************************************
*函數(shù)名稱:key_init(void)
*函數(shù)功能:初始化按鍵掃描
*函數(shù)入口:無
*函數(shù)出口:無
*******************************************/
void key_init(void)
{
keyValueBuff = 0x00;
keyValueOld = 0x00;
keyValueTemp = 0x00;
stillTimes = 0;
stillTimesMax = FIRST_TIMES;
}
/******************************************************
*函數(shù)名稱:read_all_key(uchar *buff)
*函數(shù)功能:掃描按鍵下的鍵,將按下的鍵的位置存入變量buff
*函數(shù)入口:buff
*函數(shù)出口:無
*******************************************************/
void read_all_key(uchar *buff)
{
if(IN_PRESS_FRONT==KEY_FORCE_VALUE)?? //檢測按下的鍵
READ_KEY(1,KEY_FRONT,*buff);
else
READ_KEY(0,KEY_FRONT,*buff);
if(IN_PRESS_QUEEN==KEY_FORCE_VALUE)?? //檢測按下的鍵
READ_KEY(1,KEY_QUEEN,*buff);
else
READ_KEY(0,KEY_QUEEN,*buff);
if(IN_PRESS_ADD==KEY_FORCE_VALUE)??????? //檢測按下的鍵
READ_KEY(1,KEY_ADD,*buff);
else
READ_KEY(0,KEY_ADD,*buff);
if(IN_PRESS_REDUCE==KEY_FORCE_VALUE) //檢測按下的鍵
READ_KEY(1,KEY_REDUCE,*buff);
else
READ_KEY(0,KEY_REDUCE,*buff);
if(IN_PRESS_STOP==KEY_FORCE_VALUE)? //檢測按下的鍵
READ_KEY(1,KEY_STOP,*buff);
else
READ_KEY(0,KEY_STOP,*buff);
}
/******************************************************
*函數(shù)名稱:get_key_value(void)
*函數(shù)功能:讀取鍵值
*函數(shù)入口:無
*函數(shù)出口:返回按下的有效鍵值
*******************************************************/
uchar get_key_value(void)
{
keyValueBuff = 0x00;
read_all_key(&keyValueBuff);
keyValueTemp = keyValueBuff;
keyDelay(KEY_DELAY_TIME);
read_all_key(&keyValueBuff);
/* 兩次讀到的鍵盤值相同 則為有效鍵盤值 */
if( keyValueTemp == keyValueBuff )
{
if(keyValueTemp == 0)
{
keyValueOld = 0;
stillTimes = 0;
stillTimesMax = FIRST_TIMES;
return 0;
}
else if(keyValueOld != keyValueTemp)
{
stillTimes = 0;
keyValueOld = keyValueTemp;
#if CAN_MORE_PRESS
return keyValueTemp;
#else
if( 1 == get_data_1_count(keyValueTemp) )
{
return keyValueTemp;
}
else
{
return 0;
}
#endif
}
else if(keyValueOld == keyValueTemp)
{
#if CAN_MORE_PRESS == 0
if( 1 != get_data_1_count(keyValueTemp) )
{
stillTimes = 0;
return 0;
}
#endif
#if CAN_REPEAT
stillTimes ++;
if(stillTimes > stillTimesMax)
{
stillTimes = 0;
#if REPEAT_MODE
if(FIRST_TIMES == stillTimesMax)
{
stillTimesMax = OTHER_TIMES;
}
#endif
return keyValueTemp;
}
#else
stillTimes = 0;
return 0;
#endif
}
}
else
{
stillTimes = 0;
return 0;
}
return 0;
}
/**********************************************************
*函數(shù)名稱:get_data_1_count(uchar number)
*函數(shù)功能:計算uchar類型變量中1的數(shù)量,用于檢測是否多鍵按下
*函數(shù)入口:number,需要檢測1數(shù)量的變量
*函數(shù)出口:返回變量bumber中1的數(shù)量
***********************************************************/
uchar get_data_1_count(uchar number)
{
register uchar i,j = 0;
for(i = 0; i < 8; i ++)
{
if( (number&_BV(i)) )
{
j ++;
}
}
return j;
}
case MOTER_FRONT_LOWER:????? //正轉(zhuǎn)低速運行
{
display_state(0x02);
display_speed(0x02);
break;
}
case MOTER_QUEEN_ADD:? //反轉(zhuǎn)加速運行
{
display_state(0x03);
display_speed(0x03);
break;
}
case MOTER_QUEEN_REDUCE: //反轉(zhuǎn)減速運行
{
display_state(0x03);
display_speed(0x04);
break;
}
case MOTER_QUEEN_NOMAL: //反轉(zhuǎn)正常運行
{
display_state(0x03);
display_speed(0x01);
break;
}
case MOTER_QUEEN_LOWER: //反轉(zhuǎn)低速運行
{
display_state(0x03);
display_speed(0x02);
break;
}
default:
break;
}
if(moterState!=MOTER_STOP)
{
Lcd_Character_16X16(1,60,64,SPEED_STATE[6]); //運
Lcd_Character_16X16(1,60,80,SPEED_STATE[7]); //行
}
}
/************************************************************
*實例名稱:步進(jìn)電機
*實例說明:實現(xiàn)步進(jìn)電機的正反轉(zhuǎn)以及加減速控制,并在LCD屏上實時
顯示步進(jìn)電機的狀態(tài)
*作者:yd
*時間:09.06.20
************************************************************/
#include "target.h"
#include "key.h"
#define MOTER_SPEED_NORMAL 10?? //電機正常運行上限
#define MOTER_SPEED_MIN??? 25?? //電機低速運行下限
uchar timer0Count;????????????? //timer0中斷溢出計數(shù)
uchar moterStep;?? ??????????? //電機步數(shù)??? ???? (0x00~0x07)
uint? moterSpeed;???????? ??????????? //速度值變化量??? (0~moterSpeedMax)
uint? moterSpeedMax;??????????? //速度最快值
uchar moterState;?????????????? //電機運行狀態(tài)
uchar moterDirect;????????????? //電機運轉(zhuǎn)方向,靜止/0x00;正轉(zhuǎn)/0x01;反轉(zhuǎn)/0x02
uchar moterAddReduce;?????????? //電機加速及減速量
uchar speedDirect;????????????? //0x01,電機加速;0x02,電機減速
bit display_is_ok;????????????? //顯示屏是否需要更新
/**********************************************************
MOTER_STOP,停止;MOTER_FRONT_ADD,正轉(zhuǎn)加速
MOTER_FRONT_REDUCE,正轉(zhuǎn)減速;MOTER_FRONT_NORMAL,正轉(zhuǎn)正常
MOTER_FRONT_LOWER,正轉(zhuǎn)低速;MOTER_QUEEN_ADD,反轉(zhuǎn)加速
MOTER_QUEEN_REDUCE,反轉(zhuǎn)減速,MOTER_QUEEN_NOMAL,反轉(zhuǎn)正常
MOTER_QUEEN_LOWER,反轉(zhuǎn)低速
***********************************************************/
void main()
{
uchar keyValueRead;
target_init();
moterState=MOTER_STOP;
timer0Count=0x00;
moterAddReduce=0x00;
moterStep=0x00;
moterSpeed=0x00;
moterSpeedMax=0x00;? //默認(rèn)停止
moterDirect=0x00;???? ?? //默認(rèn)停止
moter_state_display(MOTER_STOP);
display_is_ok=1;
sei();
while(1)
{
keyValueRead=get_key_value();
switch(keyValueRead)
{
case KEY_FRONT:?????? ? //正轉(zhuǎn)
{
moterState=MOTER_FRONT_LOWER;
moterDirect=0x01;
moterSpeedMax=MOTER_SPEED_MIN;
display_is_ok=0;
//moter_state_display(MOTER_FRONT_LOWER);
break;
}
case KEY_QUEEN:????? ? //反轉(zhuǎn)
{
moterState=MOTER_QUEEN_LOWER;
moterDirect=0x02;
moterSpeedMax=MOTER_SPEED_MIN;
display_is_ok=0;
//moter_state_display(MOTER_QUEEN_LOWER);
break;
}
if(moterSpeed<moterSpeedMax)
moterSpeed++;
else
{
moterSpeed=0x00;
if(moterDirect==0x01) //正轉(zhuǎn)
{
if(moterStep>0x00)
moterStep--;
else
moterStep=0x07;
}
else if(moterDirect==0x02)? //反轉(zhuǎn)
{
if(moterStep<0x07)
moterStep++;
else
moterStep=0x00;
}
ground(moterStep);