Телесистемы
 Разработка, производство и продажа радиоэлектронной аппаратуры
На главную   | Карта сайта | Пишите нам | В избранное
Требуется программист в Зеленограде
- обработка данных с датчиков; ColdFire; 40 тыс.
e-mail:jobsmp@pochta.ru

Телесистемы | Электроника | Конференция «Микроконтроллеры и их применение»

У меня тоже такой вариант есть. Но я бы не сказал, что очень удобно.

Отправлено Леонид Иванович 17 сентября 2008 г. 13:50
В ответ на: я тоже делал обработку энкодера поллингом (без прерываний)... с анализом скорости вращения отправлено ARV 17 сентября 2008 г. 09:50


//----------------------------------------------------------------------------

//Encoder support module

//----------------------------------------------------------------------------

#include <Main.h>
#include <Encoder.h>

//------------------------------ Constants: ----------------------------------


#define ENCDEB 300 //encoder debounce delay, uS
#define SWBDEB 30 //button debounce delay, mS
#define SWBHLD 1000 //button hold delay, mS
#define ENC_TMV 150 //encoder velocity measure interval, mS
#define ENC_V 2 //threshold velocity, steps per ENC_TMV

#define EF1 0x01 //encoder F1 mask
#define EF2 0x02 //encoder F2 mask

enum
{
ES0, ES1, ES3, ES2 //encoder sequential states
};

//------------------------------ Variables: ----------------------------------

static char EncPre; //previous encoder state
static char EncPpe; //pre-pre encoder state
static bool BtnPre; //previous button state
static char Msg; //encoder message

//------------------------- Function prototypes: -----------------------------

char Enc_Scan(void); //scan encoder
bool Btn_Scan(void); //scan button

//---------------------------- Encoder init: ---------------------------------

void Enc_Init(void)
{
Msg = MSG_NOP; //no message
EncPre = Enc_Scan(); //scan encoder and store state
EncPpe = EncPre; //store pre-pre state
BtnPre = 0; //allow power-up button hold
}

//------------------------- Get encoder message: -----------------------------

char Enc_Get(void)
{
return(Msg);
}

//------------------------- Set encoder message: -----------------------------

void Enc_Set(char msg)
{
Msg = msg;
}

//---------------------------- Scan encoder: ---------------------------------

char Enc_Scan(void)
{
char n = 0;
if(Pin_ENC_F1) n |= EF1; //check F1 line
if(Pin_ENC_F2) n |= EF2; //check F2 line
return(n);
}

//---------------------------- Scan button: ----------------------------------

bool Btn_Scan(void)
{
return(!Pin_BTN); //check button state
}

//-------------------------- Encoder processing: -----------------------------

//Result placed in Msg as encoder message.
//Message codes see in file Encoder.h.

void Enc_Exe(bool t)
{
static int DbncTimer = 0;
static int HoldTimer = 0;
static int EncTimer = ms2sys(ENC_TMV);
static char EncV = 0;
static char EncS = 0;
//process timers:
if(t)
{
if(DbncTimer) DbncTimer--;
if(HoldTimer) HoldTimer--;
if(EncTimer) EncTimer--;
else
{
#ifdef VAR_ENC
EncV = EncS;
#endif
EncS = 0;
EncTimer = ms2sys(ENC_TMV);
}
if(DbncTimer == 1)
{
if(BtnPre)
{
Msg = MSG_KDN; //button press
HoldTimer = ms2sys(SWBHLD);
return;
}
if(!BtnPre && HoldTimer)
{
Msg = MSG_KUP; //button release
HoldTimer = 0;
return;
}
}
if(HoldTimer == 1)
{
Msg = MSG_KHD; //button hold
return;
}
}

//scan button:
bool BtnTmp = Btn_Scan(); //scan button
if(BtnTmp != BtnPre)
{
DbncTimer = ms2sys(SWBDEB); //reload debounce timer
BtnPre = BtnTmp; //save new state
}

//scan encoder:
char EncTmp = Enc_Scan(); //scan encoder and store result
Delay_us(ENCDEB); //debounce delay for encoder
char EncNew = Enc_Scan(); //scan encoder one more time
if(EncNew != EncTmp) return; //bounce in progress, return
if(EncNew == EncPre) return; //no changes, return

EncTmp = EncPpe; //pre-pre state
EncPpe = EncPre; //pre-sate
EncPre = EncNew; //new state

if(EncNew == ES2 && EncPpe == ES1 && EncTmp == ES0)
{
EncS++;
if(EncV > ENC_V)
Msg = MSG_FDEC; //fast decrement message
else Msg = MSG_DEC; //decrement message
return;
}
if(EncNew == ES2 && EncPpe == ES3 && EncTmp == ES0)
{
EncS++;
if(EncV > ENC_V)
Msg = MSG_FINC; //fast increment message
else Msg = MSG_INC; //increment message
return;
}
}

//----------------------------------------------------------------------------


Составить ответ | Вернуться на конференцию

Ответы


Отправка ответа
Имя*: 
Пароль: 
E-mail: 
Тема*:

Сообщение:

Ссылка на URL: 
URL изображения: 

если вы незарегистрированный на форуме пользователь, то
для успешного добавления сообщения заполните поле, как указано ниже:
умножьте 2 на три:

Перейти к списку ответов | Конференция | Раздел "Электроника" | Главная страница | Карта сайта

Rambler's Top100 Рейтинг@Mail.ru
 
Web telesys.ru