View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
EX_SISR.C modification |
Posted: Fri May 18, 2018 2:04 am |
|
|
Hai,
I've modified "EX_SISR.C" file follow to my design.
I'm trying to get a "GOOD" reply when it receive "OK" signal, other than "OK" signal, it will be ignored.
My code below can be compiled, but it will not reply "GOOD" when I send "OK" signal. I need help to fix this problem.
Thanks in advance.
Code: |
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#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 main() {
enable_interrupts(int_rda);
enable_interrupts(global);
printf("\r\n\Running...\r\n");
do {
delay_ms(10000);
printf("\r\nBuffered data => ");
while(bkbhit=="OK")
printf("\r\nGOOD");
} while (TRUE);
}
|
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri May 18, 2018 2:27 am |
|
|
What is/are the value(s) that your 'bkbhit' can have?
Have you looked at it? (Either by using a watch window or printing it)
Mike |
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Fri May 18, 2018 2:58 am |
|
|
it seems that the 'bkbhit' can receive any character or numbers |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Fri May 18, 2018 3:26 am |
|
|
art wrote: | it seems that the 'bkbhit' can receive any character or numbers |
No. bkbhit() returns just true (not zero) or false (zero). It tells you whether there are any characters in the buffer. it does not tell you what the character(s) are. So you call bkbhit() to tell you if its worthwhile reading stuff from the buffer. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Fri May 18, 2018 5:00 am |
|
|
also..
whenever you're using the HW UARTs of PICs, you need to add 'ERRORS' to #use RS232(...options...). This adds a small chunk of code that will prevent the UART from 'freezing' or 'locking' up should you not read the UART buffer faster than the data comes in.
If you check the code library or do a search for 'ex_sisr.c' in this forum, you'll find a lot of 'hits' about using it . Several deal with your question.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Fri May 18, 2018 10:48 pm |
|
|
Also, if bkbhit _did_ return the data, you can't compare 'strings' in C with ==.
There is a lot of basic learning needed here... |
|
|
|