ATTiny2313 I/0 Prob/Error

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

Greetings A/All

I have a problem with an ATtiny2313 project wrt I/O.

The error must be domestic blindness as I cant find anything wrong in my code.

To confirm this I wrote a test program (see below) and tested this on the target board with an
ATtiny2313-20SU and on the STK500 with an ATtiny2313-20PU

;------------------------------------------------------------------------------
;		ioTest.asm			Ver: 0.xxx			Created: 20-MAY-08
;------------------------------------------------------------------------------
;	20-MAY-08	Created this IO Test program for Debug.
;------------------------------------------------------------------------------

;******************************************************
.include "tn2313def.inc"	;Definitions for ATTiny2313
;******************************************************

;Ram, 128 bytes from 060h to 0dfH.
.equ	RAM_START		=$60		;512 bytes of ram available.

; Data memory area.

.dseg
	.org	RAM_START

ram_base:		.byte	1		;1st Ram Location.
;
;Ram is not used.
	
;--------------------------------------------------------------------
; Used Register definitions...
;--------------------------------------------------------------------

.def	data_Transfer	=r0		;LPM Data Transfer Register.

;--------------------------------------------------------------------
;R16 & Above, Allow Immediate instructions as follows:
; subi,sbci,andi,ori,cpi, ldi & sbr, etc.
;To load other registers with immediate data, use a register >R15
;and use 'mov' to transfer Rd,Rr.
;--------------------------------------------------------------------

.def	byte_count	=r17

.def	temp		=r21

;R26 - R31 are 'X', 'Y' & 'Z' 16 bit pointers.

;--------------------------------------------------------------------

;PortB equates
.equ	AIN0		=0		;Analog Comparator +ve input from xxx
.equ	AIN1		=1		;Analog Comparator -ve input from PWM filter for yyy
.equ	PWM			=2		;PWM Output from Timer 0, output compare A.
.equ	MODE		=3		;Mode Select input ...
.equ	OUT4		=4		;PB4 output ...
.equ	LED_8		=5		;Active High LED 8.
.equ	LED_9		=6		;Active High LED 9.
.equ	LED_10		=7		;Active High LED 10.

;PortD equates.
.equ	LED_6		=0		;Active High LED 6.
.equ	LED_5		=1		;Active High LED 5.
.equ	LED_4		=2		;Active High LED 4.
.equ	LED_3		=3		;Active High LED 3.
.equ	LED_2		=4		;Active High LED 2.
.equ	LED_1		=5		;Active High LED 1.
.equ	LED_7		=6		;Active High LED 7.

;--------------------------------------------------------------------

; Program code.
.cseg

;--------------------------------------------------------------------
;Reset & Interrupt Vectors for ATTiny2313.
;--------------------------------------------------------------------


RESET:
	rjmp	initialize		;Reset vector.
	rjmp	initialize		;Unused IRQ0 External Interrupt.
	rjmp	initialize		;Unused IRQ1 External Interrupt.
	rjmp	initialize		;Unused Timer1 Capture Interrupt.
	rjmp	initialize		;Unused Timer1 Compare A Interrupt 4ms Tick.
	rjmp	initialize		;Unused Timer1 Overflow Interrupt.
	rjmp	initialize		;Unused Timer0 Overflow Interrupt.

	rjmp	initialize		;Unused UART RX Interrupt.
	rjmp	initialize		;Unused ;UART TX Buffer Emply Int.
	rjmp	initialize		;Unused UART TX complete Interrupt.
	rjmp	initialize		;Unused	;Analog Comparitor Interrupt.

	rjmp	initialize		;0x000B PCINT Pin Change Interrupt
	rjmp	initialize		;Unused ;TIMER1 COMPB Compare Match B
	rjmp	initialize		;Unused TIMER0 compare Match A, interrupt.
	rjmp	initialize		;0x000E TIMER0 COMPB Timer/Counter0 Compare Match B
	rjmp	initialize		;0x000F USI START USI Start Condition
	rjmp	initialize		;0x0010 USI OVERFLOW USI Overflow
	rjmp	initialize		;0x0011 EE READY EEPROM Ready
	rjmp	initialize		;0x0012 WDT OVERFLOW Watchdog Timer Overflow

