ZEO-S 와 ZEO-IO2 로 8채널 ADC를 모두 사용하는 예제입니다.

 

CDS 8개를 각각 ADC1 ~ ADC8에 연결하고 CDS 밝기에 따라

LED를 ON/OFF 합니다.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

윈도우 프로그램 화면 – 잠깐 동안 CDS를 가렸다가 뗄 경우

 

 

 

 

 

윈도우 프로그램 – CDS를 순차적으로 가린 경우

 

 

 

 

 

   

코드 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;

using System.Diagnostics;

using System.Threading;

 

namespace ADCTest8CH

{

public partial class Form1 : Form

{

// LED 를 그룹으로 지정

PIN_NAME LED1 = PIN_NAME.PA3 | PIN_NAME.PA4;

PIN_NAME LED2 = PIN_NAME.PA5 | PIN_NAME.PA6;

PIN_NAME LED3 = PIN_NAME.PA7 | PIN_NAME.PA8;

PIN_NAME LED4 = PIN_NAME.PA9 | PIN_NAME.PB12;

PIN_NAME LED5 = PIN_NAME.PB0 | PIN_NAME.PB1;

PIN_NAME LED6 = PIN_NAME.PB2 | PIN_NAME.PB3;

PIN_NAME LED7 = PIN_NAME.PB4 | PIN_NAME.PB5;

PIN_NAME LED8 = PIN_NAME.PB6 | PIN_NAME.PB7;

 

// ZEO용 인스턴스

const int ADC_MEM_SIZE = 0x3C00;

ZeoDotNetLib.ZeoLib ZEO1 = new ZeoDotNetLib.ZeoLib();

 

// 그래픽용

GraphControl GraphCtrl;

 

public Form1()

{

InitializeComponent();

 

this.cboADCSpeed.SelectedIndex = 0;

this.cboReadSpeed.SelectedIndex = 0;

}

 

private void Form1_Load(object sender, EventArgs e)

{

GraphCtrl = new GraphControl(new Font("Arial", 12), new Font("Arial", 10), new Font("Arial", 8));

GraphCtrl.TabIndex = 0;

GraphCtrl.Dock = DockStyle.Fill;

GraphCtrl.Name = "ADC1 ";

 

//Add Graph Control to the form

GraphPanel.Controls.Add(GraphCtrl);

 

//Add series

GraphCtrl.AddPointsSerie("ADC1", Axes.VerticalPrimary, " 1", Color.Red);

GraphCtrl.AddPointsSerie("ADC2", Axes.VerticalPrimary, " 2", Color.Green);

GraphCtrl.AddPointsSerie("ADC3", Axes.VerticalPrimary, " 3", Color.Blue);

GraphCtrl.AddPointsSerie("ADC4", Axes.VerticalPrimary, " 4", Color.DarkBlue);

GraphCtrl.AddPointsSerie("ADC5", Axes.VerticalPrimary, " 5", Color.YellowGreen);

GraphCtrl.AddPointsSerie("ADC6", Axes.VerticalPrimary, " 6", Color.SkyBlue);

GraphCtrl.AddPointsSerie("ADC7", Axes.VerticalPrimary, " 7", Color.Gray);

GraphCtrl.AddPointsSerie("ADC8", Axes.VerticalPrimary, " 8", Color.DarkGreen);

 

GraphCtrl.Invalidate();

 

 

if (FStatus.OK == this.ZEO1.Open())

{

this.ZEO1.InitZeo(ADC_MEM_SIZE);

this.lblBoardInfo.Text = "ZEO-" + this.ZEO1.GetZeroType().ToString() + ", " + this.ZEO1.GetBoardVersion().ToString();

 

this.btnStart.Enabled = this.btnStop.Enabled = true;

this.ZEO1.ADCReceived += new eventADCReceived(ZEO_ADCReceived);

this.ZEO1.Pin_DirOutput(LED1 | LED2 | LED3 | LED4 | LED5 | LED6 | LED7 | LED8);

this.ZEO1.Pin_Reset(LED1 | LED2 | LED3 | LED4 | LED5 | LED6 | LED7 | LED8);

}

else

{

this.lblBoardInfo.Text = "open fail";

this.lblBoardInfo.ForeColor = Color.Red;

}

Thread.Sleep(1000);

}

 

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

Thread.Sleep(100);

ZEO1.Close();

}

 

private void btnStart_Click(object sender, EventArgs e)

{

// ADC 8채널 지정

ADCStarter[] _start = new ADCStarter[]{

new ADCStarter(ADCChannel._1,(ADCSpeed1) cboADCSpeed.SelectedIndex),

new ADCStarter(ADCChannel._2,(ADCSpeed1) cboADCSpeed.SelectedIndex),

new ADCStarter(ADCChannel._3,(ADCSpeed1) cboADCSpeed.SelectedIndex),

new ADCStarter(ADCChannel._4,(ADCSpeed1) cboADCSpeed.SelectedIndex),

new ADCStarter(ADCChannel._5,(ADCSpeed1) cboADCSpeed.SelectedIndex),

new ADCStarter(ADCChannel._6,(ADCSpeed1) cboADCSpeed.SelectedIndex),

new ADCStarter(ADCChannel._7,(ADCSpeed1) cboADCSpeed.SelectedIndex),

new ADCStarter(ADCChannel._8,(ADCSpeed1) cboADCSpeed.SelectedIndex),

};

 

this.ZEO1.InitADC1(_start);

this.ZEO1.WaitStandby();

 

}

 

int m_iAdcDiv = 0;

int[] g_iADCData;

 

// ADC 데이터를 받으면

void ZEO_ADCReceived(byte[] bData)

{

 

if (bData == null) return;

if (bData.Length <= 7) return;

 

int iADCChannelCount = this.ZEO1.GetADCChannelCount();

if (iADCChannelCount <= 0) return;

 

UInt16[] uiData = new UInt16[bData.Length / 2];

Buffer.BlockCopy(bData, 0, uiData, 0, bData.Length);

 

g_iADCData = Array.ConvertAll(uiData, new Converter<UInt16, int>(UInt16ToInt32));

 

for (int i = 0; i < g_iADCData.Length; i += (iADCChannelCount * m_iAdcDiv))

{

GraphCtrl.GetPointsSerie("ADC1").AddPoint(g_iADCData[i]);

GraphCtrl.GetPointsSerie("ADC2").AddPoint(g_iADCData[i + 1]);

GraphCtrl.GetPointsSerie("ADC3").AddPoint(g_iADCData[i + 2]);

GraphCtrl.GetPointsSerie("ADC4").AddPoint(g_iADCData[i + 3]);

GraphCtrl.GetPointsSerie("ADC5").AddPoint(g_iADCData[i + 4]);

GraphCtrl.GetPointsSerie("ADC6").AddPoint(g_iADCData[i + 5]);

GraphCtrl.GetPointsSerie("ADC7").AddPoint(g_iADCData[i + 6]);

GraphCtrl.GetPointsSerie("ADC8").AddPoint(g_iADCData[i + 7]);

}

 

// 그래프 refresh

GraphCtrl.Invalidate();

}

 

public static int UInt16ToInt32(UInt16 pf)

{

return Convert.ToInt32(pf);

}

 

private void DisplayResult(string _str)

{

Console.WriteLine(_str);

}

 

private void button2_Click(object sender, EventArgs e)

{

this.ZEO1.DisableADC();

}

 

private void tmrRx_Tick(object sender, EventArgs e)

{

if (g_iADCData == null) return;

 

if (g_iADCData.Length>8)

{

this.ZEO1.Pin_Write(LED1, Convert.ToBoolean(g_iADCData[0] < 750));

this.ZEO1.Pin_Write(LED2, Convert.ToBoolean(g_iADCData[1] < 750));

this.ZEO1.Pin_Write(LED3, Convert.ToBoolean(g_iADCData[2] < 750));

this.ZEO1.Pin_Write(LED4, Convert.ToBoolean(g_iADCData[3] < 750));

this.ZEO1.Pin_Write(LED5, Convert.ToBoolean(g_iADCData[4] < 750));

this.ZEO1.Pin_Write(LED6, Convert.ToBoolean(g_iADCData[5] < 750));

this.ZEO1.Pin_Write(LED7, Convert.ToBoolean(g_iADCData[6] < 750));

this.ZEO1.Pin_Write(LED8, Convert.ToBoolean(g_iADCData[7] < 750));

}

}

 

private void cboReadSpeed_SelectedIndexChanged(object sender, EventArgs e)

{

// 데이터 읽기 간격을 변경하면

m_iAdcDiv = cboReadSpeed.SelectedIndex + 1;

}

 

}

}

 

 

 

 

   

   

