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.

USART Looping Problem

Status
Not open for further replies.

Suraj143

Active Member
I'm receiving a number (1-10) from PC via USART.It must turn on & off the RA0 number of times.

Means if I receive "4" it must turn on & off "4" times the RA0.

But the problem is it always looping more than 50 times every time.

Code:
Begin		call	Update		;get the USART number
		movf	Data1,W			
		movwf	Repeat		;move to Repeat
		bsf	PORTA,0		;turn on LED for a while	
		call	Delay
		call	Delay
		call	Delay
	
RepCircle	bcf	PORTA,0		;turn off LED	
		call	Delay
		bsf	PORTA,0		;turn on LED
		call	Delay
		decfsz	Repeat,F
		goto	RepCircle
		call	Delay
Waithere	goto	Waithere
 
I'm receiving the number correctly from PC.

Code:
		movf	Data1,W
		movwf	PORTA

The above code nicely put the value to PORTA.But in the RepCircle routine it always gets messy.

Its a PIC16F628A/4MHz 9600 8 N 1
 
Last edited:
Are you not sending the number to the PIC as ASCII and the pic displays it as decimal?

I just type "1" in the hyper terminal & it displays on PORTA nicely.
If I type "6" in the hyper terminal & it displays on PORTA nicely.

The problem is I want to loop this number number of times.It always looping too much.
 
Don't understand, what works when you send 1 or 6, and what do you want to loop a number of times?
I don't see a problem in your code, other than you might need to convert the incoming data from ascii to value that the PIC will understand. IE: 1 ASCII = .49 decimal.
 
Last edited:
Hi Boomslang I need to blink RA0 LED what the value get from the PC.

It means if PC sends "2" then RA0 must Blink 2 times.
If PC sends "6" then RA0 must Blink 6 times.

I just press the "1" in the hyper terminal.& its blinking closer to 50 times.
 
I think that will be the problem.

In VB (visual basic) terminal program also it happens like this.I don't know in VB default is ASCII or DECIMAL.

I'll try to send them as decimal.
 
I think that will be the problem.

In VB (visual basic) terminal program also it happens like this.I don't know in VB default is ASCII or DECIMAL.

I'll try to send them as decimal.

Send it as ASCII as you already are, and convert to a number in the PIC program, it's very simple to do.
 
The really simple ASCII - decimal conversion is this:-

Code:
		movf	Data1,W			
		andlw	0x0f
		movwf	Repeat		;move to Repeat

All I have added is the middle line.

ASCII for 0 is 0x30 or 48
ASCII for 9 is 0x39 or 57

so all you are interested in is the last 4 bits.

It is a bit crude as there are many duplications. For example ASCII "A" (0x41) or "a" (0x61) will both give the same result as "1" (0x31)

If you want to get a routine to receive "10" as well it is a lot more complicated. That is because the first character of "10" is "1" and you could get one flash as soon as the "1" has been received. You need to be able to receive two characters and have some way of deciding when the transmission is finished, either by timeout or by a return character.
 
Last edited:
Hi that was the problem. Now it solved it was coming from ASCII format. After adjusting the code it worked well. I applied Diver300’s method.

Thanks for that.
 
Everything is ok but I have one problem.

In many times I’m setting RA0 bit permanently ON & toggling RA1.
Ex: When I receive “B” from PC it must toggle RA1 while RA0 is ON.

It’s toggling, but in many times it switched off both the outputs after that code gets stucked.

This is how I configured.

MCLR off, INT RC on, 4 MHz, 9600 8 N 1, PIC16F628A
 
Here is a part of the ISR routine. The code works sometimes its getting messy.
why I'm getting a problem like that.When I toggle RA1 sometimes (not always) its turning off both the outputs & code gets stucked.

Code:
		org	0x0004	
		movwf	W_Temp
		swapf	STATUS,W
		movwf	S_Temp		
		clrf	STATUS
			
		btfss	PIR1,RCIF	;is it a reception?
		goto	Exit
		bcf	PIR1,RCIF			
								
CHR_A		movf	RCREG,W		;check charactor "A"
		movwf	T_Rcreg		;move to temperory registor		
		xorlw	'A'
		btfss	STATUS,Z
		goto	CHR_B
		btfss	Enable,0
		goto	$+3
		bsf	Enable,1
		goto	Exit
		clrf	IMAGE
		clrf	PORTA
		clrf	Enable
		call	Clear_CHR
		goto	Exit
			
CHR_B		movf	T_Rcreg,W	;check charactor "B"
		xorlw	'B'
		btfss	STATUS,Z
		goto	CHR_Number				
		btfss	IMAGE,0
		goto	Exit
		movlw	b'10'
		xorwf	IMAGE,F
		movf	IMAGE,W			
		movwf	PORTA
		goto	Exit
						
		
Read_CHR	btfss	PIR1,RCIF	;is it a reception?
		goto	Read_CHR
		bcf	PIR1,RCIF
		movf	RCREG,W		;yes,then take a copy of the character
		return
			
Exit		movf	RCREG,W
		movf	RCREG,W
		movf	RCREG,W
		swapf	S_Temp,W
		movwf	STATUS
		swapf	W_Temp,F
		swapf	W_Temp,W					
		retfie
 
Last edited:
Hi,
What's the predefined value for IMAGE? Is there anything that change the value of IMAGE? If no then bit 0 of IMAGE can be 0 or 1 we don't know that. If it is 0, it will never go to 1 so RA0 will stay low everytime it's updated.
Code:
CHR_B        movf    T_Rcreg,W    ;check charactor "B"
        xorlw    'B'
        btfss    STATUS,Z
        goto    CHR_Number                
        btfss    IMAGE,0
        goto    Exit
        movlw    b'10'
        xorwf    IMAGE,F
        movf    IMAGE,W            
        movwf    PORTA
        goto    Exit
 
IMAGE register is a shadow register of PORTA.When I update PORTA I take a copy to this register.

If the PORTA,0 (RA0) is ON this IMAGE,0 will be set.So all the time this bit is set.This bit will clear if character "A" received.

Do you see another problem in my code?
 
You know why I'm checking this btfss IMAGE,0? because RA1 must toggle if RA0 is already set.If RA0 is not set then do not toggle.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top