; Note:	All unused interrupts jump to Initialize.

;--------------------------------------------------------------------

initialize:
	ldi	temp,low(RAMEND)
	out	SPL,temp

	ldi	temp,0b11110100		;Bits 0,1,3 input, bits 2 & 4-7 output.
	out	DDRB,temp			;Set PortB direction as required.

	ldi	temp,0b00001000		;Setup port B, Outputs off, PB3 pullup on.
	out	PORTB,temp			;Set initial data, all output pins 0V.

	ldi	temp,0b01111111		;All 7 pins are LED outputs.
	out	DDRD,temp			;Set PortD direction as all outputs.

	clr	temp				;Setup port D.
	out	PORTD,temp			;Set initial data, all pins 0V.

clear_ram:				;Clear ram.
	ldi	byte_count,128		;Number of bytes to clear.
	ldi	XH,high(ram_base)	;Base of ram high.
	ldi	XL,low(ram_base)	;Base of ram low.
	clr	temp			;Initialize value.

clear_ram_loop:
	st	X+,temp			;Clear this location.
	dec	byte_count		;One less byte to clear.
	brne	clear_ram_loop		;Loop until all ram cleared.

clear_registers:			;Clear registers.
	clr	r0
	clr	r1
	clr	r2
	clr	r3
	clr	r4
	clr	r5
	clr	r6
	clr	r7

	clr	r8
	clr	r9
	clr	r10
	clr	r11
	clr	r12
	clr	r13
	clr	r14
	clr	r15

	clr	r16
	clr	r17
	clr	r18
	clr	r19
	clr	r20
	clr	r21
	clr	r22
	clr	r23

	clr	r24
	clr	r25
	clr	r26
	clr	r27
	clr	r28
	clr	r29
	clr	r30
	clr	r31

;-----------------------------------------------------------------------+

	sei				;Okay to enable interrupts, although not used.

;next 17 lines of code are DEBUG program, i.e. the whole program !

debug:
	ldi	temp,0b11110100		;Bits 0,1,3 input, bits 2 & 4-7 output.
	out	DDRB,temp			;Set PortB direction as required.

	ldi	temp,0b01111111		;All 7 pins are LED outputs.
	out	DDRD,temp			;Set PortD direction as all outputs.

;Force all leds on.
	ldi		temp,0b01111111
	out		PORTD,data_transfer	;Update PORTD LEDs.

	ldi		temp,0b11100000
	mov		data_transfer,temp

	in		temp,PORTB			;Get current PORTB data.
	andi	temp,0b00011111		;Clear only LED Display Bits.
	or		temp,data_transfer	;Update PORTB Display bits.
	out		PORTB,temp			;Update PORTB LEDs.
;Force all leds on end.

	rjmp	debug


;------------------------------------------------------------------------------

.exit
;The above Directive ends assembly, Any code that follows is ignored.

;--------------------------------------------------------------------

Both Mis-behave (dont know the politicaly correct form foe non gender specific behaviour probs, LOL) in the same way.

PORTD Bits 0,1,2,3,4 are Low, Should be High.
PORTB seems Okay.

Why did my failing eyes decieve me?

Ron.

Grrr... BTW How do you correctly post ASM Code here?

MODERATOR: YOU DID IT RIGHT BUT YOU HAD THE "Disable BBCode in this post" BOX TICKED - WHICH IS WHY IT WOULDN'T DISPLAY CORRECTLY

 

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

You have not assigned "data_transfer" before use. So it starts with 0. Hence your PORTD = 0

This is a good candidate for simulating in Studio4.

David.

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

Quote:
You have not assigned "data_transfer" before use. So it starts with 0. Hence your PORTD = 0

hrmm, i did assign ...
.def data_Transfer=r0 ;LPM Data Transfer Register.

but not assign data_transfer, I'll try the case sensitive option.

Thanks.

Edit: Changed the definition of data_Transfer to data_transfer, no change in Output data.

