PDA

View Full Version : USB to COM converter



iugmoh
- 18th April 2008, 20:43
Hi all,
after I finished our USBMOUSE and USBKeyboard successfully , I try to make usb to com converter so I try USBCDC emulator example , so now I can send from usb to com but I can't recive from COM to our usb so I think there is wrong in Hserin section, if anyone have hint to help me

here is my code , I use PIC18F4455 with 20MHZ crystal , at baud rate 9600bps
' Pic Configuration
' =================
asm

__CONFIG _CONFIG1L, _USBDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _PLLDIV_5_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_ON_1H & _IESO_OFF_1H

__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L


endasm


INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ; Include if using PBP interrupts

DEFINE OSC 48

DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h
DEFINE HSER_BAUD 9600
DEFINE HSER_CLROERR 1

' USB module
' ----------
UCFG var byte EXT ' include UCFG register... Yeah Melabs didn't :o(
ucfg = %00010100 ' enable internal USB pull-up, Full speed USB

' ANALOG I/O CONFIG
' -----------------
ADCON1 = 15 ' Set all I/Os to Digital
CMCON = 7 ' Disable Comparator

Buffer VAR BYTE[8]
Cnt VAR BYTE
B0 VAR BYTE

ASM
INT_LIST macro ; IntSource,Label,Type,ResetFlag?
INT_Handler RX_INT,_SendUsB,PBP,yes
endm
INT_CREATE ; Creates the interrupt processor
INT_ENABLE RX_INT ; enable external (RX) interrupts
ENDASM

USBInit

' Main Program Loop

MainLoop:
USBService ' Must service USB regularly
Cnt = 8
USBIn 3, Buffer, Cnt, MainLoop

HSEROUT [STR Buffer\Cnt]
GOTO Mainloop

'---[RX_INT - interrupt handler]------------------------------------------------
' PROGRAM COMES HERE WHEN DATA ENTERS THE USART PORT
SendUsB:
HSERIN [B0]
Buffer[0] = b0
cnt = 1

OutLoop:
USBService ' Must service USB regularly
USBOut 3, Buffer, Cnt, OutLoop

@ INT_RETURN

skimask
- 18th April 2008, 21:24
HSERIN [B0]
Buffer[0] = b0

You HSERIN, but how do you know anything is actually there?
How do you know that your connections are is good?
Or that your sending device is working properly?

iugmoh
- 18th April 2008, 22:18
I make a new code to test hserin and hserout , I noted that Hserout working weill but HSERIN not getting any data, I sure that the connection is true but I think that there thing error in code , I don't know

here is my code:

' Pic Configuration
' =================
asm

__CONFIG _CONFIG1L, _USBDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _PLLDIV_5_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_ON_1H & _IESO_OFF_1H

__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L

__CONFIG _CONFIG5L, _CP0_ON_5L & _CP1_ON_5L & _CP2_ON_5L
__CONFIG _CONFIG5H, _CPB_ON_5H
endasm


DEFINE OSC 48

DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h
DEFINE HSER_BAUD 9600
DEFINE HSER_CLROERR 1

' USB module
' ----------
UCFG var byte EXT ' include UCFG register... Yeah Melabs didn't :o(
ucfg = %00010100 ' enable internal USB pull-up, Full speed USB

' ANALOG I/O CONFIG
' -----------------
ADCON1 = 15 ' Set all I/Os to Digital
CMCON = 7 ' Disable Comparator

Buffer VAR BYTE[8]
Cnt VAR BYTE
B0 VAR BYTE
output portb.0
high portb.0
pause 1000
low portb.0

' Main Program Loop

MainLoop:
HSERIN 1000,goo,[B0]
high portb.0
goo:
HSEROUT ["send"]

GOTO Mainloop
end

mister_e
- 18th April 2008, 23:36
did you tried without any USB?

skimask
- 19th April 2008, 07:08
did you tried without any USB?
It always rolls right back to the basics...doesn't it...

mister_e
- 19th April 2008, 10:38
nope.. i haven't talked about led blink yet ;)

iugmoh
- 20th April 2008, 14:35
Finally I dot it , I make a USB to COM converter cable or COM to USB converter cable and save about 40 $ thanks for all members who help me in my project.

But in my project I have some notes :
SEROUT2 PIC_TX,84,["hello",13,10] ; at baud rate 9600 (work)
SEROUT PIC_TX,84,["hello",13,10] ; at baud rate 9600 (not work)
SEROUT PIC_TX,T9600,["hello",13,10] ; at baud rate 9600 (work)
serin rx,T9600,b0 ( work)
serin rx,T9600,[b0] (not work)

* configurations for USART will be on osc defined in pbp code not on external crytall
for example you will get configurations for 9600 at osc=48MHZ not on osc=20MHZ
does the brackets in serin have any meaning ?

mister_e
- 20th April 2008, 15:19
SEROUT/SEROUT2 baudrate mode are quite different. Both are listed in the manual

as for the bracket in SERIN... well, the manual also explain the syntax... the code example is also self-explanatory

' Wait until the character “A” is received serially on Pin1 and put next character into B0
SERIN 1,N2400,["A"],B0

SERIN/SEROUT don't use the USART, it's software solution. HSERIN/HSEROUT use the USART.

And you're right, you use the OSC value for your USART.