ATmega & USB guidance

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys

 

I'm going to try and implement USB with my AVR based keyboard project so that I can connect the keyboard to a PC/Mac/etc..

Ideally, I'd like a little advice on whether I have things right (see below), if I've missed anything and also if anyone has any advice for a USB newbie.

 

I've gleaned the following info from the net (i.e. a bit of googling):

 

Connection/electronic setup/knowledge/assumptions:

- As this is a keyboard project my device will be seen as a low power HID device (<100mA draw)

- Low power devices cannot exceed 100mA if bus powered

- The D- and D+ lines need to connected to two pins on the AVR (obviously!)

- USB logic HI is >2.8V and LOW is <0.3V

- As the board is bus powered from the USB, and that will be at 5V, then 3.6v zener diodes will be necessary to clamp D- and D+ lines to remain within USB spec.

- D- should have two pull up resistors (15K and 1.5K) and D+ should have one pull down resistor (15K). I've seen quite a few diagrams with different numbers of resistors and values, but think that this is correct for low powered devices.

 

Circuit diagram:

 

 

Have I got the above right? Also, with regards the firmware, does anyone have any suggestions library wise which comes with a good tutorial? Or should I go with V-USB and try and muddle my way through? Looks intimidating lol.

 

 

Any advice gratefully received!

 

[EDIT]  Oops got D1 the wrong way around. Should have the anode facing D+. Doh

Last Edited: Fri. Mar 30, 2018 - 09:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

banedon wrote:

I'm going to try and implement USB with my AVR based keyboard project so that I can connect the keyboard to a PC/Mac/etc.....Looks intimidating lol.....

 

banedon wrote:

Any advice...

 

Yes. Have a look at the HT82K629. It's a USB keyboard chip, everything in one package, DIP40, about $3/£2, widely available.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think I looked at that quite a while ago, but came to the conclusion that it wouldn't do the job due to the keyboard that I'm converting to USB is an old BBC Master one with row/columns for a given key not corresponding with what is set within the Encoder for that key.

I'll recheck to see if the majority of the keys are on the same rows/columns though as I could possibly just connect BBC Master row 1 to Encoder row 4 (for example), etc.. If that panned out I'd probably have to write a key map for windows.

 

[EDIT] Keys on, say, row 1 of the encoder and split into different rows on the BBC Master keyboard. The columns are also like this. A pity as it would have been a nice solution if it had worked out.

Last Edited: Fri. Mar 30, 2018 - 09:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wonder if you could sit an AVR between the Holtek and the matrix?

 

Maybe though it's just easier to go V-USB than mess about.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

If you are just picking an AVR for a design then why wouldn't you pick one that already has USB rather than having to "fake it" with V-USB? For example a 32U4. Is this about needing something in a DIP package?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not a bad idea if V-USB doesn't work out. Or I could possibly use a PLD, although product terms might be an issue.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
why wouldn't you pick one that already has USB

Absolutely!

 

