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

rs232/xbee baud rate problem
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kcj



Joined: 05 Oct 2011
Posts: 33

View user's profile Send private message

rs232/xbee baud rate problem
PostPosted: Thu Oct 27, 2011 2:04 pm     Reply with quote

Hi all,
I have a question regarding baud rate. I have set up 2 router xbee's sending data back to a coordinator xbee, which in turn is connected to the TX, RX pins of a PIC18F13K22, which displays the data on an 16x2 LCD.

The baud rates possible for xbee are 9600, 19200, 38400, 57600 and 115200 using X-CTU program.

When I try and change rs232, baud rate to 9600 or 19200 in CCS program the program works fine, a bit quicker displaying the data using 19200.

When I program the xbee for 38400 and change rs232 baud rate to 38400 the error message tells me to use 35714, the same is true for faster baud rates. I do this but then the data doesn't transmit right. I am thinking its because of the different rates.

Is there a coding way of getting CCS compiler to work on similar faster baud rates to xbee, the main purpose is to get the LCD to refresh the data approximately every 375 ms, is there any other way to do this?

I am using flexible driver and random pins for LCD connection.

Code:

#include <18f13k22.h>
#device ICD=TRUE                     
#use delay(clock=1M, crystal)
#use rs232(baud=19200,UART1,ERRORS)

#include <Flex_lcd.c>
#define Gled PIN_C5
#define Yled PIN_C4

int i;
int buffer [44];
int data19,data20,data40,data41,byte1,byte11,byte12;
int16 byteA,byteB;
float forceA,forceB;
float voltsA,voltsB;


void main()
{
lcd_init();

while(1)
  {   

//Checks if there are character in the buffer
     if (kbhit(true))
    {       
         //retrieves the bytes   
         output_high(Yled);
          byte1 = getc();         

            //First byte in a 22byte packet xbee sends
            if (byte1 == 0x7E)
            {
                //reads the 44 bytes
                 for(i=0;i<44;i++)
                    {                 
                      byte1 = getc();
                      buffer[i]=byte1;
                    } 
                      byte11=buffer[11];
                     
            }
           
                     //LSB of Router1 64 Bit address(unique to router1)00 13 A2 00 40 69 6E EF
                     if (byte11 == 0x6E)
                         {
                         //byte 19&20 contain the force data
                           data19=buffer[20];
                           data20=buffer[21];
                           data40=buffer[41];
                           data41=buffer[42];
   
                         //converts the 10 bit number in integer
                           byteA=((data19*256)+data20);
                           byteB=((data40*256)+data41);

                         //Formulea calculated in excell relating force data to volts                 
                         //and bytes xbee sends
                          forceA=((1.09*byteA)-305.18);
                          forceB=((1.09*byteB)-305.18);
                          voltsA=(0.0012*byteA);
                          voltsB=(0.0012*byteB);

                          //prints force and volts result on LCD
                          printf(lcd_putc, "\fVoltsA1..%4.2f\n", voltsA);                   
                          printf(lcd_putc, "\ForceA...%4.2f\n", forceA);
                          delay_ms(1000);
                          printf(lcd_putc, "\fVoltsB1..%4.2f\n", voltsB);                   
                          printf(lcd_putc, "\ForceB...%4.2f\n", forceB);
                          delay_ms(1000);
                         }
                            //LSB of Router2 64 Bit address(unique to router1)00 13 A2 00 40 69 71 AB
                            if (byte11 == 0x71)
                             {
                              //byte 19&20 contain the force data
                              data19=buffer[20];
                              data20=buffer[21];
                              data40=buffer[41];
                              data41=buffer[42];
   
                            //converts the 10 bit number in integer
                              byteB=((data19*256)+data20);
                              byteA=((data40*256)+data41);

                            //Formulea calculated in excell relating force data to volts                 
                            //and bytes xbee sends
                              forceB=((1.09*byteA)-305.18);
                              forceA=((1.09*byteB)-305.18);
                              voltsB=(.0012*byteA);
                              voltsA=(.0012*byteB);

                             //prints force and volts result on LCD
                              printf(lcd_putc, "\fVoltsA2..%4.2f\n", voltsA);                   
                              printf(lcd_putc, "\ForceA...%4.2f\n", forceA);
                              delay_ms(1000);
                              printf(lcd_putc, "\fVoltsB2..%4.2f\n", voltsB);                   
                              printf(lcd_putc, "\ForceB...%4.2f\n", forceB);
                              delay_ms(1000);
                               }                         
     }
      else
    printf(lcd_putc, "\fNo Connection\n");
    output_low(YLED);
     delay_ms(500);   
  }
}


