Professional Documents
Culture Documents
SAMD20J18 microcontroller has two 32-bit general purpose input/output ports: port A
and port B. The LSB of port A pin, for example, is identified by PA00 and the MSB of port B pin
is PB31. These I/O pins can be configured and controlled individually or as a group. Each pin
may either be used for general-purpose I/O under direct user program control or assigned to an
on-chip peripheral (such as ADC, Timer, etc.). In EE120 lab, these pins are used for general-
purpose I/O. Each pin can be configured as input or output, with high or low sink/source current
capability and with a pull-up or pull-down resistor option.
Figure 5-1 shows the 7-segment display logic. There are 4 ‘upper’ switches that enable
(when is turned on) or disable a corresponding 7-segment display. The cathode of each
segment LED of all four 7-segment displays are tied together and connected to ground through
a lower switch. There are 7 lower switches (one for each segment). To display a number on one
of the digits, the corresponding upper switch should be turned on (by a logic LOW from the
SAMD20) and the LED segments that form the pattern of the number should be turned on ) by
turning on the corresponding lower switches.
As shown in the figure, Digit ‘A’ to Digit ‘D’ are connected to pins PA04 to PA07
respectively. The decimal point and three extra dots in the display are also connected but not
used in this lab. For example, to display the number ‘0’ on the first digit (left most digit), we set
the following output pattern:
Lower switches: PB06, PB05, PB04, PB03, PB02, PB01, PB00 = 1 0 0 0 0 0 0 (0 = ON)
In other words, for displaying the digit ‘0’, all 7 segments should be turned on except the middle
bar (segment g) which is controlled by PB06.
The upper switches in Figure 5-1 are implemented by using four PNP bipolar transistors. The
lower switches are internal transistors of the SAMD20. The actual circuit diagram is shown in
the appendix of this lab manual.
3.3V
To
Keypad
a
b g f
c e a f g a f g a f g a f g
d
Figure 5-1: Seven-segment display circuit (connections for b,c,d,e segments are not shown).
Keypad circuit
Figure 5-2 shows the keypad circuit. There are 16 witches in the keypad. Each switch
corresponds to a key. The switches are organized into a 4x4 matrix. When a button is pressed,
a specific row and column are connected together. Think about a button as a light switch. When
you turn a switch on, two wires are connected.
One side of the switches in one row are connected to the same upper switch in the 7-
segment display circuit. When one of the upper switches is turned on, the corresponding row of
the keypad is energized (and also the corresponding display digit). At this point, if one of the 4
keys on the energized row is pressed, a high is output to the corresponding column output. In
this case all three other column outputs are pulled down to 0 by the resistors. To read all 4 rows,
the rows most be sequentially energized (similar to displaying a 4-digit number on the 7-
segment display). In the keypad/display program, the turning on of the upper switches should be
synchronized with the output 7-segment pattern and with the reading of the column input from
the keypad.
3.3V
To 7-seg
Display
Keypad
PA07 PA04
PA19 PA18 PA17 PA16 PA06 PA05
SAMD20
Each of the port (A and B) is configured by 24 32-bit configuration registers. For this lab, we
only need to use the first 9 of the 24 configuration register registers. For port A, these 9
configuration registers starts at address 0x41004400 are described below. For port B, these 9
register starts at address 0x41004480. These 9 registers are described below:
DIR: 32-bit, offset address: 0x00. Each bit in this register sets the direction of the
corresponding pin. 1 for output and 0 for input.
DIRCLR: 32-bit, offset address: 0x04. A ‘1’ in this register clears the corresponding bit in
DIR to zero, i.e., sets the direction of the corresponding pin to input.
DIRSET: 32-bit, offset address: 0x08. A ‘1’ in this register sets the corresponding bit in DIR
to one, i.e., sets the direction of the corresponding pin to output.
DIRTGL: 32-bit, offset address: 0x0C. A ‘1’ in this register toggles the corresponding bit in
DIR, i.e., reverse the direction of the corresponding pin.
OUT: 32-bit, offset address: 0x10. Each bit in this register sets the value of the
corresponding output pin (if it is configured as an output pin). If the pin is
configured as input, the each bit in this register determines is the pin is pull-up
(for 1) or pull-down (for 0), if the ‘pull’ function is enabled (as described later).
OUTCLR: 32-bit, offset address: 0x14. A ‘1’ in this register clear the corresponding bit in
OUT to zero, i.e., set output value of the corresponding pin to zero.
OUTSET: 32-bit, offset address: 0x18. A ‘1’ in this register sets the corresponding bit in
OUT to one, i.e., set output value of the corresponding pin to one.
OUTTGL: 32-bit, offset address: 0x1C. A ‘1’ in this register toggles the corresponding bit in
OUT, i.e., toggle the value of the corresponding pin.
IN: 32-bit, offset address: 0x20. Each bit in this register is set or cleared by an input
level sampler according to the logical level of the corresponding pin.
PINCFG[x]:
8-bit, offset address: 0x40. There are 32 of these ‘pin configuration’ registers –
one for each pin. In each of these 8-bit register, only 4 bits are used. The ‘x’
symbolizes the pin number that is being configured.
Figure 5-3
Example:
Standard input Input with pull (high or low Totem-Pole output with input
depending on the bit in OUT) disabled
PULLEN=0, INEN=1, DIR=0 PULLEN=1, INEN=1, DIR=0 PULLEN=0, INEN=0, DIR=1
Totem-pole output with input Output via a pull resistor After reset or when the pin is
enabled used as analog I/O.
PULLEN=0, INEN=1, DIR=1 PULLEN=1, INEN=0, DIR=0 PULLEN=0, INEN=0, DIR=0
At reset, all PORT lines are configured as inputs with input buffers, output buffers and pull
disabled
Upon looking at the sample code, one may notice that there are many unfamiliar lines
and syntax written in varying portions of the code. In this section, we will explain what some of
those lines represent, its purpose, and its origins. The point of this section is for you to have a
basic grasp of what you are looking at.
When creating a new project for the SAMD20J18, header files called “asf.h” is always
included at the start of the base code. The header files acts as a library that contains all the
operations and syntax used to communicate to the microcontroller. Without this line of code, the
program will not build/compile using the sample code and your future labs involving Atmel
Studio.
In the “asf.h” header files, it holds a long list of definitions and configurations set for the
user to utilize. Those files include setups for other microcontrollers in the Atmel microcontroller
family, hence a portions of the “asf.h” files can be considered irrelevant when dealing with only
the SAMD20J18.
After including the header file, you will start noticing unfamiliar syntax as you read further
down into the code. Questions such as what is “Port”, “Group[0]”, “PORT_PA14”, and many
more will come up in your mind. The syntax is actually defined from the header files that you
called from the top of the code.
*Note that “ports” is a pointer name designated by you, the coder. You can name the pointer to
anything reasonable that may not conflict with other Atmel syntax.
In figure 5-6, we dive into a portion of a header file called “samd20j18.h” and see that
“PORT_INSTS” is defined as a definition that points to the address number 0x4100440UL (UL =
unsigned long). This address number is the location where the definition “PORT” is saved and
references location of structure “Port”.
In figure 5-7, a structure is created and defined as “Port” using typedef. This section of
code is located in a header file called “port.h”. In the structure, contains another structure called
“PortGroup” which is separated into two groups, “Group[0]” and “Group[1]”. The two groups are
designated for port group “A” and group “B” respectively and are designated with a specified
offset from the base “PORT_INSTS” address. When calling a specified group, an offset is added
onto the base address and the user will be able to communicate to a specified group of ports.
Or
*Note that “porA” and “porB” are pointer names designated by you, the coder. You can name
the pointer to anything reasonable that may not conflict with other Atmel syntax.
Figure 5-8: PortGroup Structure
However what is “PortGroup”? In the same header file, “PortGroup” is a structure name
that contains all the registers that will be used for GPIO statements. Figure 5-8 displays what
registers are contained in the structure as well as the offset that will be added onto the base
address (PORT_INSTS or 0x41004400).
When all pointers are pointing to the correct address, the registers can be utilized to the
specified peripheral (GPIO for Group A in this case). Each register is can vary in size and have
different functionality, hence it is important to read what the register do in the sections above or
in the datasheet. The registers can be assigned a value or read from. In the lines below are an
example of registers being written to or read respectively.
Alternatively, when reading from a register, like in line 43, we check the “IN.reg” at bit 15
for a “1” or a “0”. If the result is “0”, the “if” statement will execute. The reason why it executes
“0” instead of “1” is due to the exclamation point which is a logical “not” operator.
To see and understand how the microcontroller reacts to the provided sample code, it is
recommended that for you to build and program the SAMD20J18 microcontroller to see the
results. For further questions, ask the instructor for additional help or information.
References:
3.3v
R1
R2 PA07
SAM D20
PA19
7-seg display R4