It's not like there's a shortage of cheap,  USB-capable microcontrollers these days!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Note that now we're allowed to mention Microchip (where is Leon?) I'll point out that if you want USB *and* DIP then they have several. (rather ironically I once thought of designing a cheap USB based AVR programmer using MCHP ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I believe the m324 does not have hardware USB, is that right?

 

For software USB http://www.fischl.de/ ( maker of USBasp) is pretty much the de-facto standard.

He has a bunch of documented projects, among which an AVR as keyboard controller.

When USB bitbanging first came around it was a pretty amazing and impressive project.

But nowaday's uC's with hardware USB are cheap and widely available and there is not much need anymore for bitbanging the USB protocol.

USB bitbanging has some serious limits. During the bit banging interrupts can not be tolerated, the bit banging part must be done in asm, and USB has a built in polling / keep alive which needs attention every milli second.

If you want to go this way then simply start with repurposing a Chinese USBasp. Nice small PCB with USB connector and all hardware needed. And the whole thing costs less than a bare uC from a shop in "the west".

Your schematic is wrong though. You can not use zener diodes without limiting the current through them. They need series resistors.

USB normally also always has series resistors, often between 22 and 68 Ohm.

Just copy the USBasp schematic from Fischl.

Or probably better:

If you use a 3V3 regulator for the uC, you do not need zener diode's at all.

R1 is parallell to R4 ???

What are the 2 15k resistors fore? I have never seen them before on USB hardware.

You have also not drawn any decoupling capacitors on your circuit.

No uC circuit can work reliably without them.

 

If you look at the obdev site (home of the V-USB bit banging stack):

https://www.obdev.at/products/vusb/index.html

You see just the 68 ohm series resistors and the 1k5 pullup to put the USB into low-speed mode.

The 2 diodes in series are to lower the USB voltage enough to not need the zener diode's anymore.

And of course, with examples:

https://www.obdev.at/products/vusb/projects.html

You can build the keyboard example on a breadboard and have something working within half an hour.

 

USB is a pretty complicated protocol (compared to uart, spi, etc) and I think that studying Fischl's usb stack is a smoother way to learning to work with USB than reading the protocol specifications themself.

 

For a "real world" application it seems more logical to use a uC with hardware USB support.

 

The Teensy is a well supported board with lots of software examples available. The Teensy boards with hardware USB are mostly ARM based if I remember correctly.

They cost quite a lot more (About USD25) tan a uC on a pcb (USD2) from China / Ebay / Ali but the time saved because of the software examples is probably well worth it.

 

For debugging I can highly recommend Sigrok with a cheap (USD5 ) LA from China. Search for: "24m 8ch" on Ebay / Ali, or buy any of the CY7C68013 development boards. The general development boards go up to 16 channels, but don't have a box or input protection.

I recommend Sigrok ( +Hardware) here because I have sucsessfully captured the low-speed (1.5Mbps) USB signalling with this combination. USB has a lot of weird and special states and there are a lot of things in the protocol which can go wrong durig software development.

Sigrok has built-in decoding for the USB signals and can alert you almost immediately on a lot of fault conditions in the USB protocol.

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:
it seems more logical to use a uC with hardware USB support.

Indeed.

 

And, as already noted, there are plenty to choose from just from Microchip - AVR, PIC, ARM - without even venturing out of this fold ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@clawson

Good suggestion with regards to the 32U4. It  has just enough I/O pins by the looks of it to cover what I need: It says 26 (hopefully USB D+/- not included) and I need:

8 pins for rows

13 pins for columns

3 for LEDs

3 for special keys - CTRL, SHIFT, BREAK

 

This totals 27, but I can drop the BBC Master power LED as that can just stay on. This leaves me with 26.

I'll have a good look through the datasheet to see if there are any snags.

 

@Paulvdh

With regards my diagram: It was just to show the relevant bits that I was interested in - I should have been clearer. In all my projects all ICs get a 100nF decoupling capacitor and (on my current design) both D- and D+ have in series 68 ohm resistors.  I haven't included series resistors for the zeners though. Thanks for advising on that.

Unless there are issues doing so I think I will go the VCC = 3.3V route as you suggest.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If 32U4 were not enough pins then maybe look to Xmega. The models with numbers ending 'U' have USB. They are 3V3 though.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

...(where is Leon?)...

 

IIRC, he fell over and broke something a couple of years ago. He does seem to be online a lot less recently; I can see he was online on another forum about 2 weeks ago so he is still around.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:

IIRC, he fell over and broke something a couple of years ago. He does seem to be online a lot less recently; I can see he was online on another forum about 2 weeks ago so he is still around.

 

Nope, I was wrong. He was hit by a car...

 

Quote:

 24 Dec 2015

More

I'm in the Conquest hospital, Hastings, having been hit by a people carrier taxi whilst crossing the road - his fault. Pelvis broken.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

banedon wrote:
Unless there are issues doing so I think I will go the VCC = 3.3V route as you suggest.
Most important issue is of course other circuitry on the same pcb.

Some uC's with built in USB hardware have an on-chip voltage regulator, I do not know how common that is though.

banedon wrote:
With regards my diagram: It was just to show the relevant bits that I was interested in
Sigh, here we go again: "We" can not see from your schematic wat are for you the relevant bits. Your "interests" are in your head and not on paper / my monitor, I just see the schematic. Spend some effort to mke sure the information you give is correct. This can even leed to you not even having to post a question because you discovered the error yourself. Try to be systematic, precise and thorough.

 

I see now that the m324p has USB hardware.

Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega324PV-AVR-MCU-with-CIP-and-picopower-40002102A.pdf

I would probably not have sidetracked into USB bitbanging if your schematic had D+ and D- in your AVR part instead of only the general PBx names.

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use a shift reg for your columns and only use 3 pins! SPI?

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@Paulvdh

I'm normally much more precise, but thought it would be obvious with regards to the bits that I missed off. I can see that perhaps I was mistaken. I do appreciate what you are saying as my day job requires such full information from other people and I know how frustrating it can be when vital clues get left out.

 

@all

Now, the 32U4 is looking good. It has application notes for both 3.3v and 5v and the block diagram indicates that there is an internal voltage regulator. As such, I'll ditch the external 3.3v regulator and run the lot of 5V and see what happens.  Going to order some 3.3v stuff any just in case though. With regards the D- and D+ lines: all that is required is a 22 ohm resistors inline on each, a 1uF cap on UCAP and 1a 10uF cap off ofUVCC.

USB power is then hooked up to VCC and USB GND to GND.

Ref: datasheet pages 256, 257

 

Only one thing I'm not sure of is UID which is shown in the diagrams, but has no pin on the AVR. The only UID I have heard is in relation to a UUID.

 

@ki0bk

I'll have a think on that. So far I'vemanaged to free a few pins up which might be enough, but if not then I'll possibly use a SIPO shift register. Cheers!

Last Edited: Fri. Mar 30, 2018 - 04:34 PM