Memory looks strange when I try to simulate

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

Hi!
I try to simulate a program on a atmega328p, when I build the project and try to simulate my memory looks like this:
 

prog....                                                                   32 c0 ff ff ff ff ff ff ff  
prog 0x0009  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  
prog 0x0026  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  
prog 0x0043  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  
prog 0x0060  ff ff ff ff ff ff 08 e0 0e bf 0f ef 0d bf 00 24 0f ef 10 2e 04 b8 07 b8 15 b8 18 b8 0b  
prog 0x007D  d0 11 d0 f0 e0 e1 e0 20 e0 2c d0 0a d0 44 d0 00 00 00 00 fa cf e9 cf 0f 93 0f ef 0a b9  
prog 0x009A  0f 91 08 95 1b b9 08 95 0f 93 3f 92 3f b6 00 91 7c 00 0f 7d 00 62 00 93 7c 00 00 91 7a  
prog 0x00B7  00 0f 77 00 68 00 93 7a 00 00 91 7a 00 08 7f 04 60 00 93 7a 00 00 91 7c 00 0f 73 00 6c  
prog 0x00D4  00 93 7c 00 3f be 3f 90 0f 91 08 95 0f 93 3f 92 3f b6 00 91 7c 00 00 7f 02 2b 00 93 7c 
prog 0x00F1  00 00 91 7a 00 0f 7b 00 64 00 93 7a 00 00 91 7a 00 00 71 00 31 d9 f7 10 91 79 00 3f be 
prog 0x010E  3f 90 0f 91 08 95 2f 93 3f 92 3f b6 ef 93 ff 93 32 97 3a f0 31 96 29 ef 00 00 2a 95 e9  
prog 0x012B  f7 31 97 d1 f7 22 ef 00 00 2a 95 e9 f7 3f be ff 91 ef 91 3f 90 2f 91 ff ff ff ff ff ff  
prog 0x0148  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  
prog 0x0165  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 
prog 0x0182  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 
prog 0x019F  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff

The ff just keeps on going after that.
I than tried to open up another project to see if it works but it gives the same ff everywhere. Anyone knows why this happens?
When I step through the program it doesn't update the effected register in the program flash.
Just ask me for any other information if there's something you need.

Thank you so much for your time!

This topic has a solution.
Last Edited: Fri. Dec 10, 2021 - 11:03 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to AVRFreaks.

 

It'd look better posted as code - to maintain the layout:

prog....                                                                 32 c0 ff ff ff ff ff ff ff
prog 0x0009  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
prog 0x0026  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
prog 0x0043  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
prog 0x0060  ff ff ff ff ff ff 08 e0 0e bf 0f ef 0d bf 00 24 0f ef 10 2e 04 b8 07 b8 15 b8 18 b8 0b
prog 0x007D  d0 11 d0 f0 e0 e1 e0 20 e0 2c d0 0a d0 44 d0 00 00 00 00 fa cf e9 cf 0f 93 0f ef 0a b9
prog 0x009A  0f 91 08 95 1b b9 08 95 0f 93 3f 92 3f b6 00 91 7c 00 0f 7d 00 62 00 93 7c 00 00 91 7a
prog 0x00B7  00 0f 77 00 68 00 93 7a 00 00 91 7a 00 08 7f 04 60 00 93 7a 00 00 91 7c 00 0f 73 00 6c
prog 0x00D4  00 93 7c 00 3f be 3f 90 0f 91 08 95 0f 93 3f 92 3f b6 00 91 7c 00 00 7f 02 2b 00 93 7c
prog 0x00F1  00 00 91 7a 00 0f 7b 00 64 00 93 7a 00 00 91 7a 00 00 71 00 31 d9 f7 10 91 79 00 3f be
prog 0x010E  3f 90 0f 91 08 95 2f 93 3f 92 3f b6 ef 93 ff 93 32 97 3a f0 31 96 29 ef 00 00 2a 95 e9
prog 0x012B  f7 31 97 d1 f7 22 ef 00 00 2a 95 e9 f7 3f be ff 91 ef 91 3f 90 2f 91 ff ff ff ff ff ff
prog 0x0148  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
prog 0x0165  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
prog 0x0182  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
prog 0x019F  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff

Presumably, this is just a small program?

 

So the FFs are just unused locations in Flash - where FF is the "erased" (ie, unused) state.

 

EDIT

 

it doesn't update the effected register in the program flash

The registers aren't in the program flash.

 

In Microchip Studio, there's a separate window to view the Registers

 

See this post for getting started with  Atmel  Microchip Studio: https://community.atmel.com/comm...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Dec 10, 2021 - 10:35 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes it's a small program, the exercise is to set up timers and interrupts as well as ADC and output to DAC.

So erased, does that mean that the program doesn't use them by my code and hence it doesn't matter what is in them? Cause I tried to hard code a small line that would just change a DDR register to 0b10101010 just to see if it would set it when simulating but nothing happens to the program flash when I try to step through the program.
Have a made some stupied coding error or do you know what I should do to be able to simulate again?

My code:

 

.NOLIST
.INCLUDE "m328pdef.inc"
.LIST

