I have come across a most curious behaviour in the ATMega328p. Under certain conditions, doing an st X,R16 appears to brick my program (but not the chip itself). Storing certain values at XH:XL as shown by the "<- *****" comment below just stops the code running. I apologize for assembler, but this is all I use. I have tried with two different 328's, from different sources. Here is my test code, with a lot of the preamble omitted:
; Background: ; This is a highly condensed version of a filing program, only to show a ; curious behaviour of the ATMega328p ; I have not included any of the initialization, ; Setting up the stack, disabling the Watchdog, clearing RAM, and ; initializing the LED output ; I use a=R16, b=R17 and 'file_pointer' happens to be R13 (must have been ; the wrong choice ;-) main: clr file_pointer mm3: sbi LED_port,LED ; Flash LED - at least we've got here rcall wait20ms cbi LED_port,LED rcall wait200ms loadZ test_string mm1: lpm a,Z+ cpi a,0 ; exit on null terminator breq mm2 mov XL,file_pointer clr XH ldi b,'f' ; 'f' is no good, 's' is OK st X,b ; <-- !!!!!!!!!!!!!!!!!!!!!!!!!! inc file_pointer rjmp mm1 mm2: sbi LED_port,LED ; Flash LED - and we've got here rcall wait20ms cbi LED_port,LED self: rjmp self ;------------------------------------------------------------------------------ test_string: .db "Now is the time for all good men to come to the aid of the party",0,0 ;------------------------------------------------------------------------------ .include "wait.inc" ; routines to provide delays ;------------------------------------------------------------------------------ .dseg .org SRAM_START file_buffer: .byte 128 ; file output buffer end_file_buffer: ;------------------------------------------------------------------------------ ; End of source code
I have tried a few different letters in the line < ldi b,'f' > and some also cause a problem. If I had to take a stab at the cause I would say it was a problem in the chip's microcode but I don't even know if this is how Atmel chips work.