전체 코드 Visual C# 2008 용

다운로드

   

출처: http://whiteat.com/bPDS_ZEO/186977 

   

 

 

Posted by WhiteAT
,

 

ZEO-S 의 Pulse Counter 로 스위치 입력을 카운터 해보았습니다. Pulse Counter 로 몇 KHz까지 정확히 읽을 수 있는지 실험해 보겠습니다.

 

ZEO-S 의 PWM (PA11) 과 Pulse Counter (PA12)를 점퍼로 연결합니다. 그러면 PA11의 PWM 출력이 PA12 의 Pulse Counter 로 입력됩니다.

 

 

 

100Khz 의 PWM으로 실험을 진행합니다.

 

 

 

 

 

10초 동안 아래처럼 약 1,000,000 개의 펄스를 입력 받게 됩니다.

 

 

 

 

 

24초 동안 아래처럼 약 24,000,000 개의 펄스를 입력 받게 됩니다.

 

 

 

 

 

 

200Khz 의 PWM에서는 10초 동안 아래처럼 약 2,000,000 개의 펄스를 입력 받게 됩니다.

 

 

200Khz 의 PWM에서도 정확한 카운터를 얻었습니다.

 

 

 

코드 C#

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZeoDotNetLib;


namespace PulseInputFromPWM
{
  public partial class Form1 : Form
  {
    Double dTotalCnt = 0;
    UInt16 lastCnt = 0;
    ZeoLib ZEO = new ZeoLib();
    int iTmrCnt = 0;

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      this.ZEO.Open();
      this.ZEO.InitZeo(0);
      this.label1.Text = this.ZEO.ToString();

