|
|
View previous topic :: View next topic |
Author |
Message |
brahme.girish
Joined: 20 Jun 2010 Posts: 7
|
18f4550 usb controller cdc |
Posted: Thu Oct 21, 2010 12:18 am |
|
|
Dear all,
With best wishes,
As suggested by dear friends from forum, I made changes in my code, hardware and fuse settings, for 18f4550 to for usb_cdc .
Now I am able to connect my pic controller to pc by usb.and also able to transfer a single character from pc.
Now what I have done, I am sending and receiving data from one controller to pic 18f4550 on serial port to send via usb to pc. And sending data from pc to 18f4550 to send it to another controller via serial port.
Now here I have a problem. When I transfer data of more than one character from usb to it is having problem,
for Ex.
If I send 255 from serial to pic 18f4550, but when pic 184550 transfer 255 to pc it is displayed as
255 525 @45 255 552 @@@ .... .... 2555 255 255
Also after some time pic 18f4550 stops sending data to pc.
My code is
Code: |
#include "D:\girish\usb_\main.h"
#use rs232(baud=9600,xmit=PIN_C6, rcv=PIN_C7,ERRORS) // Preprocessor directive that includes RS232 libraries
#define LED2 PIN_d1
#define LED3 PIN_d2
#define LED4 PIN_c0
#DEFINE BUTTON PIN_A4
#define LED_OFF output_low
#define LED_ON output_high
#define __USB_PIC_PERIF__ 1
#DEFINE LED1 PIN_d0
#DEFINE enble PIN_d3
#include <usb_cdc.h>
unsigned int1 bit_rs=0,bit_tr=0;
unsigned char tp,i=0;
unsigned char data[100];
unsigned int x=0;
#int_RDA
void RDA_isr(void){
tp=getc();
if((RS232_ERRORS & 6)!=0) return;
data[i++] = tp;
x++;
if(i>=100)
{
i = 0;
x =0;
}
LED_ON(LED4);
bit_rs =1;
}
#int_TBE
void TBE_isr(void)
{
bit_tr=1;
}
void usb_debug_task(void) {
static int8 last_connected;
static int8 last_enumerated;
int8 new_connected;
int8 new_enumerated;
static int8 last_cdc;
int8 new_cdc;
new_connected=usb_attached();
new_enumerated=usb_enumerated();
new_cdc=usb_cdc_connected();
if (new_enumerated)
LED_ON(LED1);
else
LED_OFF(LED1);
if (new_cdc)
LED_ON(LED2);
else
LED_OFF(LED2);
if (usb_cdc_carrier.dte_present)
LED_ON(LED3);
else
LED_OFF(LED3);
if (new_connected && !last_connected)
printf("USB connected, waiting for enumaration...\r\n\n");
if (!new_connected && last_connected)
printf("USB disconnected, waiting for connection...\r\n\n");
if (new_enumerated && !last_enumerated)
printf("USB enumerated by PC/HOST\r\n\n");
if (!new_enumerated && last_enumerated)
printf("USB unenumerated by PC/HOST, waiting for enumeration...\r\n\n");
if (new_cdc && !last_cdc) {
printf("Serial program initiated on USB<->UART COM Port\r\n\n");
printf(usb_cdc_putc, "\r\n\nCCS CDC (Virtual RS232) Example\r\n\n");
}
last_connected=new_connected;
last_enumerated=new_enumerated;
last_cdc=new_cdc;
}
void main(void) {
char c;
unsigned int j =0;
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
LED_OFF(LED1);
LED_OFF(LED2);
LED_OFF(LED3);
printf("\r\n\nCCS CDC (Virtual RS232) Example\r\n");
usb_init_cs();
#if !(__USB_PIC_PERIF__)
printf("USBN: 0x%X", usbn_get_version());
printf("\r\n\n");
#endif
while (TRUE) {
usb_task();
usb_debug_task();
if (usb_cdc_kbhit()){
c=usb_cdc_getc();
output_high(enble);
printf("%c",c);
if(bit_tr){
output_low(enble);
bit_tr =0;
LED_ON(LED4);
}
if(bit_rs){
while(j<=x){
usb_cdc_putc(data[j++]);//printf(usb_cdc_putc,"%c",data[x]);// printf(usb_cdc_putc, "\r\n espl \r\n");
}
bit_rs=0;
}
delay_ms(1);
LED_OFF(LED4);
}
}
}
|
My fuse settings are like this
Code: |
#include <18F4550.h>
#device ICD=TRUE
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HSPLL //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //Debug mode for use with ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES MCLR //Master Clear pin enabled
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL6 //Divide By 6(24MHz oscillator input)
#FUSES CPUDIV1 //No System Clock Postscaler
#FUSES USBDIV //USB clock source comes from PLL divide by 2
#FUSES VREGEN //USB voltage regulator enabled
#FUSES NOICPRT //ICPRT disabled
#use delay(clock=48000000)//#use delay(clock=24000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) |
Please help me,
Thanks in advance.
regards,
girish |
|
|
MikeP
Joined: 07 Sep 2003 Posts: 49
|
|
Posted: Thu Oct 21, 2010 6:18 pm |
|
|
One thing I see off hand is you never reset the J var to 0 after you are done with it in that while loop. It is set to 0 in the declaration but that only gets done once at program start. If I missed were you do I am sorry.
Code: |
if(bit_rs)
{
while(j<=x)
{
usb_cdc_putc(data[j++]);
}
bit_rs=0;
j = 0;
}
|
|
|
|
brahme.girish
Joined: 20 Jun 2010 Posts: 7
|
|
Posted: Mon Oct 25, 2010 4:49 am |
|
|
Very Very thanx for your kind advise,
i make all correction,there was problem in my coding.
Now i am able to send and receive any length of string.
But I Now i have problem of ground isolation.
If there is any contactor switching around my devise ,my usb controller gets hang.
i have gone through all doccument regarding this usb isolaton problem.including white paper from usb.Org.
But i found some expensive solution like isolation chips from ADE,etc,
is there is any cheap solution or inhouse circut etc?????????
Please help me
Thanks in advance,
Regards,
Girish. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Mon Oct 25, 2010 8:29 am |
|
|
Realistically, USB isolation, is expensive and complex. The best way to approach this, is to isolate the other connections to your device.
You need to work in stages. First, the power supply. How is it derived, isolated, and smoothed?. Can noise be picked up on this?. Then every external connection to your device. You talk about RS232, but if this is going any distance, RS485, is significantly better, and adding opto-isolation to this is relatively easy. Look similarly at every external connection. Then on the USB connection, is your layout using the USB2 layout with the added ground, or just USB1.1?. This makes a difference. Then if you still have connections coming into your device, that can't be isolated, can they be 'trapped' (clamp diodes, transorbs, capacitors, etc. etc..)?. Also look at your actual board design. If you are not using a ground plane, could this be added?. If you are using a ground plane, how do power 'paths' on the board run. Classic problems would be having power circuitry on opposite sides of the board, then logic circuitry that picks up it's 'local' grounds at different points across the board, with induced voltages as a result. This needs 'split' ground planes, and careful design...
There are hundreds of USB interfaced systems in use, that control serious power, or work close to other systems switching high power - getting this reliable, is just down to good design. However remember that USB, is designed as a _local_ bus, and if you start pushing connection distances, and want isolation, far 'better' to switch to something designed for this (Ethernet for example).
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|