Affichage des articles dont le libellé est usb. Afficher tous les articles
Affichage des articles dont le libellé est usb. Afficher tous les articles

dimanche 6 mai 2012

USB communication


In this tutorial I'm using a PIC18F4550 with 48MHz crystal to communicate through usb with PC.
If you are using proteus ISIS I'm happy to tell you that there's a wonderful feature which create a virtual usb driver so you don't need any electronics to test your program.

I'm going to show you how to create a project that includes the usb HID (Human interface devices) library. So after this project you will be able to create your own HID devices with your product and vendor IDs.

First start by creating a new Projects with all the settings mentioned above and copy this code which is by the way the example code given by the Help.

unsigned char readbuff[64] absolute 0x500;   // Buffers should be in USB RAM, please consult datasheet
unsigned char writebuff[64] absolute 0x540;

char cnt;
char kk;

void interrupt(){
   USB_Interrupt_Proc();                   // USB servicing is done inside the interrupt
}

void main(void){
  ADCON1 |= 0x0F;                         // Configure all ports with analog function as digital
  CMCON  |= 7;                            // Disable comparators

  HID_Enable(&readbuff,&writebuff);       // Enable HID communication

  while(1){
    while(!HID_Read())
      ;

    for(cnt=0;cnt<64;cnt++)
      writebuff[cnt]=readbuff[cnt];

    while(!HID_Write(&writebuff,64))
      ;
  }
}
The code simply send back what he received ^^

Now Tools->HID terminal

Set your VID and PID your Vendor Name and Product Name as you want.
Then select the mikroC radio and save your descriptor file with your project files (not an obligation but recommended :p ).

Go to view-->project manager
right click on the "source" folder and choose "add files to project" to add the descriptor file you already created.

before compiling one last step:
go to edit project and set it like in the picture


Finally Compile your project. It should work fine if you are not using the demo version.


lundi 30 janvier 2012

Reading from a Sony joystick


Well this is all started when I wanted to send data from a joystick to my laptop to control a robot. What seemed to be obvious for me is that I have to use one of the gaming library to interface my joystick.
after a lot of researches I found 2 decent library the XNA framework and the SlimDx library.
I didn't know why but I didn't like these frameworks.
So I thought .. the joystick is an HID device hence I can interface it just like any other HID device
I started with searching HID library fot c#. I found libusbdotnet seemed intresting but lack of documentation. then I found the USBHIDLIBRARY and I work on it.
This library is just amazing not so complex, simple documentation and does the job properly.
Here is my C# code for interfacing the joystick:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using USBHIDDRIVER.USB;
using USBHIDDRIVER.TESTS;
using USBHIDDRIVER.List;
using USBHIDDRIVER;
using System.Threading;

namespace USBWork
{
    public partial class Form1 : Form
    {
        USBHIDDRIVER.USBInterface usb = new USBInterface("vid_0079", "pid_0006");
        byte[] currentread=new byte[1024];

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            USBHIDDRIVER.USBInterface usb = new USBInterface("vid_0079", "pid_0006");
            if (usb.Connect()) lblcnx.BackColor = Color.Green;
        }

        private void usbEventHandler(object sender, EventArgs args)
        {
            lblreading.BackColor = Color.Green;
            ListWithEvent list = (ListWithEvent)sender;
            byte[] byte_array = (byte[])list[list.Count-1];
            //txtusb.Text = "start!\n";
            for (int i = 0; i < byte_array.Length; i++)
            {
                currentread[i] = byte_array[i];
            }
          
          
         }
       
            private void btnread_Click(object sender, EventArgs e)
            {
                usb.enableUsbBufferEvent(new EventHandler(usbEventHandler));
                usb.startRead();
                timer1.Enabled = true;
            }

            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                usb.stopRead();
            }

            private void timer1_Tick(object sender, EventArgs e)
            {
                txtusb.Text+= currentread[6].ToString();
                if (currentread[6] == 15) chknone.Checked = true; else chknone.Checked = false;
                if (currentread[6] == 143) chkcercle.Checked = true; else chkcercle.Checked = false;
                if (currentread[6] == 31) chktriangle.Checked = true; else chktriangle.Checked = false;
                if (currentread[6] == 47) chkcarreau.Checked = true; else chkcarreau.Checked = false;
                if (currentread[6] == 79) chkX.Checked = true; else chkX.Checked = false;
            }          
                  
        }      

 }