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

Getting wrong data from serial terminal using 18f2550 usb_cd

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



Joined: 11 Mar 2016
Posts: 62
Location: izmir / Turkey

View user's profile Send private message

Getting wrong data from serial terminal using 18f2550 usb_cd
PostPosted: Fri Jan 27, 2017 4:23 am     Reply with quote

Hello guys,
I'm using sparkfun hx711 load cell amp and trying to read data from a load cell and then sending it to pc via USB. It looks like its working, it gets enumerated, I can connect and listen com port and get data but there is something wrong with incoming data and I cannot see why.
The data I send is 12 bytes in total, I should read |>XXXXXXXX<| but I get incorrect readings from terminal.
Can you help me please?

Code:

#define __USB_PIC_PERIF__ 1

#include <18F2550.h>
#device ADC=10
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HSPLL                    //High Speed PLL
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 // brownout reset BROWNOUT
#FUSES BORV21                   //Brownout reset at 2.0V
#FUSES PUT                      //Power Up Timer PUT
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for 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 IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOPBADEN                 //PORTB pins are configured as digital IO channels 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) NOXINST
#FUSES PLL5                     //Divide By 5(20MHz oscillator input) PLL5
#FUSES CPUDIV1                  //No System Clock Postscaler
#FUSES USBDIV                   //USB clock source comes from PLL divide by 2
#FUSES VREGEN                   //USB voltage regulator enabled
#byte TRISB=0xF93               //TRISB register address for direct access
#use delay(clock=48000000)

#define PDSCK pin_b4
#define PDOUT pin_b0

#include <HX711.h>
#include <HX711.c>
#include <usb_cdc.h>
#include <STDLIB.h>

#define OK  1
#define NOK 0

void main(void)

   unsigned int32 loadvalue;
   TRISB=0x49;
   HX711_set_scale(1375);
   HX711_set_gain(128);
   HX711_tare(20);
   usb_init();
   while(!usb_cdc_connected());
   printf(usb_cdc_putc,"Usb READY\n\r"); 
   while(1)
   {
         loadvalue=HX711_get_units(10);
         printf(usb_cdc_putc,"|");
         printf(usb_cdc_putc,">");
         printf(usb_cdc_putc,"%2X",make8(loadvalue,3));
         printf(usb_cdc_putc,"%2X",make8(loadvalue,2));
         printf(usb_cdc_putc,"%2X",make8(loadvalue,1));
         printf(usb_cdc_putc,"%2X",make8(loadvalue,0));
         printf(usb_cdc_putc,"<");
         printf(usb_cdc_putc,"|\n\r");
         HX711_power_down();
         delay_ms(500);
         HX711_power_up();
   }// End While
}// End Main


And these are data I read from terminal:

Code:

|>00000001<|
|>0000000001<|
|>00000001<|
|>00000001<|
|>000000001<|
|>00000001<|
|>000>00000001<|
|>00000001<|
|>000000000001<|
|>000>00000001<||
|>>0000000001<|

|>00000001<|
|>00000001<|
|>000>000000011<|
|>00000001<|
|>000000001<|
|>000>00000001<|
|>000000001<|
|>00000001<|
|>002>002FA997<|
|>002F>002FA988<|
|>002FFA992<|
|>002FA98CC<|
|>000000003<|
|>002FA9999<|
|>00000000<|
|>00000001<|
|>00000004<|
|>002FA999<|
|>002FA99B<|
|>000000002<|
|>00000009<|

do you have an idea about why this is happening?
_________________
There is nothing you can't do if you try
temtronic



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

View user's profile Send private message

PostPosted: Fri Jan 27, 2017 6:14 am     Reply with quote

Overall, you need to isolate where the problem is
Step one...
I'd simplify your code. Just have the PIC send a KNOWN stream of data to the PC,something like |>12345678<| . Do this several times for various KNOWN data. Don't always use the same data. If this works then you KNOW it's not the communcation of the data, rather the data itself that is 'bad'.

As to 'bad' data...it can either be
a) a bad sensor( faulty ?)
b) incorrect driver for the sensor(timing,bits,??)
c) bad wiring /pcb traces
d) power supply( 5V PIC, 3V sensor )
e) something else....
Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Fri Jan 27, 2017 6:15 am     Reply with quote

You need to include usb_task in your loop.

Even if the USB system is being interrupt driven, this still handles the 'housekeeping' for USB, and must be called at reasonable intervals. Your half second may well be OK, but it does still need to be called....
elcrcp



Joined: 11 Mar 2016
Posts: 62
Location: izmir / Turkey

View user's profile Send private message

PostPosted: Fri Jan 27, 2017 9:16 am     Reply with quote

Thanks for advice, I'll add usb_task Ttelmah. I tried random approaches and found the source of problem and it is somehow interesting =)
I was suspecting speed issues due to data amount I get. I was using USB3 port to connect PIC then I changed it to USB2 (or maybe 1.1 I'm not sure myself) then I started to get data correctly, 12byte data and new line.. all ok now.

I didn't write the hx711 driver codes myself but there were a few little corrections needed. I'll upload full codes after finishing tests.
_________________
There is nothing you can't do if you try
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Fri Jan 27, 2017 9:23 am     Reply with quote

USB3 ports, though they will behave as USB2, have drivers that generate faster edges on the waveforms. It is common for devices that have a slight impedance mismatch, to have problems when they are attached to USB3 ports, even when running in USB2 mode. Add two series resistors in the D+ and D- lines, adjacent to the chip. 22R each typically. These help to reduce the effects of any impedance mismatch (either in the PCB or in the chip itself), when fast edges appear.
temtronic



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

View user's profile Send private message

PostPosted: Fri Jan 27, 2017 10:56 am     Reply with quote

comment: You should use USBView or similar PC program to 'look' at the USB port to confirm/deny HOW the PC has 'configured' it.
Magically 'some' program(ap) changed one I was using, one day, causing me no end of grief....

Jay
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