Menu

direct read from SRAM location using AVR code?

louie
2018-05-02
2018-05-04
  • louie

    louie - 2018-05-02

    all i wanna do is peek(x) with an atmega2560. but it seems that peek just doesn't work. so is there any way to just read memory location x?
    is it possible to do this:
    LDI 0x1E, 0x00
    LDI 0x1F, 0x22 ' 0x22, address to peek from -> register Z
    LD 0x1D, Z ' load from 0x22 -> 0x1D
    STS 0x105 0x1D ' store 0x1D -> portJ
    and then do
    localvar = PORTJ

    this is what i'm trying to do but it won't work:

    dim addresses(3)
    addresses = 34, 37, 40          ' ports a b and c
    dim pins(3)
    mask = 0b01101001
    for xx = 1 to 3
      inbyte = peek(addresses(xx))
      pins(xx) = inbyte & mask
    next
    
     

    Last edit: louie 2018-05-02
  • stan cartwright

    stan cartwright - 2018-05-02

    Peek and poke does work in gcb.
    I looked at 2560 sram map. I don't understand mapping registers or reserved and fast code in sram.

    #chip mega328p, 16
    #option explicit
    
    #define USART_BAUD_RATE 9600
    ;#define USART_DELAY 10 ms
    #define USART_BLOCKING
    
    dim temp,valu as byte
    
    do
    for temp=0 to 255
      poke (0x100,temp)
      valu=peek (0x100)
      HSerPrint str(valu)+","
    next temp
    HSersend 13
    HSersend 10
    loop
    
     
  • stan cartwright

    stan cartwright - 2018-05-02

    I don't have a 2560 but a uno 328p has ports mapped to sram.
    portb is 0x03 (0x23) pins address, 0x04(0x24) direction, 0x05(0x25) data.
    so poking these should change portb.
    If that is what your trying to do ..why not just use basic commands not peek and poke?

     
  • stan cartwright

    stan cartwright - 2018-05-02

    This turns bit 0 to 5, portb on and off,square wave on scope. Why not all bits?
    So poke works. Note the port address is the second value from last post.

    poke (0x24,255)
    do
    poke (0x25,255)
    wait 20 ms
    poke (0x25,0)
    wait 20 ms
    loop
    
     
  • stan cartwright

    stan cartwright - 2018-05-02

    Peek and poke do work with io regs it seems.
    I edited this from being an error report..my error :(

    poke (0x24,255)
    do
    poke (0x25,255)
      valu=peek (0x25)
      HSerPrint str(valu)+"," ;---prints 255
    wait 100 ms
    poke (0x25,0)
      valu=peek (0x25)
      HSerPrint str(valu)+"," ;---prints 0
    wait 100 ms
    loop
    
     

    Last edit: stan cartwright 2018-05-02
  • louie

    louie - 2018-05-03

    hey Stan this is a sorta repeat of my other question which you answered for me yesterday that i posted before i saw your answer.
    it goes back to this rooutine:

    dim addresses(3)
    addresses = 34, 37, 40          ' ports a b and c
    dim pins(3)
    mask = 0b01101001
    for xx = 1 to 3
      inbyte = peek(addresses(xx))
      pins(xx) = inbyte & mask
    next
    

    and it never worked. i connected a ground wire to one of the pins of port c, but it always came back with portc = 255. so what to do? peek is the only way i know to reference a register numerically, but afterward i thought of using assembly code.
    then i saw your answer:

    dim result(24) as byte 
    for count=1 to 24
      pbit=fnlsl(1,count%8)
      if count/8=0 then result(count)=porta and pbit
      if count/8=1 then result(count)=portb and pbit 
      if count/8=2 then result(count)=portc and pbit
    next count
    

    and it's the best solution i have. thanx :-)

     
  • stan cartwright

    stan cartwright - 2018-05-03

    First, if a port is output and high..don't ground it. If it was connected to a led to ground you'd use a resistor in series to lower the current to say 30 ma.
    I'll try your code on a uno later.

     
    • louie

      louie - 2018-05-04

      no it's input and high and yeah i'd like to see how that works

       

Log in to post a comment.