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

PIC18F2550 USB serial corruption

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



Joined: 12 Nov 2005
Posts: 27

View user's profile Send private message

PIC18F2550 USB serial corruption
PostPosted: Sat Nov 12, 2005 4:34 pm     Reply with quote

I'm having some trouble with a program that I've tried to convert from RS232 to USB. The program works perfectly when the chip is connected to a serial port; however, when sending data using USB, the output is less than ideal.

I adopted the USB code from the serial2 example and everything related to connecting and recognizing the chip as an emulated RS232 port work fine. I have the USB connected to the PIC with a connection sense pin. I also have a 220nF capacitor on Vusb as the datasheet recommends. The problem is that while it sends data to the terminal program at a relatively consistent rate, the PIC seems to send a garbled character every few seconds at an inconsistent rate. When I do the "r" command and read the data back from the EEPROM, everything goes haywire and it rarely gets past 500 outputs (the PIC either freezes up or it just breaks out of the loop somehow and resumes the outputs in the main do loop.

I've tried a larger Vusb capacitor and I've also tried inserting waits in between the printf's to give the USB module time to send the data; however, nothing is solving the problem (although the waits help cut down the frequency, the corruption is happening at a completely unacceptable level). Any suggestions would be greatly appreciated!

Compiler Version: 3.235
Target PIC Microcontroller: PIC18F2550
Standard Run Mode
Target voltage: 5V
Target oscillator speed, type: 20MHz resonator PLL & div to 48MHz

Code:
#include <18F2550.h>

//configure a 20MHz crystal to operate at 48MHz
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(stream=PC, baud=115200, parity=N, bits=8, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(master,sda=PIN_B1,scl=PIN_B0)

#build(reset=0x1, interrupt=0x5)          // Necessary for Bootloader
#ORG 0x0F00,0x0FFF {}                     // Necessary for Bootloader

#define DQ PIN_B7                         // 1-Wire pin assignment
output_float(DQ);                         // Make sure to have a 4k7 pullup on bus
#define LEDmeasurement PIN_B6             // Measurement LED pin assignment
output_high(LEDmeasurement);              // Initially makes Measurement LED high
#define USB_CON_SENSE_PIN PIN_B2

#define __USB_PIC_PERIF__ 1               // Set to 1 to use a PIC18 internal USB

#include <24256.c>                        // Include 24LC256 EEPROM Library
#include <1wire.h>                        // Include the 1-Wire Library
#include <usb_cdc.h>                      //USB code and interrupts & CDC API

//#rom int 0xf00000={1,2,3,4}

const int16 maxRecordLength = (16383*2) + 2;    // Maximum for 24LC256 is 32768
                                          // First two bytes are for app use

int8 scratch[9];                          // Lowest 2 bytes are for temp
int8 uReceive;                            // Temporary byte for USART receive
int8 loggingDelay;                        // Logging Delay in seconds
int8 theFamily;                           // The 1-wire device family code
int16 curAddress;                         // Address to write the next reading

void getScratchpad() {
   int8 i;

   ow_reset();                            // Reset the 1-wire bus, check presence
   ow_write_byte(0xCC);                   // Skip ROM command
   ow_write_byte(0x44);                   // Temperature convert command
   output_float(DQ);                      // Strong pull-up for conversion
   delay_ms(750);                         // Max. time for conversion is 750mS
   ow_reset();                            // Reset the 1-wire bus, check presence
   ow_write_byte(0xCC);                   // Skip ROM command
   ow_write_byte(0xBE);                   // Read Scratch Pad command

   dowcrc = 0;                            // Reset the crc to start a new calc.

   for (i=0;i<=7;i++)
   {
       scratch[i] = ow_read_byte();       // Read each scratchpad byte
       ow_crc(scratch[i]);                // Accumulate the CRC
   }

   scratch[8] = ow_read_byte();           // Receive CRC byte
   ow_reset();                            // Reset the 1-wire bus, check presence
}

void readUSBData()
{
   int16 i;
   int8 n;

   uReceive = usb_cdc_getc();
   printf(usb_cdc_putc, "\n\r\n\r");

   switch (uReceive) {
      case '?': {
            printf(usb_cdc_putc, "Commands (note: all lowercase):\n\r");
            printf(usb_cdc_putc, "   c = Echo counter value\n\r");
            printf(usb_cdc_putc, "   f = Echo family code\n\r");
            printf(usb_cdc_putc, "   t = Change timing interval\n\r");
            printf(usb_cdc_putc, "   r = Read back data from EEPROM\n\r");
            printf(usb_cdc_putc, "   u = Reset counter");
         }
         break;
      case 'c': printf(usb_cdc_putc, "Counter: %lu", curAddress/2);
         break;
      case 'f': {
            theFamily = ow_familyCode();
            switch (theFamily)
            {
               case 0x10: printf(usb_cdc_putc, "DS1820/DS18S20 Connected");  // DS1820
                  break;
               case 0x22: printf(usb_cdc_putc, "DS1822 Connected");          // DS1822
                  break;
               case 0x28: printf(usb_cdc_putc, "DS18B20 Connected");         // DS18B20
                  break;
               default : printf(usb_cdc_putc, "Unknown"); // Unknown Family
                  break;
            }
            printf(usb_cdc_putc, "\n\rFamily Code: 0x%X", theFamily);
         }
         break;
      case 't': {
            printf(usb_cdc_putc, "Please select logging interval:\n\r");
            printf(usb_cdc_putc, "   1 = 1 second\n\r");
            printf(usb_cdc_putc, "   2 = 2 seconds\n\r");
            printf(usb_cdc_putc, "   3 = 5 seconds\n\r");
            printf(usb_cdc_putc, "   4 = 10 seconds\n\r");
            printf(usb_cdc_putc, "   5 = 15 seconds\n\r");
            printf(usb_cdc_putc, "   6 = 30 seconds\n\r");
            printf(usb_cdc_putc, "   7 = 1 minute\n\r");
            printf(usb_cdc_putc, "   8 = 2 minutes\n\r");
            printf(usb_cdc_putc, "   9 = 4 minutes\n\r");
            uReceive = usb_cdc_getc();
            switch (uReceive) {
               case '1': write_ext_eeprom(0,1);       // Write 1 second
                  break;
               case '2': write_ext_eeprom(0,2);       // Write 2 seconds
                  break;
               case '3': write_ext_eeprom(0,5);       // Write 5 seconds
                  break;
               case '4': write_ext_eeprom(0,10);      // Write 10 seconds
                  break;
               case '5': write_ext_eeprom(0,15);      // Write 15 seconds
                  break;
               case '6': write_ext_eeprom(0,30);      // Write 30 seconds
                  break;
               case '7': write_ext_eeprom(0,60);      // Write 60 seconds
                  break;
               case '8': write_ext_eeprom(0,120);     // Write 120 seconds
                  break;
               case '9': write_ext_eeprom(0,240);     // Write 240 seconds
                  break;
               default: write_ext_eeprom(0,5);        // Write 5 seconds if unknown
                  break;
            }
            loggingDelay = read_ext_eeprom(0);
         }
         break;
      case 'r': {
            printf(usb_cdc_putc, "\n\r");
            i=2;                                      // 0,1 are reserved for program
            do {
               printf(usb_cdc_putc, "%lu,", i/2);                   // Print the index
               delay_ms(1);
               printf(usb_cdc_putc, "%u,", read_ext_eeprom(i));     // Print the MSB
               delay_ms(1);
               ++i;                                   // Increment Read Address
               printf(usb_cdc_putc, "%u\n\r", read_ext_eeprom(i));  // Print the LSB
               delay_ms(1);
               ++i;                                   // Increment Read Address
            } while (i<maxRecordLength);
         }
         break;
      case 'u': {
            curAddress = 2;                  // Reset
            printf(usb_cdc_putc, "Counter has been reset");
         }
         break;
      default: printf(usb_cdc_putc, "Unknown command (type ? for available commands)");
         break;
   }
   printf("\n\r");
}

void main() {
   int8 i;
   int8 tempT;
   int16 stemp16, tempDecimal;

   SETUP_ADC_PORTS(NO_ANALOGS);              // Turn off the ADC ports
   SETUP_ADC(ADC_OFF);                       // Turn off the ADC
   SETUP_TIMER_0(RTCC_INTERNAL|RTCC_DIV_1);  // Use the int. osc. for Timer 0
   SETUP_TIMER_1(T1_DISABLED);               // Disable Timer 1
   SETUP_TIMER_2(T2_DIV_BY_1, 127, 1);
   SETUP_TIMER_3(T3_DISABLED);               // Disable Timer 3
   SETUP_CCP1(CCP_OFF);
   SETUP_CCP2(CCP_OFF);

   usb_init();
   while(!usb_cdc_connected()) {}

   init_ext_eeprom();                        // Initialize the EEPROM
   loggingDelay = read_ext_eeprom(0);        // Read the Logging Delay

   theFamily = ow_familyCode();              // Read the Family Code

   curAddress = 2;
   loggingDelay = 1;

   getScratchpad();

   if (scratch[8] == dowcrc & scratch[4] != 0x7F & (theFamily == 0x22 | theFamily == 0x28))
   {
      ow_reset();                            // Reset the 1-wire bus, check presence
      ow_write_byte(0xCC);                   // Skip ROM command
      ow_write_byte(0x4E);                   // Write Scratchpad command
      ow_write_byte(scratch[2]);             // Write TH
      ow_write_byte(scratch[3]);             // Write TL
      ow_write_byte(0x7F);                   // Write Config (12-bit temp)
      ow_reset();                            // Reset the 1-wire bus, check presence
      printf(usb_cdc_putc, "\n\rSet sensor to 12-bit mode\n\r");
   }

   printf(usb_cdc_putc, "\n\r\n\rOK\n\r");                 // Everything started up ok

   do {
      if (usb_cdc_kbhit()) readUSBData();
     
      getScratchpad();

      if (scratch[8] == dowcrc)              // If the CRC is OK then...
      {
         printf(usb_cdc_putc, "%u,", scratch[1]);                   // Print the MSB
         printf(usb_cdc_putc, "%u\n\r", scratch[0]);                // Print the LSB
         Output_Toggle(LEDmeasurement);
      }
      else
      {
         printf("%u,", 0xFF);                         // Print 0xFF
         printf("%u\n\r", 0xFF);                      // Print 0xFF
      }

      delay_ms(250);                   // Delay for 250 ms
      for (i=1;i<=loggingDelay-1;++i)  // If loggingDelay > 1 sec, delay (loggingDelay - 1 sec)
         delay_ms(1000);
   } while (TRUE);
}


-special [k]
specialk



Joined: 12 Nov 2005
Posts: 27

View user's profile Send private message

PostPosted: Sat Nov 12, 2005 5:08 pm     Reply with quote

Just for some examples of the corruption:

When it is running normally:
Code:
...
1,109
1,110
1,110
1,110
1,110
1,11  <- Corruption
1,110
1,110
1,110
1,111
1,110
...

Code:
...
1,113
1,113
1,113
1,113
1,113
1,43  <- Corruption
1,113
1,114
1,113
1,114
1,114
...

Code:
...
1,115
1,115
1,115
1,115
1,115
1,10?  <- Corruption
1,115
1,116
1,115
1,116
1,116
...


When "r" is pressed to read back EEPROM values:
Code:
...
374,1,47
375,1,47
376,1,47
37781a12aіʍ7.522201215211`MKHjE1$B0t@1܊}82yE2|QHGQRP112j021151Q袆Qt0pȱp`d;qǀb9mVCCUWEep؁`wP0n9QpA|ذ}1aP |CƁ3LDY<TsZ0s_]`xk6uF{:iBi%@|C9Z1,47
378,1,47
379,1,47
380,1,47
381,1,47
382,1,47
383,1,47
384,1,47
385,1,47
38681a12aіʍ7.522201215211`MKHjE1$B0t@1܊}82yE2|QHGQRP112j021151Q袆Qt0pȱp`d;qǀb9mVCCUWEep؁`wP0n9QpA|ذ}1aP |CƁ3LDY<TsZ0s_]`xk6uF{:iBi%@|C9Z387,1,47
388,1,47
389,1,47
390,1,47
391,1,47
392,1,47
393,1,47
394,1,47
395,1,47
396,1,48
...
406,1,47
407,1,48
408,1,47
409,1,47
410,1,47411,1,47
412,1,47
413,1,47
414,1,47
415,1,47
...


-special [k]
Darren Rook



Joined: 06 Sep 2003
Posts: 287
Location: Milwaukee, WI

View user's profile Send private message Send e-mail

PostPosted: Sat Nov 12, 2005 10:05 pm     Reply with quote

Recently (like last week) there was some updates to the CDC driver. What is the last date it says in the version history of the CDC driver?
specialk



Joined: 12 Nov 2005
Posts: 27

View user's profile Send private message

PostPosted: Sun Nov 13, 2005 2:30 pm     Reply with quote

Darren Rook wrote:
Recently (like last week) there was some updates to the CDC driver. What is the last date it says in the version history of the CDC driver?

Code:
///////////////////////////////////////////////////////////////////////////
////                         usb_desc_cdc.h                            ////
////                                                                   ////
//// An example set of device / configuration descriptors for use with ////
//// CCS's CDC Virtual COM Port driver (see usb_cdc.h)                 ////
////                                                                   ////
//// Two examples are provided:                                        ////
////      ex_usb_serial.c                                              ////
////      ex_usb_serial2.c                                             ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////
////                                                                   ////
//// Version History:                                                  ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////

So I guess I have the older version... Is it a simple fix for the CDC issue?

-special [k]
specialk



Joined: 12 Nov 2005
Posts: 27

View user's profile Send private message

PostPosted: Mon Nov 14, 2005 6:41 pm     Reply with quote

specialk wrote:

So I guess I have the older version... Is it a simple fix for the CDC issue?

-special [k]
I'm guessing that it isn't??

-special [k]
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