Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Not reading data: USB with a PIC16F628A

Status
Not open for further replies.

nomdomokunnom

New Member
Hi guys,

Sorry if this is a frequently-asked question, but can anyone tell me where I can find a good starting guide to using USB with PICs? I'm trying to figure it out for myself using a 16F628A, a modified USB cable, and reading USB Complete 4th ed by Jan Axelson, but so far I've had no luck: as an initial connection test I'm just trying to give myself a visual confirmation that the device is receiving any data from the host upon connection, but it's coming up as a blank.

It's possible it's my code that's the issue; I'm an experienced programmer, but I've never operated at such a low level before so I'm not sure if I'm attempting to read the data correctly. If anyone can point me to some functional C or C++ code for at least performing the enumeration stage of a USB connection that'd be extremely helpful (either that or point me in the right direction for reading/writing data correctly; my code is below).

Code:
#include <system.h>

#pragma DATA 0x2007, _BODEN_ON & _CP_OFF & _DATA_CP_OFF & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _MCLRE_ON & _HS_OSC
#pragma CLOCK_FREQ 12000000

void main()
 { 
 set_bit(trisb,6); //USB D+
 set_bit(trisb,7); //USB D-
 clear_bit(trisa,1); //LED1
 clear_bit(trisb,4); //LED2
 
 while(true) 
  {
  set_bit(porta,1);
  set_bit(portb,4);
  
  char data=0;
 
  while(true) //keep checking PB6 and PB7 for data, light LEDs if '1' bits received
   {
   if(test_bit(portb,6))
    clear_bit(porta,1); //light LED1
   else
    set_bit(porta,1);
   
   if(test_bit(portb,7))
    clear_bit(portb,4); //light LED2
   else
    set_bit(portb,4);
   
   //delay_ms(255);
   }
  /*
   **Ignore: early attempt to read in a single byte
  for(int i=0;i<8;i++)
   {
   data<<=1;
   data|=test_bit(portb,6);
   }
  
  if(data==0)
   clear_bit(porta,1); //turn on the red LED if it was a null byte
  */
  }
 }

Thanks in advance. If I'm doing something fundamentally wrong which can't be simply corrected (and I won't be too surprised), I'll gratefully take a look at any tutorials someone can suggest.
 
Last edited:
I think your headed the wrong way here Usb doesn't set there and send data out. It will turn off.

You can bit bang Usb but not with a 16f628a there no room for the USB stack.

Now if you want to catch say the data that a usb mouse sends you could do that just watch the D+ and D- lines but your mouse will need to be hooked up to the computer. That's because it just sitting there waiting to be told what to do. It will not send data till you ask for it>
 
blueroomelectronics said:
The 16F series aren't powerful or fast enough, the 18F2550 has USB hardware.
Of the handful of PICs I bought a short while ago I've thus far only used the 16F628A, but I did also buy an 18F4550 and an 18F1320; will either of these do the job? If not, I'll look into getting a 2550.

blueroomelectronics said:
USB host is even more complex, you need an OTG USB PIC.

I'm not looking to use the PIC as a USB host; I'm connecting it to my computer. I'm planning on writing a driver for the device but for the moment I'm just experimenting with getting the device enumerated by the host computer; one step at a time, after all. I know little about the actual communication process in USB however, just what I'm reading in USB Complete 4th ed. as I go along, so maybe I'm off base here. I'm basically just aiming to figure out how to get an application communicating with the PIC via USB, since it's likely to be a useful feature for future projects. My understanding based on what I read in the book is that when a device is connected to a host's USB port, it'll send a request for a descriptor of the device's function, capabilities, the identity of a suitable driver, etc. so though I don't know what exactly to expect, I was expecting to see some attempt from the host to communicate with my device, but saw no evidence of any data being received.
 
Last edited:
If you want to learn about usb Microchip has all you need to no right here **broken link removed**
 
Status
Not open for further replies.

Latest threads

Back
Top