View previous topic :: View next topic |
Author |
Message |
prasadezoysa
Joined: 22 Jun 2009 Posts: 4 Location: colombo
|
Garbage characters generate when serial communication |
Posted: Mon Jul 27, 2009 3:36 am |
|
|
I want to display the condition of, if I press the button then showing the status as "Y", else "N". Here with I attached my program. When I connect to computer and communicate with Hyperterminal it gives lot of garbage values. It happens when I supply voltage only. It works correctly with Easy pic 4 programmer.
Code: |
#include <16F877.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=08000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12
char string[3], IsOk[3];
//int i;
#int_rda // Interrupt
void serial_isr()
{
strcpy(IsOk, "OK");
gets(string);
printf("%2s\n",string);
printf("%2s\n",IsOk);
if(strcmp(string, IsOk))
{
output_low(PIN_B3);
if(input(PIN_B0)) {printf("Y\r\n"); }
else {printf("N\r\n");}
}
else
output_high(PIN_B3);
if(input(PIN_B0)) {printf("Y");}
else {printf("N");}
}
void main()
{
enable_interrupts(global);
enable_interrupts(int_rda);
do {
printf("\r\n\Running....\n");
while (!input(PIN_B1) );
{
if(input(PIN_B0)) {printf("Y\r\n"); }
else {printf("N\r\n");}
}
}while (1);
} |
_________________ sanjeewa |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 27, 2009 10:48 am |
|
|
Quote: | #int_rda // Interrupt
void serial_isr()
{
strcpy(IsOk, "OK");
gets(string);
printf("%2s\n",string);
printf("%2s\n",IsOk); if(strcmp(string, IsOk))
|
Don't use gets() and printf inside an interrupt service routine.
Use the Ex_sisr.c example file to make an interrupt-driven buffer
for incoming characters. Read the characters from the buffer
from code that runs in a loop in main(). Do your character
processing in main(). Also, the gets() function is not safe to use.
There is no limit on the number of incoming characters, and it's
easy to get more characters than the buffer size, and the result
is that over variables are over-written, causing your program
behave incorrectly. Use the get_string() function instead.
Ex_sisr.c is here:
Quote: | c:\program files\picc\examples\ex_sisr.c |
get_string() is here:
Quote: | c:\program files\picc\drivers\input.c |
Here is an example that combines Ex_sisr.c and get_string():
http://www.ccsinfo.com/forum/viewtopic.php?t=34267 |
|
|
prasadezoysa
Joined: 22 Jun 2009 Posts: 4 Location: colombo
|
garbage characters generate when serial communication |
Posted: Wed Jul 29, 2009 3:50 am |
|
|
I run the program (sample program).
Still problem happen (garbage characters send).
Actually my objective is according to the input signal, "Y" should show in hyperterminal side. _________________ sanjeewa |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 29, 2009 11:19 am |
|
|
Try a very simple "Hello World" example. Get that working first:
Code: | #include <16F877.H>
#fuses HS, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//====================================
void main()
{
printf("Hello World\n\r");
while(1);
} |
Quote: |
It happens when I supply voltage only. It works correctly with Easy pic 4 programmer. |
Does this mean it works when the board has power supplied by the
Easy Pic 4 programmer ? But it fails when the board has power supplied
by a desk power supply ? Check if the desk power supply voltage
is correct. Also, you may have a grounding problem somewhere.
Or, you may be missing the MCLR pull-up resistor on the PIC.
Look for connection problems. |
|
|
Guest
|
|
Posted: Wed Jul 29, 2009 12:08 pm |
|
|
Garbage is *almost always* an incorrect baud rate setting. At least that has been my experience. Usually the PC is OK, so check your PIC clock and settings.
HTH - Steve H. |
|
|
prasadezoysa
Joined: 22 Jun 2009 Posts: 4 Location: colombo
|
garbage characters generate when serial communication |
Posted: Thu Jul 30, 2009 6:24 am |
|
|
Actually problem with my max232 ic. I change it. Then circuit works correctly without any problems. _________________ sanjeewa |
|
|
|