Bit toggling (or com'ing a bit) [solved - misunderstood SBR]

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When the following is executed in either simulator 1 or 2 in Studio 4, the I/O toggle works but not the register toggle. So can someone tell me what's wrong? Another simulator bug?

Test 1 is for register bit toggling whilst Test 2 is for I/O bit toggling. You choose.

.include	"tn2313def.inc"		

.def	Temp	=	R16

.cseg									

.org	0x0000							

	rjmp	Reset

Reset:

   	ldi 	Temp,	Low(RamEnd)			
	out		SPL,	Temp
   		
   	ldi		Temp,	$01				
;	out		PORTB,	Temp

Test1:
  
	sbrs	Temp,	0
	sbr		Temp,	0

	out		PORTB,	Temp

	sbrc	Temp,	0
	cbr		Temp,	0

	out		PORTB,	Temp

;	rjmp	Test1

Test2:
  
	sbis	PORTB,	0				
	sbi		PORTB,	0

	sbic	PORTB,	0
	cbi		PORTB,	0

	rjmp	Test2


Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Another simulator bug?
No, a bug in your code.
"sbr" is not the register equivalent to "sbi". The second parameter has a different meaning.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ahh yes, I see the numeric is in hex. got confoosed with the 0 bit. Tried it with sbr 1 and all is well again. Thanks for your quick reply. Problem solved.

sternst wrote:
Quote:
Another simulator bug?
No, a bug in your code.
"sbr" is not the register equivalent to "sbi". The second parameter has a different meaning.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is another pin toggle method in all newer AVRs including your Tiny2313. To toggle the port pin you just write "1" to the corresponding PINx register's bit - sbi PINB,0.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes. All this started because I'm trying to reinvent the wheel. I needed a block of code to replace the 8051 CPL instruction, (CPL bit) compliment a bit in a register.
So one has to read it and determine status and flip it without affecting the other bits.

Just because, I'll try the PIN function as well.

MBedder wrote:
There is another pin toggle method in all newer AVRs including your Tiny2313. To toggle the port pin you just write "1" to the corresponding PINx register's bit - sbi PINB,0.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So here's what I came up with for porting the CPL bit flipping instruction in 8051 assembler to AVR assembler. Testing bits 0-7 of a register and flipping them accordingly without affecting the other bits. Using the T bit, and jumping to the proper solution.

.include	"tn2313def.inc"		

.equ	Bit0	=	$01
.equ	Bit1	=	$02
.equ	Bit2	=	$04
.equ	Bit3	=	$08
.equ	Bit4	=	$10
.equ	Bit5	=	$20
.equ	Bit6	=	$40
.equ	Bit7	=	$80

.def	Temp	=	R16

.cseg									

.org	0x0000							

     rjmp	Reset

Reset:

     ldi   Temp,   Low(RamEnd)			
     out   SPL,	 Temp
   		
     ser   Temp
     out   PORTB,  Temp

Test0:

     bst    Temp,    0	
     brtc   SetB0
     brts   ClrB0

;Test1:
;thru
;Test7: etc.....

SetB0:

    sbr	  Temp,   Bit0		
    rjmp    End

ClrB0:

    cbr	  Temp,   Bit0		
    rjmp    End

;SetB1:
;ClrB1:
;thru
;SetB7:
;ClrB7: etc....

End:

    rjmp    End
	

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sparky85 wrote:
Testing bits 0-7 of a register and flipping them accordingly without affecting the other bits. Using the T bit, and jumping to the proper solution.
Why you don't simply use EOR?

Toggle bit 0 and 2 in register r6:

LDI Temp, $05
EOR r6, Temp

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I understand what you are saying but I'm looking for a simple toggle register or PORTx bitwise by name instruction for the original CPL instruction. Ex: eor name. Can't be done.
From the AVR instruction set: "Description: Performs the logical EOR between the contents of register Rd and register Rr and places the result in the destination register Rd."

Syntax: CPL operand
Function: Complement Port Location

So:
Outs equ P0 ; Port Location
Out0 equ P0.0
Out1 equ P0.1

CPL Out1 ;(P0.1) "Port Location")

;=============================

Whereas:
Function: Complement Memory Location

Outs: ; Memory Location
Out0 BIT Outs.0
Out1 BIT Outs.1
Out2 BIT Outs.2

CPL Out1 ;(Outs) "Memory Location")

Description: CPL complements operand, leaving the result in operand. If the operand refers to a bit of an output Port, the value that will be complemented is based on the last value written to that bit, not the last value read from it.

Damn confusing that 8051 to AVR porting.

sternst wrote:
Why you don't simply use EOR?

Toggle bit 0 and 2 in register r6:

LDI Temp, $05
EOR r6, Temp

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wonder why don't you just use the sbi PINB,X method I suggested above?

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.