PIC Vietnam

Go Back   PIC Vietnam > Truyền thông > Giao tiếp USB, CAN, I2C, SPI, USART...

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Giao tiếp USB, CAN, I2C, SPI, USART... Những giao tiếp được tích hợp trên PIC

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 18-03-2010, 08:00 AM   #7
tbk_05
Đệ tử 1 túi
 
Tham gia ngày: Mar 2010
Bài gửi: 14
:
mình đã vẽ được ba đồ thị cùng mọt lúc trong c#, cập nhập liên tục khi giá trị ADC ở VDK thay đôi.
Code như thế này:
//khai báo biến toàn cục
public ushort[] data_y0;// du lieu truc y
public ushort[] data_y1;
public ushort[] data_y2;
.....................
Khi ấn nút btlisten, thì sẽ thực hiện việc đọc liên tục sau mỗi 'polling time', máy tính sẽ quét và đọc dữ liệu từ usb driver.
void btlisten_Click(object sender, EventArgs e)
{
data_y0 = null;
data_y0 = null;
data_y1 = null;
SingleTransfer();
Listen_pipe();
}
private void Listen_pipe()
{
//--------------------
DWORD dwOptions = 0;//
usb_pipe_rev.SetContiguous(true); // khi ham nay kich hoat thi cu sau moi polling time thi computer se quet va doc du lieu tu usb device
TraceMsg(string.Format("began listening to {0} pipe number " +
"0x{1:X}", usb_device.DeviceDescription(),
usb_pipe_rev.GetPipeNum()));
usb_pipe_rev.UsbPipeTransferAsync(true, 0, TIME_OUT,
new D_USER_TRANSFER_COMPLETION(Transfer_rev_Completion ));
}
//----------------
sau đó hàm Transfer_rev_Completion mình sẽ lấy dữ liệu ra để vẻ:
private void Transfer_rev_Completion(DOAN_MINH_THUONG_Pipe pipe)
{

DWORD dwStatus = pipe.GetTransferStatus();
byte[] buff;
bool IsListenStopped = (dwStatus == (DWORD)wdu_err.WD_IRP_CANCELED)
&& !pipe.IsContiguous();
if (dwStatus != (DWORD)wdu_err.WD_STATUS_SUCCESS && !IsListenStopped)
{
ErrMsg(string.Format("Transfer Failed! Error {0}: {1} ",
dwStatus.ToString("X"), utils.Stat2Str(dwStatus)));
}
else
{
TraceMsg(DisplayHexBuffer(pipe.GetBuffer(), 3));
buff = pipe.GetBuffer();//hàm này sẽ lấy dữ liệu từ ống pipe
// TraceMsg(string.Format("{0}",
// DisplayHexBuffer(buff,pipe.GetBytesTransferred())) ); // ham pipe.getbuffer tra ve 1 mang chua cac gia tri nhan dc tu usb
// ham getbytes transferred tra ve so byte nhan duoc
time++;
//data_x[time] = time;
data_y0[time] = (ushort)(buff[0] );
data_y1[time] = (ushort)(buff[1]);
data_y2[time] = (ushort)(buff[2]);
}
//Hàm vẽ khi ấn nút btplot
private void btplot_Click(object sender, EventArgs e)
{
CreateGraph(zedGraphControl1 );
SetSize();
}
private void CreateGraph(ZedGraphControl zgc)
{
timer1.Interval = 50;
timer1.Enabled = true;
timer1.Start();
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
// get a reference to the GraphPane
GraphPane myPane = zgc.GraphPane;
double v_desire = 120;
// Set the Titles
myPane.Title.Text = "Đọc gia trị ADC";
myPane.XAxis.Title.Text = "Time ( đơn vị : 10 ms )";
myPane.YAxis.Title.Text = "ADC ";
myPane.XAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.YAxis.Scale.MagAuto = false;
myPane.XAxis.Scale.MagAuto = false;
myPane.XAxis.Scale.Mag = 1;
// Make up some data arrays based on the Sine function
// Make up some data arrays based on the Sine function
PointPairList list1 = new PointPairList();
PointPairList list2 = new PointPairList();
PointPairList list3 = new PointPairList();

LineItem myCurve1 = myPane.AddCurve("ADC 1", list1, Color.Blue, SymbolType.None);
LineItem myCurve2 = myPane.AddCurve("ADC 2", list2, Color.Green, SymbolType.None);
LineItem myCurve3 = myPane.AddCurve("ADC 3", list3, Color.Tomato, SymbolType.None);
//LineItem myCurve2 = myPane.AddCurve("ADC 2", list3, Color.Green, SymbolType.None);
//LineItem myCurve3 = myPane.AddCurve("ADC 3", list8, Color.Red, SymbolType.None);
myCurve1.Line.IsSmooth = true;
myCurve1.Line.SmoothTension = 0.5F;
//---------
myCurve2.Line.IsSmooth = true;
myCurve2.Line.SmoothTension = 0.5F;
//--------
myCurve3.Line.IsSmooth = true;
myCurve3.Line.SmoothTension = 0.5F;
//myCurve3.Line.IsSmooth = true;
// myCurve3.Line.SmoothTension = 0.5F;
myCurve1.Line.Width = 2;
myCurve2.Line.Width = 2;
myCurve3.Line.Width = 2;
//myCurve3.Line.Width = 2;
//myCurve_desire.Line.Width = 2;
zgc.AxisChange();


//RollingPointPairList list2 = new RollingPointPairList(120);
// instead of discrete step-sized jumps
myPane.XAxis.Scale.Min = 0;
myPane.XAxis.Scale.Max = 30;
myPane.XAxis.Scale.MinorStep = 1;
myPane.XAxis.Scale.MajorStep = 5;

// Save the beginning time for reference
tickStart = Environment.TickCount;
}
private void timer1_Tick(object sender, EventArgs e)//để tạo thời gian và lấy mẩu
{
if (zedGraphControl1.GraphPane.CurveList.Count <= 0)
return;
LineItem curve1 = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
if (curve1 == null)
return;
LineItem curve2 = zedGraphControl1.GraphPane.CurveList[1] as LineItem;
if (curve2 == null)
return;
LineItem curve3 = zedGraphControl1.GraphPane.CurveList[2] as LineItem;
if (curve3 == null)
return;
IPointListEdit list1 = curve1.Points as IPointListEdit;
IPointListEdit list2 = curve2.Points as IPointListEdit;
IPointListEdit list3 = curve3.Points as IPointListEdit;
if (list1 == null)
return;
if (list2 == null)
return;
if (list3 == null)
return;
double time1 = (Environment.TickCount - tickStart) / 1000.0;
// 3 seconds per cycle
list1.Add(time1, data_y0[time]);
list2.Add(time1, data_y1[time]);
list3.Add(time1, data_y2[time]);
Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale;
if (time1 > xScale.Max - xScale.MajorStep)
{
xScale.Max = time1 + xScale.MajorStep;
xScale.Min = xScale.Max - 30.0;
}
zedGraphControl1.AxisChange();
zedGraphControl1.Invalidate();
}

Nhưng ở đây mình chỉ vẽ được 3 ADC 8bit(max=255), giờ mình muốn vẽ ADC 10bit thì làm thế nào. VDK chi gửi lên được 8bit một lần(gửi nhiều byte). Như vậy thì đối với chương trình CCS thì phải cắt ra để gửi, một byte chứa 8bit, con một byte chứa 2bit, thuật toán cắt sẻ như thế nào, các bạn nào biết chỉ mình với.
Giả sử như mình gửi được rồi thì trên C# mình cũng phải có thuật toán ghép chuổi lại, chứ không thể nào cộng hai byte đó lại được. Như thế thì thuật toán trong c# cũng là vấn đề. mong các bạn biết và chỉ dùm.
tbk_05 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 05:16 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam