Вы находитесь на странице: 1из 24

Bi th nghim 2: Thit k Card thu thp d liu vi 8 DI, 8DO, 8AI, 2AO.

Lin kt d liu gia card v PC

Code PIC: printf(lcd_putc,"Welcome to..\nBTN #4");

#include "USB_HID.h" }

#include <lcd.c> void Buttons_Options()

#include <pic18_usb.h> {

#include "usb_desc_hid.h" int Counter = 0;

#include <usb.c> while(TRUE)

int LED1_To_PC = 0; {

int LED2_To_PC = 0; if(Counter < 100)

int LED3_To_PC = 0; {

unsigned char HID_InData[64]; if (!input(PIN_B4))

unsigned char HID_OutData[64]; {

char LED[3] = {'A','B','C'}; while((!input(PIN_B4)));

int i = 0; output_toggle(Pin_Table[i]);

void Message() printf(lcd_putc,"\f");

{ printf(lcd_putc,"Toggling LED %d \n with buttons",i);

printf(lcd_putc,"\f"); }
if (!input(PIN_B5)) if(Attached_HID && Enumerated_HID)

{ {

while((!input(PIN_B5))); output_high(HID_USB_ON);

output_toggle(Pin_Table[i]); }

printf(lcd_putc,"\f"); else

printf(lcd_putc,"Toggling LED %d \n with buttons",i); {

} output_toggle(HID_USB_ON);

} delay_ms(100);

else if(Counter == 100) }

{ }

break; void Clear_HID_Bus(char *var)

} {

Counter ++; int i = 0;

delay_ms(5); while(var[i] != '\0')

} {

} var[i] = '\0';

void Reading_Buttons1() i++; }

{ }

if(!input(PIN_B4)) void Toggling_LEDs()

{} { switch(HID_InData[1])

} {

void Setup_USB_HID() case 0: //LED # 1

{ output_toggle(Pin_Table[HID_InData[1]]);

usb_init(); //inicializamos el USB printf(lcd_putc,"\f");

usb_task(); //habilitaperiferico usb einterrupciones printf(lcd_putc,"Toggling LED %d \nby


USB",HID_InData[1]);
usb_wait_for_enumeration();//esperamos hasta que el PicUSB
sea configurado por el host break;

} case 1: //LED # 2

void Status_USB_HID() output_toggle(Pin_Table[HID_InData[1]]);

{ printf(lcd_putc,"\f");

int1 Attached_HID = 0; printf(lcd_putc,"Toggling LED %d \nby


USB",HID_InData[1]);
int1 Enumerated_HID = 0;
break;
Attached_HID = usb_attached();
case 2:
Enumerated_HID = usb_enumerated();
output_toggle(Pin_Table[HID_InData[1]]);
printf(lcd_putc,"\f"); k++;

printf(lcd_putc,"Toggling LED %d \nby x++;


USB",HID_InData[1]);
}
break;
}
}
void Reading_ADC()
}
{
void Writitng_Messages()
int i= 0;
{
for (i=1;i<9;i++)
int j = 0,k= 0,x=1;
{
char Buffer[64];
set_adc_channel(i-1);
printf(lcd_putc,"\f");
HID_OutData[i-1]=read_adc();
for(j = 1; j<=sizeof(HID_InData); j++)
delay_us(20);
{
}
Buffer[k] = (HID_InData[j]);
/*
if(Buffer[k] == '\0')
switch(LED1_To_PC)
{ break; }
{
else
Case 1:
{
HID_OutData[0] = 1;
if(x > 16)
break;
{
Case 0:
lcd_gotoxy(x-16,2);
HID_OutData[0] = 0;
printf(lcd_putc,"%c",Buffer[k]);
break;
}
}
if(x > 32)
*/
{
usb_put_packet(1,HID_OutData,64,USB_DTS_TOGGLE);
printf(lcd_putc,"\f");
}
printf(lcd_putc,"Overflow!!\n Lack of space");
void Reading_HID()
}
{
else
usb_get_packet(1,HID_InData,64);
{
switch(HID_InData[0])
lcd_gotoxy(x,1);
{
printf(lcd_putc,"%c",Buffer[k]);
case 1: //Toggle LEDS
}
Toggling_LEDs();
}
break;
case 2: //Message. */

Writitng_Messages(); usb_task();

break; Status_USB_HID();

case 3: //Reading ADCs. if(usb_enumerated())

Reading_ADC(); {

break; if(usb_kbhit(1))

} {

} Clear_HID_Bus(HID_InData);

void main() Clear_HID_Bus(HID_OutData);

{ //Get the Data

setup_adc_ports(AN0_TO_AN8|VSS_VDD); Reading_HID();

setup_adc(ADC_CLOCK_INTERNAL); }

setup_vref(FALSE); }

lcd_init(); }

Message(); }

Setup_USB_HID();

while(TRUE)

//output_high(PIN_B0);

