|
|
View previous topic :: View next topic |
Author |
Message |
Jerry R
Joined: 25 Apr 2007 Posts: 23
|
Problem using the get_float routine |
Posted: Tue Sep 16, 2008 1:23 pm |
|
|
I'm having a bit of a problem using the get_float routine. I use it to fetch user input (a integer value from 0 to 250). My problem is that sometimes the return is missed. I've shut down other interrupts, and got rid of all delays.
Is there a better way of inputting integers without using the get float routine?
Using 3.249 PCWH with a '4525.
Here's a snippet of code using the get_float:
Code: |
fprintf(PC,"\n\rEnter new value for HEAT set point and press RETURN "); //prompt for new value
User_Input = get_float(); //get user input into string
|
Thanks in advance. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 16, 2008 1:48 pm |
|
|
Post a short little program that demonstrates the problem.
Call the get_float() routine, and display the results with printf.
Do this in a while(1) loop.
Post the #include, #fuses, #use delay(), and #use rs232 statements,
and the main(). The program should be compilable with no errors.
It should be only a few lines. Don't put in code to setup the ADC,
or setup the CCP, etc. Only put in code required to do the test.
Post the PC terminal program that you're using, such as HyperTerminal. |
|
|
Guest
|
|
Posted: Tue Sep 16, 2008 2:22 pm |
|
|
Thanks for the quick reply PCM. I've stripped out all but the necessary code to implement the get_float function. Of course, now, the response is a bit more robust. This code misses approximately 2 out of twenty returns. The print occurs in 18/20 return attempts. Otherwise the cursor just blinks on the line.
Using Procomm Plus and Hyperterminal (occurs on both). PC doesn't have issue with other code, just when using this code with the get_float. Is there a more efficient way of inputting integer values without using the get_float? Any suggestions?
Code: |
#include <18F4525.h>
#DEVICE ICD=TRUE
#fuses H4,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=32000000)
#use rs232(baud=4800, xmit=PIN_C5, rcv=PIN_C4,ERRORS,stream=PC)
//#PRIORITY RDA, TIMER2
#include <stdlib.h>
#include <input.c>
#include <math.h>
#include <string.h>
//===========================================================================
void User_Input() //User pressed a key as input
{
float User_Input; //floating input from user
char rx_ch; //single char from user
rx_ch = fgetc(PC);
if ((rx_ch == 'H') || (rx_ch == 'h'))
{
fprintf(PC,"\n\rEnter new value for HEAT set point and press RETURN "); //prompt for new value
User_Input = get_float(); //get user input into string
fprintf(PC,"\r\nHeat Setpoint- %f\r\n",User_Input); //print for test
}
}
//===========================================================================
//==============================================================================
//Interrupt Timer2 occurs fastest (40us) of any interrupt. It is used to detect user input
//from the PC. If a character is detected, the routine calls the User_Input routine
//to process user requests.
#INT_TIMER2
void pc_com_ck_isr()
{
if (kbhit(PC) == 1)
{
User_Input(); //service PC Com from user
}
}
//==============================================================================
//==============================================================================
//MAIN calling routine
void main()
{
setup_timer_2(T2_DIV_BY_16,8,2); //set timer2 attributes
enable_interrupts(GLOBAL); //enable all interrupts
enable_interrupts(INT_TIMER2); //enable timer2 interrupts
do
{
} while (TRUE);
}
//==============================================================================
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 16, 2008 2:31 pm |
|
|
You're using a software UART. Do you have to use that ?
You wouldn't have the problem with the hardware UART.
Then with a soft UART, most people would put the Rx pin on RBO
with the external interrupt. |
|
|
Jerry R
Joined: 25 Apr 2007 Posts: 23
|
|
Posted: Tue Sep 16, 2008 2:45 pm |
|
|
Thanks PCM.
Well, in a perfect world... I'd use the hardware UART. Currently, it's reserved for something higher priority.
I can't seem to figure out what the PIC is doing while waiting for the return. Whatever it is it's interrupting the process. Very little info on the get_float routine.
I don't seem to have any issue with the kbhit. The first char gets through 100%.
Any other suggestions?
Thanks! |
|
|
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);
}
} |
|
|
|
Jerry R
Joined: 25 Apr 2007 Posts: 23
|
|
Posted: Wed Sep 17, 2008 4:58 am |
|
|
Thanks PCM. I'll give it a serious look!
All the best |
|
|
|
|
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
|