      this.ZEO.InitCount(7);
     
      // 2Khz 파형 발생
      //this.ZEO.InitPWMC(PWM_Frequency._2Khz, 0, 0, 0, 0);

      // 100Khz 파형 발생
      this.ZEO.InitPWMC(360-1, 0, 0, 0, 0);
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
      this.ZEO.Close();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
      this.GetCounts();
      this.lblTmr.Text = (iTmrCnt++/5).ToString()+"초 경과";
    }
 
    public void GetCounts()
    {
      if (!this.ZEO.IsOpened) return;

      UInt16   _cnt = 0;
      this.ZEO.GetCount(7, out _cnt);


      if (_cnt - lastCnt >=0)
        dTotalCnt += (_cnt-lastCnt);
      else
        dTotalCnt += (_cnt - lastCnt)+65536;

      lastCnt = _cnt  ;

      this.lblTotalCount.Text = dTotalCnt.ToString();
 
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
      this.ZEO.UpdatePWMC(2, 200);
      iTmrCnt = 0;
      dTotalCnt = 0;
    }

    private void btnStop_Click(object sender, EventArgs e)
    {
      this.ZEO.UpdatePWMC(2, 0);
    }
 

  }
}

 

 

 

 

DC 모터 구동에 필요한 PWM, 스텝모터 구동에 사용되는 PULSE 도 ZEO-S의 Pulse Counter로 감시하게 되면 동작 유무를 쉽게 판단할 수 있고, 데이터 라인에 연결하여 데이터 전송되는지 체크하는데 사용할 수도 있습니다.

 

 

전체 코드 Visual C# 2008 용

 

 다운로드



  

출처: http://whiteat.com/185984

 

 

 

 

 

'ZEO 시리즈' 카테고리의 다른 글

ZEO-S, CDS로 빛 밝기 -> LED ON/OFF  (0) 2013.10.30
ZEO-IO2 BOARD – ZEO IO 실험 보드2  (0) 2013.10.28
ZEO-S Pulse Counter, C#  (0) 2013.08.29
ZEO-S, 스위치로 LED ON/OFF, C#  (0) 2013.06.13
ZEO-S, LED 연속으로 이동, C#  (0) 2013.06.08
Posted by WhiteAT
,

 

ZEO-S 의 Pulse Counter 기능으로 펄스 카운터를 쉽게 구할 수 있습니다.

이 기능은 엔코더 모터 회전 수를 계산하는데 많이 사용되는데, 여기서는 버튼의 눌림을 예로 들었으며

모터 회전수 계산은 다음에 올리도록 하겠습니다.

 

 

 

 

 

준비

 

ZEO-IO BOARD에 연결하여 0.5초 간격으로 스위치 눌림 횟수를 확인해 보는 예제입니다.

Pulse Counter0, 1, 2, 4, 5, 6 이 사용되며 버튼의 눌림 횟수의 누적 값과 현재 값을 화면에 표시해 보겠습니다.

 

 

 

 

 

ZEO-S 와 스위치의 연결 상태는 아래와 같습니다.

 

스위치

ZEO-S

 

SW2

PA13

 

SW3

PA14

 

SW4

PA15

 

SW6

PB13

 

SW7

PB13

 

SW8

PB14

 

 

 

 

SW, SW3, SW4, SW6, SW7, SW8이 각각 눌린 누적 횟수는 15, 17, 19, 8, 28, 16 이며

