student companion _ usb communication device class with pic microcontroller - mikroc

13
Tweet 2 Share 0 Thích Thích Stum << P2 Semester Report Sample 1: Electronics | P1 Semester Report Sample 1 Page 3 >> USB Communication Device Class with PIC Microcontroller - MikroC 7. Tháng Hai 2014 Bitahwa Bindu Tutorials , MikroC Pro for PIC Bình lun (0) Search Here Search Chọn Ngôn ngữ Được hỗ trợ bởi Dịch Category list Projects (19) Flowcode Projects (1) MBLAB X Projects (6) MikroC Projects (9) P1 and P2 (5) Tutorials (88) Career Guidance (8) Entrepreneurship (17) Flowcode for PIC (18) Graphical User Interface (6) Student Companion Empowering you with skills Xem trang này bằng: Tiếng Việt Dịch Tắt đối với: Tiếng Anh Tùy chọn ▼ Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl... 1 trong 13 3/29/2015 11:04 AM

Upload: danhis4

Post on 15-Nov-2015

290 views

Category:

Documents


19 download

DESCRIPTION

Student Companion _ USB Communication Device Class With PIC Microcontroller - MikroC

TRANSCRIPT

  • Tweet2Share0ThchThch

    StumbleUpon

    >

    USB Communication DeviceClass with PIC Microcontroller- MikroC7. Tha ng Hai 2014 Bitahwa Bindu Tutorials , MikroC Profor PIC Bnh lun (0)

    Search Here

    Search

    Chn Ngn ng

    c h tr bi Dch

    Category list

    Projects (19)

    Flowcode Projects(1) MBLAB X Projects(6) MikroC Projects (9)

    P1 and P2 (5)

    Tutorials (88)

    Career Guidance(8) Entrepreneurship(17) Flowcode for PIC(18) Graphical UserInterface (6)

    Student CompanionEmpowering you with skills

    Xem trang ny bng: Ting Vit Dch Tt i vi: Ting Anh Ty chn

    Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl...

    1 trong 13 3/29/2015 11:04 AM

  • The CDC device class code is "0x02, this class defines

    various communications over USB.

    The CDC class can be used to emulate the RS232 serial port

    (COM Port) and thus creating an easy solution to migrate the

    application from an old RS232 to the Universal Serial Bus

    (USB) interface without implementing many changes especially

    to the PC software. The device will create a virtual COM.

    The current MikroC Pro for PIC version 6.0.0 does not have a

    built in USB CDC library in the compiler, but you can download

    the USB Device Library from LIBSTOCK. This library supports

    the USB HID Class, the USB CDC Class and the USB Mass

    Storage Device Class.

    The file you are going to download has an .mpkg extension.

    You need the package manager to integrate this USB library

    into the MikroElekronika compilers. You can download the

    latest package manager from MikroElekronika website.

    Once you have installed the package manager, Open the

    desired package file (*.mpkg) and its content will be displayed

    in Navigation and Information sections.

    Click on File->Install option and the installation will begin.

    Installer will copy your package content into destination

    folders and update your definition files.

    (26) MPLAB XC8 (21)

    Quote of the day

    "Be miserable.Motivate yourself.Whatever has to bedone, it's always yourchoice."Wayne Dyer

    Recent Posts

    Interfacing SD CardWith PICMicrocontroller - XC8

    Sending SMS TextMessage using PICMicrocontroller -Flowcode

    Interfacing GSMModem with PICMicrocontroller -Flowcode

    Digital Clock using PICMicrocontroller andthe DS1307 Real TimeClock - MikroC

    What Makes anEntrepreneurSuccessful?

    How to Get Your Ideasto Spread

    Tweet

    ShareShare

    TRANG CH LU TR LIN LC ABOUT US

    Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl...

    2 trong 13 3/29/2015 11:04 AM

  • Figure 2: Installing a library into MikroCcompiler

    New installed library will appear in Library Manager of your

    compiler, and only for those chips that are compatible with

    installed library.

    Top Posts

    (997) How to write agreat professionalCurriculum Vitae(CV)(538) Interfacing SDCard With PICMicrocontroller -MikroC(503) Top 20 JobInterview Questionsand Best Answers(448) Interfacing LCDDisplay With PICMicrocontroller - XC8(394) FreeProfessional CVTemplates(353) Digital Clockusing PICMicrocontroller andthe DS1307 RealTime Clock - XC8Compiler

    Comments

    Summary

    Post Tags

    7-Segment ADC C#

    Career guidance CV

    Digital Thermometer

    DS1307

    TRANG CH LU TR LIN LC ABOUT US

    Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl...

    3 trong 13 3/29/2015 11:04 AM

  • Figure 3: New installed library (USB_Device_PIC) in librarymanager

    Below is a quick descriptions of the USB CDC Class library

    routines as described in the USB Device Library help file.

    This library works when PIC microcontroller with built-in USB

    are used (e.g., PIC18F4550), and port pins RC4 and RC5 are

    connected to the D+ and D- pins of the USB connector

    respectively.

    USB buffers for sending and receiving data must be placed in

    the USB RAM.

    You must also remember to set your clock configurations

    Ethernet Flowcode

    Flowcode V5 Goals

    GSM GUI IC

    Interfacing Job

    Keypad LCD LED

    LM35

    Microcontroller

    MikroC MPLAB X

    Open a new business

    P1 and P2 PIC

    PIC Communication

    Projects Relay RS232

    RTC SD Card

    Success Switch

    Tutorials USB

    Wealth XC8

    Newsletter

    Get noti ed when anew post is published.

    Enter your e-mail

    Notify me

    Join our facebook

    TRANG CH LU TR LIN LC ABOUT US

    Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl...

    4 trong 13 3/29/2015 11:04 AM

  • Microcontroller article.

    To write a device class, certain function callbacks must be

    implemented in order to be used with library.

    These functions must be in the user code:

    USBDev_GetDescriptorHandler: Called when host requests

    descriptor.

    USBDev_EventHandler: This function is called when specific

    event happens, like device reset.

    USBDev_SetupReqHandler: Called when there is a need to

    process non-standard requests.

    USBDev_DataReceivedHandler: Called when data packet is

    received.

    USBDev_DataSentHandler: Called when data packet is

    sent.

    Some other Library Routines

    USBDev_Init This function initializes USB module in device mode.

    This function returns no data.

    Example:

    USBDev_Init();

    USBDev_IntHandler This is a routine for servicing usb interrupts. This function

    should be called from USB interrupt routine.

    Before using this function, the device should be initialized first

    and the USB interrupt should be enabled.

    This function returns no data.

    Example:

    // USB interrupt service routine

    void interrupt()

    {

    // Call library interrupt handler routine

    USBDev_IntHandler();

    }

    Tm chng ti trn Facebook

    Student Companion SA

    3.605 ngi thch Student Companion SA.

    Plugin x hi ca Facebook

    ThchThch

    TRANG CH LU TR LIN LC ABOUT US

    Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl...

    5 trong 13 3/29/2015 11:04 AM

  • This function returns the current USB Device state.

    Prototype: uint8_t USBDev_GetDeviceState();

    Example:

    // Wait until device is configured (enumeration is

    successfully finished)

    while(USBDev_GetDeviceState() !=

    _USB_DEV_STATE_CONFIGURED) ;

    For more functions, please read the USB Device Library help

    file.

    USB Device Library - CDCClass

    USBDev_CDCInit This function initializes the USB.

    Before you can use any other functions, this function should

    be called first.

    This function returns no data.

    Example:

    void USBDev_CDCInit(); // Initialize CDC Class

    USBDev_CDCGetLineCoding Prototype: TUSBCDCLineCoding*

    USBDev_CDCGetLineCoding();

    Returns: Pointer to line coding structure.

    Description: Returns pointer to line coding structure

    containing data rate, stop bits, parity and data bits.

    USBDev_CDCSetLineCoding Prototype: void USBDev_CDCSetLineCoding (uint32_t

    dwDTERate, uint8_t bCharFormat, uint8_t bParityType,

    uint8_t

    bDataBits);

    TRANG CH LU TR LIN LC ABOUT US

    Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl...

    6 trong 13 3/29/2015 11:04 AM

  • parity and data bits.

    Parameters:

    dwDTERate: data rate in bits per

    second.

    bCharFormat: stop bits: 0 - 1 stop bit, 1 -

    1.5 stop bits, 2 - 2 stop bits.

    bParityType: parity: 0 - None, 1 - Odd, 2

    - Even, 3 - Mark, 4 - Space.

    bDataBits: data bits: 5, 6, 7, 8 or 16.

    USBDev_CDCSendData Prototype: void USBDev_CDCSendData(uint8_t* dataBuff,

    uint16_t dataLen);

    This function returns no data.

    Description: This function sends data to the bulk data

    endpoint and requires two parameters:

    dataBuff: pointer to the data (buffer to hold the

    data).

    dataLen: length of the data (less or equal to the

    maximum packet size)

    Example:

    char buffer[64] absolute 0x500; // Buffer of 64 bytes

    uint16_t dataReceivedSize; // length of the data

    USBDev_CDCSendData(buffer, dataReceivedSize); //

    Send data in buffer memory to the bulk data endpoint

    USBDev_CDCSetReceiveBuffer Prototype: void USBDev_CDCSetReceiveBuffer(uint8_t*

    dataBuff);

    This function returns no data.

    Description: This function enables reception of one data

    packet on the data endpoint and it has one parameter:

    dataBuff: pointer to the buffer where received

    packed will be stored.

    The size of this reception buffer should

    be at least maximum packet size for that endpoint.

    TRANG CH LU TR LIN LC ABOUT US

    Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl...

    7 trong 13 3/29/2015 11:04 AM

  • USBDev_CDCSetReceiveBuffer(buffer); // Set

    receive buffer where received data is stored

    USBDev_CDCDataReceived Prototype: extern void USBDev_CDCDataReceived(uint16_t

    size);

    This function returns no data.

    Description: This function is going to be called when the

    packet is received on data endpoint and it

    has one parameters: size: size of received data.

    USBDev_CDCParamsChanged Prototype: extern void USBDev_CDCParamsChanged();

    This function returns no data.

    Description:This function is going to be called when host

    issues Set Line Coding request.

    Example

    This project example (circuit on figure 1) shows the usage of

    USB CDC device that functions as a virtual COM port.

    This example will send "This is USB CDC Example by

    studentcompanion.co.za" to terminal then send "Press any

    key..." after this any character pressed will be echoed back to

    the USART terminal.

    When the device is connected to PC for the first time,

    Windows will detect a new hardware. Windows will ask you to

    install the device drivers. Tick on Install from a list or specific

    location (Advanced) radio box. Then click Next.

    Browse for the device drivers then click Next, Windows will

    install the drivers for you. The project can be tested by

    connecting the output of the microcontroller to a USB port of a

    PC and then running the HyperTerminal terminal emulation

    software or any other serial terminal software.

    TRANG CH LU TR LIN LC ABOUT US

    Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl...

    8 trong 13 3/29/2015 11:04 AM

  • Programs, Accessories, Communications and Hyper Terminal

    to open a windows free Hyper Terminal.

    Connect your Hyper Terminal to the correct COM Port (your

    USB virtual port).

    To find out which COM Port has been assigned to your virtual

    Serial port, go to device manager (if you are using Windows

    XP, go to My Computer, at right hand side pane, click on View

    system information, click on Hardware tab then Device

    Manager) and expend the Ports (COM & LPT) to see all the

    COM ports on your PC.

    Note: Your computer will automatically allocate a COM port

    number to your virtual Com Port, it does not have to be the

    same a the one illustrated here below on figure 4 (COM25)

    Figure 4: Virtual COM Port (COM25) in device

    manager

    If your serial driver was installed correctly, you should see

    Mikroe Virtual Com Port and in bracket the COM number in

    the device manager as shown on figure 2 above.

    Set the communications parameters to 9600 baud, 8 data

    bits, 1 stop bit, and no parity bit as shown on figure 5 below.

    TRANG CH LU TR LIN LC ABOUT US

    Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl...

    9 trong 13 3/29/2015 11:04 AM

  • Figure 5: COM Port settings

    Note: If you connect your device in another USB port, you will

    have to re-install the drivers again.

    Here is the full project code:

    123456789

    10111213141516171819202122232425262728293031323334

    #include // Buffer of 64 bytes in USB RAMchar buffer[64] absolute 0x500 ; // USB interrupt service routinevoid interrupt(){// Call library interrupt handler routine USBDev_IntHandler();} void USBDev_CDCParamsChanged(){ } extern const uint8_t _USB_CDC_BULK_EP_IN; extern const uint8_t _USB_CDC_BULK_EP_OUT; uint8_t dataReceived = 0;uint16_t dataReceivedSize; void USBDev_CDCDataReceived(uint16_t size){ dataReceived = 1; dataReceivedSize = size;} //A function to send data to Uart when calledvoid Send_Data_To_Uart(char txt[64]){ int i; for(i = 0; i < strlen(txt); i++) { buffer[i] = txt[i]; }

    TRANG CH LU TR LIN LC ABOUT US

    Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl...

    10 trong 13 3/29/2015 11:04 AM

  • Figure 6 below shows data sent to terminal on a personal

    computer connected to the microcontroller via a USB port.

    .

    383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182

    void main(void){ ADCON1 |= 0x0F; // Configure all ports with CMCON |= 7; // Disable comparators // Initialize CDC Class USBDev_CDCInit(); // Initialize USB device module USBDev_Init(); // Enable USB device interrupt IPEN_bit = 1; USBIP_bit = 1; USBIE_bit = 1; GIEH_bit = 1; // Wait until device is configured (enumeration is s while(USBDev_GetDeviceState() != _USB_DEV_STATE_CONF ; // Set receive buffer where received data is stored USBDev_CDCSetReceiveBuffer(buffer); delay_ms(5000); //5s delay //Send an Introduction text to Uart Send_Data_To_Uart("This is USB CDC Example by student delay_ms(500); Send_Data_To_Uart("Press any key...\r\n" // Infinite loopwhile(1){if(dataReceived == 1){ dataReceived = 0; //Send back received packet USBDev_CDCSendData(buffer, dataReceivedSize); // Prepare receive buffer USBDev_CDCSetReceiveBuffer(buffer); } } }

    TRANG CH LU TR LIN LC ABOUT US

    Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl...

    11 trong 13 3/29/2015 11:04 AM

  • Figure 6: Computer Terminal

    >> Read also on student companion: Creating

    a Serial Port Interface with C#

    You can download the full project files (MikroC source code

    and Drivers) below here.

    All the files are zipped, you will need to unzip them (Download

    a free version of the Winzip utility to unzip files).

    USB CDC Driver.zip (6.63 kb) [Downloads: 6]

    USB CDC Example.zip (106.27 kb) [Downloads: 6]

    Tags: USB CDC , USB , PIC Communication , MikroC ,Tutorials , RS232

    Currently rated 5.0 by 1 people

    TRANG CH LU TR LIN LC ABOUT US

    Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl...

    12 trong 13 3/29/2015 11:04 AM

  • USB Communication Device Class with PICMicrocontroller - Flowcode

    Many computers especially portable ones do not hav

    USB Human Interface Device Communication withPIC Microcontroller - MikroC

    The HID device class code is "0x03, this class is used fo

    USB Communication with PIC Microcontroller

    Many computers especially portable ones do not hav

    blog comments powered by Disqus

    ...

    ...

    ...

    Copyright 2015 Student Companion SA all rights reserved ng nhp

    TRANG CH LU TR LIN LC ABOUT US

    Student Companion | USB Communication Device Class with PIC Microco... http://www.studentcompanion.co.za/post/USB-Communication-Device-Cl...

    13 trong 13 3/29/2015 11:04 AM