Báo cáo thực hành cảm biến trong điều khiển đọc xung encoder

7 344 4
Báo cáo thực hành cảm biến trong điều khiển đọc xung encoder

Đang tải... (xem toàn văn)

Thông tin tài liệu

GIÁO DỤC VÀ ĐÀO TẠO TRƯỜNG ĐẠI HỌC NÔNG LÂM THÀNH PHỐ HỒ CHÍ MINH KHOA CƠ KHÍ – CÔNG NGHỆ —–¯—– BÁO CÁO THỰC HÀNH CẢM BIẾN TRONG ĐIỀU KHIỂN ĐỌC XUNG ENCODER GVHD : Ths LÊ VĂN BẠN SVTH : Trần Đức Bảo LỚP : DH12TD MSSV : 12138017 Tháng năm 2015 I GIỚI THIỆU ENCODER: II SƠ ĐỒ NGUYÊN LÝ: III LẬP TRÌNH TRÊN ARDUINO: /* Chương trình ngắt cho Encoder quay Một encoder quay thông thường có chân, nhìn từ mặt trước A C B Quay theo chiều kim đồng hồ A(on)>B(on)>A(off)->B(off) Quay ngược chiều kim đồng hồ B(on)>A(on)>B(off)->A(off) Ngoài có chân có nút nhấn chân nguồn */ #define encoderPinA // Tương ứng chân DT Encoder: right (labeled DT on our decoder, yellow wire) #define encoderPinB // Tương ứng chân CLK Encoder: left (labeled CLK on our decoder, green wire) #define clearButton // Chân nút nhấn switch (labeled SW on our decoder, orange wire) // Chân + nối nguồn 5V chân GND nối cực âm connect the +5v and gnd appropriately volatile unsigned int encoderPos = 0; // Cho vị trí đầu a counter for the dial unsigned int lastReportedPos = 1; // Vị trí cuối i change management static boolean rotating=false; // Quản lý debounce (giống chống nhiễu) debounce management // biến cho trình phục vụ ngắt interrupt service routine vars boolean A_set = false; boolean B_set = false; void setup() { pinMode(encoderPinA, INPUT_PULLUP); // INPUT-PULLUP tương đương Mode INPUT tự động nhận trạng thái HIGH LOW pinMode(encoderPinB, INPUT_PULLUP); pinMode(clearButton, INPUT_PULLUP); // Chân encoder ngắt (chân 2): encoder pin on interrupt (pin 2) attachInterrupt(0, doEncoderA, CHANGE); // Chân encoder ngắt (chân 3) encoder pin on interrupt (pin 3) attachInterrupt(1, doEncoderB, CHANGE); Serial.begin(9600); // chuyển liệu lên cổng Serial Port } // Vòng lặp chính, công việc thực trình phục vụ ngắt: main loop, work is done by interrupt service routines, this one only prints stuff void loop() { rotating = true; // Khởi động debounce (có thể hiểu chống nhiễu): reset the debouncer if (lastReportedPos != encoderPos) { Serial.print("Index:"); Serial.println(encoderPos, DEC); lastReportedPos = encoderPos; } if (digitalRead(clearButton) == LOW ) { encoderPos = 0; } } // Ngắt chuyển trạng thái A: Interrupt on A changing state void doEncoderA(){ // debounce if ( rotating ) delay (1); // Chờ chút wait a little until the bouncing is done // Kiểm tra việc chuyển đổi trạng thái, xem có thật thay đổi trạng thái chưa: Test transition, did things really change? if( digitalRead(encoderPinA) != A_set ) { // debounce lần nữa: debounce once more A_set = !A_set; // Cộng có tín hiệu A có tín hiệu B: adjust counter + if A leads B if ( A_set && !B_set ) encoderPos += 1; rotating = false; // Không cần debounce nhấn lần nữa: no more debouncing until loop() hits again } } // Ngắt thay đổi trạng thái B, tương tự A: Interrupt on B changing state, same as A above void doEncoderB(){ if ( rotating ) delay (1); if( digitalRead(encoderPinB) != B_set ) { B_set = !B_set; // Trừ B đến A: adjust counter - if B leads A if( B_set && !A_set ) encoderPos -= 1; rotating = false; } } ... pinMode(encoderPinB, INPUT_PULLUP); pinMode(clearButton, INPUT_PULLUP); // Chân encoder ngắt (chân 2): encoder pin on interrupt (pin 2) attachInterrupt(0, doEncoderA, CHANGE); // Chân encoder. .. nút nhấn chân nguồn */ #define encoderPinA // Tương ứng chân DT Encoder: right (labeled DT on our decoder, yellow wire) #define encoderPinB // Tương ứng chân CLK Encoder: left (labeled CLK on... debouncer if (lastReportedPos != encoderPos) { Serial.print("Index:"); Serial.println(encoderPos, DEC); lastReportedPos = encoderPos; } if (digitalRead(clearButton) == LOW ) { encoderPos = 0; } } // Ngắt

Ngày đăng: 06/01/2016, 18:43

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan