Cov txheej txheem:

Rotary Encoder - Nkag siab thiab Siv Nws (Arduino/lwm tus -Controller): 3 Cov Kauj Ruam
Rotary Encoder - Nkag siab thiab Siv Nws (Arduino/lwm tus -Controller): 3 Cov Kauj Ruam

Video: Rotary Encoder - Nkag siab thiab Siv Nws (Arduino/lwm tus -Controller): 3 Cov Kauj Ruam

Video: Rotary Encoder - Nkag siab thiab Siv Nws (Arduino/lwm tus -Controller): 3 Cov Kauj Ruam
Video: Tsis tau yuav koj los tsuav nyob koom zos 3/9/2018 2024, Hlis ntuj nqeg
Anonim
Rotary Encoder - Nkag siab thiab Siv Nws (Arduino/lwm tus -Controller)
Rotary Encoder - Nkag siab thiab Siv Nws (Arduino/lwm tus -Controller)

Lub tshuab hluav taws xob hloov pauv yog lub tshuab hluav taws xob uas hloov cov lus tsa suab tig mus rau digital lossis analog cov ntaub ntawv. Nws tuaj yeem tig lub moos los yog clockwise. Muaj ob hom kev hloov pauv ntawm tus lej: Tsis meej thiab txheeb ze (nce ntxiv) encoders.

Thaum qhov kev lees paub meej meej tso tawm tus nqi sib npaug rau lub kaum ntse ntse tam sim no, ib qho ntxiv tus lej nkag mus tso tawm cov kauj ruam ntawm tus ncej thiab nws cov kev taw qhia. (Hauv qhov no peb muaj cov lej nkag mus ntxiv)

Cov encoders teb tau dhau los ua neeg nyiam vim tias koj tuaj yeem siv ob lub zog hauv ib qho qauv hluav taws xob: Ib qho yooj yim hloov rau kev lees paub kev ua haujlwm thiab cov encoder teb rau kev taw qhia, piv txwv li. los ntawm cov ntawv qhia zaub mov.

Ib qho kev sib hloov zuj zus ntxiv encoder tsim ob lub cim tso tawm thaum nws lub taub hau tig. Nyob ntawm qhov kev taw qhia, ib qho ntawm cov cim qhia rau lwm tus. (saib hauv qab)

Kauj Ruam 1: Nkag Siab Cov Ntaub Ntawv Tso Tawm

Nkag Siab Cov Ntaub Ntawv Tso Tawm
Nkag Siab Cov Ntaub Ntawv Tso Tawm

Raws li koj tuaj yeem pom thaum tus lej nkag nkag pib tig raws lub moos, cov zis A poob mus rau LOW ua ntej thiab cov zis B ua raws nws. Nyob rau hauv cov lus taw qhia-clockwise kev ua haujlwm tig rov qab.

Tam sim no peb tsuas yog yuav tsum ua qhov no ntawm peb tus tswj hwm (Kuv siv Arduino Nano).

Kauj ruam 2: Tsim Circuit Court

Tsim Circuit Court
Tsim Circuit Court
Tsim Circuit Court
Tsim Circuit Court

Raws li kuv tau piav qhia ua ntej cov txiaj ntsig tsim tau HIGH thiab LOW flank. Txhawm rau kom tau HIGH huv ntawm cov ntaub ntawv tus pin A thiab B ntawm µController peb yuav tsum ntxiv Pull-Up resistors. Tus Pin C feem ntau mus ncaj nraim rau hauv av rau LOW flank.

Txhawm rau kom tau txais cov ntaub ntawv hais txog kev hloov pauv sab hauv (thawb-khawm) peb yuav siv lwm ob tus pin. Ib ntawm lawv mus rau VCC thiab lwm qhov mus rau cov ntaub ntawv Pin ntawm µController. Peb kuj yuav tsum tau ntxiv Pull-Down Resistor rau cov ntaub ntawv tus pin kom tau txais LOW huv.

Nws kuj tseem tuaj yeem siv sab hauv Rub-Up thiab Pull-Down resistors ntawm koj tus tswj hwm!

Hauv kuv qhov teeb meem pinout zoo li:

  • +3, 3V => +3, 3V (Arduino) (kuj +5V tau)
  • GND => GND (Arduino)
  • A => Pin 10
  • IB =>

    Tus Pin

    11

  • C => GND
  • SWV =>

    Tus Pin

    12

Kauj Ruam 3: Sau Txoj Cai

pin pin = 10; // internal switch A int pinB = 11; // internal switch B int pinSW = 12; // hloov (nias Encoder) int encoderPosCount = 0; // pib ntawm xoom, hloov yog tias koj xav tau

int qhov chaw;

bool hloov pauv; koj mrotateLast; koj mrotate;

void teeb tsa () {

int mrotateLast = digitalRead (pinA); Serial.begin (9600); ncua (50); }

lub voj void () {readencoder (); yog (readswitch () == 1) {Serial.println ("Hloov = 1"); }}

koj readencoder () {

mrotate = digitalRead (pinA); yog (mrotate! = mrotateLast) {// pob qhov rooj yog tig yog (digitalRead (pinB)! = mrotate) {// hloov A hloov ua ntej -> rotating clockwise encoderPosCount ++; Serial.println ("rotated clockwise"); } lwm tus {// hloov B hloov ua ntej-> tig lub moos xauv moos encoderPosCount--; Serial.println ("tig rov qab moos"); }

Serial.print ("Txoj Haujlwm Encoder:"); Serial.println (encoderPosCount); Serial.println (""); } mrotateLast = mrotate; rov encoderPosCount; } bool readswitch () {

yog (digitalRead (pinSW)! = 0) {// hloov tau nyem

thaum (digitalRead (pinSW)! = 0) {} // hloov tam sim no nias switchval = 1; } lwm tus {switchval = 0;} // hloov tsis hloov siab rov qab hloov pauv; }

Tam sim no koj tuaj yeem tig tus lej cim thiab qhov sib txawv ntawm tus lej encoderPosCount yuav suav tau yog tias koj tig clockwise thiab suav rov qab yog tias koj tig counter clockwise.

Nws yog! Yooj yim thiab muaj txiaj ntsig.

Xav tias hloov pauv thiab ua qhov chaws. Koj tuaj yeem siv nws hauv koj qhov project.

Kuv tseem yuav tso qhov project LED uas kuv siv tus lej nkag los teeb qhov ci ntawm kuv cov LEDs.

Pom zoo: