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

all in a string

 
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

all in a string
PostPosted: Tue Feb 03, 2009 9:38 am     Reply with quote

Code:
#use rs232(UART1,baud=9600,parity=N,bits=8,stop=1)
#define BUFFER_SIZE 10
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;

#int_RDA
void RDA_isr()
{
   int t;
   output_high(PIN_B7);
   buffer[next_in]=getc();
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
     next_in=t;           // Buffer full !!
}

#define bkbhit (next_in!=next_out)

BYTE bgetc() {
   BYTE c;
   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
  }

void main()

   long an_1;
   long an_2;
   long an_3;
   char mystring;

   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,1000);  // 2ms on status
   set_pwm_duty(2,1000);  // 2ms on status
   
   output_low(PIN_B8); //common pin for led
   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();
   output_high(PIN_B6);
   printf("%Lu%c%Lu%c%Lu%c%c",an_1,10,an_2,10,an_3,10,13);
   delay_ms(1000);
   while(bkbhit)
      {
       output_low(PIN_B7);
       putc(bgetc());
      }
} while(true);
}

Hi, I need to put all result characters from [bgetc()] in one string only[mystring] which will be analyzed.
Thanks in advance
Regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 03, 2009 2:49 pm     Reply with quote

See this example of using bgetc() with a modified get_string() function:
http://www.ccsinfo.com/forum/viewtopic.php?t=34267
nicotec



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

PostPosted: Wed Feb 04, 2009 8:42 am     Reply with quote

The referenced code:

Code:
my_get_string(mystring, 3);   


works to wait a string, but I need to continue other job and only on RDA interrupt taking the string received.
Any suggestion?
Thanks and regards.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Feb 04, 2009 10:07 am     Reply with quote

If this is all you are doing with recieved data the you almost have it with your current code!

There are some unanswered questions though.

How much data ? How big does your string need to be ?

Code:

void RDA_isr()
{
   int t;
   output_high(PIN_B7);
   buffer[next_in]=getc();
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
     next_in=t;           // Buffer full !!
}


Change this routine so that when you recieve the end char or x amount of data you process that buffer and then start writing to the buffer from the start again.

One issues with this is if you recieve data while you are processing the current data then it will be lost. You can fix this with 2 buffers. If your processing time takes longer than the gap between blocks of data then you have a big problem because you would need infinate memory to store it all as you cannot process it quick enough.

Single buffer routine
Code:

#define MAX_BUFFER 100
char buffer[MAX_BUFFER];
int8 index = 0;
int1 process_flag = false;

#int_RDA
void RDA_isr()
{
   char c;
   output_high(PIN_B7);
   c=getc();
   if (!process_flag)
   {
      buffer[index++] = c;  // store data
      if (index >= MAX_BUFFER) // Got all the data
      {
        process_flag = true; // ignore data untill this buffer has been processed
        index = 0;
       }
    }
}


You can try and work out the double buffer option.
nicotec



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

PostPosted: Wed Feb 04, 2009 11:42 am     Reply with quote

First of all thanks for your help and I try to explain what I doing. I send to pic a code like "55,61" or "4,1" ended with LFCR
In my code I send to PC (my VBcode) a string for other work... now I need to use interrupt to detect a received string like as above, so I whish to obtain my string like "55,61"
nicotec



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

PostPosted: Wed Feb 04, 2009 1:44 pm     Reply with quote

I'll try (I hope tomorrow) this code; can anyone help me to confirm/suggest/correct anythings about the following code after explanation on what I want to do above?
Thanks in advance and regards.
p.s.: I'm interested to obtain in string "input_str" the string sent to pic like as "55,61" ended with LFCR

Code:
#include <24FJ16GA002.h>
#include <string.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOJTAG                   //JTAG disabled
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES ICS3                     //ICD communication channel 3
#FUSES IOL1WAY                  //Allows only one reconfiguration of peripheral pins
#FUSES WINDIS                   //Watch Dog Timer in non-Window mode
#FUSES WPRES128                 //Watch Dog Timer PreScalar 1:128
#FUSES WPOSTS16                 //Watch Dog Timer PostScalar 1:32768
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FRC                      //Internal Fast RC Oscillator
#FUSES NOCKSFSM                 //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES NOOSCIO                  //OSC2 is general purpose output
#FUSES NOPR                     //Primary oscillaotr disabled
#FUSES I2C1SELD             
#use delay(clock=8000000)

#pin_select OC1=PIN_B0    //OUTPUT OF PWM PIN 4
#pin_select OC2=PIN_B1    //OUTPUT OF PWM PIN 5
#pin_select U1TX=PIN_B10  //RS-232 TX PIN 21
#pin_select U1RX=PIN_B11  //RS-232 RX PIN 22
#use rs232(UART1,baud=9600,parity=N,bits=8,stop=1)
#BYTE U1RXREG = 0x226

#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;


#int_rda
void serial_isr() {
   int t;
   output_high(PIN_B7);
   buffer[next_in]=getc();
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
     next_in=t;           // Buffer full !!
     output_low(PIN_B7);
}

#define bkbhit (next_in!=next_out)

BYTE bgetc() {
   BYTE c;

   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}
   
   //clearing=U1RXREG;

void my_get_string(char* s, unsigned int8 max) {
   unsigned int8 len;
   char c;

   --max;
   len=0;
   do {
     //c=getc();  // Commented out.
       c = bgetc();   // Call bgetc() instead of getc()
       s[len++]=c;
   } while(c!=13);
   s[len]=0;
}
#define STRING_SIZE 40
void main()

   long an_1;
   long an_2;
   long an_3;
   char input_str[STRING_SIZE];
 
   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,1000);  // 2ms on status
   set_pwm_duty(2,1000);  // 2ms on status
   
   output_low(PIN_B8); //common pin for led
   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();
   output_high(PIN_B6);
   printf("%Lu%c%Lu%c%Lu%c%c",an_1,10,an_2,10,an_3,10,13);
   delay_ms(500);
   my_get_string(input_str, STRING_SIZE-1);
   printf("\n\rYou typed: %s \n\r\n\r", input_str);
   output_low(PIN_B7);
} while(true);   
}
nicotec



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

PostPosted: Thu Feb 05, 2009 7:09 am     Reply with quote

Below code (extracted from above) is in loop waiting character/string and I wish to not remain in loop but exit if no interrupt RDA verify since it's not necessary to be in a loop because I need to do other job for my pic24fj16ga002.
Can someone help me, please?
Thanks

Code:
void my_get_string(char* s, unsigned int8 max) {
   unsigned int8 len;
   char c;

   --max;
   len=0;
   do {
        //c=getc();  // Commented out.
       c = bgetc();   // Call bgetc() instead of getc()
     
       if(c==8) {  // Backspace
        if(len>0) {
          len--;
          putc(c);
          putc(' ');
          putc(c);
        }
     } else if ((c>=' ')&&(c<='~'))
       if(len<=max) {
         s[len++]=c;
         putc(c);
       }
   } while(c!=13);
   s[len]=0;
}
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