0.5초 동안에 눌린 횟수(즉, 변화값 )는 2,5,2,4,5,1 입니다.

 

 

 

 

 

 

 

SW, SW3, SW4, SW6, SW7, SW8이 각각 눌린 누적 횟수는 55, 53, 55, 30, 48, 63 이며

0.5초 동안에 눌린 횟수(즉, 변화값)는 3,5,3,7,6,12 입니다.

 

 

 

 

코드 C#

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
 using System.Text;
using System.Windows.Forms;
using ZeoDotNetLib;

namespace OutputTest1
{
  public partial class Form1 : Form
  {
    ZeoLib ZEO = new ZeoLib();
    UInt16[] cntLast = new UInt16[8];// 카운터 변화량 계산을 위해 최근 값을 저장


    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      this.ZEO.Open();
      this.ZEO.InitZeo(0);
      this.label1.Text = this.ZEO.ToString();

      this.ZEO.InitCount(0);
      this.ZEO.InitCount(1);
      this.ZEO.InitCount(2);
      this.ZEO.InitCount(3);
      this.ZEO.InitCount(4);
      this.ZEO.InitCount(5);
      this.ZEO.InitCount(6);
      this.ZEO.InitCount(7);

    }

 
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
      this.ZEO.Close();
     }

    public void GetCounts()
    {
      if (!this.ZEO.IsOpened) return;

      StringBuilder sb = new StringBuilder();
      UInt16[] _cnt = null;


      // 한번에 하나의 카운터만 읽기 (상황에 맞게 선택 사용)
      //UInt16   _cnt2 = 0;
      //this.ZEO.GetCount(0, out _cnt2);

      // 한번에 모든 카운터 읽기  (상황에 맞게 선택 사용)
      this.ZEO.GetCounts(out _cnt);

      this.lblCount0.Text = _cnt[0].ToString() + ".." + (_cnt[0] - cntLast[0]).ToString() + "";
      this.lblCount1.Text = _cnt[1].ToString() + ".." + (_cnt[1] - cntLast[1]).ToString() + "";
      this.lblCount2.Text = _cnt[2].ToString() + ".." + (_cnt[2] - cntLast[2]).ToString() + ""; ;
      this.lblCount4.Text = _cnt[4].ToString() + ".." + (_cnt[4] - cntLast[4]).ToString() + ""; ;
      this.lblCount5.Text = _cnt[5].ToString() + ".." + (_cnt[5] - cntLast[5]).ToString() + ""; ;
      this.lblCount6.Text = _cnt[6].ToString() + ".." + (_cnt[6] - cntLast[6]).ToString() + ""; ;

      Buffer.BlockCopy(_cnt, 0, this.cntLast, 0, cntLast.Length * 2);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
      this.GetCounts();
    }
  }
}

 

 

 

 

 

 

 

 

 

 

 

ReadPort() 와 다른 점

 

이 예제를 보면 ReadPort() 를 사용하여 HIGH, LOW 를 비교하여 카운터해도 됩니다. 하지만 ReadPort()를 사용할 경우 정확한 값을 얻기 힘듭니다. 그 이유는 ReadPort()의 경우 호출되는 순간의 상태를 읽는 것이라 빠르게 변하는 모든 값을 읽기가 거의 불가능하기 때문입니다. 예를 들어 1초에 1000번 정도 상태를 읽으려면 1초에 1000번을 읽어야 하는데 ZEO-S에서는 거의 불가능한 일입니다. 이 때는 Pulse Counter 를 사용해야 합니다.

 

Pulse Counter는 1초에 10000번 이상 변하는 상태도 다 읽어 낼 수 있기 때문입니다.

 

  

전체 코드 Visual C# 2008 용

 

 다운로드



  

출처: http://whiteat.com/185922

 

 

Posted by WhiteAT
,

 

스위치(버튼)의 입력을 받아 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.PA12this.ZEO.Pin_Set(pin1);

else this.ZEO.Pin_Reset(pin1);

 

// SW2 눌리면

