PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 16, 2008 3:51 pm |
|
|
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);
}
} |
|
|