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 transmits data incorrectly. Why?

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



Joined: 17 Aug 2006
Posts: 6
Location: Varna, Bulgaria

View user's profile Send private message

PostPosted: Wed Aug 22, 2007 6:26 am     Reply with quote

I finally solved the problem.

I just changed the baud rate to 19 200 and rs232 works without errors. However I didn't find out way rs232 didn't work correctly with 9600 baud rate. I also removed int_rda and instead I use kbhit() - I think my code allows such construction without any loss of data. Am I right?

Code:
#include <18F1320.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_B1, rcv=PIN_B4,ERRORS)

#include <stdlib.h>
#include <input.c>

void main()
{
   float rps,rpm;
   long turns,duty;
   char cmd,no;
     
   setup_ccp1(CCP_PWM);
   setup_timer_2(T2_DIV_BY_1, 255, 1);  //The cycle time will be (1/clock)*4*t2div*(period+1)
                                        // tcycle = (1/20000000)*4*1*256 = 51.2us  ->  19.53125kHz
   set_pwm1_duty(0);                    // duty = tcycle*clock/t2div
                                        // 20% -> 10.24us x 20MHz = 204.8 ~205
                                        // 50% -> 25.6 x 20 = 512
   setup_timer_0(RTCC_EXT_L_TO_H);
   printf("--- Test program ---");
   while(true)
   {
      if(kbhit())
      {     
         cmd =getc();
         switch(cmd)
         {
         case 'n':set_timer0(0);
                  delay_ms(1000);
                  no=getc();
                  turns=get_timer0();
                  rps=(float)turns/36;
                  rpm=rps*60;
                  printf("\r\n%6.3f",rpm);
                  break;
         case 'd':duty=get_long();
                  set_pwm1_duty(duty);
                  printf("\r\nduty = %Lu",duty);
                  break;
         default :printf("\r\nError");
                  break;
         }
      }
   
   }
}
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Wed Aug 22, 2007 9:17 am     Reply with quote

You should take a long look at Ttelmahs advice. Very very often the only way to get absolutely reliable RS232 reception is with a very short interrupt service routine that feeds a circular buffer. There must be now be dozens of issues with RS232 almost all were solved with the circular buffer approach. It's not that you can't find another way it is just not likely to work as well as Ttelmahs advice.
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