Thank you for you help.
I tried. The code can be complied under MB Pro 3.8, but it doesn't work. ( this project setting works for MB test code).
Code: Select all
program SERVO8_USB
'******************************************************************************************
'* 18F2550 PIC USB_HID 50Hz PWM SERVO CONTROLLER *
'* by W. Schroeder alias "xor" -- July 4, 2006 *
'* Revised on September 25, 2006 *
'* *
'* Compiled with mikroBASIC 5.0.0.1 and tested on the EasyPIC3 Development Board *
'* Uses mikroBASIC USB_HID Library and must include "USBdsc.pbas" and "HIDconstant.pbas" *
'* *
'* Also required is the free Visual Basic Servo Controller Demo program included with *
'* this file. This program has been tested on Windows XP with USB2.0. *
'* * *
'* Configuration settings for the 18F2550 are the same as the mikroBASIC 18F2550 HID *
'* example found in the mikroBASIC Examples folder in the mikroBASIC directory. *
'* These configurations are set up for an 8MHz crystal oscillator and through internal *
'* PLL settings this is increased to 48MHz. This also produces a very fast 12 MIPS. *
'* *
'* The EasyPIC3 board provides an easy connection to the PIC via a USB port. Please *
'* refer to the board schematic for ideas for your own circuit connections. *
'* *
'* For the purposes of this demo all output pins of PORTB are utilized. *
'* Servo[0] is PORTB.0, Servo[1] is PORTB.1,...., Servo[7] is PORTB.7 *
'* *
'* The range of PWM on-time is 820us to 2180us analogous with values of 1 to 256 *
'* A value of 128 is considered 50% of servo rotation with an on-time of 1500us *
'* Timer0 in 16-bit mode is used to establish an accurate 20ms (50Hz) PWM interval *
'* *
'******************************************************************************************
'******************************************************************************************
dim
wrbuf as byte[2]
wr_adr as word
servo as byte[8]
temp as byte
sub procedure interrupt
If (INTCON.TMR0IE = 1) And (INTCON.TMR0IF = 1) Then
LATB = 255
temp = 0
delay_us(820)
TMR0L = 0
asm
movlw 0
decfsz _servo+0, 1,0
iorlw 1
decfsz _servo+1, 1,0
iorlw 2
decfsz _servo+2, 1,0
iorlw 4
decfsz _servo+3, 1,0
iorlw 8
decfsz _servo+4, 1,0
iorlw 16
decfsz _servo+5, 1,0
iorlw 32
decfsz _servo+6, 1,0
iorlw 64
decfsz _servo+7, 1,0
iorlw 128
andwf LATB, 1,0
movf TMR0L, 0,0
xorwf _temp, 0,0
bz $-2
incfsz _temp, 1,0
bra $-22
end asm ' routine has used ~2180us to this point
TMR0H = Hi(65536-3362) ' load balance of 20ms in Timer0...high byte first
TMR0L = Lo(65536-3362) ' TMR0L = -3362
INTCON.TMR0IF = 0 ' reset the interrupt flag
Else
USB_Interrupt_Proc
End If
end sub
sub procedure Init_Main
INTCON2 = 0xF5
INTCON3 = 0xC0
RCON.IPEN = 0 ' disable Priority Levels on interrupts
PIE1 = 0 ' clear all interrupt enables
PIE2 = 0
PIR1 = 0
PIR2 = 0
ADCON1 = 15 ' digital IO
TRISB = 0
LATB = 0
T0CON = %00000101 ' prescaler = 64 or 5.3us per increment
TMR0H = Hi(65536-1) ' load Timer0 high byte first
TMR0L = Lo(65536-1)
INTCON = %10100000 ' Timer0 Interrupt Enabled and Flag Cleared
T0CON.7 = 1 ' Timer0 On
end sub
main:
Init_Main
HID_Enable(@servo, @wrbuf) ' @servo is equivalent to the Read_Buffer
Delay_mS(1000)
For temp = 0 to 7
servo[temp] = 255
Next temp
while true
If HID_READ <> 0 Then ' if USB package has arrived...servo positions are in Read_Buffer
wrbuf[0] = 255 ' 255 is verification value
HID_WRITE(@wrbuf, 1) ' reply to host with verification
End If
wend
HID_Disable
end.
Code: Select all
module USBdsc
const USB_VENDOR_ID as word = 0x1234
const USB_PRODUCT_ID as word = 0x0001
const USB_SELF_POWER as char = 0x80 ' Self powered 0xC0, 0x80 bus powered
const USB_MAX_POWER as char = 50 ' Bus power required in units of 2 mA
const HID_INPUT_REPORT_BYTES as char = 1
const HID_OUTPUT_REPORT_BYTES as char = 8
const EP_IN_INTERVAL as char = 1
const EP_OUT_INTERVAL as char = 1
const USB_INTERRUPT as char = 0
const USB_TRANSFER_TYPE as char = 0x03 '0x03 Interrupt
const USB_HID_EP as char = 1
const USB_HID_RPT_SIZE as char = 33
structure device_descriptor
dim bLength as char ' bLength - Descriptor size in bytes (12h)
dim bDescriptorType as char ' bDescriptorType - The constant DEVICE (01h)
dim bcdUSB as word ' bcdUSB - USB specification release number (BCD)
dim bDeviceClass as char ' bDeviceClass - Class Code
dim bDeviceSubClass as char ' bDeviceSubClass - Subclass code
dim bDeviceProtocol as char ' bDeviceProtocol - Protocol code
dim bMaxPacketSize0 as char ' bMaxPacketSize0 - Maximum packet size for endpoint 0
dim idVendor as word ' idVendor - Vendor ID
dim idProduct as word ' idProduct - Product ID
dim bcdDevice as word ' bcdDevice - Device release number (BCD)
dim iManufacturer as char ' iManufacturer - Index of string descriptor for the manufacturer
dim iProduct as char ' iProduct - Index of string descriptor for the product.
dim iSerialNumber as char ' iSerialNumber - Index of string descriptor for the serial number.
dim bNumConfigurations as char ' bNumConfigurations - Number of possible configurations
end structure
const device_dsc as device_descriptor = (
0x12, ' bLength
0x01, ' bDescriptorType
0x0200, ' bcdUSB
0x00, ' bDeviceClass
0x00, ' bDeviceSubClass
0x00, ' bDeviceProtocol
8, ' bMaxPacketSize0
USB_VENDOR_ID, ' idVendor
USB_PRODUCT_ID, ' idProduct
0x0001, ' bcdDevice
0x01, ' iManufacturer
0x02, ' iProduct
0x00, ' iSerialNumber
0x01 ' bNumConfigurations
)
' Configuration 1 Descriptor
const configDescriptor1 as byte[41] = (
' Configuration Descriptor
0x09, ' bLength - Descriptor size in bytes
0x02, ' bDescriptorType - The constant CONFIGURATION (02h)
0x29,0x00, ' wTotalLength - The number of bytes in the configuration descriptor and all of its subordinate descriptors
1, ' bNumInterfaces - Number of interfaces in the configuration
1, ' bConfigurationValue - Identifier for Set Configuration and Get Configuration requests
0, ' iConfiguration - Index of string descriptor for the configuration
USB_SELF_POWER, ' bmAttributes - Self/bus power and remote wakeup settings
USB_MAX_POWER, ' bMaxPower - Bus power required in units of 2 mA
' Interface Descriptor
0x09, ' bLength - Descriptor size in bytes (09h)
0x04, ' bDescriptorType - The constant Interface (04h)
0, ' bInterfaceNumber - Number identifying this interface
0, ' bAlternateSetting - A number that identifies a descriptor with alternate settings for this bInterfaceNumber.
2, ' bNumEndpoint - Number of endpoints supported not counting endpoint zero
0x03, ' bInterfaceClass - Class code
0, ' bInterfaceSubclass - Subclass code
0, ' bInterfaceProtocol - Protocol code
0, ' iInterface - Interface string index
' HID Class-Specific Descriptor
0x09, ' bLength - Descriptor size in bytes.
0x21, ' bDescriptorType - This descriptor's type: 21h to indicate the HID class.
0x01,0x01, ' bcdHID - HID specification release number (BCD).
0x00, ' bCountryCode - Numeric expression identifying the country for localized hardware (BCD) or 00h.
1, ' bNumDescriptors - Number of subordinate report and physical descriptors.
0x22, ' bDescriptorType - The type of a class-specific descriptor that follows
USB_HID_RPT_SIZE,0x00, ' wDescriptorLength - Total length of the descriptor identified above.
' Endpoint Descriptor
0x07, ' bLength - Descriptor size in bytes (07h)
0x05, ' bDescriptorType - The constant Endpoint (05h)
USB_HID_EP or 0x80, ' bEndpointAddress - Endpoint number and direction
USB_TRANSFER_TYPE, ' bmAttributes - Transfer type and supplementary information
0x40,0x00, ' wMaxPacketSize - Maximum packet size supported
EP_IN_INTERVAL, ' bInterval - Service interval or NAK rate
' Endpoint Descriptor
0x07, ' bLength - Descriptor size in bytes (07h)
0x05, ' bDescriptorType - The constant Endpoint (05h)
USB_HID_EP, ' bEndpointAddress - Endpoint number and direction
USB_TRANSFER_TYPE, ' bmAttributes - Transfer type and supplementary information
0x40,0x00, ' wMaxPacketSize - Maximum packet size supported
EP_OUT_INTERVAL ' bInterval - Service interval or NAK rate
)
structure hid_report_descriptor
dim report as char[USB_HID_RPT_SIZE]
end structure
const hid_rpt_desc as hid_report_descriptor = (
(0x06, 0x00, 0xFF, ' Usage Page = 0xFF00 (Vendor Defined Page 1)
0x09, 0x01, ' Usage (Vendor Usage 1)
0xA1, 0x01, ' Collection (Application)
' Input report
0x19, 0x01, ' Usage Minimum
0x29, 0x40, ' Usage Maximum
0x15, 0x00, ' Logical Minimum (data bytes in the report may have minimum value = 0x00)
0x26, 0xFF, 0x00, ' Logical Maximum (data bytes in the report may have maximum value = 0x00FF = unsigned 255)
0x75, 0x08, ' Report Size: 8-bit field size
0x95, HID_INPUT_REPORT_BYTES,' Report Count
0x81, 0x02, ' Input (Data, Array, Abs)
' Output report
0x19, 0x01, ' Usage Minimum
0x29, 0x40, ' Usage Maximum
0x75, 0x08, ' Report Size: 8-bit field size
0x95, HID_OUTPUT_REPORT_BYTES,' Report Count
0x91, 0x02, ' Output (Data, Array, Abs)
0xC0) ' End Collection
)
'Language code string descriptor
structure str1
dim bLength as char
dim bDscType as char
dim wString as word[1]
end structure
const strd1 as str1 = (
4,
0x03,
(0x0409)
)
'Manufacturer string descriptor
structure str2
dim bLength as char
dim bDscType as char
dim wString as word[16]
end structure
const strd2 as str2 = (
34, 'sizeof this descriptor string
0x03,
("M","i","k","r","o","e","l","e","k","t","r","o","n","i","k","a")
)
'Product string descriptor
structure str3
dim bLength as char
dim bDscType as char
dim wString as word[23]
end structure
const strd3 as str3 = (
32, 'sizeof this descriptor string
0x03,
("U","S","B"," ","H","I","D"," ","L","i","b","r","a","r","y")
)
dim USB_config_dsc_ptr as ^const char[1]
dim USB_string_dsc_ptr as ^const char[3]
sub procedure USB_Init_desc()
implements
sub procedure USB_Init_desc()
USB_config_dsc_ptr[0] = @configDescriptor1
USB_string_dsc_ptr[0] = ^const char(@strd1)
USB_string_dsc_ptr[1] = ^const char(@strd2)
USB_string_dsc_ptr[2] = ^const char(@strd3)
end sub
end.
Thank you.