Menu

mset on ATmega328P

2024-04-03
2024-04-03
  • Lief Koepsel

    Lief Koepsel - 2024-04-03

    Hi Mikael,

    I have been working through an analysis of timing specific words such as mset vs asm-based words and I've come to realize this....I'm not sure what to call it as its not an issue, it is more of an artifact.

    From my blog...
    As for the toggle issue, this is easy to confirm. Enter the following in your serial console and watch the values for the pins change:

    BIT4 ddrd 2constant D4  \ Board Connector
    BIT5 ddrd 2constant D5  \ Board Connector
    : tog ( bit port -- ) 1 - mset ; \ toggle output value
    
    D4 tog \ D4 goes high, D5 no change
    D5 tog \ D5 goes high, D4 goes low
    D4 tog \ D4 goes high, D5 goes low
    

    This is an artifact of using the mset word in FlashForth. The mset word will read the address, then or it with the bit mask. If a pin has been toggled, a 1 has been written to the PINn register and when read, it will read high, this gets or'd with the mask, toggling its value as well as the desired value.

    Based on our previous discussions, I will probably use an asm word which wouldn't suffer from this artifact, however, its interesting to note. I was previously under the belief that using the mask would ensure only the desired bit is changed. In testing, I've come to realize any bit (on the same port) which has been previously toggled, will toggle again because its bit in the PIN port will be read back as a 1.

    As I said, I don't see it as a bug or an issue, simply as an artifact of how the AVR family toggles pins.

    Thoughts?

     
  • Mikael Nordman

    Mikael Nordman - 2024-04-03

    As I understand, in order to read a pin it must be done from the PINx register.
    And to set pin high or low you have to write to the PORTx register.
    MSET and MCLR are designed to work with just one register or memory location so it does not really work with how the PORT/PIN system is designed.

    On AVR8 you can toggle a pin by just writing to the PIN register. Like this.

    BIT4 pind 2constant D4  \ Board Connector
    BIT5 pind 2constant D5  \ Board Connector
    D4 c!
    D5 c!
    

    Of course the inline assembler solution is at least 10 times faster.

     

    Last edit: Mikael Nordman 2024-04-03
  • Lief Koepsel

    Lief Koepsel - 2024-04-03

    Yes, its a mute point given the inline assembler version is significantly faster.
    And c! works well for toggling the pin.

    I wanted to document it, in case others have a similar results.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.