Thanks, any help appreciated
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Thu Oct 27, 2011 3:17 pm     Reply with quote

This is not a CCS problem. It is down to clock rates.
Look at the chip's data sheet. Look at section 15.3, and in particular table 15-5. Then realise that with a 1MHz master clock, even at the fastest division mode available, the chip's manufacturer shows 19K2 as possible with 0.16% error, but then doesn't show rates above this as possible with acceptable error.

You have two choices:
1) use a higher clock rate. Go to 2Mhz, and 38K4 is just as easy as 19K2 at 1MHz.
2) Select a crystal that is a binary multiple of the baud rate. So (for example), switching to a 921600Hz crystal, will allow 57600 to be used. Notice in the data sheet the use of the 3.6864MHz crystal, which allows all the displayed rates to be done with 0% error.....

Best Wishes
kcj



Joined: 05 Oct 2011
Posts: 33

View user's profile Send private message

PostPosted: Thu Oct 27, 2011 5:42 pm     Reply with quote

Hi Ttelmah,

Thanks for your reply,i changed to
Code:

#use delay(clock=2M, crystal)
#use rs232(baud=38400,UART1,ERRORS)


and configured the xbee for this rate but the program wont even run.

i have been having a problem with the clock since i started this project.
I am new to this PICS coding etc but am learning quickly. My setup is a 20Mhz crystal connected to OSC pin1& 2with 15pf capacitors and 330 ohm resistor.

I guessed a #use delay(clock=20M, crystal) , would be correct for this setup? but i had problems with the timing using this code,the debug window says MCU = 77.8Mhz using this code for some reason i am not sure off?? after trying to turn an led on and off using a delay command of 1000ms, the code #use delay(clock =1M, crystal), seemed to get the right timing, so i kept it.

I suspect there is something wrong with the initial setup but i am unsure what? or if i have configured the correct fuse, i have not said any in the code but i suspect i should include #fuses XT?? The clock has not been a problem uptill now but it would be good to finally get it sorted.

from my limited knowledge using your answer i am guessing the code would be
Code:
#use delay(clock=3.6864M, crystal)
#use rs232(baud=57600,UART1,ERRORS)

and changing to a 921600Hz crystal would solve the problem? making my circuit compatible to all baud rates?

Thanks for the help.
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Fri Oct 28, 2011 2:28 am     Reply with quote

Er.
The values used in the clock statement, _must_ match the actual crystal you have. You can't change one without the other. If you have been telling the compiler you have a 1MHz crystal, while you actually have a 20Mhz part, then if the oscillator was working, the 19200bps setting, would have been generating 384Kbps.....

However I'd suggest your crystal is not actually oscillating. The chip you are using has a 'fail safe clock monitor', that is enabled by default (you are not setting the fuses to control this), and will switch to the internal RC oscillator, if the crystal is not oscillating. The default for this is 1MHz, which is why selecting the 1MHz rate then works....

I think you have a hardware fault with your oscillator circuit, which is why you are now hitting problems.

Realistically, simplify. Go back to your clock=20MHz test, with a LED, and turn off ICD. Use fuses of:

Code:

#include <18f13k22.h>
#fuses HS,,NOPROTECT,NOBROWNOUT,NOWDT,PUT,STVREN,NOFCMEN,NOMCLR,NOXINST             
#use delay(clock=20Ml)

This forces use of the crystal, and turns off the fail safe clock monitor.
Then see if your speed now gives the right flash rate. If it does, smile, and use these settings. If it doesn't, and flashes but at the wrong rate, consider trying another crystal, and/or reducing the series resistor to about 68R. If it doesn't flash at all, get a magnifying glass on all the traces round the crystal, re-solder them all, look for whiskers, lumps of flux etc..