/*

if(!input(PIN_B4))

if(LED1_To_PC)

LED1_To_PC =0;

else

LED1_To_PC =1;

}
Code Visual Studio:

using System; richTextBox2.Text +=


using System.Collections.Generic; Environment.NewLine;
using System.ComponentModel; }
using System.Data;
using System.Drawing; Choose(USB_Host.Inputs.DataBuf[9].ToString());
using System.Linq; }
using System.Text;
using System.Windows.Forms; }
using CyUSB;
void USB_Devices_DeviceRemoved(object sender,
namespace TN4 EventArgs e)
{ {
public partial class Form1 : Form USBEventArgs usbEvent = e as USBEventArgs;
{ }
public static int[] PID_USB = new int[10];
public static int[] VID_USB = new int[10]; void USB_Devices_DeviceAttached(object sender,
bool Led1_state = false; EventArgs e)
bool Led2_state = false; {
bool Led3_state = false; USBEventArgs usbEvent = e as USBEventArgs;
bool Led4_state = false; }
bool Led5_state = false;
bool Led6_state = false;
bool Led7_state = false; private void BtSend_Click(object sender,
bool Led8_state = false; EventArgs e)
// public static Int64 ticks = 0; {
int My_Device; string text = richTextBox1.Text;
USBDeviceList USB_Devices; byte[] data =
CyHidDevice USB_Host; System.Text.Encoding.ASCII.GetBytes(text);
var NewArray = new byte[data.Length + 2];
public Form1() data.CopyTo(NewArray, 2);
{ NewArray[1] = 0x02;
InitializeComponent(); data = NewArray;
USB_Devices = new USB_Host.Outputs.DataBuf = data;
USBDeviceList(CyConst.DEVICES_HID); USB_Host.WriteOutput();
USB_Devices.DeviceAttached += new }
EventHandler(USB_Devices_DeviceAttached);
USB_Devices.DeviceRemoved += new
EventHandler(USB_Devices_DeviceRemoved); private void BtReadAdc_Click(object sender,
} EventArgs e)
{
var NewArray = new byte[64];
void Read_HID() NewArray[1] = 0x03;
{ USB_Host.Outputs.DataBuf = NewArray;
if (USB_Host.ReadInput()) USB_Host.WriteOutput();
{ Read_HID();
richTextBox2.Clear(); }
byte[] rcv = USB_Host.Inputs.DataBuf;
for (int i = 1; i < 9; i++)
//USB_Host.Inputs.RptByteLen; private void BtConnect_Click(object sender,
{ EventArgs e)
richTextBox2.Text += {
USB_Host.Inputs.DataBuf[i].ToString(); int i;
for (i = 0; i < USB_Devices.Count; i++)
{ else
USBDevice Devices = USB_Devices[i]; Led2_fcn(false);
//CCS HID Demo }
PID_USB[i] = Devices.ProductID;
VID_USB[i] = Devices.VendorID; private void Led3(object sender, EventArgs e)
if (Devices.Product == "CCS HID Demo") {
{ if (Led3_state == false)
My_Device = i; Led3_fcn(true);
USB_Host = else
USB_Devices[VID_USB[My_Device], PID_USB[My_Device]] as Led3_fcn(false);
CyHidDevice; }
if (USB_Host != null)
{ private void Led4(object sender, EventArgs e)
label1.Text = "OK! ..... "; {
} if (Led4_state == false)
else Led4_fcn(true);
{ else
label1.Text = "OFF ....."; Led4_fcn(false);
} }
}
} private void Led5(object sender, EventArgs e)
{
} if (Led5_state == false)
Led5_fcn(true);
//**************************************************** else
*****************<timer control << Led5_fcn(false);
// Timer control }

//**************************************************** private void Led6(object sender, EventArgs e)


*************************** {
private void BtStart_Click(object sender, if (Led6_state == false)
EventArgs e) Led6_fcn(true);
{ else
int value = 0; Led6_fcn(false);
value = Int16.Parse(textBox1.Text); }
timer1.Interval = value;
timer1.Start(); private void Led7(object sender, EventArgs e)
timer1.Enabled = true; {
} if (Led7_state == false)
private void BtStop_Click(object sender, Led7_fcn(true);
EventArgs e) else
{ Led7_fcn(false);
textBox1.Clear(); }
timer1.Stop();
timer1.Enabled = false; private void Led8(object sender, EventArgs e)
} {
if (Led8_state == false)
private void timer1_Tick(object sender, Led8_fcn(true);
EventArgs e) else
{ Led8_fcn(false);
var NewArray = new byte[64]; }
NewArray[1] = 0x03;
USB_Host.Outputs.DataBuf = NewArray; void Led1_fcn(bool on) // on : led
USB_Host.WriteOutput(); control
Read_HID(); {
} if (on == true)
{
//**************************************************** Shape1.FillColor = Color.Red;
*******************<end timer control << Led1_state = true;
}
//**************************************************** else
*******************<start led function << {
// LED Function Shape1.FillColor = Color.Gray;
Led1_state = false;
//**************************************************** }
*************************** Shape1.FillGradientColor =
private void Led1(object sender, EventArgs e) Color.WhiteSmoke;
{ NArray();
if (Led1_state == false) }
Led1_fcn(true);
else void Led2_fcn(bool on)
Led1_fcn(false); {
} if (on == true )
{
private void Led2(object sender, EventArgs e) Shape2.FillColor = Color.Red;
{ Led2_state = true;
if (Led2_state == false) }
Led2_fcn(true); else
{ Led7_state = true;
Shape2.FillColor = Color.Gray; }
Led1_state = false; else
} {
Shape2.FillGradientColor = Shape7.FillColor = Color.Gray;
Color.WhiteSmoke; Led7_state = false;
NArray(); }
} Shape7.FillGradientColor =
Color.WhiteSmoke;
void Led3_fcn(bool on) NArray();
{ }
if (on == true) void Led8_fcn(bool on)
{ {
Shape3.FillColor = Color.Red; if (on == true)
Led3_state = true; {
} Shape8.FillColor = Color.Red;
else Led8_state = true;
{ }
Shape3.FillColor = Color.Gray; else
Led3_state = false; {
} Shape8.FillColor = Color.Gray;
Shape3.FillGradientColor = Led8_state = false;
Color.WhiteSmoke; }
NArray(); Shape8.FillGradientColor =
} Color.WhiteSmoke;
void Led4_fcn(bool on) NArray();
{ }
if (on == true)
{ void Choose(string text)
Shape4.FillColor = Color.Red; {
Led4_state = true; switch (text)
} {
else case "11": // 1 : led1 ,
{ 1: on
Shape4.FillColor = Color.Gray; Led1_fcn(true);
Led4_state = false; break;
} case "10": // 1 : led1 ,
Shape4.FillGradientColor = 0 : off
Color.WhiteSmoke; Led1_fcn(false);
NArray(); break;
} case "21": // 1 : led2 ,
void Led5_fcn(bool on) 1 : on
{ Led2_fcn(true);
if (on == true) break;
{ case "20": // 2 : led2 ,
Shape5.FillColor = Color.Red; 0 : off
Led5_state = true; Led2_fcn(false);
} break;
else case "31": // 3 : led3 ,
{ 1 : on
Shape5.FillColor = Color.Gray; Led3_fcn(true);
Led5_state = false; break;
} case "30": // 3 : led3 ,
Shape5.FillGradientColor = 0 : off
Color.WhiteSmoke; Led3_fcn(true);
NArray(); break;
} case "41": // 1 : led1 ,
void Led6_fcn(bool on) 0 : off
{ Led4_fcn(true);
if (on == true) break;
{ case "40": // 1 : led1 ,
Shape6.FillColor = Color.Red; 0 : off
Led6_state = true; Led4_fcn(false);
} break;
else case "51": // 1 : led1 ,
{ 0 : off
Shape6.FillColor = Color.Gray; Led5_fcn(true);
Led6_state = false; break;
} case "50": // 1 : led1 ,
Shape3.FillGradientColor = 0 : off
Color.WhiteSmoke; Led5_fcn(false);
NArray(); break;
} case "61": // 1 : led1 ,
void Led7_fcn(bool on) 0 : off
{ Led5_fcn(true);
if (on == true) break;
{ case "60": // 1 : led1 ,
Shape7.FillColor = Color.Red; 0 : off
Led6_fcn(false); }
break;
case "71": // 1 : led1 , }
0 : off
Led7_fcn(true);
break;
case "70": // 1 : led1 ,
0 : off
Led7_fcn(false);
break;
case "81": // 1 : led1 ,
0 : off
Led8_fcn(true);
break;
case "80": // 1 : led1 ,
0 : off
Led8_fcn(false);
break;
}
}
void NArray()
{
var NewArray = new byte[64];
NewArray[1] = 0x01;
NewArray[2] = 0x02;
USB_Host.Outputs.DataBuf = NewArray;
USB_Host.WriteOutput();
}

private void Form1_Load(object sender,


EventArgs e)
{

}
Bi th nghim 3: Thit k Card thu thp d liu vi 8 DI, 8DO, 4AI, 4AO, 2 b

m xung, 2 b pht xung PWM. Lin kt d liu gia card v PC


Code PIC:

#include "usbhid.h" delay_ms(5);

/*---- Include the LCD include file ----*/ }

