View previous topic :: View next topic |
Author |
Message |
Tom1234
Joined: 06 Jan 2013 Posts: 39
|
USB to TTL converter |
Posted: Mon Apr 29, 2013 9:50 pm |
|
|
I need to connect a USB to TTL converter to a microcontroller but i don't find any resources on how to do this.
Firstly is needed any MAX 232 component (and from there to connect my converter) or is connected directly to microcontroller.
Also i think i need to use usb.c library is this right?
And finally except of the initialization function (in usb.c) what else i need to use ?
Is anyone how can help me ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Tue Apr 30, 2013 1:13 am |
|
|
USB, has nothing whatsoever to do with the MAX232, or TTL....
USB, is a differential signalling standard using multiple voltage levels on the lines for specific control when starting. So, pulling one line 'up' relative to the other with s particular resistor pattern, signals what bus speed is to be used. Then the signalling uses 3.3v differential for the actual data transfers (USB1).
PIC's that support USB, have the necessary hardware drivers built in. You just connect the GND, D+, and D- signals to the pins on the PIC (and if the board you are using has it's own 5v supply you should connect the incoming 5v to a 'detect' line so you can tell when power is present on the USB bus.
The usb.c library, is a small part of what you need. USB, _requires_ attached devices to tell the host what they 'are', and handle different types of packets accordingly. The simplest starting point is to load usb_cdc.h. This then itself loads usb_desc_cdc.h (the 'descriptor' code, saying that this is to be a cdc device), usb.c, and usb.h (the low level driver parts), and the 'hardware' layer (pic18_usb.h, for a standard PIC18 chip). The cdc file then provides functions emulating a serial port as far as the CCS code is concerned, and the compiler comes with a PC driver to do the same at this end.
A typical PIC end start would be:
Code: |
#define USB_CON_SENSE_PIN PIN_B1 //specify the 5v detection pin
#include <stdlib.h>
#define __USB_PIC_PERIF__ 1 //Using PIC18 hardware
#include <usb_cdc.h>
//This then loads everything needed for the cdc
|
Then to talk to it:
Code: |
int1 usb_cdc_oldconnected=FALSE;
usb_init_cs();
//tells the code to initialise the USB
if (usb_attached())
{
//Only execute USB code if the port is attached. Allows other things
//to keep working without USB
usb_task();
if (usb_enumerated())
{
//Now says the PC is attached
if (usb_cdc_carrier.dte_present)
{
if (usb_cdc_oldconnected==FALSE)
{
printf(usb_cdc_putc,"Message to say device connected\n");
usb_cdc_oldconnected=TRUE;
}
if (usb_cdc_kbhit())
{
tchar=usb_cdc_getc(); //This is how to get a character
//do what you want
}
}
else usb_cdc_oldconnected=FALSE;
}
else usb_cdc_oldconnected=FALSE;
}
|
This shows how to set the code up, so it will _only_ call the usb functionality once the port has power, and has been seen and recognised by a PC, and has an application trying to talk (like a terminal program). When this has all happened, the code will send a 'Message to say device connected', which can be anything you want. It'll handle the software disconnecting, or the PC being unplugged, happily waiting for the connection to once again be made, and sending it's message again.
Once connected, it shows how to get a character when it arrives.
Look at ex_usb_serial.c, or ex_usb_serial2.c. These don't handle the connection/reconnection fully, but for a simple 'how to connect' are the first starting point.
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Tue Apr 30, 2013 5:29 am |
|
|
Mr. T is right about the PICs with builtin USB and the drivers.
However those USB driver take up a lot of space(about 1/3 of a '4550) and you'll need a USB connector and some additional passives.
The other option, a better solution in my opinion, is to just buy a TTL<->USB module for about $3 CDN! Benefits include NO drivers need for the PIC, frees up a lot of code space in the PIC,plugs into any PC as it comes with connector,Windows 'sees' it, works with any PIC,also can provide both 5 volt and 3 volt so it interfaces with both old and new PICs.
OK, it'll cost $3...but you'll need a USB connector($1) witha USB PIC so really the price is very small and you get instant USB connection for any PIC with no coding, debugging, etc. It's a true plug and play solution.
hth
jay |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Tue Apr 30, 2013 7:38 am |
|
|
temtronic wrote: | Mr. T is right about the PICs with builtin USB and the drivers.
However those USB driver take up a lot of space(about 1/3 of a '4550) and you'll need a USB connector and some additional passives.
The other option, a better solution in my opinion, is to just buy a TTL<->USB module for about $3 CDN! Benefits include NO drivers need for the PIC, frees up a lot of code space in the PIC,plugs into any PC as it comes with connector,Windows 'sees' it, works with any PIC,also can provide both 5 volt and 3 volt so it interfaces with both old and new PICs.
OK, it'll cost $3...but you'll need a USB connector($1) witha USB PIC so really the price is very small and you get instant USB connection for any PIC with no coding, debugging, etc. It's a true plug and play solution.
hth
jay |
Totally agreed. However for anything else, he would not be using anything USB in the PIC at all. As soon as he refers to usb.c, he is effectively ruling out these other answers, though it looks as if he is actually going this way.....
Best Wishes |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Tue Apr 30, 2013 8:03 am |
|
|
Yeesh..that Sparkfun board is 5* the cost of the ones I buy about the size of a flashdrive with USB connector on one end and 5 square pins on the other. They also have 2 LEDS (power and data).
Seeing how PCs no longer have 'legacy' comports, these modules are my serial 'glue' devices.
Options, one always has to have options.
hth
jay |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Apr 30, 2013 8:19 am |
|
|
yes... its expensive... but man do they work good.. ive got 2.. flawless. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Apr 30, 2013 8:37 am |
|
|
Hi,
The problem is that all USB-to-Serial converters are not alike! My company manufactures a number of test instruments using 'legacy' serial ports. As COM ports go away on PC's, we've been recommending the use of USB-to-Serial converters for the past few years. The only converters that seem to work 100% of the time are the ones using the FTDI chipsets. Others, such as the 'Prolific' ones are really hit-or-miss...... So, 'expensive', is a bit of a relative term. You can easily burn 10 times the perceived savings if you waste an hour messing with one of the 'cheap' converters!
You pay your money and you take your chances!
John |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Tue Apr 30, 2013 2:40 pm |
|
|
John's right ...not all are 100% the same. I've bought 'RS-232<>USB' dongles with DE-9s on them and only 1 out of 4 worked reliably.Yeesh..what a pain!
When I first bought 10 of these 'ttl<>usb' modules, I hooked them up 2 at time to a 46k22 PIC system to a test PC.Had them send 'the quick brown fox...' at 115k200 for several days,never missed a character, so I felt 'good' about suggesting them.
You do have to test anything these days.
hth
jay |
|
|
Tom1234
Joined: 06 Jan 2013 Posts: 39
|
USB to TTL converter |
Posted: Tue Apr 30, 2013 6:21 pm |
|
|
I use a USB 2.0 to TTL UART 6PIN Module Serial Converter CP2102 STC
is this a good one ?
How to check if this module is ok?
Also I thought that all the PIC series are able to use a USB to TTL converter
if no, then pic24/dspic30/dspic33 series is able to use this kind of converter ? |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Apr 30, 2013 7:14 pm |
|
|
Hi,
Huh? How do you think you check if the module is OK? You load the drivers for your OS, connect the module to the UART of your PIC, plug the module into your computer, and use some sort of a 'terminal program' to see if you can send/receive data with your PIC.
John |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Wed May 01, 2013 5:15 am |
|
|
hardware debugging...
The easiest way to test your ttl-usb module is to add a loopback connection to it(that's connecting ttl-txd to ttl-rxd) ,plugging it into the PC, observe that Windows sees it and install drivers for it, then run a terminal program(Realterm, Hyperterminal,etc.). Pressing keyboard keys should be displayed on the PC screen.
I'd do this before connecting your PIC and trying your code.This test will confirm that the module is working properly.
Then connect to your PIC and try your code.
If it now fails, then you know the problem is the code in your PIC.
hth
jay |
|
|
Tom1234
Joined: 06 Jan 2013 Posts: 39
|
USB to TTL converter |
Posted: Wed May 01, 2013 11:33 am |
|
|
I have already done this and the module is working fine.
I tried to use the code that suggested by Ttelmah but is not working, i get errors.
Can i find any example (for USB to TTL converter) ? |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed May 01, 2013 6:16 pm |
|
|
Quote: |
any example (for USB to TTL converter) ?
|
There is no code as such for this, as the topic under discussion is PURELY hardware in nature - connection to the PIC EUSART TX/RX uses a different programming language called "wiring", to pins C6 and C7 on most 16, 18F pics. |
|
|
|