Best Wishes
kcj



Joined: 05 Oct 2011
Posts: 33

View user's profile Send private message

PostPosted: Thu Jan 26, 2012 4:16 pm     Reply with quote

Thanks Ttelmah,

I finally got round to testing the clock on a breadboard setup. I am using a 20M crystal with 2 x 18pf capacitors and a 68ohm resistor tied to osc2, osc1 and code
Code:

#include <18f13k22.h>
#fuses HS,NOBROWNOUT,NOWDT,PUT,STVREN,NOFCMEN,NOMCLR,NOXINST             
#use delay(clock=20M)


The Speed of the clock says 78.77Mhz instead of 20Mhz. Has anybody any suggestion why this is ? I changed the crystal to another 20M but with the same result.

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 26, 2012 4:33 pm     Reply with quote

Quote:
The Speed of the clock says 78.77Mhz

What instrument or program is reporting this speed ?
kcj



Joined: 05 Oct 2011
Posts: 33

View user's profile Send private message

PostPosted: Thu Jan 26, 2012 4:42 pm     Reply with quote

I tested the crystal using CCSload diagnostics tool.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 26, 2012 4:45 pm     Reply with quote

I'm not familiar with CCSload, but I would use a scope, or a frequency
counter (or a scope with a built-in freq. counter). Or I would use a short
program to blink an LED at a rate of 1 Hz, and then run it and see if it
really runs at that rate. I would not use CCSload for this.
kcj



Joined: 05 Oct 2011
Posts: 33

View user's profile Send private message

PostPosted: Thu Jan 26, 2012 5:02 pm     Reply with quote

Hi, i used the following code
Code:

#include <18f13k22.h>
#fuses HS,NOBROWNOUT,NOWDT,PUT,STVREN,NOFCMEN,NOMCLR,NOXINST             
#use delay(clock=20M)
#define Yled PIN_C4

 void main()
{
while (true)
{
  output_toggle(YLED);
  delay_ms(1000);
}
}

The LED flashed very alot quicker than every 1 second than it should,which makes me believe the clock reading is correct. Which still leaves the problem of how to fix it?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 26, 2012 5:16 pm     Reply with quote

And your code should actually flash the LED once every 2 seconds.
1 second On, and 1 second Off.

What's your compiler version ?
kcj



Joined: 05 Oct 2011
Posts: 33

View user's profile Send private message

PostPosted: Thu Jan 26, 2012 5:28 pm     Reply with quote

Hi,

Compiler Version 4.114
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 26, 2012 6:53 pm     Reply with quote

Add this to your #fuses statement:
Code:

NOPLLEN
kcj



Joined: 05 Oct 2011
Posts: 33

View user's profile Send private message

PostPosted: Fri Jan 27, 2012 4:48 am     Reply with quote

Hi PCM Programmer

The clock now has a frequency of 19.88MHz, you are the MAN!!!

Thanks very much,
KJ
slavka012



Joined: 10 Feb 2012
Posts: 28

View user's profile Send private message

PostPosted: Fri Feb 10, 2012 1:08 pm     Reply with quote

I'm facing a strange issue of the s/w RS232 implementation.

I run PIC16F1939 @ 4MHz, and I'm trying to use s/w serial running @ 38400. I only use transmit function. However, the actual transmission that I observe with a logic analyzer is running @ 41200 baud. I changed the specified baud rate in #rs232 directive from 38400 to 35758 and now I get 38400.

The CPU runs at correct frequency on internal oscillator. I confirmed it. Looks like a bug in RS232 block.

Any input is appreciated.
temtronic



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

View user's profile Send private message

PostPosted: Fri Feb 10, 2012 1:24 pm     Reply with quote

Internal oscillators aren't all that stable(1%) which can account for the frequency being off. This is more noticeable at higher baudrates( >9600) especially when using software UART code that relies upon PERFECT timing.
Do a test of using a real 4MHz xtal and caps and compare the results.

I always use external xtal/caps,costs maybe a buck but saves hours of R&D time/money trying to figure out why code is 'funny'. 4 MHz xtals are used for my 4550 projects, never had a problem even at 115K2.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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