#include <lcd.c> }

/*---- USB Libraries ----*/ void Reading_Buttons1(){

#include <pic18_usb.h> if (!input(PIN_B6)){

#include "usb_desc_hid.h" delay_ms(100);

#include <usb.c> while((!input(PIN_B6))){

//Declare Variables /*--------------- MENU --------------------*/

unsigned char HID_InData[64]; switch(LED[i]){

unsigned char HID_OutData[64]; case 'A':

char LED[3] = {'A','B','C'};

int i = 0,temp1,temp2,temp3,temp4; Buttons_Options();

INT count1,count2; break;

case 'B':

void Buttons_Options(){

int Counter = 0; Buttons_Options();

while(TRUE){ break;

if(Counter < 100){ case 'C':

if (!input(PIN_B4)){

while((!input(PIN_B4))); Buttons_Options();

output_toggle(Pin_Table[i]); break;

default:

if (!input(PIN_B5)){ break;

while((!input(PIN_B5))); }

output_toggle(Pin_Table[i]); }

i++;

} if(i>2) i = 0;

} }

else if(Counter == 100){ }

break; void Setup_USB_HID(){

} usb_init(); //inicializamos el USB

Counter ++; usb_task(); //habilita periferico usb e interrupciones


usb_wait_for_enumeration(); //esperamos hasta que el PicUSB case 2:
sea
output_toggle(Pin_Table[HID_InData[1]]);
}
break;
void Status_USB_HID(){
case 3:
int1 Attached_HID = 0;
output_toggle(Pin_Table[HID_InData[1]]);
int1 Enumerated_HID = 0;
break;
Attached_HID = usb_attached();
case 4:
Enumerated_HID = usb_enumerated();
output_toggle(Pin_Table[HID_InData[1]]);

break;
if(Attached_HID && Enumerated_HID){
case 5:
output_high(HID_USB_ON);
output_toggle(Pin_Table[HID_InData[1]]);
}
break;
else{
case 6:
output_toggle(HID_USB_ON);
output_toggle(Pin_Table[HID_InData[1]]);
delay_ms(100);
break;
}
case 7:
}
output_toggle(Pin_Table[HID_InData[1]]);

break;
void Clear_HID_Bus(char *var) {
}
int i = 0;
}
while(var[i] != '\0') {
void Writitng_Messages(){
var[i] = '\0';
int j = 0,k= 0,x=1;
i++;
char Buffer[64];
}
printf(lcd_putc,"\f");
}
for(j = 1; j<=sizeof(HID_InData); j++){
void Toggling_LEDs(){
Buffer[k] = (HID_InData[j]);
switch(HID_InData[1])
if(Buffer[k] == '\0'){
{
break;
case 0:
}
output_toggle(Pin_Table[HID_InData[1]]);
else{
break;
if(x > 16){
case 1:
lcd_gotoxy(x-16,2);
output_toggle(Pin_Table[HID_InData[1]]);
printf(lcd_putc,"%c",Buffer[k]);
break;
}
if(x > 32){ void pwm2(){

printf(lcd_putc,"\f"); int j = 0,k= 0,x=1;

//! printf(lcd_putc,"Overflow!!\n Lack of space"); char Buffer[64];

} set_tris_c(PIN_C1);

else{ setup_ccp2(ccp_pwm);

lcd_gotoxy(x,1); setup_timer_2(T2_DIV_BY_16,249,1);

//! printf(lcd_putc,"%c",Buffer[k]); for(j = 1; j<=sizeof(HID_InData); j++)

} {

} Buffer[k] = (HID_InData[j]);

k++; if(Buffer[k] == '\0')

x++; break;

} else{

} if(x > 5)

void pwm1(){ break;

int j = 0,k= 0,x=1; }

char Buffer[64]; k++;

set_tris_c(PIN_C2); x++;

setup_ccp1(ccp_pwm); }

setup_timer_2(T2_DIV_BY_16,249,1); SET_PWM2_DUTY(Buffer[k]);

for(j = 1; j<=sizeof(HID_InData); j++){ }

Buffer[k] = (HID_InData[j]); void Reading_ADC(){

if(Buffer[k] == '\0'){ int i= 0;

break; set_adc_channel(0);

} delay_us(20);

else{ temp1=read_adc();

if(x > 5) set_adc_channel(1);

break; delay_us(20);

} temp2=read_adc();

k++; set_adc_channel(2);

x++; delay_us(20);

} temp3=read_adc();

SET_PWM1_DUTY(Buffer[k]); set_adc_channel(3);

} delay_us(20);

temp4=read_adc();
delay_us(20);

for (i=0;i<64;i++) }

{ usb_put_packet(1,HID_OutData,64,USB_DTS_TOGGLE);

HID_OutData[i]= temp1; }

delay_us(20); void DemXung1()

} {

usb_put_packet(1,HID_OutData,64,USB_DTS_TOGGLE); count1=get_timer0();

for (i=0;i<64;i++)

for (i=0;i<64;i++) {

{ HID_OutData[i]= count1;

HID_OutData[i]= temp2; delay_us(20);

delay_us(20); }

} usb_put_packet(1,HID_OutData,64,USB_DTS_TOGGLE);

usb_put_packet(1,HID_OutData,64,USB_DTS_TOGGLE); }

