Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Adapting Code for the 1-Key Keyboard
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adapting Code for the 1-Key Keyboard

by Tiberius on Tue Apr 05, 2011 4:35 am

I posted this on V-USBs forums but I don't think it's very active. If anyone could help me out, that would be nice.

I have basic experience with the Atmega series of microcontrollers(simple IO, timers, interrupts, PWM) and wanted to try this project out:
http://blog.flipwork.nl/?x=entry:entry081009-142605

I wanted(for experimental and learning purposes) to use this code on an Atmega32 instead of the Attiny85 suggested in the blog.
Please tell me what I should look out for while editing and compiling the code.

Sourcecode from flipwork: http://blog.flipwork.nl/fp-content/attachs/1_key_keyboard.zip
Here is a list of things that I think need to be changed in the source code so that it will work:
1. In usbconfig.h, I changed the code to:


Code: Select all | TOGGLE FULL SIZE
    #define USB_CFG_IOPORTNAME      D
    #define USB_CFG_DMINUS_BIT      0
    #define USB_CFG_DPLUS_BIT       2 //because INT0 on the mega32 is PinD2



Anything else in usbconfig.h?

2.In main.c

Code: Select all | TOGGLE FULL SIZE
    static void timerInit(void)
    {
        TCCR1 = 0x0b;           /* select clock: 16.5M/1k -> overflow rate = 16.5M/256k = 62.94 Hz */
       
    }




which is just setting the prescaler at CK/1024, so I thought I could do the same with TCCR1B= 5; // to set CS12 and CS10 (clk/1024)
for the Atmega32

3. My Atmega32's fuse bits have already been set for high frequency external crystal and I have both a 16Mhz and 12Mhz crystal. (I read that I can use either?)


Are there any other things I should be careful of while changing the code ?
Thank you.
Tiberius
 
Posts: 3
Joined: Tue Apr 05, 2011 4:31 am

Re: Adapting Code for the 1-Key Keyboard

by chatham on Tue Apr 12, 2011 7:28 pm

So, in my experience, V-USB is great, but kind of a bear to work with/get set up with. My personal recommendation would be to get up and running with the HID mouse example they've got at the V-USB site - I think that's the easiest to understand, easiest to test if it's working project out there. The 1-key keyboard project you've got there is a little more tricky since it uses the internal oscillator option, and it might actually be using the PLL clock option that's unique to the ATtiny line. Regardless, using a crystal just makes for one less thing that can go wrong when using V-USB, and there are a lot of little things that can go wrong.

The big changes you'll need to make will be changing the pin definitions like you did and then changing the makefile. You'll need to make sure that you're choosing the right chip, and then choosing the proper clock speed. That clock speed is important - since V-USB is written in assembly that's tuned for specific clock frequencies, the compiler needs to know which speed of the library it needs to use.

What development system are you using? There are a couple more caveats if you're using AVR Studio, but I generally find it to be easier, and if you're using it, I can give you some more pointers.

Hope it helps!
OpenChord.org - Open Source kits to let you play Guitar Hero with a real guitar, or build your own Wii/PS3/USB controllers.

chatham
 
Posts: 144
Joined: Thu Feb 04, 2010 2:30 am

Re: Adapting Code for the 1-Key Keyboard

by Tiberius on Sat Apr 16, 2011 9:39 am

My personal recommendation would be to get up and running with the HID mouse example they've got at the V-USB site - I think that's the easiest to understand, easiest to test if it's working project out there.

Thanks Alan! I guess I should try the HID mouse first, but I don't know if there's point in just feeding a pre-compiled .hex onto a uC (that's bound to work without any problems anyway). I'll try and recompile the files with some silly changes(like speed of the HID mouse if I can find the right bits of code) instead of just testing if it works or not.

About the crystal: After I have set my fuse bits for a high frequency ext crystal on the Atmega, where do I define the clock speed(12MHz) ? In the makefile or in the main.c source ?

What development system are you using? There are a couple more caveats if you're using AVR Studio, but I generally find it to be easier, and if you're using it, I can give you some more pointers.

I've actually been using WinAVR all this time to program my microcontrollers, and they have their own Makefile generating program, where I only change the uC, port, programmer, and clock speed. Most of these projects come with their own Makefile so I got rather confused...
I have AVRStudio installed and I was fiddling around with it a few days ago.. It accepts the makefile given with the examples as an external one so I guess there's nothing stopping me from using AVRStudio if you think it is easier to work with while using V-USB.
It'll be great if you could share those pointers!

Thank you.
Tiberius
 
Posts: 3
Joined: Tue Apr 05, 2011 4:31 am

Re: Adapting Code for the 1-Key Keyboard

by chatham on Tue Apr 19, 2011 11:56 pm

Clock speed:
You can either define this in the Makefile, or somewhere in one of your files near the top. You'll add a line like this (and it's defined in hertz):
Code: Select all | TOGGLE FULL SIZE
#define F_CPU 12000000

It usually gets defined in the Makefile, but some people put in in the code, or a library if the library needs specific clock speeds.

HID-Mouse:
Especially with your situation, you'll be wanting to compile it for your particular chip. Of course, you'll have to change the TARGET in the makefile to the chip that you're using. But I like it as an example, since the actual main.c code it's got is simple, and if it's working, it's super obvious.

Compliers:
If you're comfortable with WinAVR, just use that. There are some things with paths that AVRStudio doesn't quite do the same, so for V-USB, you have to move files around and change the #include's to point to the correct directories, so if you're comfortable with WinAVR, it'll be easier to get things compiling. I just love me some GUI's, that's all.
OpenChord.org - Open Source kits to let you play Guitar Hero with a real guitar, or build your own Wii/PS3/USB controllers.

chatham
 
Posts: 144
Joined: Thu Feb 04, 2010 2:30 am

Re: Adapting Code for the 1-Key Keyboard

by Tiberius on Sun Apr 24, 2011 1:55 pm

Hey Alan,
I finally got the HIDkeys project working on a mega8, my next task will be to port it properly onto a mega32 or mega16, but before that I have a little problem...

It seems that the code allows for only one key-press to be sent at a time. I can't press 2 keys simultaneously (and I really really want to be able to get 3 key-presses together).
I thought it would be something in the report descriptor but that would call for a revamping of the method:
Code: Select all | TOGGLE FULL SIZE
uchar   usbFunctionSetup(uchar data[8])

And since I'm not too confident with editing that part of the code I thought I'd ask first.

Code: Select all | TOGGLE FULL SIZE
/* We use a simplifed keyboard report descriptor which does not support the
 * boot protocol. We don't allow setting status LEDs and we only allow one
 * simultaneous key press (except modifiers). We can therefore use short
 * 2 byte input reports.

I just found this in main.c... Do you know what/how to edit the code ?

I used WinAVR to program the chip with a USBasp, and it compiled and ran fine on my chip so yeah, I'm rather comfortable using that.

Thanks!


EDIT: never mind, i figured it out !
Tiberius
 
Posts: 3
Joined: Tue Apr 05, 2011 4:31 am

Re: Adapting Code for the 1-Key Keyboard

by Fratsen on Thu Aug 04, 2011 4:28 am

If anyone else is interested in how to get multiple simultaneous button presses to work, check the 4-key-keyboard project, which grew out of the original 1-key-keyboard. The USB report descriptor used in this project can handle up to 8 simultaneous button presses.. which is equal to what an ordinary USB keyboard can handle. - see http://blog.flipwork.nl/?x=entry:entry100224-003937
Fratsen
 
Posts: 6
Joined: Thu Oct 16, 2008 11:15 am

Please be positive and constructive with your questions and comments.