Edit: Found error in output routine ... now:
;Force all leds on.
ldi temp,0b01111111
out PORTD,temp ;Update PORTD LEDs.

ldi temp,0b11100000
mov data_transfer,temp

in temp,PORTB ;Get current PORTB data.
andi temp,0b00011111 ;Clear only LED Display Bits.
or temp,data_transfer ;Update PORTB Display bits.
out PORTB,temp ;Update PORTB LEDs.
;Force all leds on end.
edit: Found bug in output routine, thanks david, it wasnt just a case error thing, will check my real code.

 

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

You also might want to avoid mixing up the base numbers when loadind registers. Meaning I noticed you had decimal, hex, and binary.

While it is the programmers own personal preference, when posting code for others to see it could be a tad confusing.

JMO.

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
.equ   RAM_START      =$60      ;512 bytes of ram available. 

You don't need this. Just use the correct name in the definition file SRAM_START

   .org   RAM_START 

Don't need this either as the assembler start from the first byte of SRAM_START. Unless of course you want something at a particular location.

.def   data_Transfer   =r0      ;LPM Data Transfer Register. 

You are no longer limited to r0 with newer processors like it used to be the case earlier. Any register can be used as long as you use z as a pointer.

.def   temp      =r21 

Use r24 and r25 as temp and temp1, it is a bit more useful.

Also may find this useful:

;Clear all memory
clr_mem:
	ldi		yh,high(SRAM_START)	
	ldi		yl,low(SRAM_START)
	clr		temp
	ldi		temp1,high(ramend+1)
clr_mem_lp:
	st		y+,temp
	cpi		yl,low(ramend+1)	
	cpc		yh,temp1
	brne	clr_mem_lp
;Clear all registers
clr_regs:
	clr		zh
	ldi		zl,0x1d				;Start at YH
	clr		r0
clr_reg_lp:
	st		z,r0				;Clear register
	dec		zl					;Decrement pointer
	brne	clr_reg_lp

And PLEASE use the CODE button when posting any code.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

@david

Quote:
You have not assigned "data_transfer" before use. So it starts with 0. Hence your PORTD = 0

Yes You were spot on. The error was in the first output instruction. Temp was loaded with
the I/O data and the data_transfer register was written to the port. After your post, I was looking more closely at the second output write, as it included some bit manipulations.

The second line of code was the error: Should have been: out PORTD,temp !
; Force all leds on.
ldi temp,0b01111111
out PORTD,data_transfer ;Update PORTD LEDs.

@Jim

Quote:
You also might want to avoid mixing up the base numbers when loading registers. Meaning I noticed you had decimal, hex, and binary.
While it is the programmers own personal preference, when posting code for others to see it could be a tad confusing.

Noted, But for me, I prefer binary for bit manipulation, as it gives a clearer picture
other times Hex or Decimal are easier.

@John

Quote:
You are no longer limited to r0 with newer processors like it used to be the case earlier. Any register can be used as long as you use z as a pointer.

Noted, but in this application, R0 suits me fine, though I should use the auto increment function, as it would save the ADIW stuff in the final code.
Quote:
Use r24 and r25 as temp and temp1, it is a bit more useful.

Good idea to reserve these as permanent temp egisters, often need more than one.

Thanks for the "Clear all memory" & "Clear all registers" routines.
The clear all memory one is tidier than mine, and the clear registers routine
is much better than the one I previously used. Note that in the posted code the
registers were individually cleared to minimise the possibility of errors.

Quote:
And PLEASE use the CODE button when posting any code.

Did that, but Moderator informed me: ...
Quote:
MODERATOR: YOU DID IT RIGHT BUT YOU HAD THE "Disable BBCode in this post" BOX TICKED - WHICH IS WHY IT WOULDN'T DISPLAY CORRECTLY

Noted I hope i remember for next time.

Thanks to All who replied, through your help I have found my error and learned lots.

(the error in the final code was the offset register looking into a word table was not doubled, as the table was words, not bytes) The problem in the simple debug program was found by dave, and not immediatly recognised by me.

Kind Regards, Ron