Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

getting help with 89v51rd2 serial port data

Status
Not open for further replies.

dongdurex

New Member
I'm trying to receive data from PC via serial port.
But so far I can't get the data in array of chars (char[]).
Here is the program I've from my department.

Code:
#include <p89v51rd2.h> // Header file register for P89V51RD2FN
unsigned char Get_Serial(void)
{
	bit Timeout=0;
	unsigned char count=252, Data = 0;
	while((Timeout == 0))
	{
		if(RI == 0)
		count++; //waiting
		else
		{
			Data = SBUF;
			RI=0;
			return(Data);
		}
		if(count <= 2)
		{
			Timeout = 1; //Timeout!!
		}
	}
	return(Data);
}

void Send_Serial(unsigned char x)
{
	TI = 0;
	SBUF = x;
	while(TI==0);
}

void Serial_initial()
{
	TMOD = 0x21;
	SCON = 0x50;
	TH1 =0xFD;
	TL1 =0xFD;
	TF1 = 0;
	TR1 = 1;
	TI = 0;
}
void main(void) // Main loop
{
	unsigned char Data=0; // ******** I'm thinking that the data gets in from this line
	Serial_initial();
	P1 = 0x80;
	while(1) // Infinite loop
	{
         //My main loop which I need to apply a string like data.
        }
}

If I send a data from PC as "10"
The program will only read '1'
which is not correct.

please give me some advice.
ps. sorry for my English, it's not my primary language.
 
The Get_Serial function can recieve only one bit at a time.
In your main function, you need to collect each byte and store it in a array

ex: to recive "10", you nedd to call Get_Serial two times.
 
By any chance I can change my code to something else.
Because this method is troublesome, I have been stuck with it for a very long time now.

Can I change it to get the data by something like scanf or something else.
 
Unfortunately No!! scanf() would be nice.

I haven't got C code to utilize a circular buffer and use an ISR to do the job
If you can covert this to C (should be easy)
Code:
; THIS CODE IS WRITTEN BY " MUBASHAR YASIN " (ELECTRICAL ENGINEER)
; ISLAMABAD,PAKISTAN, CELL# (+92)0301-8557978 ,mubasharpk09@yahoo.com
; please visit--> www.geocities.com/kool_projects
;----------------------------------------------------------------------------
;THIS PROGRAM IMPLEMENTS A CYCLIC BUFFER ON THE SERIAL PORT(RECEIVED DATA)
;MEMORY POINTERS ARE STORED IN THE REGISTERS R0&R1(STARTING ADDRESS IS 20H)
;BUFFER LENGTH IS 20H TO 30H LOCATION
;BUFFER SIZE CAN BE VARIED ACCORDING TO YOUR REQUIREMENT AND SIZE OF RAM
;----------------------------------------------------------------------------	
	
	START EQU 20H

	ORG 0000H
	LJMP MAIN

	ORG 0023H
        LJMP SERIAL_ISR
	
;;;;;;;;;;;;;;;;
SERIAL_INIT:
;;;;;;;;;;;;;;;;
        MOV SCON,#52H ;8-BIT UART,REN
        MOV TMOD,#20H ;8-BIT AUTO RELOAD MODE
        MOV TH1,#-3   ;9600 BPS AUTO RELOAD VALUE
        SETB EA       ;GLOBAL INTERRUPT ENABLE
        SETB ES       ;SERIAL INTERRUPT ENABLE
        SETB PS       ;HIGH PRIORITY FOR SERIAL INTERRUPTS
        SETB TR1      ;START TIMER
        RET
;;;;;;;;;;;;;;;;
SERIAL_ISR:
;;;;;;;;;;;;;;;;
	PUSH ACC
	JNB RI,TMT
	CLR RI
	MOV A,SBUF
	LCALL WRITE_BUF
	LJMP EXIT1
TMT:    CLR TI
EXIT1:	POP ACC
	RETI
;;;;;;;;;;;;;;;;
WRITE_BUF:
;;;;;;;;;;;;;;;;
	CJNE R0,#31H,NEXT1  ;CHECK FOR BUFFER RECYCLE 
	MOV R0,#20H         ;CYCLE AGAIN
NEXT1:	MOV @R0,A
	INC R0
	RET
;;;;;;;;;;;;;;;;
SERIAL_READ:
;;;;;;;;;;;;;;;;
	MOV A,R0
	MOV B,R1
	CJNE A,B,NEXT2       ;COMPARE R0 & R1
	LJMP EXIT2	     ;NOTHING TO READ(BUFFER EMPTY)
NEXT2:	CJNE R1,#31H,NEXT3
        MOV R1,#20H          ;JUMP TO START (RECYCLE)
NEXT3:	MOV A,@R1
        INC R1
	LCALL DELAY
	LCALL SERIAL_OUT     ;BOUNCE BACK
EXIT2:	RET
;;;;;;;;;;;;;;;;
SERIAL_OUT:
;;;;;;;;;;;;;;;;
	MOV SBUF,A
	RET
;;;;;;;;;;;;;;;;
DELAY:
;;;;;;;;;;;;;;;;
	MOV R3,#250
RPT:	NOP
	NOP
	DJNZ R3,RPT
	RET
;;;;;;;;;;;;;;;;
MAIN:
;;;;;;;;;;;;;;;;
	MOV R0,#START        ; 'WRITE' POINTER
	MOV R1,#START        ; 'READ' POINTER
	LCALL SERIAL_INIT
AGAIN:	LCALL SERIAL_READ
	LJMP AGAIN
	END

This is from 8052.com.
 
i am using philips P89v51rd2 Uc kit in lab.
whenever i burn my program for LCD it works without DEALY only
Is there a problem in kit??
 
Works without delay's but doesn't work with delay's? There is a problem with the code...

Check the link in my signature ( last one) My tutorials have quite a bit of what you are trying to achieve... there are LCD routines you can port... there are also RX / TX routines.
 
i am programming in Assembly language... code is correct.... simply displaying name on lcd... single alphabet is displaying
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top