本帖最后由 baoyansheng 于 2009-12-1 14:06 编辑
经过这些日子以来对usb模块使用的反复阅读,终于解决了与pc通信的问题,供想学习改芯片的人提供参考,程序很粗略,但对于入门应该有些许帮组。下面的程序都已修改
这是我修改后上位机程序,用MFC编写的:
//全局变量
CCyUSBDevice *USBDevice = new CCyUSBDevice();
CCyControlEndPoint *ept = USBDevice->ControlEndPt;
CCyInterruptEndPoint *IntOut = USBDevice->InterruptOutEndPt;
CCyInterruptEndPoint *IntIn = USBDevice->InterruptInEndPt;
int epCnt = USBDevice->EndPointCount();
void CWirelessDlg::OnButtonCheck() //按下“设备检测”的程序响应
{
// TODO: Add your control notification handler code here
SetDlgItemText(IDC_EDIT_RECV,"");
if(USBDevice->IsOpen())
{
bool bBulk, bIn;
int blkInCnt = 0;
int vID,pID;
CString str,temp;
str = "USB设备已经连接!";
vID = USBDevice->VendorID;
pID = USBDevice->ProductID;
temp.Format("VendorID = 0x%x",vID);
str = str + "\r\n";
str = str + temp;
temp.Format("ProductID = 0x%x",pID);
str = str + "\r\n";
str = str + temp;
PCHAR dn = new CHAR[256];
dn = USBDevice->DeviceName;
temp.Format("设备名称:%s",dn);
str = str + "\r\n";
str = str + temp;
SetDlgItemText(IDC_EDIT_RECV,str);
temp.Format("%s",dn);
SetDlgItemText(IDC_COMBO_SET,temp);
for (int e=0; e<epCnt; e++) {
bBulk = USBDevice->EndPoints[e]->Attributes == 3;
bIn = USBDevice->EndPoints[e]->Address & 0x80;
if (bBulk && bIn)
{
MessageBox("检测到输入终端");
}
if (bBulk && !bIn)
{
MessageBox("检测到输出终端");
}
}
}
else
{
SetDlgItemText(IDC_EDIT_RECV,"USB设备连接失败!");
}
}
void CWirelessDlg::OnButtonRevclear()
{
// TODO: Add your control notification handler code here
SetDlgItemText(IDC_EDIT_RECV,"");
}
void CWirelessDlg::OnButtonMansend() //按下“手动放松”的程序响应
{
// TODO: Add your control notification handler code here
unsigned char buf[4];
ZeroMemory(buf, 4);
LONG buflen = 4;
buf[0] = 0x05;
buf[1] = 0xc0;
buf[2] = 0x05;
buf[3] = 0xc0;
if(IntOut)
MessageBox("找到输出设备") ;
if((IntOut->XferData(buf,buflen))==TRUE&&USBDevice->IsOpen())
MessageBox("传输成功");
else
MessageBox("传输失败");
}
void CWirelessDlg::OnButtonRecv() //按下“开始接收”的程序响应
{
// TODO: Add your control notification handler code here
CString temp;
unsigned char buf[6];
LONG buflen = 6;
if(IntIn->XferData(buf,buflen)==TRUE)
{
temp.Format("传输数据:%s",buf);
MessageBox(temp); //检测收到的数据,并显示
}
}
固件程序如下:
#include <m8c.h> // part specific constants and macros
#include "PSoCAPI.h" // PSoC API definitions for all User Modules
#include "psocgpioint.h"
void SoftDelay50msec(void);
void SoftDelay1000msec(void);
#define RED_LED_Data_ADDR P0DATA
#define GREEN_LED_Data_ADDR P0DATA
#define GREEN_LED_ON {GREEN_LED_Data_ADDR &= ~GREEN_LED_MASK;}
#define GREEN_LED_OFF {GREEN_LED_Data_ADDR |= GREEN_LED_MASK;}
#define GREEN_LED_BLINK {GREEN_LED_Data_ADDR &= ~GREEN_LED_MASK;SoftDelay50msec();GREEN_LED_Data_ADDR |= GREEN_LED_MASK;}
#define RED_LED_OFF {RED_LED_Data_ADDR |= RED_LED_MASK;}
#define RED_LED_ON {RED_LED_Data_ADDR &= ~RED_LED_MASK;}
#define RED_LED_BLINK {RED_LED_Data_ADDR &= ~RED_LED_MASK;SoftDelay50msec();RED_LED_Data_ADDR |= RED_LED_MASK;}
unsigned char unch[6]="Hello";
//--------------------------------------
// Register Definitions
//--------------------------------------
#pragma ioport EP2_DR0: 0x060 // Endpoint 2 Data Register 0
BYTE EP2_DR0;
#pragma ioport EP2_DR1: 0x061 // Endpoint 2 Data Register 1
BYTE EP2_DR1;
#pragma ioport EP2_DR2: 0x062 // Endpoint 2 Data Register 2
BYTE EP2_DR2;
#pragma ioport EP2_DR3: 0x063 // Endpoint 2 Data Register 3
BYTE EP2_DR3;
#pragma ioport EP2_DR4: 0x064 // Endpoint 2 Data Register 4
BYTE EP2_DR4;
#pragma ioport EP2_DR5: 0x065 // Endpoint 2 Data Register 5
BYTE EP2_DR5;
#pragma ioport EP2_DR6: 0x066 // Endpoint 2 Data Register 6
BYTE EP2_DR6;
#pragma ioport EP2_DR7: 0x067 // Endpoint 2 Data Register 7
BYTE EP2_DR7;
void main()
{
// Insert your main routine code here.
USB_Start(0);
M8C_EnableGInt; //Start USB
GREEN_LED_OFF;
RED_LED_OFF; //Initial light off
while(USB_bGetConfiguration()==0)
{
GREEN_LED_ON;
RED_LED_ON;
} //Waiting until usb configuration complete
GREEN_LED_OFF;
RED_LED_ON; //usb complete
while(1)
{
USB_EnableEP(2); //allow pc to trasfer datas to device
if(0x05==EP2_DR0)
{
if(0xc0==EP2_DR1)
{
if(0x05==EP2_DR2)
{
if(0xc0==EP2_DR3)
{
GREEN_LED_ON;
RED_LED_OFF; //Check the data recevied from pc,Green led on if correct
}
}
}
}
USB_LoadEP(1,unch,6); //Allow device to trasfer data to pc
}
}
void SoftDelay50msec(void)
{
unsigned int i;
for(i=0;i<23100;i++)
asm("NOP");
}
void SoftDelay1000msec(void)
{
unsigned int i,j;
for(j=0;j<20;j++)
for(i=0;i<23100;i++)
asm("NOP");
}
附上我的工程,供大家学习! |