스위치(버튼)의 입력을 받아 LED 를 ON/OFF 하는 예제입니다.
스위치의 눌림 상태에 따라 LED ON 위치가 변경되는 것을 알 수 있습니다.
스위치(버튼)의 눌림 상태를 읽는 방법은 아래 코드와 같이 3가지 방법이 있습니다.
코드 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 ZeoDotNetLib;
namespace WATInputOutput { public partial class Form1 : Form { #region ZEO-IO Pin Define const PIN_NAME D1 = PIN_NAME.PA0; const PIN_NAME D2 = PIN_NAME.PA1; const PIN_NAME D3 = PIN_NAME.PA2; const PIN_NAME D4 = PIN_NAME.PA3; const PIN_NAME D5 = PIN_NAME.PA4; const PIN_NAME D6 = PIN_NAME.PA5; const PIN_NAME D7 = PIN_NAME.PA6; const PIN_NAME D8 = PIN_NAME.PA7; const PIN_NAME D9 = PIN_NAME.PA8; const PIN_NAME D10 = PIN_NAME.PA9; const PIN_NAME D11 = PIN_NAME.PA10; const PIN_NAME D12 = PIN_NAME.PA11; const PIN_NAME D13 = PIN_NAME.PA16; const PIN_NAME D14 = PIN_NAME.PB0; const PIN_NAME D15 = PIN_NAME.PB1; const PIN_NAME D16 = PIN_NAME.PB2; const PIN_NAME D17 = PIN_NAME.PB3; const PIN_NAME D18 = PIN_NAME.PB4; const PIN_NAME D19 = PIN_NAME.PB5; const PIN_NAME D20 = PIN_NAME.PB6; const PIN_NAME D21 = PIN_NAME.PB7; const PIN_NAME D22 = PIN_NAME.PB8; const PIN_NAME D23 = PIN_NAME.PB9; const PIN_NAME D24 = PIN_NAME.PB10; const PIN_NAME D25 = PIN_NAME.PB11;
const PIN_NAME SW1 = PIN_NAME.PA12; const PIN_NAME SW2 = PIN_NAME.PA13; const PIN_NAME SW3 = PIN_NAME.PA14; const PIN_NAME SW4 = PIN_NAME.PA15;
const PIN_NAME SW5 = PIN_NAME.PB12; const PIN_NAME SW6 = PIN_NAME.PB13; const PIN_NAME SW7 = PIN_NAME.PB14; const PIN_NAME SW8 = PIN_NAME.PB15;
#endregion
PIN_NAME pin1 = D1 | D2| D3; PIN_NAME pin2 = D4 | D5 | D6; PIN_NAME pin3 = D7 | D8 | D9; PIN_NAME pin4 = D10 | D11 | D12 | D13; PIN_NAME pin5 = D14 | D15 | D17; PIN_NAME pin6 = D16 | D18 | D19; PIN_NAME pin7 = D20 | D21 | D22; PIN_NAME pin8 = D23 | D24 | D25;
// ZEO-S 인스턴스 ZeoLib ZEO = new ZeoLib();
public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { // ZEO 초기화 this.ZEO.Open(); if (this.ZEO.IsOpened) { this.ZEO.InitZeo(0); this.ZEO.Pin_Set(this.ZEO.PIN_ALL); this.ZEO.PORT_DirOutputALL(); // 스위치를 입력으로 설정 this.ZEO.PORT_DirInput(SW1 | SW2 | SW3 | SW4 | SW5 | SW6 | SW7 | SW8 ); }
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // ZEO 닫기 if (this.ZEO.IsOpened) this.ZEO.Close(); }
private void timer1_Tick(object sender, EventArgs e) { if (!this.ZEO.IsOpened) { Console.WriteLine("ZEO-S 가 연결되어 있지 않습니다."); return; }
// 모든 포트의 값을 읽어 UInt32[] uiData = this.ZEO.ReadPortAll();
////////////////////////////////////////////////// // 방법1
// SW1 눌리면 if (this.ZEO.PA12) this.ZEO.Pin_Set(pin1); else this.ZEO.Pin_Reset(pin1);
// SW2 눌리면 if (this.ZEO.PA13) this.ZEO.Pin_Set(pin2); else this.ZEO.Pin_Reset(pin2);
// SW3 눌리면 if (this.ZEO.PA14) this.ZEO.Pin_Set(pin3); else this.ZEO.Pin_Reset(pin3);
// SW4 눌리면 if (this.ZEO.PA15) this.ZEO.Pin_Set(pin4); else this.ZEO.Pin_Reset(pin4);
///////////////////////////////////////////// // 방법2
// SW5 눌리면 this.ZEO.Pin_Write(pin5, this.ZEO.PB12);
// SW6 눌리면 this.ZEO.Pin_Write(pin6, this.ZEO.PB13);
////////////////////////////////////// // 방법3
// SW7 눌리면 if ((uiData[1] & 0x4000) == 0x4000) this.ZEO.Pin_Set(pin7); else this.ZEO.Pin_Reset(pin7);
// SW8 눌리면 if ((uiData[1] & 0x8000) == 0x8000) this.ZEO.Pin_Set(pin8); else this.ZEO.Pin_Reset(pin8);
} } } |
전체 코드 Visual C# 2008 용
'ZEO 시리즈' 카테고리의 다른 글
ZEO-S PWM 펄스를 Pulse Counter로 읽기, C# (0) | 2013.09.02 |
---|---|
ZEO-S Pulse Counter, C# (0) | 2013.08.29 |
ZEO-S, LED 연속으로 이동, C# (0) | 2013.06.08 |
ZEO-S, LED 이동 속도 조절, C# (0) | 2013.06.06 |
ZEO-S ADC 샘플링 테스트 C# (0) | 2013.04.16 |
댓글을 달아 주세요