View previous topic :: View next topic |
Author |
Message |
jojos
Joined: 30 Apr 2007 Posts: 64
|
A small rs232 receive issue |
Posted: Wed Nov 28, 2007 7:09 am |
|
|
Hello i use this code to confirm that i can recieve the button i press ,with my pc keyboard,through Rs232.When i press No1 key on my keyboard i cant see an ok mesage on hyperterminal.I can see the message that the pic transimts on hyper terminal but the recieve function doesn't work.
PIC18f4620
compiler version 3.249
Code: |
#use rs232(baud=9600, parity=N,xmit=PIN_C6 ,rcv=PIN_C7,bits=8)
.
.
.
//i have configured the c7 (Rx) pin as input
loop:
// .
// .
// .
ready=kbhit();
if(ready==1)
{
key=getc();
if(key==49) // 1 in ASCII
printf("OK");
// .
// .
// .
goto loop;
}
|
|
|
|
Audi80
Joined: 07 Sep 2007 Posts: 41
|
|
Posted: Wed Nov 28, 2007 7:21 am |
|
|
Instead of using if(key==49) try using if(key=='1').
And to receive anything i would recomend you to use the communications interruptions. #INT_RDA |
|
|
jojos
Joined: 30 Apr 2007 Posts: 64
|
|
Posted: Wed Nov 28, 2007 8:15 am |
|
|
Nothing happens.I 've tryed the key=='1' didnt't work.I tryed the int_rda and i can see that it never gets in this interrupt ,ike there is nothing coming from rs232 so to produce an interrupt(i have enabled the interrupt of rda).
Could it be any setting that i have to do in hyperterminal or it doesn't matter?
In hyper terminal i use ANSI emulation and i have tryed all of flow control options (None,Xon/Xoff,Hardware) |
|
|
Audi80
Joined: 07 Sep 2007 Posts: 41
|
|
Posted: Wed Nov 28, 2007 9:14 am |
|
|
Here´s a simple example...
Code: | #include <16F876A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//////////////////////////////////////////////
char cartao[10];
int a=0;
char aux;
//////////////////////////////////////////////
void escreve();
//////////////////////////////////////////////
#int_rda
void serial_isr() {
aux=getc();
cartao[a]=aux;
++a;
escreve();
if(a==10){
a=0;
}
}
void main() {
enable_interrupts(global);
enable_interrupts(int_rda);
while(true);
}
void escreve(){
int i;
for(i=0;i<strlen(cartao);++i){
printf("%c",cartao[i]);
}
} |
Dont forget that you´ve got to put a while(true) in the main so that the pic never stops. And you have to check your cable pin 2 is rx and 3 tx. to use the Hyperterminal with this example just select your comX and set to restore defaults. Hope it helps! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 28, 2007 1:49 pm |
|
|
To check if RS232 is working correctly, try a very simple test program.
In the following program, any character that you type on the PC keyboard
will be sent back to the PC and displayed in the terminal window. Make sure that you configure the terminal program to disable "local echo".
Code: |
#include <18F4620.h>
#fuses XT,NOWDT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//================================
void main(void)
{
char c;
while(1)
{
c = getc();
putc(c);
}
} |
|
|
|
jojos
Joined: 30 Apr 2007 Posts: 64
|
|
Posted: Thu Nov 29, 2007 3:25 am |
|
|
Pcm i cant see anything on the screen of Hyperterminal.What could be wrong?.I can transmit but i cant recieve. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 29, 2007 3:32 am |
|
|
Check all the connections in your MAX232 circuit. |
|
|
jojos
Joined: 30 Apr 2007 Posts: 64
|
|
Posted: Thu Nov 29, 2007 5:27 am |
|
|
I checked all my connection they appear to be O.K.
Using the oscilloscope i can see in the Rx pin of the pic that when i press a button on the pc keyboard there are pulses of max. 2V,but from the Tx pin it doesn't appear anything.I would expect that form the putc(c); instruction there had to be something.
Are the 2volts pulses to low or they are the nominal voltage? |
|
|
jojos
Joined: 30 Apr 2007 Posts: 64
|
|
Posted: Thu Nov 29, 2007 7:38 am |
|
|
Found the problem.The capacitor on pin 16 and pin 2 of the max232 has failed. Guess everything is possible
Thanks for the respond. |
|
|
Ttelmah Guest
|
|
Posted: Thu Nov 29, 2007 7:38 am |
|
|
jojos wrote: | I checked all my connection they appear to be O.K.
Using the oscilloscope i can see in the Rx pin of the pic that when i press a button on the pc keyboard there are pulses of max. 2V,but from the Tx pin it doesn't appear anything.I would expect that form the putc(c); instruction there had to be something.
Are the 2volts pulses to low or they are the nominal voltage? |
The signal at PIC, should be _high_ when no data is arriving, and drop when data is received. It needs to be reaching at least 0.8*the PIC supply rail (4v, for a 5v PIC). 2v, would normally imply something is badly wrong, and if the data is positive going, something else is wrong as well...
Best Wishes |
|
|
Ttelmah Guest
|
|
Posted: Thu Nov 29, 2007 8:00 am |
|
|
I see the hardware problem was found, while I posted. :-)
Best Wishes |
|
|
|