.DEF temp = r16                ; Temporary variable


; Define variables stored in SRAM
.DSEG
.ORG 0x0100
DAC_ONOFF: .BYTE 1            ; Store DAC ON/OFF
MUX_CHANNEL: .BYTE 1        ; Store analog channel


.CSEG
; Program reset
.ORG 0
    rjmp RESET

; Interrupt PCINT0
.ORG 0x0006
    rcall pinInterrupt

; Interrupt timer0 compare match A
.ORG 0x001C
    rcall timerInterrupt


.ORG 0x033
RESET:
    ldi temp, LOW(RAMEND)    ; Initalize
    out SPL, temp            ; stack pointer
    ldi temp, HIGH(RAMEND)    ; to RAMEND
    out SPH, temp            ;
    ldi temp, 0x00            ;
    sts MUX_CHANNEL, temp    ; Select analog channel ADC0
    cli                        ; Turn off interupts
    rcall initDAC            ; Initalize DAC
    rcall initADC            ; Initalize ADC
    rcall initTimer            ; Initalize timer
    rcall initPCINT0        ; Initalize external interrupt
    sei                        ; Turn on interupts

busyWait:
    ldi r18, 0xff
    out PORTB, r18
    rjmp busyWait


;--------------------------------------------------- 
; pinInterrupt: Read PINB0 and toggle DAC ON/OFF.
;                If PINB0 = 1, DAC is ON.
;                If PINB0 = 0, DAC is OFF.
;   
; Input: None.
; Output: None.
; Sideeffects: None.
; Registers affected: None.
;--------------------------------------------------- 
pinInterrupt:
    push r18                ; Save environment
    push r19
    in r19, SREG

    in r18, PINB0            ; Read PINB0
    cpi r18, 1<<PINB0        ; Compare if 1
    brne DACOFF                ; If 0, branch to label
    ldi r18, 0b1            ; Toggle ON
    rjmp pinEnd
    
DACOFF:
    ldi r18, 0b0            ; Toggle OFF
    
pinEnd:
    sts DAC_ONOFF, r18        ; Store ON/OFF in SRAM
    out SREG, r19            ; Restore environment
    pop r19
    pop r18

    reti                    ; Return from interrupt

;--------------------------------------------------- 
; timerInterrupt: Sample ADC by calling inADC8 subroutine and output
;                  value to PORTB by calling outDAC subroutine.
;                  No output to PORTB is DAC is toggled OFF.
;   
; Input: None.
; Output: None.
; Sideeffects: None.
; Registers affected: None.
;--------------------------------------------------- 
timerInterrupt:
    push r18                ; Save environment
    push r19
    in r19, SREG
    
    rcall inADC8            ; Read ADC

    lds r18, DAC_ONOFF        ; Load DAC ON/OFF from SRAM
    cpi r18, 0b1            ; Compare if ON
    brne timerDACOFF
    rcall outDAC            ; Output ADC conversation to DAC
    rjmp timerEnd


timerDACOFF:
    ldi r20, 0x00            ; No output to DAC
    rcall outDAC


timerEnd:    
    out SREG, r19            ; Restore environment
    pop r19
    pop r18

    reti                    ; Return from interrupt

;--------------------------------------------------- 
; initTimer: Initalize timer 0 with compare match A interupts.
;             The timer is set with a prescaling factor of 8.
;             The compare value is set to 0x0F.
;   
; Input: None.
; Output: None.
; Sideeffects: None.
; Registers affected: TCCR0A, TCCR0B, TIMSK0 and OCR0A
;--------------------------------------------------- 
initTimer:
    push r16                        ; Save enviornment
    ldi r16, 1<<WGM01                
    out TCCR0A, r16                    ; Clear timer on compare Match
    ldi r16, 1<<CS01                 
    out TCCR0B, r16                    ; Set prescaler to 8
    ldi r16, 0x0F                    
    sts OCR0A, r16                    ; Set compare value to 0x0F
    ldi r16, 1<<OCIE0A                
    sts TIMSK0, r16                    ; Enable Timer0 compare match A 
    pop r16                            ; Restore environment

    ret                                ; return from subroutine

;---------------------------------------------------------------------
; initPCINT0: Set PINB0 to be read, enable interrupts triggered by voltage change
;              at PINB0.
;   
; Input: None
; Output: None
; Sideeffects: None
; Registers affected: PCICR and PCMSK0
;---------------------------------------------------------------------- 
initPCINT0:
    push r16                ; Save environment
    ldi r16, 1<<PCIE0 
    sts PCICR, r16            ; Pin change interrupt enable 0
    ldi r16, 1<<PCINT0         
    sts PCMSK0, r16            ; Enable interrupt on PINB0
    pop r16                    ; Restore environment

    ret                        ; Return from subroutine

;---------------------------------------------------------------------
; initDAC: Initalizes PortD to output.
;   
; Input: None
; Output: None
; Sideeffects: None
; Registers affected: DDRD
;---------------------------------------------------------------------- 
initDAC:
    push r18
    ldi r18, 0xFF            ; Set PortD
    out DDRD, r18            ; to output
    pop r18

    ret                        ; Return from subroutine

