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

Help me in edit my coding

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



Joined: 17 Jul 2010
Posts: 4
Location: Malaysia

View user's profile Send private message Send e-mail Yahoo Messenger

Help me in edit my coding
PostPosted: Thu Oct 07, 2010 5:43 am     Reply with quote

Code:

#include <18F14K50.h>
#include <string.h>
#include <stdlib.h>

#fuses INTRC,NOWDT,NOPROTECT,NOLVP // use Internal osc
#use delay(clock=16000000)
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B5,stream=COM_A)
#use rs232(baud=9600,xmit=PIN_C7,rcv=PIN_C6,stream=COM_B)

char header_data[10],Buffer[30],Buff=0,b,term[3],*ptr,i=0;
float data[3];
char hr[15];
char h;

#int_rda
void rs232_handler() //interrupt
{
   b= fgetc(COM_A); //load character
     
   if(b == '1') //check for starting point
      Buff=0;
   
   if(b == '\r')
   {   
      ptr = strtok(Buffer, term);
     
      //if(!strcmp(ptr,header_data))  //check if header is $MH
      {
         ptr = strtok(0, term);
         
         while(ptr!=0)
         {
            data[i++] = atof(ptr);
            ptr = strtok(0, term);
         }
         i = 0;
      } 
   }
   
   Buffer[Buff]=b; //store character
   Buff++; //increment pointer
}


void main()
{
   /*Initialize */
   setup_oscillator(OSC_16MHZ|OSC_INTRC);
       
   /*Code */   
   while(true)
   {             
      i=0;
      hr[0]= "";
      fprintf(COM_A,"G1\r");
     
      Do
      {
         if(kbhit(COM_A))
         {
            fgets(hr,COM_A);
            i=5;
         }
      }while(i != 5 );
         
      fprintf(COM_B,"\rHeart Rate = %s\n",hr);
      }
}

Output:
Heart Rate = 3 205 97

Hi all, please help me to check this program and edit by adding STROK() function where I want to eliminate the 3 and 205 from the output and only printf output as Heart Rate = 97. Looking forward for a good helping results from all of you. Thank you.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Thu Oct 07, 2010 9:39 am     Reply with quote

you are calling several functions from your ISR...
might work now, but this will cause problems because you are taking to long, if it hasent given you any problems already

Further more
Quote:

fprintf(COM_B,"\rHeart Rate = %s\n",hr);


Thats your problem

%s prints a string... hr is a string... it prints the string till it reaches the end....

thus printing all its contents... 3, 205 and 97
you can set how many characters from this string are printed but not where to start printing as far as i know..
you dont have a precision specification so it prints the whole thing.


if the data you want to print is ALWAYS in the same spot of the string hr,

Code:
fprintf(COM_B,"\rHeart Rate = %u%u\n",hr[14],hr[15]);


(the indexes in the example above are probably wrong but you get the idea) (and %u might be %x ... i dont remember... but its a single byte specifier ... check the fprintf options)
_________________
CCS PCM 5.078 & CCS PCH 5.093
mahalingam



Joined: 17 Jul 2010
Posts: 4
Location: Malaysia

View user's profile Send private message Send e-mail Yahoo Messenger

Help me in edit my coding
PostPosted: Thu Oct 07, 2010 9:54 am     Reply with quote

Hi Gabriel. Thanks for your reply. Actually the coding is correct already. Based on the heart rate monitor's protocol, must get a result as so. 3 205 97 3 206 99 and so on. The 97 and 99 is the heart rate. Other 2 values are just protocol. I want to eliminate that values and just printf 97 or 99. My lecturer asked me to do so by using strtok (). Please help me on edit my program with strtok (). Thank you.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Thu Oct 07, 2010 10:12 am     Reply with quote

google: http://www.cplusplus.com/reference/clibrary/cstring/strtok/

There is an example. Its for C++ but should be the same.


Still, your isr is quite long.

Strtok seems pointless if your data is in a fixed place on the string.
At print time its probably faster just to direct adress the bytes and print.
Specially if its just 2 bytes.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri Oct 08, 2010 3:09 am     Reply with quote

A couple of things.

You have an interrupt routine but it is not enabled, I assume you hope to be using the interrupt routine once you have got the printout working.

You try to use strtok in yout isr. you would be better off terminating the input with a null and setting a flag, then doing the parsing in main. Once you have it working with the gets you can just replace the gets with the isr routine and the rest should still work.

Read up on the strtok function, try out a few things and when you get stuck post the code on here for some guidence.
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