View previous topic :: View next topic |
Author |
Message |
volcane
Joined: 23 Feb 2008 Posts: 29
|
Reset RCREG |
Posted: Fri Apr 24, 2009 2:00 pm |
|
|
Hi!
You can reset RCREG directly? or the system is this:
Code: | while(kbhit())
{
printf("the character is:%c", getc());
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
volcane
Joined: 23 Feb 2008 Posts: 29
|
|
Posted: Fri Apr 24, 2009 3:33 pm |
|
|
hi!
if i use the program crashes, i think it is a problem with the USART:
Code: | #byte RCREG = 0x1A
void clear_usart_receiver(void)
{
char c;
c = RCREG;
c = RCREG;
c = RCREG;
} |
if I use this I have no crashes:
Code: | void clear_usart(void)
{
char c;
c = getc();
c = getc();
c = getc();
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 24, 2009 3:52 pm |
|
|
Post your PIC. |
|
|
volcane
Joined: 23 Feb 2008 Posts: 29
|
|
Posted: Fri Apr 24, 2009 5:33 pm |
|
|
Hi!
PCM programmer wrote: | Post your PIC. |
with this work:
Code: | #include <16F877a.h>
#include <apricancello_gsm.h>
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <string.h>
#include <input.c>
#byte RCREG = 0x1A
#define STRING_SIZE 51 // The number of characters allowed to input
void clear_usart(void)
{
char c;
c = getc();
c = getc();
c = getc();
}
void tipo_chiamata(char *chiamata)//, char *prima)
{
delay_ms (1000);
output_low (red_led);
printf("\n\nil valore di chiamata e' %s\n\n", chiamata);
delay_ms (1000);
output_high (red_led);
delay_ms (1000);
clear_usart();
}
void inizializza()
{
printf("at+ipr=9600\r");
delay_ms(2000);
printf("at+clip=1\r");
delay_ms(1000);
printf("ate0\r");
delay_ms(2000);
clear_usart();
}
void main() {
int a;
char input_str[STRING_SIZE];
inizializza();
while(true)
{
for (a=0; a<4; a++)
{
get_string(input_str,STRING_SIZE); // gets the string x4
}
printf("ath\r");
tipo_chiamata(input_str);
}
} |
This does not work:
Code: | void clear_usart(void)
{
char c;
c = RCREG;
c = RCREG;
c = RCREG;
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 24, 2009 5:42 pm |
|
|
The RCREG version immediately clears the UART receiver and returns.
This routine takes only maybe 2us to execute, with a 20 MHz crystal.
Your getc() version waits for the first 3 characters to arrive in the UART
and throws them away. It could wait forever, if there are no characters
in the UART, or if less than 3 characters arrive.
I don't really know what the purpose of your code is. |
|
|
Ttelmah Guest
|
|
Posted: Sat Apr 25, 2009 2:21 am |
|
|
The 'safe' way to ue the standard getc, to clear the UART, is:
Code: |
void clear_usart(void) {
char c;
while (kbhit()) c=getc();
}
|
This will read any characters waiting in the hardware buffer, but unlike the three successive reads, won't hang if charcaters are not available.
Just reading RCREG three times, will generate much more compact code, and do the same job.
Best Wishes |
|
|
|