|
|
View previous topic :: View next topic |
Author |
Message |
CPing Guest
|
usb not working properly |
Posted: Thu Jul 02, 2009 1:27 am |
|
|
Hi,
I want to use the CDC Driver to send a 5 Digit Value to the PIC and store it and read it. I have assigned the commands but when I compile the value being read is not what is programmed. For Example the 89098 Initial Value is read as 23562 When a Read Command is issued. Also is there is a better way to read the USB CDC Input Data in Strings rather than as characters. Please Help.
Code: | #DEFINE LED1 PIN_A5
#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7)
#include <usb_cdc.h>
#include <LcdCustom.c>
/////////////////////////////////////////////////////////////////////////////
//
// Configure the demonstration I/O
//
/////////////////////////////////////////////////////////////////////////////
#define LED2 PIN_B4
#define LED3 PIN_B5
#DEFINE BUTTON PIN_A4
#define LED_ON output_low
#define LED_OFF output_high
Long Int Value=89098;
/////////////////////////////////////////////////////////////////////////////
//
// usb_debug_task()
//
// When called periodically, displays debugging information over serial
// to display enumeration and connection states. Also lights LED1 based upon
// enumeration and status.
//
/////////////////////////////////////////////////////////////////////////////
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"); //none of this is output to hyperterminal
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\nCDC\r\n\n");
}
last_connected=new_connected;
last_enumerated=new_enumerated;
last_cdc=new_cdc;
}
void SetValue(char Num,Int8 Position)
{
int Number;
Number = Num %10;
if(Position == 0)
Value = Value + Number;
else if(Position == 1)
Value = Value + (Number*10);
else if(Position == 2)
Value = Value + (Number*100);
else if(Position == 3)
Value = Value + (Number*1000);
else if(Position == 4)
Value = Value + (Number*10000);
}
void main(void) {
char c;
Int8 Loop=0;
Int8 TxFlag=0;
LED_OFF(LED1);
LED_OFF(LED2);
LED_OFF(LED3);
printf("\r\n\nCDC\r\n");
#ifdef __PCH__
printf("PCH: v");
printf(__PCH__);
#else
printf("PCM: v");
printf(__PCM__);
#endif
printf("\r\n");
usb_init();
#if !(__USB_PIC_PERIF__)
printf("USBN: 0x%X", usbn_get_version());
printf("\r\n\n");
#endif
Lcd_Init();
printf(lcd_putc,"\fCDC Demo");
delay_ms(300);
while (TRUE) {
usb_task();
usb_debug_task();
Lcd_putc("\f");
if (usb_cdc_kbhit())
{
c=usb_cdc_getc();
if(TxFlag==0)
{
Loop=0;
if (c=='\n') {putc('\r'); putc('\n');}
if (c=='\r') {putc('\r'); putc('\n');}
if (c=='$') {printf(usb_cdc_putc, "\r\n\nRESET COMMAND\r\n\n");}
if (c=='V') {printf(usb_cdc_putc, "\r\n\nVerison = 1.0\r\n\n");}
if (c=='P') {printf(usb_cdc_putc, "\r\n\nProduct = Demo\r\n\n");}
if (c=='M') {printf(usb_cdc_putc, "\r\n\nManufacturer = CCS\r\n\n");}
if (c=='s')
{
printf(usb_cdc_putc,"\r\nEnter Value\n\r");
TxFlag=1;
Value = 0;
}
if (c=='R')
{
printf(usb_cdc_putc,"\r\nValue = %LD",Value);
usb_cdc_putc("\n\r");
}
}
else
{
SetValue(c-0x30,4-Loop);
Loop++;
usb_cdc_putc(c);
if(Loop==5)
{
TxFlag=0;
printf(usb_cdc_putc,"\r\nAcknowledged.\n\r");
printf(usb_cdc_putc, "Set Value = %LU",Value);
usb_cdc_putc("\n\r");
Loop=0;
}
}
}
}
} |
|
|
|
Ttelmah Guest
|
|
Posted: Thu Jul 02, 2009 2:43 am |
|
|
The USB hardware transfers bytes.
Bytes=characters.
Why should anything be better?.
Reason for difference. Long integer = 16bit = 65536 _max_. Can't hold 89098. Take the low 16bits of 89098, and you get 23562...
Best Wishes |
|
|
CPing Guest
|
usb not working properly |
Posted: Thu Jul 02, 2009 3:52 am |
|
|
Thanks. That solved the issue but how do I get the input as a string so that I can parse the string. I want to attempt to implement SCPI. |
|
|
Ttelmah Guest
|
|
Posted: Thu Jul 02, 2009 9:48 am |
|
|
The commonest 'parsers' published here, have been for modem control strings, and GPS data. Both could provide bases for parsing SCPI strings with a bit of modification. I posted a 'tree search parser, which could also provide some ideas.
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|
|
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
|