3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Connecting Nokia 3310 LCD to USB using AVR

Step 5Understanding the USB code

This is where the fun begins!

If you've come this far, you should have a display that says "Display Initialized" when you power up the device.

If you haven't done it already, download the source code attached to this instructable, and we'll have a closer look on how the AVR communicates with the display and the computer.

If you've never worked with V-USB before, it might be very confusing at first. At least it was for me. Scroll down to the usbFunctionSetup function and we'll have a closer look on how it actually works. This is about the only function you need to care about editing besides the main function.

usbFunctionSetup is the function where you process all the data that i sent to the microcontroller over USB. If you notice the IF statements, they all check for a specific number at the rq->bRequest variable. This i where the request codes are stored. You can think of request codes as commands. On the host software, we have a function like this: SendData(int request, in data), the value you put in request parameter will be transferred to the microcontroller and stored in the rq->bRequest variable, and you use this request code to do various things in the usbFunctionSetup.

I have only added 6 request codes, but if you want to make a 7th request code simply type this in somewhere in the usbFunctionSetup function:

// If the request is 7
if(rq->bRequest == 7){ // Command 7 - SEND_MYCOMMAND
   // Do my code
}


If you now call the SendData function like this: SendData(7, 0);
The IF statement you just created will run.

The 2nd parameter in the SendData function is the actual data you send to the microcontroller. This number will be stored in rq->wValue struct. This is a WORD (2 bytes), but if you only want to use 1 of the 2 bytes stored here, you can do that by using the bytes array, like this: rq->wValue.bytes[0]. If you'd like to use the entire word, you can access it like this: rq->wValue.word. Note that you should use an unsigned int if you want to use the entire word.

You should put advanced functions that take a long time to do OUTSIDE of the usbFunctionSetup function, and instead let the main loop execute them. If you have them in the usbFunctionSetup and they use a long time to finish (50ms), you might lose the USB connection to the computer. I chose to have them in the usbFunctionSetup because writing to the display is MUCH faster than the USB (In fact, we write a stable 1,33Mbit/s to the display, and the display supports up to 4Mbits/s ) so I do not risk losing the connection by taking too long.

The rest of the code should be commented enough for you to understand. If something is unclear, let me know and I'll try to clear things up for you.
« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
7
Followers
1
Author:wkter