CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

RS232 USB VCOM

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
moryoav



Joined: 23 Feb 2014
Posts: 35

View user's profile Send private message

RS232 USB VCOM
PostPosted: Mon Mar 10, 2014 3:13 pm     Reply with quote

helllo,
I'm using pic18f2553 and trying to make a simple adc sampling code via rs232. the problem I'm having is setting up the vcom to use in the pcw terminal.
my code so far is this:
Code:
#include <18f2553.h>
#fuses NOWDT, NOPROTECT, HS   
#DEVICE ADC=10           
#use DELAY (crystal=24MHz)     
#use rs232 (xmit=pin_C6, rcv=pin_C7)                 
#include <usb_cdc.h>   //*****       
                           
void main(void)               
{                               
   int16 res_X;
   setup_ADC_ports (AN0);             
   setup_ADC (ADC_CLOCK_DIV_32);       
   set_ADC_channel(0);                                                                                                                                 
   while(TRUE)
   {                             
      delay_us(15);
      res_X=read_ADC();                               
                                                                         
      printf("X=%lu\n\r", res_X);   
   }                                           
}                     

the #include marked with *** is from what i understood, what needed to be to set up the vcom.
I've got a code from my teacher relating to the vcom issue in this uC though I'm not sure how to use it (include <>||"" ?) and if it relates to ADC at all, because i see mostly SPI stuff in it.
this is the code:
Code:
// this is a CDC USB code
//   
// Send 1 or 2 or 3 or or 5 to turn the sector laser On.
// Send  0xff and 0x00 - 0xfe multiply by 0.25 ms  to set the period and 0x00 - 0xfe multiply by 0.25 ms  to set the duty cycle
// default settings is 200Hz 20% Duty cycle
/////////////////////////////////////////////////////////////////////////

//set to 1 to use a PIC's internal USB Peripheral
//set to 0 to use a National USBN960x peripheral
#define __USB_PIC_PERIF__ 1

#if !defined(__PCH__)
 #error USB CDC Library requires PIC18
#endif

#if __USB_PIC_PERIF__
  #include <18F2553.h>

  //configure a 20MHz crystal to operate at 48MHz
  #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL6,CPUDIV1,VREGEN
  //#fuses   USBDIV, PLL1, CPUDIV1, PROTECT, NOCPD, noBROWNOUT,HSPLL,NOWDT,nolvp, VREGEN
  #use delay(clock=48000000)

/////////////////////////////////////////////////////////////////////////////
//
// If you are using a USB connection sense pin, define it here.  If you are
// not using connection sense, comment out this line.  Without connection
// sense you will not know if the device gets disconnected.
//       (connection sense should look like this:
//                             100k
//            VBUS-----+----/\/\/\/\/\----- (I/O PIN ON PIC)
//                     |
//                     +----/\/\/\/\/\-----GND
//                             100k
//        (where VBUS is pin1 of the USB connector)
//
/////////////////////////////////////////////////////////////////////////////
///only the 18F4550 development kit has this pin
//#if __USB_PIC_PERIF__ && defined(__PCH__)
// #define USB_CON_SENSE_PIN PIN_B2
//#endif
//
// Includes all USB code and interrupts, as well as the CDC API
#include <usb_cdc.h>
#include <XL362.h>
#include <xl362_io.h>
#define DefPeriod   20     
#define DefDuty     4   
#use spi(Di=PIN_B0, Do=PIN_C7, CLK=PIN_B1)
//!#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
//!#define SPI_MODE_1  (SPI_L_TO_H)
//!#define SPI_MODE_2  (SPI_H_TO_L)
//!#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)
//#rom int 0xf00000={1,2,3,4}

char InChar=0;

void main() {
 
 //  setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);
   
   

   usb_cdc_init();
   usb_init();

 

   while(!usb_cdc_connected()) {}
      printf(usb_cdc_putc, "Go yooo\r\n");              // Display contents of the first 64
   //   (0x0a,0x00);

   do {
      usb_task();
      if (usb_enumerated()) {
        if (usb_cdc_kbhit())
        {
            InChar=usb_cdc_getc();
            output_toggle(PIN_C1);
        }   
      }
     
   } while (TRUE);
   

}