if (this.ZEO.PA13this.ZEO.Pin_Set(pin2);

else this.ZEO.Pin_Reset(pin2);

 

// SW3 눌리면

if (this.ZEO.PA14this.ZEO.Pin_Set(pin3);

else this.ZEO.Pin_Reset(pin3);

 

// SW4 눌리면

if (this.ZEO.PA15this.ZEO.Pin_Set(pin4);

else this.ZEO.Pin_Reset(pin4);

 

 

/////////////////////////////////////////////

// 방법2

 

 

// SW5 눌리면

this.ZEO.Pin_Write(pin5this.ZEO.PB12);

 

// SW6 눌리면

this.ZEO.Pin_Write(pin6this.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 용

다운로드

 

출처: http://whiteat.com/163865 

 

 

'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
Posted by WhiteAT
,

 

Timer 를 이용하여 LED 를 연속으로 이동하는 예제입니다.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

동영상

 


 

 

 

 

 

코드 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 ZEO_Flow2

{

public partial class Form1 : Form

{

// LED 상태

int iStatus = 0;

 

// 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.PORT_DirOutputALL();

this.ZEO.Pin_Set(this.ZEO.PIN_ALL);

}

}

 

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;

}

 

switch (iStatus)

{

case 0: this.ZEO.Pin_Reset(PIN_NAME.PA0); break;

case 1: this.ZEO.Pin_Reset(PIN_NAME.PA2); break;

case 2: this.ZEO.Pin_Reset(PIN_NAME.PA4); break;

case 3: this.ZEO.Pin_Reset(PIN_NAME.PA6); break;

 

case 4: this.ZEO.Pin_Set(PIN_NAME.PA0); break;

case 5: this.ZEO.Pin_Reset(PIN_NAME.PA8); break;

case 6: this.ZEO.Pin_Set(PIN_NAME.PA2); break;

case 7: this.ZEO.Pin_Reset(PIN_NAME.PA10); break;

case 8: this.ZEO.Pin_Set(PIN_NAME.PA4); break;

case 9: this.ZEO.Pin_Reset(PIN_NAME.PA16); break;

case 10: this.ZEO.Pin_Set(PIN_NAME.PA6); break;

case 11: this.ZEO.Pin_Reset(PIN_NAME.PA1); break;

 

case 12: this.ZEO.Pin_Set(PIN_NAME.PA8); break;

case 13: this.ZEO.Pin_Reset(PIN_NAME.PA3); break;

case 14: this.ZEO.Pin_Set(PIN_NAME.PA10); break;

case 15: this.ZEO.Pin_Reset(PIN_NAME.PA5); break;

case 16: this.ZEO.Pin_Set(PIN_NAME.PA16); break;

case 17: this.ZEO.Pin_Reset(PIN_NAME.PA7); break;

 

 

case 18: this.ZEO.Pin_Set(PIN_NAME.PA1); break;

case 19: this.ZEO.Pin_Reset(PIN_NAME.PA9); break;

case 20: this.ZEO.Pin_Set(PIN_NAME.PA3); break;

case 21: this.ZEO.Pin_Reset(PIN_NAME.PA11); break;

case 22: this.ZEO.Pin_Set(PIN_NAME.PA5); break;

case 23: this.ZEO.Pin_Reset(PIN_NAME.PB0); break;

 

 

case 24: this.ZEO.Pin_Set(PIN_NAME.PA7); break;

case 25: this.ZEO.Pin_Reset(PIN_NAME.PB2); break;

case 26: this.ZEO.Pin_Set(PIN_NAME.PA9); break;

case 27: this.ZEO.Pin_Reset(PIN_NAME.PB4); break;

case 28: this.ZEO.Pin_Set(PIN_NAME.PA11); break;

case 29: this.ZEO.Pin_Reset(PIN_NAME.PB6); break;

 

case 30: this.ZEO.Pin_Set(PIN_NAME.PB0); break;

case 31: this.ZEO.Pin_Reset(PIN_NAME.PB9); break;

 

case 32: this.ZEO.Pin_Set(PIN_NAME.PB2); break;

case 33: this.ZEO.Pin_Reset(PIN_NAME.PB11); break;

case 34: this.ZEO.Pin_Set(PIN_NAME.PB4); break;

case 35: this.ZEO.Pin_Reset(PIN_NAME.PB1); break;

case 36: this.ZEO.Pin_Set(PIN_NAME.PB6); break;

case 37: this.ZEO.Pin_Reset(PIN_NAME.PB3); break;

case 38: this.ZEO.Pin_Set(PIN_NAME.PB9); break;

case 39: this.ZEO.Pin_Reset(PIN_NAME.PB5); break;

case 40: this.ZEO.Pin_Set(PIN_NAME.PB11); break;

case 41: this.ZEO.Pin_Reset(PIN_NAME.PB7); break;

case 42: this.ZEO.Pin_Set(PIN_NAME.PB1); break;

case 43: this.ZEO.Pin_Reset(PIN_NAME.PB8); break;

case 44: this.ZEO.Pin_Set(PIN_NAME.PB3); break;

case 45: this.ZEO.Pin_Reset(PIN_NAME.PB10); break;

 

case 46: this.ZEO.Pin_Set(PIN_NAME.PB5); break;

case 47: this.ZEO.Pin_Reset(PIN_NAME.PA0); break;

case 48: this.ZEO.Pin_Set(PIN_NAME.PB7); break;

case 49: this.ZEO.Pin_Reset(PIN_NAME.PA2); break;

case 50: this.ZEO.Pin_Set(PIN_NAME.PB8); break;

case 51: this.ZEO.Pin_Reset(PIN_NAME.PA4); break;

case 52: this.ZEO.Pin_Set(PIN_NAME.PB10); break;

default:

iStatus = 2;

 

break;

}

 

iStatus++;

 

}

}

}

 

 

 

