This drive is working well with "LPD3806-600BM-G5-24G", and have Simple program, external pull-up resistance is required
注意:需要外置上拉电阻。
Rotary_Encoder.cpp@2:7d34758c3cc4, 2019-02-08 (annotated)
- Committer:
- Yifan_Du
- Date:
- Fri Feb 08 07:11:22 2019 +0000
- Revision:
- 2:7d34758c3cc4
- Parent:
- 0:b2ffb830539c
Add instruction
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yifan_Du | 0:b2ffb830539c | 1 | #include "Rotary_Encoder.h" |
Yifan_Du | 0:b2ffb830539c | 2 | |
Yifan_Du | 0:b2ffb830539c | 3 | Rotary_Encoder::Rotary_Encoder(PinName White, PinName Green) : Green_Pin(Green), White_Pin(White) |
Yifan_Du | 0:b2ffb830539c | 4 | { |
Yifan_Du | 0:b2ffb830539c | 5 | Green_Pin.rise(this, &Rotary_Encoder::Green_Pin_Rise); |
Yifan_Du | 0:b2ffb830539c | 6 | White_Pin.rise(this, &Rotary_Encoder::White_Pin_Rise); |
Yifan_Du | 0:b2ffb830539c | 7 | } |
Yifan_Du | 0:b2ffb830539c | 8 | |
Yifan_Du | 0:b2ffb830539c | 9 | void Rotary_Encoder::Green_Pin_Rise(void) |
Yifan_Du | 0:b2ffb830539c | 10 | { |
Yifan_Du | 0:b2ffb830539c | 11 | if(White_Pin.read() == 0) |
Yifan_Du | 0:b2ffb830539c | 12 | Encoder_Counter++; |
Yifan_Du | 0:b2ffb830539c | 13 | else |
Yifan_Du | 0:b2ffb830539c | 14 | Encoder_Counter--; |
Yifan_Du | 0:b2ffb830539c | 15 | } |
Yifan_Du | 0:b2ffb830539c | 16 | |
Yifan_Du | 0:b2ffb830539c | 17 | void Rotary_Encoder::White_Pin_Rise(void) |
Yifan_Du | 0:b2ffb830539c | 18 | { |
Yifan_Du | 0:b2ffb830539c | 19 | if(Green_Pin.read() == 0) |
Yifan_Du | 0:b2ffb830539c | 20 | Encoder_Counter--; |
Yifan_Du | 0:b2ffb830539c | 21 | else |
Yifan_Du | 0:b2ffb830539c | 22 | Encoder_Counter++; |
Yifan_Du | 0:b2ffb830539c | 23 | } |
Yifan_Du | 0:b2ffb830539c | 24 | |
Yifan_Du | 0:b2ffb830539c | 25 | char *Rotary_Encoder::Calculate_Direction(void) |
Yifan_Du | 0:b2ffb830539c | 26 | { |
Yifan_Du | 0:b2ffb830539c | 27 | // Data processing -2^32< "int" <2^32-1 |
Yifan_Du | 0:b2ffb830539c | 28 | while(Encoder_Counter < -1275) |
Yifan_Du | 0:b2ffb830539c | 29 | Encoder_Counter = Encoder_Counter + 1200; |
Yifan_Du | 0:b2ffb830539c | 30 | while(Encoder_Counter > 1275) |
Yifan_Du | 0:b2ffb830539c | 31 | Encoder_Counter = Encoder_Counter - 1200; |
Yifan_Du | 0:b2ffb830539c | 32 | |
Yifan_Du | 0:b2ffb830539c | 33 | // Calculate the direction |
Yifan_Du | 0:b2ffb830539c | 34 | if(((-1275<=Encoder_Counter)&&(Encoder_Counter<=-1126)) || ((-76 <= Encoder_Counter)&&(Encoder_Counter <= 75)) || ((1126 <= Encoder_Counter)&&(Encoder_Counter <= 1275))) |
Yifan_Du | 0:b2ffb830539c | 35 | strcpy(Direction,"N"); // N 北 |
Yifan_Du | 0:b2ffb830539c | 36 | else if (((76 <= Encoder_Counter)&&(Encoder_Counter <= 225)) || ((-1126 <= Encoder_Counter)&&(Encoder_Counter <= -975))) |
Yifan_Du | 0:b2ffb830539c | 37 | strcpy(Direction,"NE"); // NE 东北 |
Yifan_Du | 0:b2ffb830539c | 38 | else if (((226 <= Encoder_Counter)&&(Encoder_Counter <= 375)) || ((-976 <= Encoder_Counter)&&(Encoder_Counter <= -825))) |
Yifan_Du | 0:b2ffb830539c | 39 | strcpy(Direction,"E"); // E 东 |
Yifan_Du | 0:b2ffb830539c | 40 | else if (((376 <= Encoder_Counter)&&(Encoder_Counter <= 525)) || ((-826 <= Encoder_Counter)&&(Encoder_Counter <= -675))) |
Yifan_Du | 0:b2ffb830539c | 41 | strcpy(Direction,"SE"); // SE 东南 |
Yifan_Du | 0:b2ffb830539c | 42 | else if (((526 <= Encoder_Counter)&&(Encoder_Counter <= 675)) || ((-676 <= Encoder_Counter)&&(Encoder_Counter <= -525))) |
Yifan_Du | 0:b2ffb830539c | 43 | strcpy(Direction,"S"); // S 南 |
Yifan_Du | 0:b2ffb830539c | 44 | else if (((676 <= Encoder_Counter)&&(Encoder_Counter <= 825)) || ((-526 <= Encoder_Counter)&&(Encoder_Counter <= -375))) |
Yifan_Du | 0:b2ffb830539c | 45 | strcpy(Direction,"SW"); // SW 西南 |
Yifan_Du | 0:b2ffb830539c | 46 | else if (((826 <= Encoder_Counter)&&(Encoder_Counter <= 975)) || ((-376 <= Encoder_Counter)&&(Encoder_Counter <= -225))) |
Yifan_Du | 0:b2ffb830539c | 47 | strcpy(Direction,"W"); // W 西 |
Yifan_Du | 0:b2ffb830539c | 48 | else if (((927 <= Encoder_Counter)&&(Encoder_Counter <= 1125)) || ((-226 <= Encoder_Counter)&&(Encoder_Counter <= -75))) |
Yifan_Du | 0:b2ffb830539c | 49 | strcpy(Direction,"NW"); // NW 西北 |
Yifan_Du | 0:b2ffb830539c | 50 | |
Yifan_Du | 0:b2ffb830539c | 51 | return Direction; |
Yifan_Du | 0:b2ffb830539c | 52 | } |