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
,

 

먼저 서보모터에 대한 설명을 참조하세요

http://whiteat.com/58171

 

 

 

아날로그 서보 모터 제어

 

ZEO-S 모듈로 간단하게 아날로그 서보 모터를 제어할 수 있습니다.

 

 

 

 

ZEO-S 모듈에 연결

 

ZEO-S 모듈

서보 모터

3.3V (우측 1번핀)

VCC

PA0 (PWMA-1)

Control

GND (우측 20번 핀)

GND

 

서보모터 컨트롤 핀을 ZEO-S 모듈의 PA0 핀에 연결합니다.

서보모터의 VCC 핀은 ZEO-S 모듈의 3.3V에 연결하고 GND끼리 연결합니다.

 

 

 

 

C# 소스 코드

 

먼저 C#으로 작성된 전체 소스를 보면 아래와 같습니다.

 

namespace ZEO_SERVOApp

{

public partial class Form1 : Form

{

// ZEO 모듈 제어용 클래스 선언

ZeoLib ZEO = new ZeoLib();

 

UInt16 MAX_PWMA = 0;

 

public Form1()

{

InitializeComponent();

 

// 트랙바(슬라이드바)의 최소, 최대값 설정

this.trackBar1.Minimum = 700;

this.trackBar1.Maximum = 2300;

}

 

private void Form1_Load(object sender, EventArgs e)

{

// ZEO 모듈 열기

this.ZEO.Open();

 

// ZEO 모듈 초기화

this.ZEO.InitZeo(0);

 

// PWMA에 20mS 의 주기의 PWM 생성

this.ZEO.InitPWMA_US(20000, 10, 20, 20, out MAX_PWMA);

}

 

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

this.ZEO.Close();

}

 

private void trackBar1_Scroll(object sender, EventArgs e)

{

// 700uS 에서 2300uS 의 ON 신호 발생

this.ZEO.UpdatePWMA(1, Convert.ToUInt16((sender as TrackBar).Value));

}

}

}

 

 

윈도우 폼이 열릴 때 ZEO 모듈을 초기화해 주고 (Form1_Load 함수)

윈도우 폼이 닫힐 때 ZEO 모듈을 종료합니다. (Form1_FormClosing 함수)

그리고 트랙바(슬라이드바)를 움직일때마다 PWMA-1번 핀의 ON 값을 변경해 주면 됩니다.

 

 

 

 

서보 모터 제어 동영상

 

아래의 동영상과 같이 ON 값에 따라 -90도 ~ +90도를 회전합니다.

 

 


 

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

 

Posted by WhiteAT
,


서보 모터 종류

 

서보 모터는 아날로그 서보 모터와 디지털 서보모터가 있습니다.

큰 차이점은 아날로그 서보 모터는 지속적인 PWM 신호를 주어야 하고 디지털 서보 모터는 해당 서보모터에서 제공하는 프로토콜에 맞게 시리얼 데이터를 줘야 하는 것입니다.

 

아날로그 서보 모터는 PWM 입력이 없을 경우 모터가 고정(Lock)되지 않고 풀리(UnLock)게 되어 힘을 받지 못하게 되고

디지털 서보 모터는 한번의 데이터 전송으로도 해당 각도를 유지하게 됩니다.

 

아날로그 서보 모터의 경우 제품 특성상 약간의 차이가 있을 수 있으나 대부분 조작 방법이 거의 동일합니다. 반면 디지털 서보 모터의 경우 공급업체에서 제공하는 프로토콜을 참조하여 작성 가능하며 업체마다 약간의 차이가 있을 수 있습니다.

 

 

 


 

아날로그 서보 모터








 

핀 명

설명

VCC

전원 (3~5V)

CONTROL

컨트롤 신호

GND

그라운드

 


 

PWM 20mS의 주기로 ON 시간을 0.7mS ~ 2.3mS로 하여 -90도 ~ +90도 까지 제어할 수 있습니다.

 

아래는 ON 시간에 따른 위치입니다.

 

ON시간: 0.7mS

축의 위치: -90도

 

서보 모터 -90 도서보 모터 -90 도 이동

 

 

 

 

ON시간: 1.5mS

축의 위치: 0도

 

서보 모터 0 도서보 모터 0 도 이동


 

 

 

 

ON시간: 2.3mS

축의 위치: +90도

 

 

서보 모터 90 도서보 모터 90 도 이동

 

 


-90도 +90도 사이의 임의의 각으로 이동하려면 ON 시간을 적당히 조절하면 됩니다. 
전체 움직이는 각도(180도)와 ON 시간의 차이(2.3mS -  0.7ms) 는 1.6mS/180도 = 0.0088888mS/1도 가 됩니다.
(즉, 펄스의 HIGH 시간을 0.0088888ms 늘릴수록 서보모터는 1도씩 더 이동하게 됩니다.)
예를 들어 30도에 위치하려면 
 1.5mS(0도 기준) + 0.26mS(30도 이동하려면  0.0088888*30 = 0.26mS ) 이니 1.76mS 만큼 HIGH 를 유지하면 됩니다.

 

 

 

 

파형은 어떻게 만드나요?

 

PWM 파형은 AVR 이나 PIC 등의 MCU으로 구현할 수 있습니다.

만약 PWM 파형 구현이 어려우시다면 Output 포트를 직접 구동할 수도 있습니다.

 

 

// -90 도 이동 예제

PORT = 1 // ON 시킵

Delay(700);// // 700uS 딜레이

PORT = 0;//

Delay(19300);// 19.3mS 딜레이







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



 

 

'전자/일반' 카테고리의 다른 글

빵판에서 사용 가능한 푸쉬,리셋 스위치  (0) 2016.04.20
USB2.0 MINI 5p cable 미니5핀 케이블  (0) 2016.03.31
다양한 저항 종류 – 용량  (0) 2016.03.31
체리, ON/OFF 제어 방식  (2) 2012.11.28
PID 제어  (4) 2012.11.28
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
,