Hex Values:
0xFF = all bits set (turned
on/high/1/5V) __________________________ binary equivalent is 0b11111111
0x00 = all bits cleared (turned
off/low/0/0V)_______________________ binary
equivalent is 0b00000000
0xF0 = bits 0-3 are cleared
(0) and bits 4-7 are set (1)_________________ binary equivalent is 0b11110000
0x88 = bits 3 and 7 set
(1) the rest are cleared (0)____________________ binary equivalent
is 0b10001000
Look up
Wikipedia: hexadecimal for an entire list of possible hex values.
ATmega pin number equivalents example:
Number
|
7 6 5 4 3 2 1 0 – Pin numbers on the port (and bit numbers)
|
0x0F = = 0b00001111
|
0 0 0 0 1 1 1 1 – set to high and low with hex/binary values
|
Setting Bit Values:
Many times
you would like to set the voltage high for only a single pin of a port, and to do that you create what is called a
‘bit mask’, which is a binary number that has single 1-bit in the location
corresponding to the pin you want to set, and all the rest of the bits are
zero. You then use this number with a bitwise logical operator to finish the
task. To create such a number, you would use the command:
_BV(PXn) //
Where X is the port letter, and n is the pin number. For example, suppose you
would like to make pin 2 on PORTA to
be an output. You therefore need
a bit mask with a 1 in bit number 2 location. The corresponding bit mask is
easily built by the command: _BV(PA2)
Setting the port’s data direction (input or output) on an ATmega:
DDRX = 0x00; // Clears all the bits in the PORTx register,
which makes all the associated pins to be inputs
(keep in mind there are only ports A, B, C, and D on the ATmega328), so you can
read various signals from things such
as switches, sensors, or for analog to digital conversion, etc. (by
default all pins are set to input, but you should always set them to make sure
anyway.)
DDRX = 0xFF; // Sets all the bits in the PORTx register, which
makes all the associated pins to be outputs
(keep in mind there are only ports A,B,C, and D on the ATmega328), so you can control devices, such as motors, LED’s, speakers, etc., pretty much anything you would want
the microcontroller to control (keep in mind that the ATmega cannot source much
current, so you will probably need to use a transistor between the
microcontroller and whatever you are trying to control if it needs more than 10
mA of current).
Initializing Port Values:
PORTX = 0xFF; // Clears all the bits in the PORTx register, and
assuming that the pins are outputs, will make all the pins in PORTx go high (as
mentioned in the section on Hex values above). So for example, PORTA = 0xFF; would
set all pins in port A to on/high/1/5V, which would be the same as writing:
PORTA = 0b11111111; Keep in mind that the right-most bit corresponds to pin 0
on PORTA in this example.
PORTX = 0x00; // Clears all bits in the PORTx register and makes
all pins in PORTx go low (as
mentioned in the section on Hex values above). So for example, PORTB = 0x00;
would clear all bits in the PORTB register and make all pins in PORTB go
off/low/0/0V. This would be the same as writing: PORTB = 0b00000000;
Bitwise Logic:
PORTX |= 0xF0; // Method for setting
bits. Performs a bitwise OR operation
with the current bit values of PORTX and the bit mask represented by the binary
number, 0xF0. The result is that only pins 4-7 in PORTX are set high, and the other pins are not affected.
Equivalent in ‘long hand’ would be: PORTX = PORTX | 0xF0;
PORTX &= ~0x01; //
Method for clearing bits.
Performs a bitwise AND operation
with the current bit values of PORTX and the bit mask represented by the binary
number, ~(0b00000001) or 0b11111110. The result is that pin 0 is set low (referred to as ‘cleared’), regardless of what was
there before. In this example, only pin 0 is cleared, and the state of the
other pins are not affected. Equivalent in ‘long hand’ would be PORTX = PORTX
& ~0x01;
PORTX ^= 0x02; // Method for toggling
bits. Performs a bitwise XOR operation
with the current bit values of PORTX and the bit mask represented by the binary
number, 0x02. The result is that pin 1 is
‘toggled’ between the off and on states. Think of this operation like a
light switch. Each time the statement is executed, it changes the state to be
the opposite of what it was previously. Similar to the prior statements, this
method of toggling only affects the bits in the locations where there are ones
in the bit mask hex value. ‘Long hand’ would be
PORTX = PORTX ^ 0x02;
GPIO IS THE BASICS IN PROGRAMMING THE MICROCONTROLLER BY UNDERSTANDING
THE GPIO PROTOCOL THEN YOU CAN START PROGRAMMING YOURSELF IN EMBEDDED.
THE DIRECTORY REGISTER ARE THE BASIC REGISTER AND BY USING THIS REGISTER WE
CAN FIX THE PORT AS OUTPUT OR INPUT CATEGORY AND THIS WILL LEAD US TO THE
ENTIRE PROJECT TO COMPLETE THE TASK......GPIO IS NOTHING BUT THE GENERAL
PURPOSE INPUT AND OUTPUT REGISTER.......
The configuration of GPIO in all controllers are nearly same as the atmega or avr and there is only a small changes in the names of the directory.
No comments:
Post a Comment
share your thoughts