Today, I am familiar with some gpio applications. The simplest is the control of LED lights.
The following example uses L5 to describe it.
Sysctlperipheralenable (sysctl_periph_gpiob); // enable gpio B
Gpiopintypegpiooutput (gpio_portb_base, gpio_pin_0); // set pb0 output
Another mode setting method is gpiodirmodeset (gpio_portb_base, gpio_pin_0, gpio_dir_mode_out). // multiple pins can be set in this mode. Parameter 2 is a bit pin or
Controls the lighting of LED lights, that is, to write values to the pins.
Ledstatus = gpiopinread (gpio_portd_base, gpio_pin_0 );
Gpiopinwrite (gpio_portd_base, gpio_pin_0, gpio_pin_0 &(~ Ledstatus ));
The above program is to read the value of the corresponding pin of L5, and then turn the light to the ground.
The usage of gpiopinwrite () is described here.
Gpiopinwrite (gpio_portd_base, gpio_pin_1, gpio_pin_1 );
Gpiopinwrite (gpio_portd_base, gpio_pin_1 ,~ Gpio_pin_1 );
The preceding method is often used in routine. This is the pin you want to set to one. The bit corresponding to the pin in parameter 3 must be set to one.
Each of the following commands can light up the response port (assuming it has been defined as output ).
Gpiopinwrite (gpio_portd_base, gpio_pin_0, 1 );
Gpiopinwrite (gpio_portd_base, gpio_pin_1, 2 );
Gpiopinwrite (gpio_portd_base, gpio_pin_2, 4 );
Gpiopinwrite (gpio_portd_base, gpio_pin_3, 8 );
Gpiopinwrite (gpio_portd_base, gpio_pin_4, 0x10 );
Gpiopinwrite (gpio_portd_base, gpio_pin_5, 0x20 );
Gpiopinwrite (gpio_portd_base, gpio_pin_6, 0x40 );
Gpiopinwrite (gpio_portd_base, gpio_pin_7, 0x80 );
Next I will mainly introduce the process of using the key to interrupt the control light.
First, configure the key.
Sysctlperipheralenable (sysctl_periph_gpioc); // enable gpio C
Gpiopinintenable (gpio_portc_base, gpio_pin_4); // enable gpio C pin 4
Gpiopintypegpioinput (gpio_portc_base, gpio_pin_4); // set the pin mode is input
Gpiointtypeset (gpio_portc_base, gpio_pin_4, gpio_low_level); // set interrupt type is low falling edge
Intenable (int_gpioc); // enable gpio C interrupt
The following are the processing functions for key interruption.
Void userkey_handler (void)
{
Unsigned char ucval;
Unsigned long ulstatus;
Ulstatus = gpiopinintstatus (gpio_portc_base, true); // get gpio C interrupt status
Gpiopinintclear (gpio_portc_base, ulstatus );
// Clear interrupt status
If (ulstatus & gpio_pin_4) // if the key interruption status is valid
{
Ucval = gpiopinread (gpio_portd_base, gpio_pin_0); // flip the LED
Gpiopinwrite (gpio_portd_base, gpio_pin_0 ,~ Ucval );
Sysctldelay (10 * (sysctlclockget ()/3000); // The latency is about 10 ms to eliminate key Jitter
While (gpiopinread (gpio_portc_base, gpio_pin_4) = 0x00); // wait for the key to be lifted
Sysctldelay (10 * (sysctlclockget ()/3000); // The latency is about 10 ms to eliminate the loose key jitter.
}
}
The sysctldelay () function is used. Here, we can also analyze it.
# If defined (ewarm) | defined (doxygen) // In the IAR Environment
Void
Sysctldelay (unsigned long ulcount)
{
_ ASM ("Subs r0, #1 \ n"
"BnE. N sysctldelay \ n"
"Bx LR ");
}
# Endif
# If defined (CodeRed) | defined (GCC) | defined (sourcerygxx) // CodeRed, GCC, and sourcerygcc
Void _ attribute _ (naked ))
Sysctldelay (unsigned long ulcount)
{
_ ASM ("Subs r0, #1 \ n"
"BNE sysctldelay \ n"
"Bx LR ");
}
# Endif
# If defined (rvmdk) | defined (_ armcc_version) // In the Keil MDK Environment
_ ASM void
Sysctldelay (unsigned long ulcount)
{
Subs r0, #1;
BNE sysctldelay;
Bx lr;
}
# Endif
# If defined (CCS) // CCS
Volatile unsigned long g_ulinlineccsworkaround;
Void
Sysctldelay (unsigned long ulcount)
{
_ ASM ("delay? : Subs r0, #1 \ n"
"BnE. N delay? \ N"
"Bx LR \ n ");
//
// This is needed to keep Ti compiler from optimizing away this code.
//
G_ulinlineccsworkaround + = ulcount;
}
# Endif
Sysctldelay () executes three Assembly statements and runs for three periods. Function Description: The loop takes 3 cycles/loop.
1. 6 MHz Primary Crystal Oscillator
Sysctldelay (2); delaytime = 2*3 * (1/6000000) = 1us
Sysctldelay (10
* (Thesysclock/3000); delaytime = 10 * (6000000/3000) * 3 * (1/6000000) = 10 ms
2. 8 MHz Primary Crystal Oscillator
Sysctldelay (2 );
Delaytime = 2*3 * (1/8000000) = 0.75us
Sysctldelay (10
* (Thesysclock/3000); delaytime = 10 * (8000000/3000) * 3 * (1/8000000) = 10 ms
Therefore
We can see that no matter how large the crystal oscillator is, it is divided by 3000. Sysctldelay (10 * (thesysclock
/3000 ).