전체 코드 Visual C# 2008 용

 

 다운로드



  

출처:  http://docs.whiteat.com/?p=396


'ZEO 시리즈' 카테고리의 다른 글

ZEO-S Pulse Counter, C#  (0) 2013.08.29
ZEO-S, 스위치로 LED ON/OFF, C#  (0) 2013.06.13
ZEO-S, LED 이동 속도 조절, C#  (0) 2013.06.06
ZEO-S ADC 샘플링 테스트 C#  (0) 2013.04.16
ZEO-IO BOARD – ZEO IO 실험 보드  (0) 2013.04.10
Posted by WhiteAT
,

Timer 의 Interval 값을 조정하여 LED 이동속도를 조절하는 예제입니다.

 

 

 

 

 

 

 

 

동영상

 

 


 

 

 

 

코드 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 ZEO_Flow

{

public partial class Form1 : Form

{

const int STEP = 25;

int iStatus = 0;

int NowStep = -STEP;

 

PIN_NAME pin1 = PIN_NAME.PA0 | PIN_NAME.PA2| PIN_NAME.PA4 | PIN_NAME.PA6

| PIN_NAME.PA8 | PIN_NAME.PA10 | PIN_NAME.PA16;

PIN_NAME pin2 = PIN_NAME.PA1 | PIN_NAME.PA3 | PIN_NAME.PA5 | PIN_NAME.PA7

| PIN_NAME.PA9 | PIN_NAME.PA11;

PIN_NAME pin3 = PIN_NAME.PB0 | PIN_NAME.PB2 | PIN_NAME.PB4 | PIN_NAME.PB6

| PIN_NAME.PB9 | PIN_NAME.PB11;

PIN_NAME pin4 = PIN_NAME.PB1 | PIN_NAME.PB3 | PIN_NAME.PB5 | PIN_NAME.PB7

| PIN_NAME.PB8 | PIN_NAME.PB10;

 

ZeoLib ZEO = new ZeoLib();

public Form1()

{

InitializeComponent();

}

 

private void Form1_Load(object sender, EventArgs e)

{

this.ZEO.Open();

this.ZEO.InitZeo(0);

this.ZEO.PORT_DirOutputALL();

this.ZEO.Pin_Set(this.ZEO.PIN_ALL);

}

 

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

this.ZEO.Close();

}

 

private void timer1_Tick(object sender, EventArgs e)

{

if (timer1.Interval < 200)

{

timer1.Interval += NowStep/10;

}

else

{

timer1.Interval += NowStep;

}

 

if (timer1.Interval < 100)

{

NowStep = STEP;

}

else if (timer1.Interval > 1000)

{

NowStep = -STEP;

}

 

 

Console.WriteLine("int:"+timer1.Interval.ToString() +", nowstep:"+NowStep.ToString() );

 

switch (iStatus)

{

case 0:

this.ZEO.Pin_Reset(pin1);

this.ZEO.Pin_Set(pin4);

break;

case 1:

this.ZEO.Pin_Reset(pin2);

this.ZEO.Pin_Set(pin1);

 

break;

case 2:

this.ZEO.Pin_Reset(pin3);

this.ZEO.Pin_Set(pin2);

break;

case 3:

this.ZEO.Pin_Reset(pin4);

this.ZEO.Pin_Set(pin3);

break;

default:

break;

}

 

iStatus = (++iStatus)%4;

}

}

}

 

 

 

전체 코드 Visual C# 2008 용


 다운로드




출처: http://whiteat.com/154626



 

'ZEO 시리즈' 카테고리의 다른 글

ZEO-S, 스위치로 LED ON/OFF, C#  (0) 2013.06.13
ZEO-S, LED 연속으로 이동, C#  (0) 2013.06.08
ZEO-S ADC 샘플링 테스트 C#  (0) 2013.04.16
ZEO-IO BOARD – ZEO IO 실험 보드  (0) 2013.04.10
ZEO-S 모듈로 서보모터 제어  (0) 2012.11.16
Posted by WhiteAT
,

 

