ATtiny12 gone mad

Status
Not open for further replies.

DigiTan

New Member
I've been working on a security device program all afternoon. The program so far was supposted to be simple: use the hardware timer to blink an LED while the device is calibrating.

This worked perfectly until just a few hours ago, when the program just "fell apart" and appearantly does nothing. First, the LED refused to blink at all, even though single-steping through the code revealed software had no errors--yet the chip was running the timer interrupt at all.

Next, it stayed "frozen" unless I uploaded from the Simulator instead of the HEX file. Doing this made it work perfectly--but after doing this three more times, AVR Studio reads: WARNING: FLASH contents differs from file.. FAILED! The strange part is, this chip is brand new, and the flash only has maybe 30-40 writes to it. Also, strange it started "freezing" before any FLASH problems were detected.
-------------
I've re-checked the programming 20 times, simulated, and commented out all but the most basic functions. Obviously, the chip is defective, but I wanted to run the code by you guys just in case.

Also when loading the AVR programmer window, it said it needed a firmware upgrade, but I cancelled this. This would've caused suspicion, but the ATtiny12 programs have gone perfectly until just this evening.

* I have AVR Studio v4.11 and recently upgraded from build 171 to build 401--and I'm using the built-in simulator for debugging. I'm programming with STK500 (firmware v1.0A), and it's suggesting I upgrade to v2.00. Will the upgrade make any difference? The ATtiny12 is using its internal 1.2MHz clock.


Code:
;- Register Aliases -----
.def	quarters		= R1 ; Quarter-second counter (int-driven)
.def 	blink_rate		= R2 ; LED flash rate	; 0 = off
												; 1 = ~1/2 sec
.def	prev_sensors	= R3 ; Previous sensor state
.def	delay_ovf		= R4 ; Used by 1/4sec delay routine

;.def					= R16 ; General-purpose register by tradition
.def	loop			= R17 ; Loop/increment counter by tradition
.def	addr_a			= R18 ; 8-bit Address-holder by tradition
.def	addr_b			= R19 ; 8-bit Address-holder by tradition
.def	i_temp			= R20 ; Interrupt's temp register
.def	instability		= R21 ; Sensor stability failures
.def 	loop2			= R22 ; Secondary loop counter
;########################
;# Flash Memory			#
;########################
.cseg					; (Begin) Code Segment
.org 0x00				; Start address

	rjmp main			; Reset Vector
;########################
; Interrupt Vectors		#
; Tiny12				#
;########################
	RETI				; IRQ0 Handler
	RETI 				; Pin Change Handler
	rjmp tc0i			; Timer0 overflow
	RETI				; EEPROM Ready Handler
	RETI 				; Analog Comparitor Handler
main:
;---Port Direction Setup-		
	ldi R16,0b00011000	; Create input/output states
	out DDRB,R16		; All ports used for I/O roles
;---Port Initialization--
	ldi R16,0b00011111	; Pull-up resistors on (PB.5 not equipped)
	out PORTB,R16		; LED and Alarm on breifly
;---Analog Comparitor Disable
	ldi R16,0b10000000	; Sent config: [7] Comparitor turn-off
	out ACSR,R16		; [3] Comparitor interrupt disabled
;---Watchdog Timer Disable
	ldi R16,0b00010000	; Sent config: [4] Watchdog turn-off
	out WDTCR,R16		; [3] Watchdog disable
;---Timer 0 Interrupt----
	ldi R16,0b00000101	; Prescale: 1.2MHz / 1024
	out TCCR0,R16		; Interval: 853us  Overflows: 0.218s

STANDBY:
;---External Interrupt Setup
	ldi R16,0b01000000	; Sent config: [6] IRQ0 Enabled
;	out GIMSK,R16		; [5] Pin-change interrupt disabled
	SEI					; Global Interrupt Enable
;---Power-down Enable/IRQ Mode
;	cbi PORTB,3			; Alarm latch off
;	cbi PORTB,4			; LED latch off
;	ldi R16,0b00110010	; Sent config: [5] Sleep Enabled
;	out MCUCR,R16		; [4] Sleep mode [0,1] falling edge trigger	
;---Micro-power state----
;	sleep				; Awaken to IRQ0 routine
;---Sleep/IRQ0 Disable---
	clr R16				; R16 = 0
	out MCUCR,R16		; Sent: [5] Sleep Disabled
	out GIMSK,R16		; Sent: [6] IRQ0 Disabled

;---Check IR Data Port

ARMED:
;---Timer Interrupt Enable
	ldi R16,0b00000010	; Timer overflow interrupt enable bit set
	out TIMSK,R16		; 
;---Stabilization-Countdown
	ldi R16,0b00000010		; Change LED blink rate
	mov blink_rate,R16		; New blink rate = ~1.2 Hz

debug:
	rjmp debug

tc0i: ; [Nominal int rate = 4.57Hz]
	inc quarters		; Update counter for main loop codes
	mov i_temp,quarters	; Store "timer" to temporary register
	and i_temp,blink_rate; Mask to determine LED blink rate	
	breq led_off		; If 
led_on:
	sbi PORTB,4			; LED on
	rjmp led_done
led_off:
	cbi PORTB,4			; LED off
led_done:
	reti
 
Okay, I checked with avrfreaks and it was a combination of not saving the SREG flags in the tc0i interrupt, and the auto-chip erase option not really working. I did a basic chip erase and changed the interrupt to the one below. Everything seems to be working now...

Code:
tc0i: ; [Nominal int rate = 4.57Hz] 
   in sreg_save,SREG
   inc quarters      ; Update counter for main loop codes 
   mov i_temp,quarters   ; Store "timer" to temporary register 
   and i_temp,blink_rate; Mask to determine LED blink rate    
   breq led_off      ; If 
led_on: 
   sbi PORTB,4         ; LED on 
   rjmp led_done 
led_off: 
   cbi PORTB,4         ; LED off 
led_done: 
   out SREG,sreg_save
   reti
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…