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

Problem using the get_float routine

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 16, 2008 3:51 pm     Reply with quote

You can try this one. This is an adaptation of CCS code in Ex_Sisr.c.
It uses the INT_EXT interrupt on Pin B0 to receive characters.
It appears to work. There is no limit checking on the size of the
float value that's typed in. There's a limit on the number of digits,
but some part of this program can't handle large numbers. I don't
have time to look for the problem. Your problem was in properly
receiving the typed in digits. This may work better.
Code:
#include <18F452.h>
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_B1, rcv=PIN_B0)

#include <stdlib.h>

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


#int_ext
void serial_isr()
{
int8 t;

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)

int8 bgetc(void)
{
int8 c;

while(!bkbhit);

c = buffer[next_out];
next_out = (next_out+1) % BUFFER_SIZE;
return(c);
}


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

--max;
len=0;

do{
   c=bgetc();
   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;

}


float get_float(void)
{
char s[9];
float f;

get_string(s, sizeof(s));
f = atof(s);
return(f);
}

//=======================
void main()
{
float result;

printf("Start\n\r");

ext_int_edge(H_TO_L);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);

while(1)
  {
   result = get_float();
   printf("\n\r%f \n\r", result);

  }
}
Jerry R



Joined: 25 Apr 2007
Posts: 23

View user's profile Send private message

PostPosted: Wed Sep 17, 2008 4:58 am     Reply with quote

Thanks PCM. I'll give it a serious look!

All the best
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