Собрал в PROTON IDE и PROTEUS две схемы на pic18F4550: USB вольтметр для двух ног (2 и 3) и USB LED на две ноги (33 и 34). Отдельно обе схемы работают отлично. Меня интересует USB LED. Отдельно эта схема работает и включает LEDы по команде с ПК кнопками СТАРТ1 и СТАРТ2. Также и выключает LEDы СТОП1 и СТОП2. Попробывал объединить две схемы в одну - срабатывать стала только LED на кнопке СТАРТ2. Если сделать еще пару кнопок и добавить в схему пару LED (т.е. LED1 (33), LED2 (34), LED3 (35) и Led4 (36)), с добавлением команд в PROTON и DELPHI для этих кнопок, при нажатии на программные кнопки СТАРТ 1,2,3 и 4 срабатывает только последняя 4 кнопка - первые три полный мертвяк. Подскажите пожалуйста, как сделать, чтобы при нажатии кнопок СТАРТ 1,2,3 и 4 срабатывали, соответственно, LED 1,2,3 и 4. Вот исходник для PROTON IDE для двух кнопок и двух вольтметров:
Код:
Declare Reminders = off
' select MCU and clock speed
Device = 18F4550
Xtal = 48
' descriptor file, located in \inc\usb_18 - a copy
' is located in the same folder as this file
USB_Descriptor = "USBLedVolt4DESC.inc"
' USB Buffer...
Symbol USBBufferSizeMax = 32
Symbol USBBufferSizeTX = 32
Symbol USBBufferSizeRX = 32
Dim USBBuffer[USBBufferSizeMax] As Byte
Dim USBBufferCount As Byte
Dim USB_BUFFER As String * 20
Dim USB_BUFFER0 As String * 20
Dim USB_BUFFER1 As String * 20
Dim B0 As ADRESL.Word
Dim B1 As ADRESL.Word
Dim VALUE0 As Word
Dim VALUE1 As Word
Dim H0 As Byte
Dim H1 As Byte
Dim A As Byte
Dim C As Byte
' some useful flags...
Dim PP0 As Byte System ' USBPOLL status return
Symbol CARRY_FLAG = STATUS.0 ' high if microcontroller does not have control over the buffer
Symbol ATTACHED_STATE = 6 ' is USB attached
' ************************************************************
' * main program loop - remember, you must keep the USB *
' * connection alive with a call to USBPoll, USBIn or USBOut *
' * every couple of milliseconds or so *
' ************************************************************
GoSub AttachToUSB
TRISA.0 = 1
TRISA.1 = 1
TRISA.2 = 1
TRISA.3 = 1
TRISB.0 = 0
TRISB.1 = 0
ADCON1 = %10000010
ADCON2 = %10000000
ProgramLoop:
GoSub DoUSBIn
A = USBBuffer[4]
C = USBBuffer[5]
PORTB.0 = A
PORTB.1 = C
GoSub DoUSBOut
GoTo ProgramLoop
' ************************************************************
' * receive data from the USB bus *
' ************************************************************
DoUSBIn:
USBBufferCount = USBBufferSizeRX
USBIn 1, USBBuffer, USBBufferCount, DoUSBIn
Return
' ************************************************************
' * transmit data *
' ************************************************************
DoUSBOut:
B0 = ADIn 0
VALUE0 = B0
H3 = VALUE0 / 1000
H2 = VALUE0 / 100-H3*10
H1 = VALUE0 /10-H3*100-H2*10
H0 = VALUE0 - H3*1000-H2*100-H1*10
USB_BUFFER0 = Str$(Dec1,H3) + Str$(Dec1,H2) + Str$(Dec1, H1) + Str$(Dec1, H0)
B1 = ADIn 1
VALUE1 = B1
G3 = VALUE1 / 1000
G2 = VALUE1 / 100-G3*10
G1 = VALUE1 /10-G3*100-G2*10
G0 = VALUE1 - G3*1000-G2*100-G1*10
USB_BUFFER1 = Str$(Dec1,G3) + Str$(Dec1,G2) + Str$(Dec1, G1) + Str$(Dec1, G0)
USB_BUFFER = USB_BUFFER0 + USB_BUFFER1
USBOut 1, USB_BUFFER, USBBufferSizeTX, DoUSBOut
Return
' ************************************************************
' * wait for USB interface to attach *
' ************************************************************
AttachToUSB:
Repeat
USBPoll
Until PP0 = ATTACHED_STATE
Return
и код на Delphi:
Код:
function TForm1.USBEvent(var Msg: TMessage): Boolean;
var
DevHandle:cardinal;
// TextBuffer:array[0..255] of char;
begin
result := False;
if Msg.Msg = WM_HID_EVENT then
begin
case Msg.WParam of
// a HID device has been plugged in...
NOTIFY_PLUGGED :
begin
// is it our HID device...
DevHandle := Msg.LParam; // handle of HID device in this message
if (GetVendorID(DevHandle) = VENDOR_ID) and (GetProductID(DevHandle) = PRODUCT_ID) then
begin
Label1.Caption:= 'Прибор включен';
// add your code here, for example...
// GetProductName(DevHandle, TextBuffer, 256);
// ALabel.Caption := string(TextBuffer);
end;
result := true;
end;
// a HID device has been device removed...
NOTIFY_UNPLUGGED :
begin
Label1.Caption:= 'Прибор отключен';
// is it our HID device...
DevHandle := Msg.LParam; // handle of HID device in this message
if (GetVendorID(DevHandle) = VENDOR_ID) and (GetProductID(DevHandle) = PRODUCT_ID) then
begin
Label1.Caption:= 'Прибор отключен';
// add you code here
end;
result := true;
end;
// a HID device has been attached or removed. This event is fired after
// either NotifyPlugged or NotifyUnplugged.
NOTIFY_CHANGED :
begin
// get the handle of the device we are interested in
// and set it's read notification flag to true...
DevHandle := GetHandle(VENDOR_ID,PRODUCT_ID);
SetReadNotify(DevHandle,true);
result := true;
end;
// a HID device has sent some data..
NOTIFY_READ :
begin
DevHandle := Msg.LParam; // handle of HID device in this message
if (GetVendorID(DevHandle) = VENDOR_ID) and (GetProductID(DevHandle) = PRODUCT_ID) then
begin
// read the data - remember that first byte is report ID...
Read(DevHandle,@FBufferIn);
Edit1.Text:= String(@FBufferIn[1]);
// process data here...
end;
result := true;
end;
end;
end;
end;
(*
if you want to write some data to the USB device, then
try using something like this...
// first element is report ID, set to 0...
FBufferOut[0] := $0;
// fill the buffer with some data...
for index := 1 to BufferOutSize do
FBufferOut[index] := index;
DevHandle := GetHandle(VendorID, ProductID);
Write(DevHandle,@FBufferOut);
*)
procedure TForm1.Button1Click(Sender: TObject);
var
DevHandle:cardinal;
begin
FBufferOut[5] := $1;
DevHandle := GetHandle(Vendor_ID, Product_ID);
Write(DevHandle,@FBufferOut);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
DevHandle:cardinal;
begin
FBufferOut[5] := $0;
DevHandle := GetHandle(Vendor_ID, Product_ID);
Write(DevHandle,@FBufferOut);
end;
procedure TForm1.Button3Click(Sender: TObject);
var
DevHandle:cardinal;
begin
FBufferOut[6] := $1;
DevHandle := GetHandle(Vendor_ID, Product_ID);
Write(DevHandle,@FBufferOut);
end;
procedure TForm1.Button4Click(Sender: TObject);
var
DevHandle:cardinal;
begin
FBufferOut[6] := $0;
DevHandle := GetHandle(Vendor_ID, Product_ID);
Write(DevHandle,@FBufferOut);
end;