LẬP TRÌNH ĐIỂU KHIỂN ARDUINO VỚI MÀN HÌNH LCD

54 272 0
LẬP TRÌNH ĐIỂU KHIỂN ARDUINO VỚI MÀN HÌNH LCD

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

1. INTRODUCE LCD An LCD is an electronic display module which uses liquid crystal to produce a visible image. 1. GIỚI THIỆU KHÁI QUÁT VỀ MÀN HÌNH LCD Màn hình LCD là một môđun điện tử hiển thị sử dụng tinh thể lỏng để tạo ra hình ảnh có thể nhìn thấy được A liquidcrystal display (LCD) is used in a wide range of applications including LCD televisions, computer monitors, instrument panels, aircraft cockpit displays, and indoor and outdoor signage. Small LCD screens are common in portable consumer devices such as digital cameras, watches, calculators, and mobile telephones, including smartphones. LCD screens are also used on consumer electronics products such as DVD players, video game devices and clocks. LCD screens have replaced heavy, bulky cathode ray tube (CRT) displays in nearly all applications. LCD screens are available in a wider range of screen sizes than CRT and plasma displays, with LCD screens available in sizes ranging from tiny digital watches to huge, bigscreen television sets. Trong thời đại công nghệ ngày nay, thiết bị hiển thị LCD (Liquid Crystal Display) được sử dụng rộng rãi trong rất nhiều các lịch vực khác nhau. LCD có rất nhiều ưu điểm so với các dạng hiển thị khác: Nó có khả năng hiển thị đa dạng các ký tự (chữ, số và kí tự đồ họa), dễ dàng đưa vào mạch ứng dụng theo nhiều giao thức giao tiếp khác nhau, tốn rất ít tài nguyên hệ thống và giá thành rẻ … 1602 LCD display The 16×2 LCD display is a very basic module commonly used in DIYs and circuits. The 16×2 translates a display 16 characters per line in 2 such lines. In this LCD each character is displayed in a 5×7 pixel matrix. Màn hình LCD 16x2 Có rất nhiều loại màn hình tinh thể lỏng vói nhiều chức năng và kích thước khác nhau. Màn hình LCD 16x2 là một màn hình cơ bản thường được sử dụng nhiều trong các mạch. Màn hình LCD 16x2 có thể hiển thị tối đa 16 ký tự trên một dòng và có tất cả hai dòng trên một màn hình. Trong đó, mỗi ký tự được hiển thị trong ma trận ô vuông 5x7. It has 16 pins and the first one from left to right is the Ground pin. The second pin is the VCC which we connect the 5 volts pin on the Arduino Board. Next is the Vo pin on which we can attach a potentiometer for controlling the contrast of the display. LCD 16x2 có tất cả là 16 chân. Chân số 1 tính từ trái sang phải là chân nối đất cho LCD (GND), chân thứ 2 là chân cấp nguồn cho LCD (VCC), chân thứ 3 Vo để điều chỉnh độ tương phản của màn hình. LCD 16x2 được sử dụng phổ biến ( Nguồn: howtomechatronics.comt) Next, The RS pin or register select pin is used for selecting whether we will send commands or data to the LCD. For example if the RS pin is set on low state or zero volts, then we are sending commands to the LCD like: set the cursor to a specific location, clear the display, turn off the display and so on. And when RS pin is set on High state or 5 volts we are sending data or characters to the LCD. Tiếp theo, chân RS được sử dụng để chọn xem chúng ta sẽ gửi lệnh hoặc dữ liệu tới màn hình LCD hay không. Ví dụ chân RS được đặt ở trạng thái thấp hoặc không có điện áp, thì chúng ta sẽ gửi lệnh đến màn hình LCD như: đặt con trỏ tới một vị trí cụ thể, xóa màn hình, tắt màn hình hiển thị. Và khi chân RS được đặt ở trạng thái cao hoặc 5 volts, chúng sẽ gửi dữ liệu hoặc các ký tự tới màn hình LCD. Next comes the R W pin which selects the mode whether we will read or write to the LCD. Here the write mode is obvious and it is used for writing or sending commands and data to the LCD. The read mode is used by the LCD itself when executing the program which we don’t have a need to discuss about it in this tutorial. Tiếp đến chân số 5 (R W) Chân chọn chế độ đọcghi (ReadWrite). Ở đây chế độ ghi là hiển nhiên và nó được sử dụng để viết hoặc gửi lệnh và dữ liệu vào màn hình LCD. Chế độ đọc được sử dụng bởi chính LCD khi thực hiện chương trình mà chúng ta không cần phải thảo luận về nó trong hướng dẫn này. Next is the E pin which enables the writing to the registers. Chân số 7 (E) là chân cho phép (Enable). Sau khi các tín hiệu được đặt lên bus DB0DB7, các lệnh chỉ được chấp nhận khi có một xung cho phép của chân E. The next 8 data pins from D0 to D7. So through this pins we are sending the 8 bits data when we are writing to the registers or for example if we want to see the latter

LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO TRƯỜNG ĐẠI HỌC SƯ PHẠM KỸ THUẬT THÀNH PHỐ HỒ CHÍ MINH KHOA: ĐIỆN – ĐIỆN TỬ BÁO CÁO THỰC TẬP ĐIỆN CƠ BẢN ĐỀ TÀI: LẬP TRÌNH ĐIỂU KHIỂN ARDUINO VỚI MÀN HÌNH LCD Sinh viên thực hiện: Nguyễn Ngọc Nhiệm MSSV: 16141218 TP HỒ CHÍ MINH – 3/2018 LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO INTRODUCE LCD An LCD is an electronic display module which uses liquid crystal to produce a visible image GIỚI THIỆU KHÁI QUÁT VỀ MÀN HÌNH LCD Màn hình LCD mơ-đun điện tử hiển thị sử dụng tinh thể lỏng để tạo hình ảnh nhìn thấy A liquid-crystal display (LCD) is used in a wide range of applications including LCD televisions, computer monitors, instrument panels, aircraft cockpit displays, and indoor and outdoor signage Small LCD screens are common in portable consumer devices such as digital cameras, watches, calculators, and mobile telephones, including smartphones LCD screens are also used on consumer electronics products such as DVD players, video game devices and clocks LCD screens have replaced heavy, bulky cathode ray tube (CRT) displays in nearly all applications LCD screens are available in a wider range of screen sizes than CRT and plasma displays, with LCD screens available in sizes ranging from tiny digital watches to huge, big-screen television sets Trong thời đại công nghệ ngày nay, thiết bị hiển thị LCD (Liquid Crystal Display) sử dụng rộng rãi nhiều lịch vực khác LCD có nhiều ưu điểm so với dạng hiển thị khác: Nó có khả hiển thị đa dạng ký tự (chữ, số kí tự đồ họa), dễ dàng đưa vào mạch ứng dụng theo nhiều giao thức giao tiếp khác nhau, tốn tài nguyên hệ thống giá thành rẻ … 1602 LCD display The 16×2 LCD display is a very basic module commonly used in DIYs and circuits The 16×2 translates a display 16 characters per line in such lines In this LCD each character is displayed in a 5×7 pixel matrix LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO Màn hình LCD 16x2 Có nhiều loại hình tinh thể lỏng vói nhiều chức kích thước khác Màn hình LCD 16x2 hình thường sử dụng nhiều mạch Màn hình LCD 16x2 hiển thị tối đa 16 ký tự dòng có tất hai dòng hình Trong đó, ký tự hiển thị ma trận ô vuông 5x7 It has 16 pins and the first one from left to right is the Ground pin The second pin is the VCC which we connect the volts pin on the Arduino Board Next is the Vo pin on which we can attach a potentiometer for controlling the contrast of the display LCD 16x2 có tất 16 chân Chân số tính từ trái sang phải chân nối đất cho LCD (GND), chân thứ chân cấp nguồn cho LCD (VCC), chân thứ Vo để điều chỉnh độ tương phản hình LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO LCD 16x2 sử dụng phổ biến ( Nguồn: howtomechatronics.comt) Next, The RS pin or register select pin is used for selecting whether we will send commands or data to the LCD For example if the RS pin is set on low state or zero volts, then we are sending commands to the LCD like: set the cursor to a specific location, clear the display, turn off the display and so on And when RS pin is set on High state or volts we are sending data or characters to the LCD Tiếp theo, chân RS sử dụng để chọn xem gửi lệnh liệu tới hình LCD hay khơng Ví dụ chân RS đặt trạng thái thấp khơng có điện áp, gửi lệnh đến hình LCD như: đặt trỏ tới vị trí cụ thể, xóa hình, tắt hình hiển thị Và chân RS đặt trạng thái cao volts, chúng gửi liệu ký tự tới hình LCD Next comes the R / W pin which selects the mode whether we will read or write to the LCD Here the write mode is obvious and it is used for writing or sending commands and data to the LCD The read mode is used by the LCD itself when executing the program which we don’t have a need to discuss about it in this tutorial Tiếp đến chân số (R / W) Chân chọn chế độ đọc/ghi (Read/Write) Ở chế độ ghi hiển nhiên sử dụng để viết gửi lệnh liệu vào hình LCD Chế độ đọc sử dụng LCD thực chương trình mà khơng cần phải thảo luận hướng dẫn Next is the E pin which enables the writing to the registers LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO Chân số (E) chân cho phép (Enable) Sau tín hiệu đặt lên bus DB0-DB7, lệnh chấp nhận có xung cho phép chân E The next data pins from D0 to D7 So through this pins we are sending the bits data when we are writing to the registers or for example if we want to see the latter uppercase A on the display we will send 0100 0001 to the registers according to the ASCII table DB0 - DB7 chân liệu chuyển vào LCD, thông qua chân gửi liệu bit viết tới ghi ví dụ muốn nhìn thấy chữ hoa A gửi 0100 0001 đến ghi theo bảng ASCII And the last two pins A and K, or anode and cathode are for the LED back light Và hai chân cuối A K, hai chân cực dương cực âm đèn LED LCD After all we don’t have to worry much about how the LCD works, as the Liquid Crystal Library takes care for almost everything From the Arduino’s official website you can find and see the functions of the library which enable easy use of the LCD We can use the Library in or bit mode In this tutorial we will use it in bit mode, or we will just use of the data pins Sau tất hiểu rõ cách hình LCD hoạt động Từ trang web thức Arduino, chúng tìm xem chức thư viện cho phép dễ dàng sử dụng hình LCD Chúng ta sử dụng Thư viện chế LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO độ bit Trong hướng dẫn sử dụng chế độ bit, sử dụng chân liệu CONNECT THE LCD SCREEN WITH ARDUINO Before wiring the LCD screen to your Arduino or Genuino board we suggest to solder a pin header strip to the 14 (or 16) pin count connector of the LCD screen KẾT NỐI MÀN HÌNH LCD VỚI ARDUINO Trước nối hình LCD với bảng Arduino, nên hàn dãy chân nối vào đầu nối hình LCD Linh kiện cần có: Bo Arduino LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO Màn hình LCD Biến trở 10k ohm để điểu chỉnh độ tương phản hình Dây nối LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO Bảng nối mạch To wire your LCD screen to your board, connect the following pins: • LCD VSS pin to Arduino GND • LCD VDD pin to Arduino 5V • LCD RS pin to digital pin 12 • LCD RW pin to Arduino GND • LCD Enable pin to digital pin 11 • LCD D4 pin to digital pin • LCD D5 pin to digital pin • LCD D6 pin to digital pin • LCD D7 pin to digital pin LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO Sơ đồ nối chân: LCD Arduino VSS GND VDD 5V RS Chân 12 RW GND E Chân 11 D4 Chân D5 Chân D6 Chân D7 Chân A 5V K GND Additionally, wire a 10k pot to +5V and GND, with it's wiper (output) to LCD screens VO pin (pin3) A 220 ohm resistor is used to power the backlight of the display, usually on pin 15 (A+) and 16 (K-) of the LCD connector Bên cạnh ta nối chân E LCD với chân biến trở, hai chân lại biến trở, chân nối 5v Arduino chân A LCD, chân lại nối GND Arduino chân K LCD Một điện trở 220 ohm sử dụng để cấp nguồn cho đèn hình, thường pin 15 (A +) 16 (K-) đầu nối LCD LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO The process of controlling the display involves putting the data that form the image of what you want to display into the data registers, then putting instructions in the instruction register The LiquidCrystal Library simplifies this for you so you don't need to know the low-level instructions Q trình kiểm sốt hiển thị bao gồm việc đưa liệu hình thành hình ảnh mà muốn hiển thị vào ghi liệu, sau đặt hướng dẫn sổ đăng ký hướng dẫn Thư viện LiquidCrystal đơn giản hóa điều cho khơng cần phải biết hướng dẫn mức thấp Kết nối mạch Để kết nối LCD với arduino, nối chân sau: 10 LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO SIMPLE ARDUINO DIGITAL CLOCK WITHOUT RTC It's just a simple digital clock controlled by Arduino without using any RTC module (Real Time Clock) Every time you switch on this clock you've to set it to the present time, just like the analog clocks found in homes My aim is to give beginners some understanding on how to work with Arduino using only simple materials and without lots of Incomprehensible codes So let's start 40 LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO TẠO ĐỒNG HỒ SỐ ĐƠN GIẢN KHÔNG CẦN SỬ DỤNG MÔ-ĐUN THỜI GIAN Chúng ta vừa thực thao tiếp với LCD Arduino với số loại cảm biến Trong phần này, tạo đồng hồ đếm phút giây đơn giản với arduino LCD Nó đồng hồ kỹ thuật số đơn giản điều khiển Arduino mà không sử dụng mô-đun RTC (Đồng hồ thời gian thực) Mỗi bạn bật đồng hồ này, bạn phải đặt vào thời điểm tại, giống đồng hồ analog tìm thấy nhà Mục đích tơi cung cấp cho người bắt đầu số hiểu biết làm để làm việc với Arduino sử dụng vật liệu đơn giản khơng có nhiều mã khơng thể hiểu Vì vậy, bắt đầu Connect board: Let's Start Anyone with little experience in using a breadboard, connecting wires can make this circuit easily There is nothing to worry just follow the diagram and connect using male jumper wires or you could use the perfboard and solder everything Bất có kinh nghiệm việc sử dụng breadboard, kết nối dây làm cho mạch cách dễ dàng Khơng có phải lo lắng làm theo sơ đồ kết nối cách sử dụng dây nối Follow the circuit diagram: 41 LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO ( Ảnh: create.arduino.cc) Linh kiện cần có thêm: Điện trở 10k ohm Điện trở 221 ohm 42 LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO Nút nhấn Phần cứng Kết nối phần cứng hình dưới, kiểm tra thật kỹ dây nối trước bạn cấp nguồn cho bo mạch Arduino Nhớ ngắt kết nối nguồn cho bo mạch Arudino nối dây mạch hay sữa chữa CODE: /* Digital Winding Clock - Arduino Clock for beginners No RTC, Just set it to exact time using the push buttons each time you switch on the clock Made by Techno (sǝɹoɟ ǝǝ ‫ן‬uuɐ) Feel free to modify */ #include LiquidCrystal lcd(12,11,5,4,3,2); Phần mềm 43 LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO Trước tiên, cần khai báo thư viện cho LCD khai báo chân nối cảu LCD với Arduino phần trước làm: #include LiquidCrystal lcd(12,11,5,4,3,2); int h=12; int m; int s; Sau đó, cần khai báo biến cần thiết: int h=24; // khai báo số tối đa 24 int m; // khai báo biến phút int s; // khai báo biến giây int flag; int TIME; const int hs=8; const int ms=9; int TIME; // khai báo biến thời gian const int hs=8; // khai báo chân điều chỉnh từ nút nhấn const int ms=9; // khai báo chân điều chỉnh phút từ nút nhấn 44 LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO int state1; int state2; int state1; // biến cài đặt int state2;// biến cài đặt phút void setup() { lcd.begin(16,2); } Tiếp theo, k hởi tạo giao diện cho hình LCD, định kích thước (chiều rộng chiều cao) hình hiển thị: void setup() { lcd.begin(16,2); } void loop() { lcd.setCursor(0,0); s=s+1; lcd.print("TIME:"); lcd.print(h); 45 LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO lcd.print(":"); lcd.print(m); lcd.print(":"); lcd.print(s); Tiếp theo vòng lập thời gian: void loop() { lcd.setCursor(0,0); // đặt trỏ vị trí cột hàng s=s+1; // khởi tạo lệnh đếm cho s lcd.print("BAY GIO LA:"); // In hình LCD lcd.setCursor(3,1);// đặt trỏ vị trí cột dòng lcd.print(h); //in hình LCD đơn vị lcd.print(":"); lcd.print(m); // in hình LCD đơn vị phút lcd.print(":"); lcd.print(s); // in hình LCD đơn vị giây delay(1000); lcd.clear(); if(flag12)lcd.print("PM"); if(flag==24)flag=0; delay(1000); lcd.clear(); if(s==60){ s=0; m=m+1; } Sau đây, ta bắt đầu tạo mã lệnh để Arduino đếm thời gian: Trước tiên, cần so sánh số giây Nếu số giây (s) 60, s lại trở giây Và số phút lúc (m) bắt đầu tăng lên phút: if(s==60){ s=0; m=m+1; } if(m==60) { m=0; h=h+1; 47 LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO flag=flag+1; } Ta làm tương tự với số phút, số phút (m) 60 số tăng lên giờ: if(m==60) { m=0; h=h+1; } if(h==13) { h=1; } Và thế, số tăng lên số cuối 24 ngày Chúng ta bắt đầu tạo lệnh so sánh, số 24 ta lại cho h=0 giờ: if(h==24) { h=0;} lcd.setCursor(0,1); lcd.print("HAVE A NICE DAY"); // -Time // setting -// state1=digitalRead(hs); if(state1==1) { h=h+1; 48 LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO //cài đặt thời gian// Khi nhấn giữ nút nhấn hs ms phút tăng lên: // cài đặt state1=digitalRead(hs); if(state1==1) { h=h+1; if(h==24)h=0 ; // số 24 lúc số trở lại } flag=flag+1; if(flag12)lcd.print("PM"); if(flag==24)flag=0; if(h==13)h=1; } state2=digitalRead(ms); if(state2==1){ s=0; m=m+1; } 49 LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO // cài đặt phút tate2=digitalRead(ms); if(state2==1){ s=0; m=m+1; } Finish the connections with Arduino And that's it you've built your Arduino digital clock Và thành Testing 50 LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO Now power up the Arduino either by connecting the USB cable or by using a 12V adapter Arduino should start and the LCD backlight should glow Don't worry if you see nothing on the LCD Try turning the potentiometer, you can see the text becoming clearer Stop turning when you've achieved your desired contrast The button on the right is for changing the hour and the one on left is for changing the minute according to the circuit diagram Kiểm tra Bây kích hoạt Arduino cách kết nối cáp USB cách sử dụng adapter 12V Arduino nên bắt đầu đèn LCD sáng lên Đừng lo lắng bạn khơng thấy hình LCD Hãy thử chuyển Potentiometer, bạn thấy văn trở nên rõ ràng Dừng chuyển bạn đạt độ tương phản bạn muốn Nút bên phải để thay đổi nút bên trái để thay đổi phút theo sơ đồ mạch Ngoài ra, tạo mã lênh để LCD hiển thị thêm ký tự đặc biệt để trang trí cho đồng hồ sau: Ở đây, tạo ký tự trái tim: byte traitim[8]={ B01010, B11111, B11111, B01110, B00100, 51 LẬP TRÌNH ĐIỀU KHIỂN VỚI ARDUINO B00000, B00000, B00000, }; Bằng cách tạo lệnh để bật sáng tắt vng nhỏ hình LCD tương tự phần làm lcd.createChar(1,traitim); // gán cho ký tự vừa tạo for(int i=0; i

Ngày đăng: 28/12/2019, 00:02

Từ khóa liên quan

Mục lục

  • 6. TẠO ĐỒNG HỒ SỐ ĐƠN GIẢN KHÔNG CẦN SỬ DỤNG MÔ-ĐUN THỜI GIAN

    • Let's Start

    • Testing

    • Now power up the Arduino either by connecting the USB cable or by using a 12V adapter. Arduino should start and the LCD backlight should glow. Don't worry if you see nothing on the LCD. Try turning the potentiometer, you can see the text becoming clearer. Stop turning when you've achieved your desired contrast. The button on the right is for changing the hour and the one on left is for changing the minute according to the circuit diagram.

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

  • Đang cập nhật ...

Tài liệu liên quan