打印
[菜农助学交流]

第五批-学习之USB通信,温度采集,峰鸣器,定时器笔记

[复制链接]
12114|66
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
yy2bbs|  楼主 | 2011-12-17 17:26 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
大家一定要记得顶楼呀!别只下载不回复呀,那样我很没成就感的

以下为上位机(VS2010版本 C#):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UsbLibrary;

namespace UsbApp
{
public partial class Sniffer : Form
{
private string rec_data_old;
private byte[] commandDate=new byte[66];

public Sniffer()
{
InitializeComponent();
}

private void usb_OnDeviceArrived(object sender, EventArgs e)
{
this.lb_message.Items.Add("找到USB设备!");
}

private void usb_OnDeviceRemoved(object sender, EventArgs e)
{
if (InvokeRequired)
{
Invoke(new EventHandler(usb_OnDeviceRemoved), new object[] { sender, e });
}
else
{
this.lb_message.Items.Add("USB设备移出!");
}
}

private void usb_OnSpecifiedDeviceArrived(object sender, EventArgs e)
{
this.lb_message.Items.Add("USB设备已连接!");
}

protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
usb.RegisterHandle(Handle);
}

protected override void WndProc(ref Message m)
{
usb.ParseMessages(ref m);
base.WndProc(ref m); // pass message on to base form
}

private void btn_ok_Click(object sender, EventArgs e)
{
try
{
this.usb.ProductId = Int32.Parse(this.tb_product.Text, System.Globalization.NumberStyles.HexNumber);
this.usb.VendorId = Int32.Parse(this.tb_vendor.Text, System.Globalization.NumberStyles.HexNumber);
this.usb.CheckDevicePresent();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void btn_send_Click(object sender, EventArgs e)
{
try
{
string text = this.tb_send.Text + " ";
text.Trim();
string[] arrText = text.Split(' ');
byte[] data = new byte[65];
data[0] = 0x0;
for (int i = 0; i < arrText.Length; i++)
{
if (arrText != "")
{
int value = Int32.Parse(arrText, System.Globalization.NumberStyles.HexNumber);
data[i+1] = (byte)Convert.ToByte(value);
}
}

if (this.usb.SpecifiedDevice != null)
{
this.usb.SpecifiedDevice.SendData(data);
}
else
{
MessageBox.Show("Sorry but y**ice is not present. Plug it in!! ");
}

}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void usb_OnSpecifiedDeviceRemoved(object sender, EventArgs e)
{
if (InvokeRequired)
{
Invoke(new EventHandler(usb_OnSpecifiedDeviceRemoved), new object[] { sender, e });
}
else
{
this.lb_message.Items.Add("USB设备移出!");
}
}

private void usb_OnDataRecieved(object sender, DataRecievedEventArgs args)
{
if (InvokeRequired)
{
try
{
Invoke(new DataRecievedEventHandler(usb_OnDataRecieved), new object[] { sender, args });
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
else
{

string rec_data = "Data: ";
foreach (byte myData in args.data)
{
if (myData.ToString().Length == 1)
{
rec_data += "00";
}

if (myData.ToString().Length == 2)
{
rec_data += "0";
}

rec_data += myData.ToString() + " ";
}
if (this.rec_data_old != rec_data)
{

this.rec_data_old = rec_data;
//this.lb_read.Items.Insert(0, rec_data);
commandSet(args);
}
}
}

private void usb_OnDataSend(object sender, EventArgs e)
{
//this.lb_message.Items.Add("Some data was send");
}
private void commandSet(DataRecievedEventArgs args)
{
//处理返回的数据 返回的数据从第2(1)位开始
// this.lb_read.Items.Insert(0, this.rec_data_old);
switch(args.data[1])
{
case 1:
//获取
switch (args.data[2])
{
case 1:
//温度
now_temperature.Text = args.data[4].ToString()+"." + (args.data[5]>9?args.data[5].ToString():"0"+args.data[5].ToString ()) + " 度";
break;
case 2:
//管脚电平
if (args.data[4] == 0 && args.data[5] == 2)
{
this.LED1.Checked = args.data[6]==1?false:true;
}
else if (args.data[4] == 0 && args.data[5] == 3)
{
this.LED2.Checked = args.data[6] == 1 ? false : true;
}
else if (args.data[4] == 0 && args.data[5] == 4)
{
this.LED3.Checked = args.data[6] == 1 ? false : true;
}
else if (args.data[4] == 0 && args.data[5] == 5)
{
this.LED4.Checked = args.data[6] == 1 ? false : true;
}
else
{
this.lb_read.Items.Insert(0, this.rec_data_old);
}
break;
}
break;
case 2:
//设置
break;
default:
//无法识别的指令
this.lb_read.Items.Insert(0, this.rec_data_old);
break;
}
}

private void get_temperature_Tick(object sender, EventArgs e)
{
//自动获取温度数据
this.commandDate[1] = 0x1; //指令 1获取 2设置
this.commandDate[2] = 0x1; //类型 1温度 2LED灯
this.SendData();
}

private void read_temperature_CheckedChanged(object sender, EventArgs e)
{
if (this.read_temperature.Checked)
{
get_temperature.Enabled = true;

}
else
{
get_temperature.Enabled = false;
}
}

private void SendData()
{
try
{
if (this.usb.SpecifiedDevice != null)
{
this.usb.SpecifiedDevice.SendData(this.commandDate);
}
else
{
//MessageBox.Show("Sorry but y**ice is not present. Plug it in!! ");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void LED1_CheckedChanged(object sender, EventArgs e)
{
//LED1~LED4 are defined GPA2~GPA5

this.commandDate[1] = 0x2; //指令 1获取 2设置
this.commandDate[2] = 0x2; //类型 1温度 2LED灯
if(this.LED1.Checked)
this.commandDate[3] = 0x0; //操作 0开灯 1关灯
else
this.commandDate[3] = 0x1; //操作 0开灯 1关灯

//port设置
this.commandDate[4] = 0x0; //类型 0为GPIOA 1为GPIOB 2为GPIOC 3为GPIOD ....
this.commandDate[5] = 0x2; //类型 0该区0脚,1为该区1脚 .........
this.SendData();
}

private void LED2_CheckedChanged(object sender, EventArgs e)
{
//LED1~LED4 are defined GPA2~GPA5

this.commandDate[1] = 0x2; //指令 1获取 2设置
this.commandDate[2] = 0x2; //类型 1温度 2LED灯
if (this.LED2.Checked)
this.commandDate[3] = 0x0; //操作 0开灯 1关灯
else
this.commandDate[3] = 0x1; //操作 0开灯 1关灯
//port设置
this.commandDate[4] = 0x0; //类型 0为GPIOA 1为GPIOB 2为GPIOC 3为GPIOD ....
this.commandDate[5] = 0x3; //类型 0该区0脚,1为该区1脚 .........
this.SendData();
}

private void LED3_CheckedChanged(object sender, EventArgs e)
{
//LED1~LED4 are defined GPA2~GPA5

this.commandDate[1] = 0x2; //指令 1获取 2设置
this.commandDate[2] = 0x2; //类型 1温度 2LED灯
if (this.LED3.Checked)
this.commandDate[3] = 0x0; //操作 0开灯 1关灯
else
this.commandDate[3] = 0x1; //操作 0开灯 1关灯
//port设置
this.commandDate[4] = 0x0; //类型 0为GPIOA 1为GPIOB 2为GPIOC 3为GPIOD ....
this.commandDate[5] = 0x4; //类型 0该区0脚,1为该区1脚 .........
this.SendData();
}

private void LED4_CheckedChanged(object sender, EventArgs e)
{
//LED1~LED4 are defined GPA2~GPA5

this.commandDate[1] = 0x2; //指令 1获取 2设置
this.commandDate[2] = 0x2; //类型 1温度 2LED灯
if (this.LED4.Checked)
this.commandDate[3] = 0x0; //操作 0开灯 1关灯
else
this.commandDate[3] = 0x1; //操作 0开灯 1关灯
//port设置
this.commandDate[4] = 0x0; //类型 0为GPIOA 1为GPIOB 2为GPIOC 3为GPIOD ....
this.commandDate[5] = 0x5; //类型 0该区0脚,1为该区1脚 .........
this.SendData();
}

private void set_temperature_Click(object sender, EventArgs e)
{
//warn_temperature
this.commandDate[1] = 0x2; //指令 1获取 2设置
this.commandDate[2] = 0x1; //类型 1温度 2LED灯

try
{
string text = this.warn_temperature.Text + " ";
text.Trim();
string[] arrText = text.Split(' ');

for (int i = 0; i < arrText.Length; i++)
{
if (arrText != "")
{
int value = Int32.Parse(arrText, System.Globalization.NumberStyles.Number);
this.commandDate[i + 3] = (byte)Convert.ToByte(value);
}
}

this.SendData();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
评分
参与人数 1威望 +10 收起 理由
hotpower + 10

相关帖子

沙发
缥缈九哥| | 2011-12-17 17:48 | 只看该作者
顶起。

使用特权

评论回复
板凳
yy2bbs|  楼主 | 2011-12-17 17:56 | 只看该作者
下位机程序:
以为下HID_API.C
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include
#include
#include
#include "Driver\DrvUSB.h"
#include "Driver\DrvTimer.h"
#include "Driver\DrvSYS.h"

#include "HIDSysIO.h"
#include "HID_API.h"
#include "18B20\ds18b20.h" //温度传感器

#define HID_FUNCTION 0

/* Define the vendor id and product id */
#define USB_VID 0x0416+0x104 // 0x051A
#define USB_PID 0x501B+0x100 // 0x511B

#define HID_DEFAULT_INT_IN_INTERVAL 1
#define HID_IS_SELF_POWERED 0
#define HID_IS_REMOTE_WAKEUP 0
#define HID_MAX_POWER 50 /* The unit is in 2mA. ex: 50 * 2mA = 100mA */

//#define HID_REPORT_SIZE 64 /* 14 */
#define HID_REPORT_SIZE HID_MAX_PACKET_SIZE_EP0 /* The unit is in bytes */
#define LEN_CONFIG_AND_SUBORDINATE (LEN_CONFIG+LEN_INTERFACE+LEN_HID+LEN_ENDPOINT*2)

unsigned char DS18B20_ID[8]={0,0,0,0,0,0,0,0};
unsigned char NOW_TEMP[6]={0};
static uint8_t temperature_warn[3]={20,21,22};//定义及初始化报警温度(共分三级报警)
uint32_t uTimerEventNo[4];//环境句柄
uint8_t temperature_warnNo=0x0;//当前报警级别

extern S_HID_DEVICE g_HID_sDevice;

const uint8_t g_HID_au8DeviceReportDescriptor[] =
{
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x00, // USAGE (0)
0xa1, 0x01, // COLLECTION (Application)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0xff, // LOGICAL_MAXIMUM (255)
0x19, 0x01, // USAGE_MINIMUM (1)
0x29, 0x08, // USAGE_MAXIMUM (8)
0x95, HID_MAX_PACKET_SIZE_EP0, // REPORT_COUNT (8)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x19, 0x01, // USAGE_MINIMUM (1)
0x29, 0x08, // USAGE_MAXIMUM (8)
0x91, 0x02, // OUTPUT (Data,Var,Abs)
0xc0 // END_COLLECTION
};

#define HID_DEVICE_REPORT_DESCRIPTOR_SIZE \
sizeof (g_HID_au8DeviceReportDescriptor) / sizeof(g_HID_au8DeviceReportDescriptor[0])
const uint32_t g_HID_u32DeviceReportDescriptorSize = HID_DEVICE_REPORT_DESCRIPTOR_SIZE;


#define HID_REPORT_DESCRIPTOR_SIZE HID_DEVICE_REPORT_DESCRIPTOR_SIZE

static uint8_t g_au8DeviceReport[HID_REPORT_SIZE];
static const uint32_t g_u32DeviceReportSize = sizeof(g_au8DeviceReport) / sizeof(g_au8DeviceReport[0]);

const uint8_t gau8DeviceDescriptor[] =
{
LEN_DEVICE, // bLength
DESC_DEVICE, // bDescriptorType
0x10, 0x01, // bcdUSB
0x00, // bDeviceClass
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
HID_MAX_PACKET_SIZE_EP0, // bMaxPacketSize0
// idVendor
USB_VID & 0x00FF,
(USB_VID & 0xFF00) >> 8,
// idProduct
USB_PID & 0x00FF,
(USB_PID & 0xFF00) >> 8,
0x00, 0x00, // bcdDevice
0x01, // iManufacture
0x02, // iProduct
0x03, // iSerialNumber
0x01 // bNumConfigurations
};

const uint8_t gau8ConfigDescriptor[] =
{
LEN_CONFIG, // bLength
DESC_CONFIG, // bDescriptorType
// wTotalLength
LEN_CONFIG_AND_SUBORDINATE & 0x00FF,
(LEN_CONFIG_AND_SUBORDINATE & 0xFF00) >> 8,
0x01, // bNumInterfaces
0x01, // bConfigurationValue
0x00, // iConfiguration
0x80 | (HID_IS_SELF_POWERED << 6) | (HID_IS_REMOTE_WAKEUP << 5),// bmAttributes
HID_MAX_POWER, // MaxPower

// I/F descr: HID
LEN_INTERFACE, // bLength
DESC_INTERFACE, // bDescriptorType
0x00, // bInterfaceNumber
0x00, // bAlternateSetting
0x02, // bNumEndpoints
0x03, // bInterfaceClass
0x00, // bInterfaceSubClass
0x00, // bInterfaceProtocol
0x00, // iInterface

// HID Descriptor
LEN_HID, // Size of this descriptor in UINT8s.
DESC_HID, // HID descriptor type.
0x10, 0x01, // HID Class Spec. release number.
0x00, // H/W target country.
0x01, // Number of HID class descriptors to follow.
DESC_HID_RPT, // Dscriptor type.
// Total length of report descriptor.
HID_REPORT_DESCRIPTOR_SIZE & 0x00FF,
(HID_REPORT_DESCRIPTOR_SIZE & 0xFF00) >> 8,

// EP Descriptor: interrupt in.
LEN_ENDPOINT, // bLength
DESC_ENDPOINT, // bDescriptorType
HID_IN_EP_NUM | EP_INPUT, // bEndpointAddress
EP_INT, // bmAttributes
// wMaxPacketSize
HID_MAX_PACKET_SIZE_EP1 & 0x00FF,
(HID_MAX_PACKET_SIZE_EP1 & 0xFF00) >> 8,
HID_DEFAULT_INT_IN_INTERVAL, // bInterval

// EP Descriptor: interrupt out.
LEN_ENDPOINT, // bLength
DESC_ENDPOINT, // bDescriptorType
HID_OUT_EP_NUM | EP_OUTPUT, // bEndpointAddress
EP_INT, // bmAttributes
// wMaxPacketSize
HID_MAX_PACKET_SIZE_EP2 & 0x00FF,
(HID_MAX_PACKET_SIZE_EP2 & 0xFF00) >> 8,
HID_DEFAULT_INT_IN_INTERVAL // bInterval

};

const uint8_t gau8StringLang[4] =
{
4, // bLength
DESC_STRING, // bDescriptorType
0x09, 0x04
};

const uint8_t gau8VendorStringDescriptor[] =
{
16,
DESC_STRING,
'N', 0,
'U', 0,
'V', 0,
'O', 0,
'T', 0,
'O', 0,
'N', 0
};

const uint8_t gau8ProductStringDescriptor[] =
{
22,
DESC_STRING,
'U', 0,
'S', 0,
'B', 0,
' ', 0,
'H', 0,
'I', 0,
'D', 0,
' ', 0,
'I', 0,
'O', 0
};

const uint8_t gau8StringSerial[26] =
{
26, // bLength
DESC_STRING, // bDescriptorType
'B', 0,
'0', 0,
'2', 0,
'0', 0,
'0', 0,
'6', 0,
'0', 0,
'9', 0,
'2', 0,
'1', 0,
'1', 0,
'4', 0
};

void HID_Init(void *pfGetInReport, void *pfSetOutReport)
{
/* Open HID to initial the descriptors and control handlers */
HID_Open(HID_REPORT_SIZE, pfGetInReport, pfSetOutReport);

/* Set the HID report descriptor */
HID_SetReportDescriptor(g_HID_au8DeviceReportDescriptor, g_HID_u32DeviceReportDescriptorSize);

/* Set the HID report buffer */
HID_SetReportBuf(g_au8DeviceReport, g_u32DeviceReportSize);
}


/* Data for input & output */
static uint8_t g_u8CmdIndex = 0;
void USB_SendBackData(uint8_t bError, const uint8_t *pu8Buffer, uint32_t u32Size)
{
uint8_t *pu8EpBuf;
pu8EpBuf = (uint8_t *) & g_au8DeviceReport;
if (u32Size > sizeof(g_au8DeviceReport))
u32Size = sizeof(g_au8DeviceReport);

pu8EpBuf[0] = (g_u8CmdIndex & (uint8_t)0x7F) | (bError ? (uint8_t)0x80 : (uint8_t)0x00);
pu8EpBuf[1] = 11;

memcpy(&pu8EpBuf[2], pu8Buffer, 62);
//DrvUSB_DataIn(0,g_au8DeviceReport,1);
}
//====== 事务处理函数 ==================================
//*************** 温度报警相关模块 ***********************
void TMR1_Callback()
{
DrvGPIO_Open(E_GPB, 10, E_IO_OUTPUT);
DrvGPIO_SetBit(E_GPB, 10);
DrvSYS_Delay(100);
DrvGPIO_Open(E_GPB, 10, E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPB, 10);
}
void SetTemperatureTimer(const uint8_t buf)
{
if(uTimerEventNo[1]>0)
{
DrvTIMER_ClearTimerEvent(E_TMR1, uTimerEventNo[1]); //设置中断响应函数
DrvTIMER_Close(E_TMR1); //打开中断??
}
if(temperature_warnNo>0)
{
DrvTIMER_Open(E_TMR1, buf*10, E_PERIODIC_MODE); //打开中断??
uTimerEventNo[1] = DrvTIMER_SetTimerEvent(E_TMR1, 1, (TIMER_CALLBACK)TMR1_Callback, 0); //设置中断响应函数
DrvTIMER_EnableInt(E_TMR1);
DrvTIMER_Start(E_TMR1);
}

}
void Set_temperature_warn(const uint8_t * buf)
{
//设置报警温度(共分为三级)目前只实现1级
temperature_warn[0] = buf[2];
temperature_warn[1] = buf[3];
temperature_warn[2] = buf[4];
}

void TMR0_Callback()
{
//执行温度轮询比较操作
unsigned int temp=0;
unsigned char temp_H=0;
get_rom(DS18B20_ID);
temp = readTempDS18B20();
temp_H = temp/100;
if(temp_H>=temperature_warn[0])
{
DrvGPIO_Open(E_GPB, 10, E_IO_OUTPUT);
DrvGPIO_SetBit(E_GPB, 10);
}
else
{
DrvGPIO_Open(E_GPB, 10, E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPB, 10);
}

}
//*************** 温度报警相关模块 ***********************
void get_temperature() //获取温度控制
{
unsigned int temp=0;
unsigned char temp_H=0;
unsigned char temp_L=0;
uint8_t pu8Buffer[62] = {0};//输出用的数组(含指令及数据)

pu8Buffer[0] = 0x1; //指令 1获取 1设置
pu8Buffer[1] = 0x1; //类型 1温度
pu8Buffer[2] = 0x1; //数据类型 1整数 2十六进制整数 3字符 0数据结束

get_rom(DS18B20_ID);
temp = readTempDS18B20();
temp_H = temp/100;
temp_L = temp%100;

//3及之后的数据为数据内容
pu8Buffer[3] = temp_H;
pu8Buffer[4] = temp_L;
memcpy(g_au8DeviceReport,pu8Buffer,62);//输出到上位机
}

void get_portBit(const uint8_t * buf)
{
uint8_t pu8Buffer[62] = {0};
pu8Buffer[0] = 0x1; //指令 获取
pu8Buffer[1] = 0x2; //类型 管脚电平
pu8Buffer[2] = 0x1; //返回数据类型 1整数
pu8Buffer[3] = buf[3];
pu8Buffer[4] = buf[4];

switch(pu8Buffer[3])
{
case 0:
DrvGPIO_Open(E_GPA, pu8Buffer[4], E_IO_INPUT);
pu8Buffer[5] = DrvGPIO_GetBit(E_GPA, pu8Buffer[4]);
break;
case 1:
DrvGPIO_Open(E_GPB, pu8Buffer[4], E_IO_INPUT);
pu8Buffer[5] = DrvGPIO_GetBit(E_GPB, pu8Buffer[4]);
break;
case 2:
DrvGPIO_Open(E_GPC, pu8Buffer[4], E_IO_INPUT);
pu8Buffer[5] = DrvGPIO_GetBit(E_GPC, pu8Buffer[4]);
break;
case 3:
DrvGPIO_Open(E_GPD, pu8Buffer[4], E_IO_INPUT);
pu8Buffer[5] = DrvGPIO_GetBit(E_GPD, pu8Buffer[4]);
break;
}
memcpy(g_au8DeviceReport,pu8Buffer,62);//输出到上位机
}

void set_portBit(uint8_t *pu8Buffer)
{
if(pu8Buffer[2] == 0)
{
//点灯
//LED5~LED8 are defined GPA2~GPC5
pu8Buffer[5] = 0x0;
switch(pu8Buffer[3])
{
case 0:
DrvGPIO_Open(E_GPA, pu8Buffer[4], E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPA, pu8Buffer[4]);
break;
case 1:
DrvGPIO_Open(E_GPB, pu8Buffer[4], E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPB, pu8Buffer[4]);
break;
case 2:
DrvGPIO_Open(E_GPC, pu8Buffer[4], E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPC, pu8Buffer[4]);
break;
case 3:
DrvGPIO_Open(E_GPD, pu8Buffer[4], E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPD, pu8Buffer[4]);
break;
}
}else{
//灭灯
pu8Buffer[5] = 0x1;
switch(pu8Buffer[3])
{
case 0:
DrvGPIO_Open(E_GPA, pu8Buffer[4], E_IO_OUTPUT);
DrvGPIO_SetBit(E_GPA, pu8Buffer[4]);
break;
case 1:
DrvGPIO_Open(E_GPB, pu8Buffer[4], E_IO_OUTPUT);
DrvGPIO_SetBit(E_GPB, pu8Buffer[4]);
break;
case 2:
DrvGPIO_Open(E_GPC, pu8Buffer[4], E_IO_OUTPUT);
DrvGPIO_SetBit(E_GPC, pu8Buffer[4]);
break;
case 3:
DrvGPIO_Open(E_GPD, pu8Buffer[4], E_IO_OUTPUT);
DrvGPIO_SetBit(E_GPD, pu8Buffer[4]);
break;
}
}
pu8Buffer[0] = 0x1;
pu8Buffer[1] = 0x2;
pu8Buffer[2] = 0x1;

memcpy(g_au8DeviceReport,pu8Buffer,62);//输出到上位机
}


//====== 事务处理函数 ======================

/* This callback will be executed whenever the interrupt IN transaction is received from HOST to get
report. */

void HID_GetInReport(uint8_t *buf)
{

}

/* This callback will be executed whenever the interrupt OUT transaction is received from HOST to set
report. */
void HID_SetOutReport(uint8_t *pu8EpBuf)
{
static uint8_t pu8Buffer[62] = {0};
memcpy(pu8Buffer, pu8EpBuf, 62);

switch(pu8Buffer[0])
{
case 1:
//获取指令
switch(pu8Buffer[1])
{
case 1:
//温度控制
get_temperature();
break;
case 2:
//获取针脚电平状态
get_portBit((uint8_t *)&pu8Buffer);
break;
}
break;
case 2:
//设置指令
switch(pu8Buffer[1])
{
case 1:
//温度控制
Set_temperature_warn((uint8_t *)&pu8Buffer);
break;
case 2:
//LED灯控制
set_portBit((uint8_t *)&pu8Buffer);
break;
}
break;
default:
//无法识别的指令
memcpy(g_au8DeviceReport,pu8Buffer,62);
break;
}

//memcpy(g_au8DeviceReport,pu8Buffer,62);

//memcpy(&pu8EpBuf[5], pu8Buffer, 62);
//uint32_t u32Size = (uint32_t)pu8EpBuf[1];
//memcpy(au8CmdBuffer, pu8EpBuf, 62);
//USB_SendBackData(FALSE , (const uint8_t *)&pu8Buffer, 0);

//Set new index
//g_u8CmdIndex = pu8EpBuf[0];

//V6M_ProcessCommand((uint8_t *)&pu8EpBuf[2], u32Size);
}


int32_t HID_MainProcess(void)
{
int32_t i32Ret = E_SUCCESS;
E_DRVUSB_STATE eUsbState;

DrvGPIO_Open(E_GPC, 0, E_IO_OUTPUT);
DrvGPIO_Open(E_GPC, 1, E_IO_OUTPUT);
//DrvGPIO_Open(E_GPC, 2, E_IO_OUTPUT);
//DrvGPIO_Open(E_GPC, 3, E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPC, 0);
DrvGPIO_ClrBit(E_GPC, 1);
//DrvGPIO_ClrBit(E_GPC, 2);
//DrvGPIO_ClrBit(E_GPC, 3);

DrvTIMER_Init();
// DrvTIMER_OpenCounter((E_TIMER_CHANNEL)u8TimerCH, u32Boundary, E_ONESHOT_MODE);
// DrvTIMER_SetTimerEvent((E_TIMER_CHANNEL)u8TimerCH, 1, (TIMER_CALLBACK)TMR_CounterCallback, 0);
// DrvTIMER_EnableInt((E_TIMER_CHANNEL)u8TimerCH);
// DrvTIMER_StartCounter((E_TIMER_CHANNEL)u8TimerCH);
DrvTIMER_Open(E_TMR0, 1, E_PERIODIC_MODE); //打开中断??
uTimerEventNo[0]=DrvTIMER_SetTimerEvent(E_TMR0, 1, (TIMER_CALLBACK)TMR0_Callback, 0); //设置中断响应函数
DrvTIMER_EnableInt(E_TMR0);
DrvTIMER_Start(E_TMR0);

i32Ret = DrvUSB_Open(0);
if(i32Ret != E_SUCCESS)
return i32Ret;

while(1)
{
/* Disable USB-related interrupts. */
_DRVUSB_ENABLE_MISC_INT(0);

/* Enable float-detection interrupt. */
_DRVUSB_ENABLE_FLDET_INT();

// Wait for USB connected.
while (1)
{
// Order here is significant.
// Give a chance to handle remaining events before exiting this loop.
eUsbState = DrvUSB_GetUsbState();

//DrvUSB_DispatchEvent();

if (eUsbState >= eDRVUSB_ATTACHED &&
eUsbState != eDRVUSB_SUSPENDED)
{
break;
}
}

/* Start HID and install the callback functions to handle in/out report */
HID_Init((void *)HID_GetInReport, (void *)HID_SetOutReport);

// Enable USB-related interrupts.
_DRVUSB_ENABLE_MISC_INT(INTEN_WAKEUP | INTEN_WAKEUPEN | INTEN_FLDET | INTEN_USB | INTEN_BUS);

// Poll and handle USB events.
while(1)
{
eUsbState = DrvUSB_GetUsbState();

DrvUSB_DispatchEvent();

if (eUsbState == eDRVUSB_DETACHED)
{
break;
}
}

// Disable USB-related interrupts.
_DRVUSB_ENABLE_MISC_INT(0);
}
}

使用特权

评论回复
评分
参与人数 1威望 +10 收起 理由
hotpower + 10
地板
yy2bbs|  楼主 | 2011-12-17 18:04 | 只看该作者
本笔记 采用VS2010中的C#语言开发上位机,通过USB接口与助学板进行通信。
项目功能:
1、温度采集:通过DS18B20进行温度采集,采集结果上传到上位机显示
2、温度报警:上位机设置报警温度,下位机设置定时器查询DS18B20采集到的温度与设置的温度比较,室温>=设置温度  时启动峰鸣器进行报警。(目前只能实现整数温度的报警,如果想更精确请自行修改程序)
3、LED灯控制:通过上位机发送相应指令给下位机,下位机执行开关灯操作,并返回结果。
4、扩展功能:可以给任意的管脚做 DrvGPIO_ClrBit 和 DrvGPIO_SetBit 操作

更多功能自己去发现吧!

很多功能都是以最简单方式写的,比如定时器只用了9行代码(不包括回调函数)

有不明白的地方可以直接问我,QQ:PHP兼职者

友情提示:如果没有VS2010环境的网友,上位机可以直接运行 UsbApp\bin\Debug\UsbApp.exe 看效果

下面是项目文件:
上位机:
yy2bbs_USB测试.rar (345.44 KB)

下位机:
yy2bbs_USB_IO.rar (213.12 KB)

使用特权

评论回复
评分
参与人数 1威望 +10 收起 理由
hotpower + 10
5
yy2bbs|  楼主 | 2011-12-17 18:10 | 只看该作者
上位机界面图:

使用特权

评论回复
6
hotpower| | 2011-12-17 18:18 | 只看该作者
哈哈,到底是IT人风采,继续努力,新生MM非你莫属!

使用特权

评论回复
7
murex| | 2011-12-17 18:19 | 只看该作者
嗯,IT人风采

使用特权

评论回复
8
hotpower| | 2011-12-17 18:21 | 只看该作者
哈哈,电工有救了。

使用特权

评论回复
9
consif| | 2011-12-17 18:43 | 只看该作者
试了下,太强大了

使用特权

评论回复
10
Ryanhsiung| | 2011-12-17 20:17 | 只看该作者
这不就是我想要的双向通信么,顶起

使用特权

评论回复
11
缥缈九哥| | 2011-12-17 21:05 | 只看该作者
USB用什么协议?HID?

使用特权

评论回复
12
yy2bbs|  楼主 | 2011-12-17 22:20 | 只看该作者
HID

使用特权

评论回复
13
plc_avr| | 2011-12-17 22:24 | 只看该作者
这个得顶!

使用特权

评论回复
14
hotpower| | 2011-12-18 01:05 | 只看该作者
哈哈,继续努力!!!

俺有权强行让你做250选美状元的~~~但愿又诞生一个新的250~~~

使用特权

评论回复
15
毅如靳往| | 2011-12-18 07:45 | 只看该作者
很久没有看到这么高质量的帖子了…顶

使用特权

评论回复
16
Swallow_0322| | 2011-12-18 11:03 | 只看该作者
这个必须要顶起!会上位机就是好,玩得HIGH...

使用特权

评论回复
17
ddllxxrr| | 2011-12-18 14:11 | 只看该作者
恩,顶

使用特权

评论回复
18
weshiluwei6| | 2011-12-18 14:20 | 只看该作者
:victory:牛啊

使用特权

评论回复
19
weshiluwei6| | 2011-12-18 14:21 | 只看该作者
学习 太牛了啊

使用特权

评论回复
20
snowdpq| | 2011-12-18 17:30 | 只看该作者
mark

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

85

帖子

0

粉丝