General Prepose I/O

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

hi,

 

i was reading Data sheet of SAMD20J18 MCU and at page 299 they have said

 

"Changes in this (DIRCLR) register will also be reflected in the Data Direction (DIR), Data Direction Toggle (DIRTGL) and Data
Direction Set (DIRSET) registers."

 

can you explain me what changes will be there in register  DIR, DIRTGL and DIRSET resisters."

 

for ex:- Let i write 

                               DIRCLR = 0xFF;

now, how this above statement is going to effect DIR,DIRSET, DIRTGL

gaurav

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

It should be obvious how it will affect the dir register!

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

ok

 

statement is

               DIRCLR = 0xFF;

 

Now as per my understanding of MCU datasheet

 

 (Here " _ " represent previous state may be input(0) or output(1))

 

DIR = 0x _ _ _ _ _ _ 0 0  

DIRTGL =  0x _ _ _ _ _ _ F F (if previously pin0 to pin7 are output)

                 or

                 doesn't change if pin0 to pin7 are previously input

DIRSET = there nothing will be chance..

 

Is above values of registers are correct. if not then can you please elaborate where i am making any mistake in deciding the value of DIR, DIRCLR, DIRTGL due to change in values of DIR.

 

thanks.

gaurav

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

If you have a board with a debugger you can simply demonstrate what happens for yourself.

Apart from the DIR register, why would you want to read the DIRCLR,DIRSET and DIRTGL registers?

 

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

I want to add here that i have used debugger one that on Keil.

 

my code was :-

 

#include "samd20.h"

 

int main()

{

      REG_PORT_DIRSET0 = 0x00000001

 

      while(1)

      {}

}

 

but after debugging  the above code, 

the values are

 

DIR0 = 0x00000001

DIRTGL0 = 0x00000001

DIRCLR0 = 0x00000001

 

but my point here is DIRCLR0 value must not change because a 1 here means make that pin as an input. Can you explain why DIRCLR0 changed here.

gaurav

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

I'd say what Atmel is telling you that when you read the DIRCLR,DIRSET and DIRTGL registers, you're reading the DIR register. Since these registers only do something useful when you write them, why are you worried about reading them? Nevertheless, Atmel could have written the explanation clearer methinks.

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

Well your confusion is reasonable. I'll try to explain you in simple words.. DIRCLR, DIRTGL, DIRSET are apparent registers to do desired operation ( DIRCLR- Clear that particular bit in DIR register so that now the direction of that bit(pin) becomes Input etc) on DIR register. This is an additional feature by the Atmel so that user can avoid writing directly on DIR register. Now if you observe the data sheet, the changes made on DIR register will reflect on DIRCLR, DIRTGL and DIRSET register ( in the form of status ). It doesn't mean that all SET, CLR and TGL operations are done simultaneously. 

Salman Ali

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

thanks for your reply. But effect must reflected on another registers according to definition.. for example

 

if   DIRSET = 1

 then there must no change in DIRCLR and DIRTGL must only be changed if previously corresponding pin(s) is input...... 

gaurav

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

DIR is the actual register, while the DIRSET, DIRTGL and DIRCLR are "bit operator functions" that allow you to more efficiently modify content in the DIR register (by avoiding a read modify-write operation).

DIRSET corresponds to DIR = DIR | DIRSET

DIRCLR corresponds to DIR = DIR & ~DIRCLR

DIRTGL corresponds to DIR = DIR ^ DIRTGL

 

So if you did not have the set, clear and toggle registers your would need to read DIR, do a logic operation on the value, and write it back (aka read-modify-write). This is avoided by having these special registers.

Since it does not make sense to read the DIRSET, DIRCLR and DIRTGL registers, as they are just bit operator functions, reading them is therefore returning the value of DIR (the result of the operation).

In principle you could state that these are read-only registers - as Kartman says it doesn't make sense to read them back.

Jacob

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

This got woken up on AVRFreaks.

 

Jacob wrote:
DIR is the actual register, while the DIRSET, DIRTGL and DIRCLR are "bit operator functions" that allow you to more efficiently modify content in the DIR register

Illustrated here:

 

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

 

 

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...