View previous topic :: View next topic |
Author |
Message |
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
problem with "get_string" in #int_rda |
Posted: Tue Apr 01, 2008 4:31 pm |
|
|
I am trying to receive a string through the serial port that only updates when new data is sent.
the string will be sent at random time intervals so thought i should use #int_rda but once the string has been sent the micro does not leave the interrupt.
is there also a way to clear the String in "array" or does one have to use a for statement?
the high/low pin d2 is an LED to see if the code is looping.
Code: | #include <16F877a.H>
#device icd=true
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud = 19200, xmit=PIN_C6, rcv = PIN_C7, ERRORS)
#include <input.c>
#include <Flex_LCD420.c>
#define BUF_SIZE 25
char array[BUF_SIZE];
//=====================================================================
#int_rda
void Serial_isr()
{
get_string(array, BUF_SIZE); // Get string from serial port
}
//=====================================================================
void main()
{
enable_interrupts(int_rda);
enable_interrupts(GLOBAL);
lcd_init();
printf(lcd_putc, "\f");
delay_ms(100);
while(1)
{
output_high(pin_d2);
delay_ms(100);
printf(lcd_putc, "\f\r%s", array); // Display string recieved
output_low(pin_d2);
delay_ms(100);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 01, 2008 7:07 pm |
|
|
Try this. This program combines the Ex_Sisr.c with the get_string()
function. Instead of calling getc(), get_string() now calls bgetc(), so
that it gets characters from the receive buffer that is filled byint_rda.
Example of output:
Quote: |
Enter a string of text: hello world
You typed: hello world
|
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#int_rda
void serial_isr() {
int 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)
BYTE bgetc() {
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
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;
}
#define STRING_SIZE 40
//===================================
void main()
{
char input_str[STRING_SIZE];
enable_interrupts(int_rda);
enable_interrupts(global);
while(1)
{
printf("Enter a string of text: ");
my_get_string(input_str, STRING_SIZE-1);
printf("\n\rYou typed: %s \n\r\n\r", input_str);
}
} |
|
|
|
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
|
Posted: Tue Apr 01, 2008 9:12 pm |
|
|
Thanks PCM
but i ran that exact code you posted but it doesn't seem to work. Using the CCS serial terminal it say"Enter a string of text:" then i enter a "michael" and it echo's "michael" into the serial window but then doesn't do anything else. if i continue to send more data it i presumes fills the buffer and then stops echoing to the port and totally stops working. |
|
|
Ttelmah Guest
|
|
Posted: Wed Apr 02, 2008 3:27 am |
|
|
You do need to hit the carriage return key, to say that the string has finished.
Best Wishes |
|
|
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
|
Posted: Wed Apr 02, 2008 1:23 pm |
|
|
what is the carriage return key?
once the string has been typed i push send and thats it. what else am i supposed to do? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Apr 02, 2008 2:15 pm |
|
|
umka wrote: | what is the carriage return key? | I'm so glad we have internet these days. Now the smart people will surf to the Google website and find the answers much quicker there than they would get an answer in a forum.
umka wrote: | once the string has been typed i push send and thats it. what else am i supposed to do? | How does the program know the end of the string is reached? You need to send a special character here, the 'Carriage Return' code (ASCII value 13, abbreviated CR, generated by the enter/return key on your keyboard). Check also your CCS manual in the chapter on the gets() function. |
|
|
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
|
Posted: Wed Apr 02, 2008 3:18 pm |
|
|
thats what i have been doing. As stated above i have been using the terminal that comes with the ccs ide. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 02, 2008 4:15 pm |
|
|
The 'send' button in SIOW.exe doesn't send a carriage return (0D).
It just transmits the hex bytes that you have typed into the lower box.
Instead of doing that, just click on the black area and type your text in, and press the Enter key on your PC.
Or use hyperterminal. |
|
|
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
|
Posted: Wed Apr 02, 2008 4:30 pm |
|
|
thanks yet again PCM works a treat |
|
|
Guest
|
|
Posted: Sun Apr 13, 2008 9:09 am |
|
|
its working good in the hyperterminal.. thanks alot PCM... do u mind explain more about the working of the program? because i'm still a beginner in CCS and i'm trying to learn more to complete my project...
welldone! |
|
|
|