Hope you guys can help because I'm lost.
Ttelmah



Joined: 11 Mar 2010
Posts: 19366

View user's profile Send private message

PostPosted: Mon Mar 10, 2014 4:06 pm     Reply with quote

Specific things:
1) Look at the sections that setup the crystal oscillator in what you have been given. These and the fuses that go with these are essential. USB _requires_ certain clock rates. You are not using one of these.... Lines 20, and 22.

2) Currently 'printf' is being sent to the hardware serial port, not the USB 'virtual' comm. Look at how the printf is done in what you have been given. Note the 'usb' bit in front of the format data.

3) Slow down. 5uSec between samples, dumped straight to the USB. If it worked, your screen would be full of values in a few tenths of a second.
moryoav



Joined: 23 Feb 2014
Posts: 35

View user's profile Send private message

PostPosted: Mon Mar 10, 2014 4:57 pm     Reply with quote

ok so i read here and there about the usb and here is what i've came up with so far:

Code:
#include <18f2553.h>
#fuses NOWDT, NOPROTECT, HS   
#DEVICE ADC=10           
#use DELAY (crystal=48MHz)     
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)                 
#include <usb_cdc.h>                       
                         
void main(void)               
{                               
   int16 res_X;   
   setup_ADC_ports (AN0);             
   setup_ADC (ADC_CLOCK_DIV_32);                                                                                                                   
   set_ADC_channel(0);
   usb_init();
   while(TRUE)   
   {                             
      delay_ms(150);                                                                                                                         
     
     
      while (!usb_cdc_kbhit())
      {                     
      res_X=read_ADC();
      usb_cdc_getc()=res_X;
      if usb_cdc_putready()   
      printf("X=\n\r",usb_cdc_putc(res_X));
      }
   }                                             
}                                           

also idk why but i get the "a numeric expression must appear here" error at the last line [ printf("X=\n\r",usb_cdc_putc(res_X)); ] where the bold text is.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Mar 11, 2014 10:21 am     Reply with quote

Quote:

where the bold text is.


i suspect the printf compilation is failing because you did NOT
include a formatting call - but did include a function that returns a value.

if the second parameter were to be ignored - all that would be emitted is
"X=" and a new line

"X=%u\n\r" is more likely to compile
temtronic



Joined: 01 Jul 2010
Posts: 9173
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 11, 2014 10:47 am     Reply with quote

hmm...
I think ...
"X=%u\n\r" is more likely to compile

should be ...
"X=%Lu\n\r"

as the variable 'res_X' is an int16 ( a 'long' or 16bit value).


hth
jay
moryoav



Joined: 23 Feb 2014
Posts: 35

View user's profile Send private message

PostPosted: Tue Mar 11, 2014 10:48 am     Reply with quote

asmboy wrote:

"X=%u\n\r" is more likely to compile

that was what i first did...it says the numeric expression need to be in the () of the usb_cdc_putc.
i tried playing with intx, char, different order to stuff and nothing seemed to help.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 11, 2014 11:24 am     Reply with quote

The usb_cdc_putc() function doesn't return a value.
Look at the function definition in the CCS driver file. It returns 'void'.
Nothing will be displayed by printf even if he has the correct format string.
The compiler knows this and that's why it's giving you the error message.
It wants some value that it can display.
Quote:

void usb_cdc_putc(char c)
{
while (!usb_cdc_putready())
{
.
.
.


Also, the original poster needs to learn C. This is not a beginning C forum.
You're expected to already know the basics (like printf format strings), etc.
before you ever post on here. We do not want to teach bonehead C.
http://www.ccsinfo.com/forum/viewtopic.php?t=48708&highlight=tutorials
moryoav



Joined: 23 Feb 2014
Posts: 35

View user's profile Send private message

PostPosted: Tue Mar 11, 2014 12:20 pm     Reply with quote

PCM programmer wrote:
The usb_cdc_putc() function doesn't return a value.

true tbh i didnt notice that when i checked the header. though now i'm lost. after getting a reading via adc_read(), how can i print the results with the usb-rs232 connection? :\
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 11, 2014 1:11 pm     Reply with quote

What you do is, get a text search utility program, or you could use the
text search built-in to Windows (press F3), and search the CCS Examples
directory for usb_cdc_putc. This will show you how CCS does it.

For example, in this CCS file,
Quote:
c:\program files\picc\examples\ex_usb_hid_and_cdc.c

they are reading the eeprom, and then using printf to format that number
into ASCII Hex, and then they are transmitting it via the usb_cdc_put()
function. CCS has a special feature for printf where you can put a
different output function as the first parameter and it send printf's output
to that function, one character at a time until it's done. See below:
Code:
            printf(usb_cdc_putc, "%2x ", MY_READ_EEPROM( i*16+j ) );

Here is one where they are not displaying a parameter, but instead just
sending a text string to usb_cdc_putc() to be displayed:
Code:
      printf(usb_cdc_putc, "\r\n\nLocation to change: ");
moryoav



Joined: 23 Feb 2014
Posts: 35

View user's profile Send private message

PostPosted: Tue Mar 11, 2014 2:04 pm     Reply with quote

i'd like to know if i understood it:

Quote:
while (1)
{
X_VAL=read_ADC(); //adc value go to rcv
res_X=usb_cdc_getc(); //value go from rcv to xmit
printf(usb_CDC_putc, "X=\n\r") ; //print value from xmit
}


the program compiles with usb_CDC_putc. what i dont understand is why the numeric expression error shows only for usb_CDC_putc() but not usb_CDC_putc.

also i get a few warnings now "interrupt disabled during call to prevent re-entrancy" about the functions usb_tbe, usb_cdc_flush_out_buffer, usb_flush_out and - usb_token_reset, although beside being in the <usb_cdc.h> include, they're not being used in the program
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 11, 2014 2:42 pm     Reply with quote

Quote:
printf(usb_CDC_putc, "X=\n\r") ; //print value from xmit

You're still not using printf correctly. You need to read all the previous
posts in this thread, AND you need to read tutorials on the internet of
how to give a proper format string and data to printf.

You must study.

When I was first starting out, eons ago, learning Z-80 assembler,
I spent 2-3 hrs every night after work studying the Intel 8080 manual
and learning ASM.

You have to study.
Ttelmah



Joined: 11 Mar 2010
Posts: 19366

View user's profile Send private message

PostPosted: Tue Mar 11, 2014 3:23 pm     Reply with quote

Whoa....

Look at what was in the example you have been given:
Code:

//configure a 20MHz crystal to operate at 48MHz
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL6,CPUDIV1,VREGEN
#use delay(clock=48000000)


Then look at what you have:
Code:

#fuses NOWDT, NOPROTECT, HS           
#use DELAY (crystal=48MHz)


Very different.

The _maximum supported crystal frequency_ is 25MHz (read the data sheet). Operating above this is done using the PLL.

Now the posted example is wrong. It'll require a 24MHz crystal. It says 20MHz.

What happens is that the crystal, is divided by the PLL value (PLL6 in the example), and must give 4MHz. So with your 20MHz crystal PLL5 is needed. Then this is fed to the PLL, which multiples it by 24* to feed the USB. The USB then divides this by 2, and the fuse 'CPUDIV1', says to feed the CPU off this frequency (hence 48MHz).

If you are using a 48MHz crystal, this won't work (probably run at 24MHz).

You also need the VREGEN fuse (which says turn on the USB voltage regulator), and the USBDIV fuse (which feeds the USB off the PLL), and NOLVP (otherwise the chip won't run reliably, unless the LVP programming pin is pulled to ground).
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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