void Reading_HID(){

for (i=0;i<64;i++) usb_get_packet(1,HID_InData,64);

{ switch(HID_InData[0])

HID_OutData[i]= temp3; {

delay_us(20); case 1: //Toggle LEDS

} Toggling_LEDs();

usb_put_packet(1,HID_OutData,64,USB_DTS_TOGGLE); break;

for (i=0;i<64;i++) case 2: //Message.

{ Writitng_Messages();

HID_OutData[i]= temp4; break;

delay_us(20); case 3: //Reading ADCs.

} Reading_ADC();

usb_put_packet(1,HID_OutData,64,USB_DTS_TOGGLE); break;

} case 4:

void DemXung2() DemXung1();

{ break;

count2=get_timer1(); case 5:

for (i=0;i<64;i++) DemXung2();

{ break;

HID_OutData[i]= count2; case 6:


pwm1(); //Init the USB

break; Setup_USB_HID();

case 7:

pwm2(); while(TRUE)

break; {

} Reading_Buttons1();

usb_task();

void main() Status_USB_HID();

{ if(usb_enumerated())

setup_adc_ports(AN0_TO_AN3|VSS_VDD); {

setup_adc(ADC_CLOCK_INTERNAL); if(usb_kbhit(1))

setup_timer_0(T0_EXT_L_TO_H); {

SET_TIMER0(0); //Clean array for fixing problems

setup_timer_1(T1_EXTERNAL); Clear_HID_Bus(HID_InData);

SET_TIMER1(0); Clear_HID_Bus(HID_OutData);

/*

setup_adc_ports(NO_ANALOGS|VSS_VDD); //Get the Data

setup_adc(ADC_CLOCK_DIV_2); Reading_HID();

setup_spi(SPI_SS_DISABLED); }

setup_wdt(WDT_OFF); }

setup_timer_1(T1_DISABLED); }

setup_timer_2(T2_DISABLED,0,1);

setup_timer_3(T3_DISABLED|T3_DIV_BY_1);

setup_ccp1(CCP_OFF);

setup_comparator(NC_NC_NC_NC);

setup_vref(FALSE);

*/

setup_vref(FALSE);

lcd_init();

//! Message();
{
richTextBox3.Text =
Code Visual studio: USB_Host.Inputs.DataBuf[i].ToString();
}
using System; }
using System.Collections.Generic; }
using System.ComponentModel;
using System.Data; void Read_Counter1_HID()
using System.Drawing; {
using System.Linq; if (USB_Host.ReadInput())
using System.Text; {
using System.Threading.Tasks; richTextBox4.Clear();
using System.Windows.Forms; byte[] rcv =
using CyUSB; USB_Host.Inputs.DataBuf;
namespace CARD2 for (int i = 1; i <
{ USB_Host.Inputs.RptByteLen; i++)
public partial class Form1 : Form {
{ richTextBox4.Text =
public static int[] PID_USB = new USB_Host.Inputs.DataBuf[i].ToString();
int[10]; }
public static int[] VID_USB = new }
int[10]; }
bool Led1_state = false;
bool Led2_state = false; void USB_Devices_DeviceRemoved(object
bool Led3_state = false; sender, EventArgs e)
bool Led4_state = false; {
bool Led5_state = false; USBEventArgs usbEvent = e as
bool Led6_state = false; USBEventArgs;
bool Led7_state = false; }
bool Led8_state = false;
public static Int64 ticks = 0; void USB_Devices_DeviceAttached(object
int My_Device; sender, EventArgs e)
USBDeviceList USB_Devices; {
CyHidDevice USB_Host; USBEventArgs usbEvent = e as
public Form1() USBEventArgs;
{ }
InitializeComponent();
USB_Devices = new
USBDeviceList(CyConst.DEVICES_HID); private void button3_Click(object
USB_Devices.DeviceAttached += new sender, EventArgs e)
EventHandler(USB_Devices_DeviceAttached); {
USB_Devices.DeviceRemoved += new int i;
EventHandler(USB_Devices_DeviceRemoved); for (i = 0; i < USB_Devices.Count;
} i++)
{
void Read_HID() USBDevice Devices =
{ USB_Devices[i]; //CCS HID Demo
if (USB_Host.ReadInput()) PID_USB[i] = Devices.ProductID;
{ VID_USB[i] = Devices.VendorID;
richTextBox2.Clear(); if (Devices.Product == "CCS HID
byte[] rcv = Demo")
USB_Host.Inputs.DataBuf; {
for (int i = 1; i < My_Device = i;
USB_Host.Inputs.RptByteLen; i++) USB_Host =
{ USB_Devices[VID_USB[My_Device],
richTextBox2.Text = PID_USB[My_Device]] as CyHidDevice;
USB_Host.Inputs.DataBuf[i].ToString(); if (USB_Host != null)
} {
} label1.Text = "OK! .....
} ";
void Read_Counter_HID() }
{ else
if (USB_Host.ReadInput()) {
{ label1.Text = "OFF
richTextBox3.Clear(); .....";
byte[] rcv = }
USB_Host.Inputs.DataBuf; }
for (int i = 1; i < }
USB_Host.Inputs.RptByteLen; i++) }
ovalShape1.FillColor =
private void Form1_Load(object sender, Color.Gray;
EventArgs e) ovalShape1.FillGradientColor =
{ Color.WhiteSmoke;
var NewArray = new byte[64];
} NewArray[1] = 0x01;
NewArray[2] = 0x00;
private void button2_Click(object USB_Host.Outputs.DataBuf =
sender, EventArgs e) NewArray;
{ USB_Host.WriteOutput();
string text = richTextBox1.Text; }
byte[] data = Led1_state = !Led1_state;
System.Text.Encoding.ASCII.GetBytes(text); }
var NewArray = new byte[data.Length
+ 2]; private void ovalShape2_Click(object
data.CopyTo(NewArray, 2); sender, EventArgs e)
NewArray[1] = 0x02; {
data = NewArray; if (Led2_state == false)
USB_Host.Outputs.DataBuf = data; {
USB_Host.WriteOutput(); ovalShape2.FillColor =
} Color.Red;
ovalShape2.FillGradientColor =
private void button4_Click(object Color.WhiteSmoke;
sender, EventArgs e) var NewArray = new byte[64];
{ NewArray[1] = 0x01;
var NewArray = new byte[64]; NewArray[2] = 0x01;
NewArray[1] = 0x03; USB_Host.Outputs.DataBuf =
USB_Host.Outputs.DataBuf = NewArray; NewArray;
USB_Host.WriteOutput(); USB_Host.WriteOutput();
Read_HID(); }
} else
{
private void Start_Click(object sender, ovalShape2.FillColor =
EventArgs e) Color.Gray;
{ ovalShape2.FillGradientColor =
int value = 0; Color.WhiteSmoke;
value = Int16.Parse(textBox1.Text); var NewArray = new byte[64];
timer1.Interval = value; NewArray[1] = 0x01;
timer1.Start(); NewArray[2] = 0x01;
timer1.Enabled = true; USB_Host.Outputs.DataBuf =
} NewArray;
USB_Host.WriteOutput();
private void Stop_Click(object sender, }
EventArgs e) Led2_state = !Led2_state;
{ }
textBox1.Clear();
timer1.Stop(); private void ovalShape3_Click(object
timer1.Enabled = false; sender, EventArgs e)
} {
if (Led3_state == false)
private void ovalShape1_Click(object {
sender, EventArgs e) ovalShape3.FillColor =
{ Color.Red;
if (Led1_state == false) ovalShape3.FillGradientColor =
{ Color.WhiteSmoke;
ovalShape1.FillColor = var NewArray = new byte[64];
Color.Red; NewArray[1] = 0x01;
ovalShape1.FillGradientColor = NewArray[2] = 0x02;
Color.WhiteSmoke; USB_Host.Outputs.DataBuf =
var NewArray = new byte[64]; NewArray;
NewArray[1] = 0x01; USB_Host.WriteOutput();
NewArray[2] = 0x00; }
USB_Host.Outputs.DataBuf = else
NewArray; {
USB_Host.WriteOutput(); ovalShape3.FillColor =
Color.Gray;
} ovalShape3.FillGradientColor =
else Color.WhiteSmoke;
{ var NewArray = new byte[64];
NewArray[1] = 0x01; }
NewArray[2] = 0x02; Led5_state = !Led5_state;
USB_Host.Outputs.DataBuf = }
NewArray;
USB_Host.WriteOutput(); private void ovalShape6_Click(object
} sender, EventArgs e)
Led3_state = !Led3_state; {
} if (Led6_state == false)
{
private void ovalShape4_Click(object ovalShape6.FillColor =
sender, EventArgs e) Color.Red;
{ ovalShape6.FillGradientColor =
if (Led4_state == false) Color.WhiteSmoke;
{ var NewArray = new byte[64];
ovalShape4.FillColor = NewArray[1] = 0x01;
Color.Red; NewArray[2] = 0x05;
ovalShape4.FillGradientColor = USB_Host.Outputs.DataBuf =
Color.WhiteSmoke; NewArray;
var NewArray = new byte[64]; USB_Host.WriteOutput();
NewArray[1] = 0x01; }
NewArray[2] = 0x03; else
USB_Host.Outputs.DataBuf = {
NewArray; ovalShape6.FillColor =
USB_Host.WriteOutput(); Color.Gray;
} ovalShape6.FillGradientColor =
else Color.WhiteSmoke;
{ var NewArray = new byte[64];
ovalShape4.FillColor = NewArray[1] = 0x01;
Color.Gray; NewArray[2] = 0x05;
ovalShape4.FillGradientColor = USB_Host.Outputs.DataBuf =
Color.WhiteSmoke; NewArray;
var NewArray = new byte[64]; USB_Host.WriteOutput();
NewArray[1] = 0x01; }
NewArray[2] = 0x03; Led6_state = !Led6_state;
USB_Host.Outputs.DataBuf = }
NewArray;
USB_Host.WriteOutput(); private void ovalShape7_Click(object
} sender, EventArgs e)
Led4_state = !Led4_state; {
} if (Led7_state == false)
{
private void ovalShape5_Click(object ovalShape7.FillColor =
sender, EventArgs e) Color.Red;
{ ovalShape7.FillGradientColor =
if (Led5_state == false) Color.WhiteSmoke;
{ var NewArray = new byte[64];
ovalShape5.FillColor = NewArray[1] = 0x01;
Color.Red; NewArray[2] = 0x06;
ovalShape5.FillGradientColor = USB_Host.Outputs.DataBuf =
Color.WhiteSmoke; NewArray;
var NewArray = new byte[64]; USB_Host.WriteOutput();
NewArray[1] = 0x01; }
NewArray[2] = 0x04; else
USB_Host.Outputs.DataBuf = {
NewArray; ovalShape7.FillColor =
USB_Host.WriteOutput(); Color.Gray;
} ovalShape7.FillGradientColor =
else Color.WhiteSmoke;
{ var NewArray = new byte[64];
ovalShape5.FillColor = NewArray[1] = 0x01;
Color.Gray; NewArray[2] = 0x06;
ovalShape5.FillGradientColor = USB_Host.Outputs.DataBuf =
Color.WhiteSmoke; NewArray;
var NewArray = new byte[64]; USB_Host.WriteOutput();
NewArray[1] = 0x01; }
NewArray[2] = 0x04; Led7_state = !Led7_state;
USB_Host.Outputs.DataBuf = }
NewArray;
USB_Host.WriteOutput();
private void ovalShape8_Click(object data = NewArray;
sender, EventArgs e) USB_Host.Outputs.DataBuf = data;
{ USB_Host.WriteOutput();
if (Led8_state == false) }
{
ovalShape8.FillColor = private void button8_Click(object
Color.Red; sender, EventArgs e)
ovalShape8.FillGradientColor = {
Color.WhiteSmoke; string text = richTextBox7.Text;
var NewArray = new byte[64]; byte[] data =
NewArray[1] = 0x01; System.Text.Encoding.ASCII.GetBytes(text);
NewArray[2] = 0x07; var NewArray = new byte[data.Length
USB_Host.Outputs.DataBuf = + 2];
NewArray; data.CopyTo(NewArray, 2);
USB_Host.WriteOutput(); NewArray[1] = 0x07;
} data = NewArray;
else USB_Host.Outputs.DataBuf = data;
{ USB_Host.WriteOutput();
ovalShape8.FillColor = }
Color.Gray; }
ovalShape8.FillGradientColor = }
Color.WhiteSmoke;
var NewArray = new byte[64];
NewArray[1] = 0x01;
NewArray[2] = 0x07;
USB_Host.Outputs.DataBuf =
NewArray;
USB_Host.WriteOutput();
}
Led8_state = !Led8_state;
}

private void
richTextBox2_TextChanged(object sender,
EventArgs e)
{

private void button1_Click(object


sender, EventArgs e)
{
var NewArray = new byte[64];
NewArray[1] = 0x04;
USB_Host.Outputs.DataBuf = NewArray;
USB_Host.WriteOutput();
Read_Counter_HID();
}

private void button5_Click(object


sender, EventArgs e)
{
var NewArray = new byte[64];
NewArray[1] = 0x05;
USB_Host.Outputs.DataBuf = NewArray;
USB_Host.WriteOutput();
Read_Counter1_HID();
}

private void button7_Click(object


sender, EventArgs e)
{
string text = richTextBox6.Text;
byte[] data =
System.Text.Encoding.ASCII.GetBytes(text);
var NewArray = new byte[data.Length
+ 2];
data.CopyTo(NewArray, 2);
NewArray[1] = 0x06;
Bi th nghim 4: ng dng card 2, vit chng trnh iu khin PID 4 l nhit, chu k ly mu 0.5ms.

Code:

using System; public static Int64 ticks = 0;

using System.Collections.Generic; int My_Device;

using System.ComponentModel; // bool Status_HID = false;

using System.Data; USBDeviceList USB_Devices;

using System.Drawing; CyHidDevice USB_Host;

using System.Linq; double Tsample = 0.5;

using System.Text; int[] uk = new int[4];

using System.Threading.Tasks; int[] uk1 = new int[4];

using System.Windows.Forms; int[] ek = new int[4];

using CyUSB; int[] ek1 = new int[4];

int[] ek2 = new int[4];

namespace USB_HID_Project int temp;

{ double Kp, Ki, Kd;

public partial class Form1 : Form int[] PWM = new int[4];

{ int PWM_time = 0;

public static int[] PID_USB = new int[10]; public Form1()

public static int[] VID_USB = new int[10]; {

bool Led1_state = false; InitializeComponent();

bool Led2_state = false; USB_Devices = new


USBDeviceList(CyConst.DEVICES_HID);
bool Led3_state = false;
USB_Devices.DeviceAttached += new
EventHandler(USB_Devices_DeviceAttached);
private void Led1(object sender, EventArgs e)
USB_Devices.DeviceRemoved += new
EventHandler(USB_Devices_DeviceRemoved); {

} if (Led1_state == false)

void Read_HID() {

{ ovalShape2.FillColor = Color.Red;

if (USB_Host.ReadInput()) var NewArray = new byte[64];

{ NewArray[1] = 0x01;

richTextBox2.Clear(); NewArray[2] = 0x00;

byte[] rcv = USB_Host.Inputs.DataBuf; USB_Host.Outputs.DataBuf = NewArray;

richTextBox2.Clear(); USB_Host.WriteOutput();

for (int i = 1; i < USB_Host.Inputs.RptByteLen; }


i++)
else
{
{
richTextBox2.Text =
ovalShape2.FillColor = Color.Black;
USB_Host.Inputs.DataBuf[i].ToString();

}
var NewArray = new byte[64];
}
NewArray[1] = 0x01;
}
NewArray[2] = 0x00;
void USB_Devices_DeviceRemoved(object sender,
EventArgs e) USB_Host.Outputs.DataBuf = NewArray;
{ USB_Host.WriteOutput();
USBEventArgs usbEvent = e as USBEventArgs; }
//label3.Text = usbEvent.FriendlyName + " Led1_state = !Led1_state;
removed.";
}
}
private void Led2(object sender, EventArgs e)
void USB_Devices_DeviceAttached(object sender,
EventArgs e) {

{ if (Led2_state == false)

USBEventArgs usbEvent = e as USBEventArgs; {

//label4.Text = usbEvent.Device.FriendlyName + " ovalShape3.FillColor = Color.Red;


connected.";
var NewArray = new byte[64];
}
NewArray[1] = 0x01;

NewArray[2] = 0x01;
USB_Host.Outputs.DataBuf = NewArray; USB_Host.Outputs.DataBuf = NewArray;

USB_Host.WriteOutput(); USB_Host.WriteOutput();

} }

else Led3_state = !Led3_state;

{ }

ovalShape3.FillColor = Color.Black;

private void timer1_Tick(object sender, EventArgs e)

var NewArray = new byte[64]; {

NewArray[1] = 0x01; var NewArray = new byte[64];

NewArray[2] = 0x01; NewArray[1] = 0x03;

USB_Host.Outputs.DataBuf = NewArray; USB_Host.Outputs.DataBuf = NewArray;

USB_Host.WriteOutput(); USB_Host.WriteOutput();

} Read_HID();

Led2_state = !Led2_state; }

private void Led3(object sender, EventArgs e) private void btnConnect_Click(object sender,


EventArgs e)
{
{
if (Led3_state == false)
Kp = Convert.ToDouble(txtKp.Text);
{
Ki = Convert.ToDouble(txtKi.Text);
ovalShape4.FillColor = Color.Red;
Kd = Convert.ToDouble(txtKd.Text);
var NewArray = new byte[64];

NewArray[1] = 0x01;
int i;
NewArray[2] = 0x02;
for (i = 0; i < USB_Devices.Count; i++)
USB_Host.Outputs.DataBuf = NewArray;
{
USB_Host.WriteOutput();
USBDevice Devices = USB_Devices[i];
}
PID_USB[i] = Devices.ProductID;
else
VID_USB[i] = Devices.VendorID;
{
if (Devices.Product == "CCS HID Demo")
ovalShape4.FillColor = Color.Black;
{
var NewArray = new byte[64];
My_Device = i;
NewArray[1] = 0x01;

NewArray[2] = 0x02;
USB_Host = }
USB_Devices[VID_USB[My_Device],
PID_USB[My_Device]] as

CyHidDevice; private void btnReadADC_Click(object sender,


EventArgs e)
if (USB_Host != null)
{
{
var NewArray = new byte[64];
LbStatus.Text = "OK! ..... ";
NewArray[1] = 0x03;
ovalShape1.FillColor = Color.Red;
USB_Host.Outputs.DataBuf = NewArray;
timer2.Enabled = true;
USB_Host.WriteOutput();
}
Read_HID();
else
}
{

LbStatus.Text = "OFF .....";


private void btnStopTimer_Click(object sender,
ovalShape1.FillColor = Color.Black; EventArgs e)

timer2.Enabled = false; {

} textBox1.Clear();

} timer1.Stop();

timer1.Enabled = false;

// timer2.Enabled = false; }

// timer3.Enabled = false;

private void btnSend_Click(object sender, EventArgs


e)
}
{
}
string text = richTextBox1.Text;

byte[] data =
System.Text.Encoding.ASCII.GetBytes(text);

private void btnStartTimer_Click(object sender, var NewArray = new byte[data.Length + 2];


EventArgs e)
data.CopyTo(NewArray, 2);
{
NewArray[1] = 0x02;
int value = 0;
data = NewArray;
value = Int16.Parse(textBox1.Text);
USB_Host.Outputs.DataBuf = data;
timer1.Interval = value;
USB_Host.WriteOutput();
timer1.Start();
}
timer1.Enabled = true;
PWM[i] = 5 * uk[i]; // Chuyen sang don vi ms cua
timer
private void Form1_Load(object sender, EventArgs e)

{
ek2[i] = ek1[i];
timer3.Interval = 1;
ek1[i] = ek[i];
timer3.Enabled = false;
uk1[i] = uk[i];
timer2.Interval = 500;
}
timer2.Enabled = false;

timer2.Enabled = false;
}
PWM_time = 0;

timer2.Enabled = true;
private void timer2_Tick(object sender, EventArgs e)

// cp nht gi tr nhit cc knh 1 2 3 4

for (int i = 0; i < 4;i++ )


}
{

Read_ADC(i);
void Read_ADC(int No)
temp = Convert.ToByte(txtValue.Text) * 100/255;
{
ek[i] = Convert.ToInt16(set_temp1.Text) - temp;
var NewArray = new byte[64];

NewArray[1] = (byte)No; // kenh 1 2 3 4


uk[i] = (int)(uk1[i] + Kp * (ek[i] - ek1[i]) + Ki * xuat xuong pic de nhan data
Tsample * (ek[i] + ek1[i]) / 2 + Kd * (ek[i] - 2 * ek1[i] +
ek2[i])/Tsample ); USB_Host.Outputs.DataBuf = NewArray;

if(uk[i] > 100 ) USB_Host.WriteOutput();

uk[i] = 100; // max = 100 % if (USB_Host.ReadInput())

} {

else if(uk[i]<0) txtValue.Clear();

{ byte[] rcv = USB_Host.Inputs.DataBuf;

uk[i]=0; for (int i = 1; i < USB_Host.Inputs.RptByteLen;


i++)
}
{

txtValue.Text =
USB_Host.Inputs.DataBuf[i].ToString();

// uk dc tinh theo gia tri xuat xung PWM tu 0-100%


}

private void timer3_Tick(object sender, EventArgs e)

int PID_out = 0;

for(byte i= 0;i<4;i++)

if (PWM_time < PWM[i])

PID_out = PID_out+ 1<<i;

var NewArray = new byte[64];

NewArray[1] = Convert.ToByte(PID_out|0xF0); //
if data send >= 128 => output for PWM

USB_Host.Outputs.DataBuf = NewArray;

USB_Host.WriteOutput();

Вам также может понравиться