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

rs232 between pic

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



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

rs232 between pic
PostPosted: Wed Feb 18, 2009 2:03 pm     Reply with quote

Hi, I have a problem connecting PIC16F876A which send data RS232 buil-tin with MAX233 to PIC24FJ16GA002 rs232 built-in with MAX233. I tested each c code using hyper terminal and all is ok; also verified connection TX RX baud rate etc. but still not working; I suspect the problem is due to the format coming from PIC16F876 which appear to be ok with hyperterminal but probably there is something that i don't understand.
Here follows part of code of both devices.


Code:
#include <input.c>
char temp[10];
char *valore;
char *comando;

#int_rda
void Serial_isr()
   {
   get_string(temp, sizeof(temp));
   delay_ms(20);
   //printf("\n\rComando= %s \n\r", comando);
   //printf("\n\rValore= %s \n\r", valore);
   }

void main()

   long an_1;
   long an_2;
   long an_3;
   char string[8], term[3], *ptr;
   char clearing;
   
   ODCB=0b00000011;  //set pull-up pin
   setup_spi(SPI_SS_DISABLED);
   setup_spi2(SPI_SS_DISABLED);
   setup_wdt(WDT_ON);
   setup_timer1(TMR_DISABLED|TMR_DIV_BY_1);
   enable_interrupts(INT_RDA);
   //enable_interrupts(intr_global);
   
   setup_timer2(TMR_INTERNAL | TMR_DIV_BY_8,10000 );
   setup_compare(1,COMPARE_PWM | COMPARE_TIMER2 );
   setup_compare(2,COMPARE_PWM | COMPARE_TIMER2 );
   setup_adc_ports( sAN9 | sAN10 | sAN11 );
   setup_adc(ADC_CLOCK_INTERNAL );
   
   set_pwm_duty(1,750);  // 1.5ms on status
   set_pwm_duty(2,750);  // 1.5ms on status
   
   output_low(PIN_B8); //common pin for led
   output_high(PIN_B6); //led on
   output_high(PIN_B7); //led on
   delay_ms(2000);
   output_low(PIN_B6); //led off
   output_low(PIN_B7); //led off

do
{
   set_adc_channel( 9 );
   an_1 = read_adc();
   set_adc_channel( 10 );
   an_2 = read_adc();
   set_adc_channel( 11 );
   an_3 = read_adc();
   printf("%Lu%c%Lu%c%Lu%c%c",an_1,10,an_2,10,an_3,10,13);
   //delay_ms(300);
   strcpy(string,temp);
   strcpy(term,",");
   ptr = strtok(string, term);
   comando=ptr;
   while(ptr!=0) {
      //puts(ptr);
      valore=ptr;
      ptr = strtok(0, term);
   }
   switch(atoi(comando)){
      case 3:
         if(atoi(valore)==1)
         output_high(PIN_B7); //zoom out
         else
         output_low(PIN_B7); //zoom out
         break;
      case 4:
         if(atoi(valore)==1)
         output_high(PIN_B6); //zoom out
         else
         output_low(PIN_B6); //zoom out
         break;
      case 55:
         set_pwm_duty(1,atoI(valore));
         break;
      case 66:
         set_pwm_duty(2,atoI(valore));
         break;
   }
   clearing=U1RXREG;
} while(true);   
}



Code:
void main()
{
   int cmd_pan;
   int cmd_tilt;
   long valore_pan;
   long valore_tilt;
   int pan_p;
   int pan_m;
   int tilt_p;
   int tilt_m;
   int zoom_in;
   int zoom_out;
   int zin;
   int zout;
   int val_zin;
   int val_zout;
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   
   cmd_pan=55;
   cmd_tilt=66;
   valore_pan=750;
   valore_tilt=750;
   zin=3;
   zout=4;
   val_zin=1;
   val_zout=1;
   delay_ms(3000);   //linea temporanea
   do
   {
   pan_p=input(PIN_C0);
   pan_m=input(PIN_C1);
   tilt_p=input(PIN_C2);
   tilt_m=input(PIN_C3);
   zoom_in=input(PIN_C5);
   zoom_out=input(PIN_C4);
   //printf("%d,%d%c%c",zout,val_zout,10,13);
   //delay_ms(1000);
   //printf("%d,%d%c%c",zin,val_zin,10,13);
   //delay_ms(1000);
   if (pan_p==0) //increasing pan movement
      {
      valore_pan=valore_pan+20;
      if (valore_pan>1000)
         valore_pan=1000;
      printf("%d,%Ld%c%c",cmd_pan,valore_pan,10,13);
      delay_ms(100);
      }
   if (pan_m==0)  //decreasing pan movement
      {
      valore_pan=valore_pan-20;
      if (valore_pan<500)
         valore_pan=500;
      printf("%d,%Ld%c%c",cmd_pan,valore_pan,10,13);
      delay_ms(100);
      }     
   if (tilt_p==0) //increasing tilt movement
      {
      valore_tilt=valore_tilt+20;
      printf("%d,%Ld%c%c",cmd_tilt,valore_tilt,10,13);
      }
   if (tilt_m==0)  //decreasing tilt movement
      {
      valore_tilt=valore_tilt-20;
      printf("%d,%Ld%c%c",cmd_tilt,valore_tilt,10,13);
      }           
   if (zoom_in==0)  //zoom in
      {
      printf("%d,%d%c%c",zin,val_zin,10,13);
      }               
   if (zoom_out==0)  //zoom out
      {
      printf("%d,%d%c%c",zout,val_zout,10,13);
      }                 
   }
   while (true);
}


Please note that each code has been tested with pc using hyperterminal so for me is very hard to find the matter.
Thanks in advance and regards.
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Wed Feb 18, 2009 2:09 pm     Reply with quote

Did you use a null modem cable instead of a normal serial cable? If not, you'll have to.

r.b.
nicotec



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

PostPosted: Wed Feb 18, 2009 2:22 pm     Reply with quote

I have verified all connection and using a led to detect when code running inside the interrupt and exit and appear to work but the command is not recognized probably depend of an error in declaration of variable but i do not understand.
nicotec



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

PostPosted: Thu Feb 19, 2009 5:44 am     Reply with quote

additional test with no success, anyone can help me?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Feb 19, 2009 6:03 am     Reply with quote

The usual way to check, why a command is not recognized would be to visualize the received string, e.g. save it to a buffer and replay it to an auxilary serial interface later. ICD-U40 has a dedicated debugging pin for such purposes, B.T.W. Or use a debugger. A typical reasons for failure is e.g. an unexpected leading character, I didn't check if your code can tolerate it.

Instead of posting a lot of unrelated code, you may want to show the used command string format. It's a bit annoying, that we have to conclude it from the code.
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Thu Feb 19, 2009 9:35 am     Reply with quote

Did you try a null modem cable? Do you know what that is?

A computer always transmits data on pin 3 of the DB9 connector, and receives it on pin 2. Any device that is designed to connect to a computer using a regular RS232 cable must therefore receive on pin 3 and transmit on pin2.

When you connect two such devices together, as you are doing, and you use the same cable you used to connect to the computer, they will both be transmitting on pin 2 and both receiving on pin 3. This means absolutely nothing will work, as the transmitted signals are colliding and they are both listening on pin 3 while no one is transmitting on that wire.

You therefore need to use a null modem cable, which crosses over the connections for pins two and three. Then one will be transmitting while the other is receiving, and vice versa.

r.b.
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