ZEO 모듈을 처음 사용하시는 분은 http://whiteat.com/57501 를 참조하여 드라이버를 설치하시고, 프로그래밍 가이드를 따라 해 보시기 바랍니다.

 

 

 

 

하드웨어 연결

 

 

 

데이터시트의 핀명을 참조하여 아래 사진처럼 PWMA-1,2,3, PWMB-1,2,3,4, PWMC-1,2,3,4 에 각각 적색 LED와 1K옴 저항을 연결합니다.

(VCC – 1K옴저항 – 포트로 연결 합니다.)

 

 

 

 

Visual Studio 2008 의 C#으로 PWM11App 라는 응용프로그램을 만들어 보겠습니다.

 

먼저 Windows Forms Application Template 으로 PWM11App 라는 프로젝트를 생성합니다.

C#을 처음 접하시는 분은 http://whiteat.com/31559 를 먼저 해보시기 바랍니다.

 

 

 

 

 

라이브러리 추가 & 기본 코드 추가

http://whiteat.com/product/ZEO/ZEO-Programming_Guide.pdf 의 프로그래밍 가이드를 참조하여 라이브러리를 추가합니다.

 

 

라이브러리를 추가하면 아래와 같이 솔루션 창에 ZeoDotNetLib 와 LibUsbDotNet 이 생성됩니다.

 

 

 

 

 

Form_Load 이벤트와 Form_Closing 이벤트에 각각 ZEO 모듈의 초기화코드와 종료 코드를 추가합니다.

 

 

using ZeoDotNetLib;

 

namespace PWM11App

{

public partial class Form1 : Form

{

ZeoLib ZEO = new ZeoLib();

 

public Form1()

{

InitializeComponent();

}

 

private void Form1_Load(object sender, EventArgs e)

{

this.ZEO.Open();

this.ZEO.InitZeo(0);

this.label1.Text = "ZEO-" + this.ZEO.GetZeroType().ToString();

 

// PWM 초기화 모든 LED 를 최대값으로 ON 한다.

this.ZEO.InitPWMA(PWM_Frequency._2Khz, 0, 0, 0);

this.ZEO.InitPWMB(PWM_Frequency._2Khz, 0, 0, 0,0);

this.ZEO.InitPWMC(PWM_Frequency._2Khz, 0, 0, 0,0);

}

 

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

this.ZEO.Close();

}

}

}

 

 

 

 

컨트롤 추가

11개의 PWM 을 제어하기 위해 11개의 Trackbar ( 슬라이드바)를 추가하고 좌측에 라벨을 붙여 줍니다.

 

 

 

 

 

TrackBar 의 Scroll 이벤트를 걸어 스크롤을 변경할 때마다 LED 밝기를 조절할 수 있습니다.

 

 

private void trbPWMA1_Scroll(object sender, EventArgs e)

{

this.ZEO.SetPWM(ZeoLib.PWM.A,1,Convert.ToUInt16((sender as TrackBar).Value));

}

 

private void trbPWMA2_Scroll(object sender, EventArgs e)

{

this.ZEO.SetPWM(ZeoLib.PWM.A, 2, Convert.ToUInt16((sender as TrackBar).Value));

}

 

private void trbPWMA3_Scroll(object sender, EventArgs e)

{

this.ZEO.SetPWM(ZeoLib.PWM.A, 3, Convert.ToUInt16((sender as TrackBar).Value));

}

 

private void trbPWMB1_Scroll(object sender, EventArgs e)

{

this.ZEO.SetPWM(ZeoLib.PWM.B, 1, Convert.ToUInt16((sender as TrackBar).Value));

}

 

private void trbPWMB2_Scroll(object sender, EventArgs e)

{

this.ZEO.SetPWM(ZeoLib.PWM.B, 2, Convert.ToUInt16((sender as TrackBar).Value));

}

 

private void trbPWMB3_Scroll(object sender, EventArgs e)

{

this.ZEO.SetPWM(ZeoLib.PWM.B, 3, Convert.ToUInt16((sender as TrackBar).Value));

}

 

private void trbPWMB4_Scroll(object sender, EventArgs e)

{

this.ZEO.SetPWM(ZeoLib.PWM.B, 4, Convert.ToUInt16((sender as TrackBar).Value));

}

 

private void trbPWMC1_Scroll(object sender, EventArgs e)

{

this.ZEO.SetPWM(ZeoLib.PWM.C, 1, Convert.ToUInt16((sender as TrackBar).Value));

}

 

private void trbPWMC2_Scroll(object sender, EventArgs e)

{

this.ZEO.SetPWM(ZeoLib.PWM.C, 2, Convert.ToUInt16((sender as TrackBar).Value));

}

 

