July 9, 2015

Unlocking NMI to use as an input pin

Overview In this tutorial, we will be try to use a normally locked pin as a general input pin. Actually,if we check properly the datasheet of LM4F120H5QR and the schematics, then we can see that the SW1 is actually connected to PF1 which is also having an additional function of NMI which means a non maskable interrupt which is a powerfull and essentialfeature. But right now, i am interested in blinking an LED using this particular pin. Actually, it is easy, but a newbie wont be able too find how to do it easily. This is what forced me write this tutorial. In this tutorial actually the comments are more than enough to explain what is being done and therefore i am not actually including any additional documentation on how it is to be done because I find it pointless and moreover as a waste of time. If you find it hard to understand the code, please contact us. We will be happy to help you.

/*
 * Stellaris LAUNCHPAD LM4F120H5QR
 * C++ code for unlocking and using PF0(SW_2) which is normally configured as NMI.
 * This program unlocks and reconfigure the pin as GPIO
 */
#include 
#include 
#include 
#include 
#include  //for macro declaration of GPIO_O_LOCK and GPIO_O_CR

int main(void)
{
  /*
   * The System clock is run using a 16 Mhz crystal connected to the main oscillator pins of the microcontroller
   * This generates a internal clock signal of 400 Mhz using the PLL
   * The signal is prescaled by the system by 2
   * Now we are defining a prescale of 5 in addition to make the clock frequency 40MHz
   * The system clock frequency must be less than or equal to 80MHz
   */
  SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
  /*
   * Peripherals are enabled with this function.
   *  At power-up, all peripherals are disabled.
   *  System Clock to the peripheral must be enabled in order to operate or respond to register reads/writes.
   */
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
  //Set PF0 as input pin
  GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_0);
  //Unlocks the GPIO pin PF0
  HWREG(GPIO_PORTF_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY_DD;
  /*
   * HWREG is used to access the hardware registers whose physical address is provided.
   * GPIO_PORTF_BASE is the Base address while GPIO_O_CR is the offset address for Commit Register
   * Physical address can be found by adding Base address and offset address.
   * PORTF 0 is to be enabled which is done by setting the value 0x01
   * if PORTD 7 is to be enabled, we have to write:
   * HWREG(GPIO_POTRD_BASE+GPIO_O_CR) = 0x80; --> 0x80 ~ 0b10000000 -->which means PD7 is masked
   * HWREG must be done for NMI pins and JTAG pins. Otherwise use PinMux Utility from TI to generate sample code
   */
  HWREG(GPIO_PORTF_BASE+GPIO_O_CR) = 0x01;
  /*
   * As it is a switch, we should use a pull up resistor.
   * Stellaris can be configured with various pull up/down resistors based on the drive strength(current) specified.
   * Refer to the Stellaris API page number 124 for more configurations
   * Link: www.ti.com/lit/ug/spmu019p/spmu019p.pdf
   */
  GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD_WPU);
  //Set PF1(RED LED) as output
  GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);
  //Turn of the LED
  GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);

  while(1)
  {
    //Check whether the button is pressed.
    if(!GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0))
      //Set PF1 as high. 2 ~ 0b00000010-->mask PF1
      GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 2);
    else
      GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *