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

Multiple fprintf statements with different streams problem
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Feb 25, 2011 12:40 am     Reply with quote

I'm under the impression, that you're still not aware of the timing requirements that are placed by the way, you arrange the multiple RS232 actions, considering they are carried out by software RS232.

What you show is not just "back-to-back" but sending a sequence of five that causes a respective sequence of answers. But most likely, you won't be able to get all answers this way. Toggling the RS232 line to low (break state) before the data transfer only complicates things further, I fear.

Ttelmah's previous suggestion to "scan" the slaves sequentially is a good approach to escape from the confusion. As you already found out, it doesn't work exactly as written. You have to code a Tx/Rx sequence with fixed streams instead to make it work. I guess, you didn't try yet.

More generally, you should always try to visualize the timing created by your arrangement: When are individual slave commands sent, are the slaves ready for it, when will the answer arrive at the master. Is the master ready to receive the answer from this slave right now. Pencil and paper is best for it. In addition, the slave and master operation should be fail-safe, not get stuck in case of a missing RX character.
misperry



Joined: 03 Mar 2009
Posts: 40

View user's profile Send private message

PostPosted: Mon Feb 28, 2011 10:41 pm     Reply with quote

Just so everyone knows who views this thread. I have gotten it to work!! Finally!.

He he Thanks so much to everyone who posted on this thread. You all helped out so much!

Special thanks to Ttelmah for giving me some sample code to work with.

I have below the test code I used that worked with a PIC16f886 chip for anyone who wonders how to do this.

This is using six software UART's to run data between a PC and five other PIC16f886 chips.

Enjoy:

Master Chip:
Code:

#include <16f886.h>
#include <string.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20M)
//#use fast_io(B)
//#use fast_io(C)
#use rs232(stream=pc, baud=9600, xmit=PIN_A1, rcv=PIN_A0)
#use rs232(stream=chipA, baud=9600, xmit=PIN_B0, rcv=PIN_B1)
#use rs232(stream=chipB, baud=9600, xmit=PIN_B2, rcv=PIN_B3)
#use rs232(stream=chipC, baud=9600, xmit=PIN_B4, rcv=PIN_B5)
#use rs232(stream=chipD, baud=9600, xmit=PIN_B6, rcv=PIN_B7)
#use rs232(stream=chipE, baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void scan_chip(int8 stream, int8 delay) {
      int8 c;
      unsigned int16 LIM;
      LIM = 0;
      if (stream==1){
       LIM=0;
         //You must now handle the response for _this_ chip
         fprintf(chipA,"%d",delay);
         while (!kbhit(chipA) && (++LIM < 50000)){
            delay_us(10);
       }
       if ((kbhit(chipA)) && (LIM<50000)){   
            c=fgetc(chipA);// get the letter that was sent to other chip back
            fprintf(pc,"%c",c); //print the letter received from sub chip to screen              
        }       
      
      }
      else if (stream==2){
       LIM=0;
         //You must now handle the response for _this_ chip
         fprintf(chipB,"%d",delay);
         while (!kbhit(chipB) && (++LIM < 50000)){
            delay_us(10);
       }
       if ((kbhit(chipB)) && (LIM<50000)){   
            c=fgetc(chipB);// get the letter that was sent to other chip back
            fprintf(pc,"%c",c); //print the letter received from sub chip to screen              
        }       
      
      }
      else if (stream==3){
       LIM=0;
         //You must now handle the response for _this_ chip
         fprintf(chipC,"%d",delay);
         while (!kbhit(chipC) && (++LIM < 50000)){
            delay_us(10);
       }
       if ((kbhit(chipC)) && (LIM<50000)){   
            c=fgetc(chipC);// get the letter that was sent to other chip back
            fprintf(pc,"%c",c); //print the letter received from sub chip to screen              
        }       
      
      }
      else if (stream==4){
       LIM=0;
         //You must now handle the response for _this_ chip
         fprintf(chipD,"%d",delay);
         while (!kbhit(chipD) && (++LIM < 50000)){
            delay_us(10);
       }
       if ((kbhit(chipD)) && (LIM<50000)){   
            c=fgetc(chipD);// get the letter that was sent to other chip back
            fprintf(pc,"%c",c); //print the letter received from sub chip to screen              
        }       
      
      }
      else if (stream==5){
       LIM=0;
         //You must now handle the response for _this_ chip
         fprintf(chipE,"%d",delay);
         while (!kbhit(chipE) && (++LIM < 50000)){
            delay_us(10);
       }
       if ((kbhit(chipE)) && (LIM<50000)){   
            c=fgetc(chipE);// get the letter that was sent to other chip back
            fprintf(pc,"%c",c); //print the letter received from sub chip to screen              
        }       
      
      }
                  
}

void main(){
   int delay_num=0;
   set_tris_a(0b00000001);
   set_tris_b(0b10101010);
   set_tris_c(0b10111111);
  output_high(PIN_B0);
   output_high(PIN_B2);
   output_high(PIN_B4);
   output_high(PIN_B6);
  output_high(PIN_C6);
   //Assuming the remote chips are switched on, they will now start
   fprintf(pc,"hello");

   for (;;) { 
      //get which switch is flipped
      if (input(PIN_C5))
         delay_num=1;
      else if (input(PIN_C4))
         delay_num=2;
      else if (input(PIN_C3))
         delay_num=3;
      else if (input(PIN_C2))
         delay_num=4;
      else if (input(PIN_C1))
         delay_num=5;
      else if (input(PIN_C0))
         delay_num=6; 
      //print number to all five chips _in turn_
     scan_chip(1,delay_num);
      scan_chip(2,delay_num);
      scan_chip(3,delay_num);
      scan_chip(4,delay_num);
      scan_chip(5,delay_num);
      fprintf(pc,"\n\r");
      //Add a LF/CR, so you can see that one sequence is complete

      delay_ms(500); 
   } //And loop to repeat     
}

Slave chip:
Code:

#include <16f886.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(internal=4M)
#use rs232(baud=9600, xmit=PIN_A1, rcv=PIN_A0)

void main(){
   char c2;
   set_tris_a(0b00111101);
   set_tris_b(0b11100000);
   set_tris_c(0b00111111);
   while (INPUT(PIN_A0)==0) ; //Wait till you see the serial line go high
   //Master is now 'awake'
   for (;;){
    while (!kbhit());
    if (kbhit()){
        c2=getch();  //get the number from main chip
        //Why delay?.
        putc(c2); //send the number received back to main chip
      }      
 
  } //and loop to wait for the next character
}

Thanks to you all again! I could have never done it without all of you!
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 Previous  1, 2
Page 2 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