• AVR Freaks

Hot!How to connect a keyboard to a PIC16F via CH376?

Author
LeBon
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2016/07/08 06:37:17
  • Location: 0
  • Status: offline
2020/11/26 09:26:42 (permalink)
0

How to connect a keyboard to a PIC16F via CH376?

I want to connect a USB keyboard to a PIC16F688, using a CH376 interface module and UART.
I believe there is a series of commands required for initialization, but the CH376 datasheet does not help too much.
Can someone show me an example code in assembly on how to do that?
Thanks.
#1

8 Replies Related Threads

    Aussie Susan
    Super Member
    • Total Posts : 3857
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: How to connect a keyboard to a PIC16F via CH376? 2020/11/29 18:33:02 (permalink)
    -1 (3)
    Firstly, why write in assembler - that just seems to be deliberately taking a painful path.
    The first Google result was https://www.mpja.com/download/ch376ds1.pdf that contains the commands and values that the CG376 uses - although the English is generally terrible,  a quick glance shows the information is there.
    Section 6 shows how to select the UART interface.
    Susan
    #2
    LeBon
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2016/07/08 06:37:17
    • Location: 0
    • Status: offline
    Re: How to connect a keyboard to a PIC16F via CH376? 2020/11/29 19:47:02 (permalink)
    0
    Aussie Susan
    Firstly, why write in assembler - that just seems to be deliberately taking a painful path.
    The first Google result was https://www.mpja.com/download/ch376ds1.pdf that contains the commands and values that the CG376 uses - although the English is generally terrible,  a quick glance shows the information is there.
    Section 6 shows how to select the UART interface.
    Susan


    That is the datasheet I was talking about. It does not answer the question. Regarding assembly, it is just my preferred language, but the example code can be in C as well. Thanks anyway.
    #3
    Aussie Susan
    Super Member
    • Total Posts : 3857
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: How to connect a keyboard to a PIC16F via CH376? 2020/11/30 18:11:08 (permalink)
    0 (2)
    In that case what 'initialisation' codes are you talking about that are not mentioned in the datasheet?
    Susan
    #4
    LeBon
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2016/07/08 06:37:17
    • Location: 0
    • Status: offline
    Re: How to connect a keyboard to a PIC16F via CH376? 2020/11/30 18:34:36 (permalink)
    +1 (1)
    Aussie Susan
    In that case what 'initialisation' codes are you talking about that are not mentioned in the datasheet?
    Susan



    Section 8.4.1 reads:
    8.4.1. Initialization, for any one file operations a necessary step before
    ① CMD_SET_USB_MODE command, into the USB-HOST work mode or SD card host work mode (mode
    3)
    ② Waiting for connecting USB Flash Drive or SD card, USB Flash Drive can be checked by CH376
    automatically and generate an interrupt notification, or by the MCU send CMD_DISK_CONNECT
    command to CH376 for regularly check, SD cards must to be checked by MCU itself.
    ③ CMD_DISK_MOUNT command, initialize USB Flash Drive or SD card, and test the disk ready or not,
    you can try again up to 5 times after failure
    ④The above steps need implementation only once, unless USB Flash Drive or SD card to reconnect after
    disconnect, that must return to step ②
     
    This is supposed to initialize a SD card or a flash drive. The datasheet does not mention how to initialize other devices, such as a keyboard, anywhere. I have seen people doing that, but I don't know how.
     
    #5
    Aussie Susan
    Super Member
    • Total Posts : 3857
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: How to connect a keyboard to a PIC16F via CH376? 2020/12/01 18:52:14 (permalink)
    +1 (3)
    And Section 5.9 explains the CMD_SET_USB_MODE command. There are several USB-HOST codes that the command requires so you will need to work out which one suits your requirements - perhaps 0x07 which does a USB reset as well.
    I suspect your next question will be how to actually send those values to the CH376. For that you need to read Section 5 where is clearly states that you first send 0x15 as the SET_USB_MODE command. The next column (input Data) says to send the mode code (the 0x07 or whatever you selected above). The next column (Output data) says to wait 10uSec and then read the operation status value which will be either 0x51 (success) or 0x5f (failure).
    As you say you are using the UART in the MCU, then sending and receiving the values is a simple case of writing to the UART buffer and reading from it in the correct manner.
    As for initialising devices other than the SD card or flash drive, you may need to read the description of the CMD_SET_USB_MODE carefully and understand the USB protocol as to what (for example) SOF means etc. My guess is that this chip requires that you provide a lot of the lower level USB layers in your code. Also the other manual mentioned has an automated Chinese-English translation at http://pulko.mandy.pagesperso-orange.fr/shinra/CH376_ext.shtml but at first glance doesn't seem tp help much. However the first set of commands on both manuals (i.e. the ones where the 'Output Data' column is not shaded) would appear to be ones to investigate.
    I must admit, this must be one of the more complicated chips to use for connecting a keyboard. If you are prepared to  go to a PIC18F level device, many have USB host capabilities and the MCC can generate much of the lower-level code for you. Also I've used some of the FTDI chips (mainly for audio but they also handle HID interfaces).
    Susan
    #6
    Aussie Susan
    Super Member
    • Total Posts : 3857
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: How to connect a keyboard to a PIC16F via CH376? 2020/12/02 19:01:09 (permalink)
    0 (2)
    My thanks to whoever down-voted my posts to 0, -1 and -2.
    I trust that, if you are the same person that down-voted the OPs post #5, you realise that 3 stars is 'neutral' in this strange rating scheme and lower than that indicates 'badness'. So surely the OPs post was not all that bad (regardless of what you think of my contributions - which will now cease in this thread).
    Susan
    #7
    LeBon
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2016/07/08 06:37:17
    • Location: 0
    • Status: offline
    Re: How to connect a keyboard to a PIC16F via CH376? 2020/12/03 11:27:34 (permalink)
    0
    Aussie Susan
    My guess is that this chip requires that you provide a lot of the lower level USB layers in your code.


    Susan, thank you for your time and help. I believe you are right about the low level code.
     
    The following page shows how to connect a mouse in a similar way, although it's not an easy task: http://pulko.mandy.pagesperso-orange.fr/shinra/albireo.shtml
     
    Here are some highlights from that page:
    • CH376DS1.pdf is only the first part of the datasheet. It covers the use with mass storage drives only.
    • Using the CH376 in this way (to connect a HID) requires some understanding of how USB works in general.
    • As far as I can tell, all 3 devices are essentially rebadged 8051 microcontrollers.
    • You would think a single datasheet would provide a complete list of commands. But, that is not the case. WCH decided to scatter the command list in different documents.
    • Since we are only interested in the mouse, we can send it some HID commands.
     
    Unfortunately, I couldn't get it to work using UART. If someone can, please post it here.
    #8
    suspicion
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2020/05/19 11:14:02
    • Location: 0
    • Status: offline
    Re: How to connect a keyboard to a PIC16F via CH376? 2020/12/06 01:17:36 (permalink)
    0
    The datasheet does not mention how to initialize other devices, such as a keyboard, anywhere. I have seen people doing that.
    #9
    Jump to:
    © 2021 APG vNext Commercial Version 4.5