private void trbPWMC3_Scroll(object sender, EventArgs e)

{

this.ZEO.SetPWM(ZeoLib.PWM.C, 3, Convert.ToUInt16((sender as TrackBar).Value));

}

 

private void trbPWMC4_Scroll(object sender, EventArgs e)

{

this.ZEO.SetPWM(ZeoLib.PWM.C, 4, Convert.ToUInt16((sender as TrackBar).Value));

}

 

 

 

 

 

위와 같은 설정이 되면 아래의 결과를 얻을 수 있습니다.

 

 

 

 

PWMA-1부터 PWMC-4 의 PWM 을 차례대로 제어하는 동영상입니다.

 




Posted by WhiteAT
,

 

ZEO 모듈을 사용하시는 분은 http://whiteat.com/57501 를 참조하여 드라이버를 설치하시고, 프로그래밍 가이드를 따라해 보시기 바랍니다.

 

 

 

C#으로 HumanDetectApp 라는 응용프로그램을 만들어 보겠습니다.

여기서는 Visual Studio 2008 의 C#을 사용하게 됩니다.

 

먼저 Windows Forms Application Template 으로 HumanDetectApp 라는 프로젝트를 생성합니다.

C#을 처음 접하시는 분은 http://whiteat.com/31559 를 한번만 해보셔도 금방 따라 하실 수 있습니다.

 

 

 

 

기본 폼이 완성 되면 LABEL 2개를 추가합니다.

각각의 이름을 lblModel, lblAlarm로 하겠습니다.

lblModel에는 현재 사용중인 ZEO 의 모델명과 버전을 표시할 것이고,

lblAlarm 에는 "침입자 발견" 메시지를 보여줄 것입니다.

 

 

그리고 일정시간마다 센서 값을 읽어 올 수 있게 Timer 를 추가하고 타이머의 이름을 tmrMain 이라고 하겠습니다.

 

 

 

이제 각 기능을 구현하면 됩니다.

 

 

 

센서가 감지되었을 경우 경고 표시

 

 

 

 

평상시 화면

 

 

 

 

 

 

아래는 인체감지 센서를 읽어 알람을 화면에 표시하는 전체 코드입니다.

 

 

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 HumanDetectApp

{

public partial class Form1 : Form

{

ZeoLib ZEO = new ZeoLib();

 

public Form1()

{

InitializeComponent();

}

 

private void Form1_Load(object sender, EventArgs e)

{

// ZEO 모듈 열기

this.ZEO.Open();

 

// ZEO 모듈 초기화

this.ZEO.InitZeo(0);

 

// 모델명 버전 표시

this.lblModel.Text = "MODEL: ZEO-" + this.ZEO.GetZeroType().ToString();

this.lblModel.Text += " VERSION: " + this.ZEO.GetBoardVersion();

 

// PA16을 입력으로 설정 (센서 값 읽기)

this.ZEO.PORT_DirInput(PIN_NAME.PA16);

 

}

 

private void tmrMain_Tick(object sender, EventArgs e)

{

// 만약 ZEO와 연결되지 않았다면 아무것도 안하겠다.

if (!this.ZEO.IsOpened) return;

 

// 모든 포트의 값을 읽어

UInt32[] uiData = this.ZEO.ReadPortAll();

 

// PA16의 값이 HIGH 이면

if ((uiData[0] & 0x10000) == 0x10000)

{

// 알람 표시

lblAlarm.Visible = true;

}

else

{

lblAlarm.Visible = false;

}

}

 

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

// 프로그램 종료시에는 반드시 Close 를 해줘야 한다.

this.ZEO.Close();

}

}

}

 

 

 

 

Posted by WhiteAT
,

 

ZEO 모듈과 인체감지 센서 모듈로 침입자를 감시하는 예제를 만들어 보겠습니다.

 

침입자 감시 시스템

 

 

 

 

회로 꾸미기

 

아래처럼 ZEO-S 모듈과 인체감지모듈을 브레드보드에 연결합니다.

 

 

 

 

센서에는 VCC, GND, OUT 핀 명이 표기되어 있으며

VCC 에는 3.3V(양극)

GND 에는 그라운드(음극)

OUT 에는 ZEO-S 의 PA16번에 연결합니다.

 

GND 옆의 신호가 OUT 이라서 별도의 점퍼선 없이 기판에 바로 연결할 수 있습니다.

센서의 VCC는 ZEO-S의 우측 1번 핀에 연결해 줘야겠죠?

 

 

 

 

 

이제 USB 케이블을 PC에 연결하면

하드웨어 준비는 끝입니다.^^

 

Posted by WhiteAT
,