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 CCS Technical Support

18f87j50 usb problem (serial example) - Solved

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



Joined: 10 Oct 2012
Posts: 2

View user's profile Send private message

18f87j50 usb problem (serial example) - Solved
PostPosted: Mon Oct 15, 2012 4:37 pm     Reply with quote

I am using PIC18F87J50 USB demo board, MPLAB IDE 8.50 editor and Ccsc.exe (4.3.0.337) compiler, and created a USB project based on PICC Examples ex_usb_serial.c. The PIC works from the beginning by blinking LED0, but my PC did not recognize the USB.
I defined OSCILLATOR_FREQUENCY as 48000000, and I tried #FUSES PLL in different value but I believe it should be PLL3.
Anyone can tell me what I did is wrong? And could you please post a simple workable USB code for 18F87J50?

Thank you very much.
Code:

//******* My code ************************
#ignore_warnings none
#include <18F87J50.h>
#define OSCILLATOR_FREQUENCY 48000000

#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOXINST
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NOPROTECT //Code not protected from reading
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES MSSPMSK7
#FUSES NOCPUDIV
#FUSES PLL3 //PLL PreScaler
#FUSES HS
#FUSES HSPLL

#use delay(clock=OSCILLATOR_FREQUENCY)
#use rs232(baud=9600,parity=N,xmit=PIN_C6, rcv=PIN_C7, bits=8,STREAM=DEBUG232,errors)
#define USB_CON_SENSE_PIN PIN_B5
#define LED1 PIN_E1
#define LED2 PIN_E0
#define LED3 PIN_E2
#DEFINE BUTTON PIN_B4
#define LED_ON output_low
#define LED_OFF output_high
#define LED0 PIN_E0
#define LED1 PIN_E1

#define BUTTON_PRESSED() FALSE //no push button on this hardware
#include "ex_usb_common.h"
#include <usb_cdc.h>
#include <stdio.h>
//
// 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)
uart_printf("USB connected, waiting for enumaration...\r\n\n");
if (!new_connected && last_connected)
uart_printf("USB disconnected, waiting for connection...\r\n\n");
if (new_enumerated && !last_enumerated)
uart_printf("USB enumerated by PC/HOST\r\n\n");
if (!new_enumerated && last_enumerated)
uart_printf("USB unenumerated by PC/HOST, waiting for enumeration...\r\n\n");
if (new_cdc && !last_cdc)
uart_printf("Serial program initiated on USB<->UART COM Port\r\n\n");

last_connected=new_connected;
last_enumerated=new_enumerated;
last_cdc=new_cdc;
}

// transmit to the host new dcd/dsr, it's value based on button.
void cdc_serial_state_task(void)
{
cdc_serial_state_t newState;
static cdc_serial_state_t lastState;

memset(&newState, 0x00, sizeof(newState));

newState.bRxCarrier = BUTTON_PRESSED();
newState.bTxCarrier = newState.bRxCarrier;

if (memcmp(&newState, &lastState, sizeof(newState)) != 0)
{
uart_printf("\r\nNew button state being sent: ");
//change in state, send it.
if (usb_cdc_serial_state(newState))
{
//state was sent ok, save it in memory so we don't resend it.
memcpy(&lastState, &newState, sizeof(lastState));
uart_printf("OK");
}
else
{
uart_printf("FAIL");
}
uart_printf("\r\n");
}
}


void main (void)
{
char c;

////////////////// Initialization PIC ////////////////////////////////////
set_tris_a(0xFF); output_a(0xFF); set_tris_b(0x00); output_b(0x00);
set_tris_c(0xA0); output_c(0xA0); set_tris_c(0xB8); output_c(0xF8); set_tris_d(0x00); output_d(0x00); set_tris_e(0x00); output_e(0x00); set_tris_f(0x1F); output_f(0x1F);
set_tris_g(0x03); output_g(0x03); set_tris_h(0x00); output_g(0x00);
// Verify if the PIC is running
output_high(LED0);
delay_ms(250);
output_low(LED0);
delay_ms(250);
HW_INIT();
LED_OFF(LED1);
LED_OFF(LED2);
LED_OFF(LED3);
uart_printf("\r\n\nCCS CDC (Virtual RS232) Example\r\n");

#if defined(__PCH__)
uart_printf("\r\nPCH: v");
uart_printf(__PCH__);
#elif defined(__PCD__)
uart_printf("\r\nPCD: v");
uart_printf(__PCD__);
#else
uart_printf("\r\n\PCM: v");
uart_printf(__PCM__);
#endif
uart_printf("\r\n");
usb_init_cs();
#if !(__USB_PIC_PERIF__)
uart_printf("USBN: 0x%X", usbn_get_version());
uart_printf("\r\n\n");
#endif
while(TRUE)
{
output_toggle(LED0);
usb_task();
usb_debug_task();
cdc_serial_state_task();
if (uart_kbhit())
{
c=uart_getc();
if (c=='\n') {usb_cdc_putc('\r'); usb_cdc_putc('\n');}
else if (c=='\r') {usb_cdc_putc('\r'); usb_cdc_putc('\n');}
else if (c=='!') {uart_printf(usb_cdc_putc,"HELLO-WORLD-HELLO-WORLD-HELLO-WORLD-HELLO-WORLD-HELLO-WORLD-HELLO-WORLD-HELLO-WORLD-HELLO-WORLD-DONE");}
else {usb_cdc_putc(c);}
}
if (usb_cdc_kbhit())
{
c=usb_cdc_getc();
if (c=='\n') {uart_putc('\r'); uart_putc('\n');}
else if (c=='\r') {uart_putc('\r'); uart_putc('\n');}
else {uart_putc(c);}
}
delay_ms(250);
}
}


Last edited by blueseasd on Mon Oct 22, 2012 3:56 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 15, 2012 4:45 pm     Reply with quote

What's your crystal frequency ?

Read Ttelmah's post near the end of this thread. He explains how the
PLLx fuse works and how to select the correct one:
http://www.ccsinfo.com/forum/viewtopic.php?t=48812
blueseasd



Joined: 10 Oct 2012
Posts: 2

View user's profile Send private message

PostPosted: Mon Oct 15, 2012 6:04 pm     Reply with quote

The crystal frequency is 12 MHZ.

Thanks.
jsafaie



Joined: 21 Dec 2012
Posts: 5

View user's profile Send private message

PostPosted: Sat May 18, 2013 8:11 am     Reply with quote

hi blueseasd,
hope your problem has been solved.
I have a same problem with PIC18F87J50 USB demo board,
Could you give me a very simple and basic program for communicating some data between a PC and PIC ?
thanks in advance
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