;---------------------------------------------------------------------
; initADC: Initalize and set up ADC to free-running mode. The ADC will
;           have a reference voltage AVcc. The result will be left adjusted
;           and have a 8-bit precision.
;           The ADC will only read the input from channel 0.
;           Frequency of samples: 8MHz
;
;           Hardware: Set external capacitor at AREF pin
;   
; Input: None
; Output: None
; Sideeffects: None
; Registers affected: ADMUX and ADCSRA
;---------------------------------------------------------------------- 
initADC:
    push r18
    ldi r18, (0<<REFS1) | (1<<REFS0) | (1<<ADLAR)        ; Set reference voltage to AVcc and
    sts ADMUX, r18            ; set the result to be left adjusted
    ldi r18, 1<<ADEN        ; ADC enable, 
    sts ADCSRA, r18            ; sample division factor set to 2
    pop r18

    ret                        ; Return from subroutine

;---------------------------------------------------------------------
; inADC8: Reads data from ADC pins.
;   
; Input: MUX-channel from SRAM
; Output: Result from ADC stored in register r20
; Sideeffects: 
; Registers affected: r20
;---------------------------------------------------------------------- 
inADC8:
    push r18                ; Save environment
    push r19                ;
    in r19, SREG            ;
    
    lds r18, ADMUX            ; Set analog channel without
    andi r18, 0b11110000    ; effecting bit 7:4
    lds r19, MUX_CHANNEL    ; Load analog channel from SRAM
    or r18,    r19                ; 
    sts ADMUX, r18            ; Store selected channel
    
    lds r18, ADCSRA            ; Start conversation
    ori r18, 0b01000000        ;
    sts ADCSRA, r18            ;


checkInteruptFlag:
    lds r18, ADCSRA            ; Store bits from ADCSRA and
    andi r18, 0b00010000    ; check interupt flag if
    cpi r18, 0b00010000        ; conversation is done
    brne checkInteruptFlag    ; 
    lds r20, ADCH            ; Read result from ADC
    out SREG, r19            ; Restore environment
    pop r19
    pop r18

    ret                        ; Return from subroutine

;---------------------------------------------------------------------
; outDAC: Output result from read ADC pins.
;   
; Input: r20
; Output: None
; Sideeffects: None
; Registers affected: None.
;---------------------------------------------------------------------- 
outDAC:
    out PORTD, r20            ; Output the result

    ret                        ; Return from subroutine

 

Last Edited: Fri. Dec 10, 2021 - 10:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SpaceCoffee wrote:
So erased, does that mean that the program doesn't use them by my code and hence it doesn't matter what is in them?

Exactly.

 

I tried to hard code a small line that would just change a DDR register to 0b10101010 just to see if it would set it when simulating but nothing happens to the program flash when I try to step through the program.

Again, the registers are not in Flash - Microchip Studio has separate windows for viewing the registers.

 

Flash is, effectively,  "permanent" - it does not change while your code is running.

 

See this post for getting started with Microchip Studio: https://community.atmel.com/comm...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SpaceCoffee wrote:
the exercise is to set up timers and interrupts as well as ADC and output to DAC

That's actually rather ambitious to be starting with.

 

Probably better to start with some basics: https://www.avrfreaks.net/commen...

 

Also, the simulator isn't a great tool for dealing with real-world interfaces like ADC and DAC; you would be far better off on real hardware - such as an Xplained or Curiosity board:

 

https://www.avrfreaks.net/commen...

 

These give you an on-board debugger - so that you can still step the code, and see what's going on inside the microcontroller.

 

EDIT

 

For an ATmega328P, you'd want one of these: https://www.avrfreaks.net/forum/xplained-mini-mega328pb

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Dec 10, 2021 - 11:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay thank you again for the help! I started watching the videos and found the data registers which do update when I step through the code. :)

The program still doesn't find the compare match though but I'll keep on trying to make it work, at least now I can see what is happening.

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

This is an exercise for a course I read 1 and a half years ago and I got a return on this one, which I need to fix to get the points be able to write my master's thesis in spring. I just haven't coded in assembler and used Atmel studio since then so I had forgotten a lot of it.

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

SpaceCoffee wrote:
The program still doesn't find the compare match

AIUI, the Microchip Studio simulator runs waaay slower that real time - so you might just need to be more patient?

 

Again, an  ATmega328 Xplained-Mini would be a better bet ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wow thank you, I tried increasing the compare match as well as clicking under the interrupt label, "run to curser", and it it took 2100 clock cycles to get there - but it get's there!

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

awneil wrote:

AIUI, the Microchip Studio simulator runs waaay slower that real time - so you might just need to be more patient?

 

 

+1  If you wanna really see how slow it runs, insert a  _delay_ms() and let 'er rip tater chip

 

SpaceCoffee wrote:
clicking under the interrupt label, "run to curser"

 

Yup, What I do is set a breakpoint and let the simulator run to the breakpoint.

 

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

Wow, Learned something new ie. AIUI

Thanks Andy!

 

FF = PI > S.E.T