July 9, 2015

Using Serial communication on launch-pad

This tutorial is about how to enable and properly configure UART on your Stellaris LaunchPad.This tutorial is the first of its kind and is exclusively meant for teaching how to write into a serial stream, how to set the baud rate parity bit, stop bit etc. These tutorials may at the first look seems useless but I only mean to teach you the basic concepts of how to program the ARM based microcontroller.Therefore these can be used to implement small small subsystems in your project. Before going to the detailed analysis please go through the code which I have provided. I think that once you have went through the previous tutorial, you must be able to understand the tutorial even without checking the line by line explanations.

/*
 * Stellaris LAUNCHPAD LM4F120H5QR
 * Using UART to write a string into the Serial Monitor without using interrupts
 * Add the following to Pre-define NAME tab in the predefined Symbols in the Advanced Options
 * of the ARM compiler in the CCS Build in the project properties
 * --->  PART_LM4F120H5QR  <---
 */
#include 
#include 
#include 
#include 
#include 
#include  


void blinkled(void)
{
  GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 2);
  SysCtlDelay(200000);
  GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);
}
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 use ADC
   */
  SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // As GPIO Pins A0 and A1 are multiplexed with U0RX and U0TX
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
  //Configuring the LED as output
  GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);
  //Configuring the GPIO A0 and A1 for UART functionality
  GPIOPinConfigure(GPIO_PA0_U0RX);
  GPIOPinConfigure(GPIO_PA1_U0TX);

  GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0|GPIO_PIN_1);
  IntEnable(INT_UART0);
  UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);
  UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200, UART_CONFIG_WLEN_8|UART_CONFIG_PAR_NONE|UART_CONFIG_STOP_ONE);
  /*
   * Configures the UART to use the current system clock
   * Sets the baud rate to 115200 and enables an 8bit no parity one stop bit communication(8-N-1)
   */

  while(1)
  {

    unsigned char *buf = "Aravind RIG\n\r";
    //Waits until the UART is Ready
    while(UARTBusy(UART0_BASE));
    while(*buf != '\0')
      //Put the string character by character into the FIFO for transmission
      UARTCharPut(UART0_BASE, *buf++);
    blinkled();//blink led to denote a transmission
    SysCtlDelay(SysCtlClockGet()/2);//wait for a while
  }
}

Leave a Reply

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