.
LCD顯示器也算是一般設備上蠻常用到的裝置,主要可以用來顯示英文與數字,如果要顯示中
文,則需要購買其他規格的顯示器
以下為LCD 點陣液晶模組的角位與對應的控制和接線圖
圖源: http://coopermaa2nd.blogspot.tw/2010/12/arduino-lab9-2x16-lcd-world.html
以下範例程式主要功能為顯示從PC端傳來的訊息,以及顯示裝置從剛通電開始執行經過的時間
同時也會顯示從 arduino 每秒所產生的亂數
arduino source code
- #include <LiquidCrystal.h>
- // 建立 LiquidCrystal 的變數 lcd
- // LCD 接腳: rs, enable, d4, d5, d6, d7
- // 對應到 Arduino 接腳: 12, 11, 5, 4, 3, 2
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- unsigned long time_previous,now;
- void setup() {
- Serial.begin(9600);
- // 設定 LCD 的行列數目 (2 x 16)
- lcd.begin(16, 2);
- // 列印 "Hello World" 訊息到 LCD 上
- lcd.print("hello, World!");
- delay(1000);
- lcd.clear();
- time_previous = millis();
- //利用analog pin的noise產生隨機的seed
- randomSeed(analogRead(0));
- }
- void loop() {
- // 將游標設到 column 0, line 1
- // (注意: line 1 是第二行(row),因為是從 0 開始數起):
- lcd.setCursor(0, 0);
- String s = "";
- //接收字元組成字串
- while (Serial.available()) {
- char c = Serial.read();
- if(c!='\n'){
- s += c;
- }
- delay(5);
- lcd.clear();//每次印結果前清除先前畫面
- }
- lcd.print(s);
- lcd.setCursor(0, 1);
- // 列印 Arduino 重開之後經過的秒數
- lcd.print(String("") + millis()/1000+"s");
- now = millis();
- if(now - time_previous > 1000)
- {
- time_previous += 1000;
- lcd.setCursor(6, 1);
- int randNumber = random(0,999);
- lcd.print(String("")+"Random "+ randNumber);
- }
-
- }
C#
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- using System.IO.Ports;
-
- namespace Serial_Send
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- serialPort1.BaudRate = 9600;
- serialPort1.Parity = Parity.None;
- serialPort1.DataBits = 8;
- serialPort1.StopBits = StopBits.One;
- serialPort1.Encoding = Encoding.GetEncoding("UTF-8");
- /指定編碼格式 收到資訊才不會出現亂碼
- label1.Text = "PC狀態:尚未連線";
- serialPort1.PortName = "COM3";
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.Open();
- Thread.Sleep(1000);
- button1.Enabled = false;
- label1.Text = "PC狀態:連線中";
- }
- catch
- {
- label1.Text = "PC狀態:Comport open fail";
- }
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- string data = "";
- data = textBox1.Text;
- serialPort1.Write(data);
- }
- }
- }
執行畫面
影片2執行結果為 先送出 當日時間,接著送出訊息 Test 顯示器立即刷新內容
以下影片為修改前的原始範例執行畫面,存粹顯示內建訊息與執行時間