F2812实现电机控制源程序.rar
源代码在线查看: unit1.cpp
//---------------------------------------------------------------------------
#include
#include
#pragma hdrstop
#include "Unit1.h"
#include "usb.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TUSB *myusb;
char HexToInt(char x)
{
if((x>=0x30)&& (x else if(x=='a') return 10;
else if(x=='b') return 11;
else if(x=='c') return 12;
else if(x=='d') return 13;
else if(x=='e') return 14;
else if(x=='f') return 15;
else return 0;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
myusb =new TUSB;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Image1->Align = alClient;
if (!myusb->OpenDevice()) {
ShowMessage("没有找到USB设备");
Application->Terminate();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
myusb->CloseDevice();
delete myusb;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1DblClick(TObject *Sender)
{
Image1->Canvas->Brush->Color = clBlack;
Image1->Canvas->Rectangle(0,0,255,255);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
unsigned char buf[512];
/////////////////////////////
int i,j,len;
unsigned char str[64];
for(i=0;i
len=Edit1->GetTextLen()+1;
Edit1->GetTextBuf(str,len) ;
j=2;
for(i=0;i {
buf[j]= HexToInt(str[i])*16+HexToInt(str[i+1]);
j+=2;
}
/////////////////////////////
buf[0] = 0;
myusb->SendCommand(buf);
Sleep(100);
myusb->GetResponse(buf);
Edit2->Text = "";
for(i=2;i Edit2->Text=Edit2->Text+IntToHex(buf[i], 2) + " ";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
unsigned char buf[512];
/////////////////////////////
int i,j,len;
unsigned char str[64];
for(i=0;i
len=Edit1->GetTextLen()+1;
Edit1->GetTextBuf(str,len) ;
j=2;
for(i=0;i {
buf[j]= HexToInt(str[i])*16+HexToInt(str[i+1]);
j+=2;
}
/////////////////////////////
buf[0] = 1;
myusb->SendCommand(buf);
Sleep(100);
myusb->GetResponse(buf);
Edit2->Text = "";
for(i=2;i Edit2->Text=Edit2->Text+IntToHex(buf[i], 2)+" ";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
unsigned char buf[512];
/////////////////////////////
int i,j,len;
unsigned char str[64];
for(i=0;i
len=Edit1->GetTextLen()+1;
Edit1->GetTextBuf(str,len) ;
j=2;
for(i=0;i {
buf[j]= HexToInt(str[i])*16+HexToInt(str[i+1]);
j+=2;
}
/////////////////////////////
buf[0] = 5;
myusb->SendCommand(buf);
Sleep(100);
myusb->GetResponse(buf);
Edit2->Text = "";
for(i=2;i Edit2->Text=Edit2->Text+IntToHex(buf[i], 2)+" ";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
unsigned char buf[65536];
Graphics::TBitmap *MyBitmap = new Graphics::TBitmap();
MyBitmap->PixelFormat = pf24bit;
MyBitmap->Width = 256;
MyBitmap->Height = 256;
//myusb->Reset();
buf[0] = 7;
myusb->SendCommand(buf);
myusb->ReadData(buf);
for(int y=0;y for(int x=0;x int t = buf[y*256+x];
MyBitmap->Canvas->Pixels[x][y] = (TColor)RGB(t,t,t);
}
}
Image1->Canvas->Draw(0,0,MyBitmap);
MyBitmap->Free();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
unsigned char buf[65536];
Graphics::TBitmap *MyBitmap = new Graphics::TBitmap();
MyBitmap->PixelFormat = pf24bit;
MyBitmap->Width = 256;
MyBitmap->Height = 256;
//myusb->Reset();
buf[0] = 8;
myusb->SendCommand(buf);
myusb->ReadData(buf);
for(int y=0;y for(int x=0;x int t = buf[y*256+x];
MyBitmap->Canvas->Pixels[x][y] = (TColor)RGB(t,t,t);
}
}
Image1->Canvas->Draw(0,0,MyBitmap);
MyBitmap->Free();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button8Click(TObject *Sender)
{
char buf[65536];
int t0,t1;
t0 = GetTickCount();
for(int i=0;i buf[0] = 8;
myusb->SendCommand(buf);
myusb->ReadData(buf);
}
t1 = GetTickCount();
sprintf(buf, "%4.2f", 6400.0 / (t1-t0));
Edit3->Text = buf;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
Edit1->Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
Edit2->Text = "";
}
//---------------------------------------------------------------------------