Menu

mysterious malfunction when using PEEK with atmega2560

louie
2018-05-01
2018-05-02
  • louie

    louie - 2018-05-01

    hey guys i got some wierd !#$@ here i'm tryin to read PORTC using the peek command:
    first i set PORTC to in and pullup

    dir PORTC.0 in
    set PORTC.0 on
    dir PORTC.1 in
    set PORTC.1 on
    dir PORTC.2 in
    set PORTC.2 on
    dir PORTC.3 in
    set PORTC.3 on
    dir PORTC.4 in
    set PORTC.4 on
    dir PORTC.5 in
    set PORTC.5 on
    dir PORTC.6 in
    set PORTC.6 on
    dir PORTC.7 in
    set PORTC.7 on
    

    then i read the port by using peek:

        if (peek(40) = 255) then    ' 40 is the address of port C on atmega2560
          DMXCHAN(8) = 0              'this is a LED light. 0 = off
        end if
        if (peek(40) < 255) then   ' if i ground one of the pins, it goes low
          DMXCHAN(8) = 255            ' 255 = on
        end if
    

    then i take a wire and ground one of the PORTC pins, but nothing happens. the light won't come on.
    then i change to this:

          if (PORTC3 = 1) then
            DMXCHAN(8) = 0
          end if
          if (PORTC3 = 0) then
            DMXCHAN(8) = 255
          end if
    

    now it works. ground out pinC3 and the lite comes on. but i gotta figure out why the peek does not work. anybody know what's up?

     

    Last edit: louie 2018-05-01
  • Anobium

    Anobium - 2018-05-01

    Dont peek - in all my years.. never used peek. Simply use PINC. See the Help Inputs/Outputs.

     
    • louie

      louie - 2018-05-01

      ok i'll check that......
      well i don't follow what it's saying about the specific syntax of uservar = pinx
      i have to do a for loop. so will it work if i do this?

      for x = 1 to 10
      array(x) = pinx
      next
      
       
  • Anobium

    Anobium - 2018-05-01

    No. Is there 10 ports on the microcontroller you are using?

    Something like this... this is portable code - no peek. It may be faster to simply assigned the pin to the array element.

    PinState = PortC
    for LocalVar = 1 to 10   'do not use x character vars
            pinArray (LocalVar) = PinState.0
            Set C off
            rotate PinState right
    next
    
     
  • louie

    louie - 2018-05-01

    no there's actually 69 pins that i have to address via a FOR loop.
    every pin on the mega board. ports A thru L
    so i had an array of port memory addresses and a for loop like
    " for xx = 1 to 69
    localvar = peek(addresses(xx))"
    but that didn't work because peek(address) always returns 255 regardless of the state of the pins.

     

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

    stan cartwright - 2018-05-01

    Do you monitor each port bit then stop? else localvar will always be port 69.
    Please explain your plan in more detail.

     
  • louie

    louie - 2018-05-01

    well here's a simplified example:

    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
    

    problem is that peek doesn't work. so the code that Anobium posted above will work, but only for a single port.

     

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

    stan cartwright - 2018-05-02

    it's late. not elegant.

    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
    
     
  • louie

    louie - 2018-05-02

    yeah..... that's damn clever. and it's probably as elegant as it's gonna get. cause it looks like there's just no way to numerically reference those ports, at least not with GCB.
    THANX, stan. i'm gonna use your routine and get back to the job.
    mike :-)

     

Log in to post a comment.