; CE-2800 ProgramMemoryAccess.asm ; Purpose: Demonstrate how use Program Memory as permanent storage for data ; and how to store/load from Program Memory .NOLIST .INCLUDE "m32def.inc" ; contains .EQU's for DDRB, PORTB, and a lot more .LIST .DEF TEMP=R25 .DEF SUM=R26 .EQU start = 0x2a ; define a symbol to represent address 0x2a in program memory .CSEG ; this means further directives are for the Code Segment ;*********************************************************************************************************** .ORG 0x0 rjmp start ; initialize Reset Vector at 0x0000 ;*********************************************************************************************************** .DSEG ; switch subsequent directives to apply to the Data Segment .ORG SRAM_START ; SRAM_START from m32def.inc: the address (0x0060) where SRAM memory begins in the Data Segment result: .byte 1 ; reserve a byte of data memory (SRAM) ;*********************************************************************************************************** .CSEG ; switch back to Code Segment .ORG start ; defines where these instructions start getting placed in Program Memory ldi TEMP, 0xff ; init PortB for output out DDRB, R25 ldi TEMP, 0x0 out PORTB, TEMP ; all LEDs off ; The address assigned to x1 by the assembler is a WORD address; Z needs the corresponding BYTE address ; There are two bytes for every word in Program Memory, so the byte address is just twice the word address ; Thus, 2*x1 is the byte address, so we load the low part of that result into ZL and the high part into ZH ldi ZL, LOW(2*x1) ; initialize low byte of the Z index register ldi ZH, HIGH(2*x1) ; initialize high byte of Z; now Z contains the address of the first byte of x1 clr SUM loop: lpm TEMP, Z+ ; load the contents pointed to by register Z and post-increment out PORTB, TEMP ; "display" the value to the LEDs add SUM, TEMP ; add to the running sum tst TEMP ; test the value loaded from memory (sets bits in SREG) brmi store_result ; break on negative rjmp loop ; otherwise, repeat the loop store_result: ; we get here when the loop Counter reaches 0 sts result, SUM ; finally, shove the total Sum into data memory at the address represented by "result" done: ; spin here forever rjmp done ;*********************************************************************************************************** ; In this section of Program Memory, we define data we want to persist through power cycles .ORG done+0x10 ; start storing data 0x10 words past the location of the rjmp instruction x1: .DB 72, 24, 35, 16, 100, -1 ; an "array" of numerical values, with the -1 at the end indicating end of array title: .DB 'c','s','2','8','0', 0 ; a null-terminated array of chars the 0 at the end indicates null-terminator for end-of-chars course: .DB "CS280",0 ; a null-terminated string, the 0 at the end indicates end-of-string