Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

RS232 serial communication

Status
Not open for further replies.

candy66

Newbie level 6
Joined
Dec 17, 2007
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,363
hello i am working on pic18f452.I wrote a program to receive data from com and light up led when recieved data, but it failed to function.This is my program...can any one help?

list P=PIC18F452, F=INHX32, C=160, N=0, ST=OFF, MM=OFF, R=DEC, X=ON
#include "P18F452.inc"
__CONFIG _CONFIG1H, _HS_OSC_1H

variables UDATA
trisc res 1
status res 1
rcsta res 1
rcreg res 1
recieve res 1



org 0x00
clrf PORTB ; clear PORTB output latch
movlw 00H ; Set all pins PORTB as output
movwf TRISB
movlw 0x08
movwf TRISC
movlw 0x19
movwf SPBRG
movlw b'00100100'
movwf TXSTA
bsf RCSTA,SPEN

loop call receive ; wait for a char
call ON
goto loop

receive btfss PIR1,RCIF ; (5) check for received data
goto receive
movf RCREG; save received data in W

ON
btfss PIR1,RCIF
goto ON
movlw 08H ; move it from register 1 to w.
movwf LATB ; move it to PORTB to light up the
return

end
 

You can do it with hardware. Just connect pin TxD from your PC (the pin where PC sends it serial data to) with a diode, resistor and LED, in parallel with RS 232 level converter (if you are using one). Mind also the RS 232 voltage level, logic 1 is -5 to -25V and logic 0 is +5 to +25V. When serial line is idle, it is in logic 1.

I have always used this arrangement in my projects, i put one LED as serial data receiving indicator and another LED as transmitting indicator. When data is transmitted or received, the proper LED is blinking rapidly. I don't have any experience with microcontroller other than AT89S51, so i'm not sure if it is what you need or want, but it's the simplest solution for me, no programming, if you just want a simple indicator.

Added after 32 minutes:

If it's not a simple indicator that you want to make, maybe something like this can help:

Code:
....    /* some code */
void SerialInterruptHandler(void) interrupt 4 using 1
{
    ES=0;        /* disable serial interrupt temporarily */
    RI=0;        /* reset receive flag */
    P0=SBUF;  /* copy received data to port 0 */
    ES=1;        /* enable serial interrupt */
}
.....    /* another code */

The piece of code is written in C with SDCC for AT89S51. You have to change it accordingly for PIC. It just copies the serial buffer to port 0 when serial interrupt occurs.
 

Hello
It's always better to write a commented code.
I'd suggest you put the "intitialization module" (TXSTA, SPBRG....) before "ORG".
I'd also put some error checking routine (overrun, frame..) by receiving the data (RS232)
Regards
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top