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).
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.
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: