This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

random flickering of led

Sir ,using the below program i am trying to flicker led .For generating pseudorandom numbers i am using lfsr technique.To check if i am getting the random number or not i am displaying it on the serial monitor using uart.The output which i am getting on the serial monitor is garbage value,along with that ,no flickering of led is hapenning.

/* Defines the base address of the memories and peripherals */
#include "inc/hw_memmap.h"

/* Defines the common types and macros */
#include "inc/hw_types.h"

/* Defines and Macros for GPIO API */
#include "driverlib/gpio.h"

/* Prototypes for the system control driver */
#include "driverlib/sysctl.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
#include "driverlib/uart.h"
#include "driverlib/interrupt.h"
#include "utils/uartstdio.h"

unsigned int red,grn,op,lfsr=0x1F61; //declaring the hex value for lfsr
unsigned int bit;

void InitConsole(void)
{

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

/* Make the UART pins be peripheral controlled. */
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);


/* Initialize the UART for console I/O */
UARTStdioInit(0);
}

int main(void)
{
/*Set the clocking to directly run from the crystal at 16MHz*/
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_20MHZ);

/* Set the clock for the GPIO Port F */
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

/* Set the type of the GPIO Pin */
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);


/* GPIO Pin 1 on PORT F initialized to 0 */
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1,0);

InitConsole();

while(1)
{
bit=((lfsr>>0)^(lfsr>>1)^(lfsr>>3)^(lfsr>>5))&1; // right shift operation

lfsr=(lfsr>>1)|(bit<<15); //changing the value of lfsr


GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1,bit); //switching on and off led
SysCtlDelay(SysCtlClockGet()/3); //delay


UARTprintf("\nrandom no"); //printing the random number on serial monitor
UARTprintf("%d",lfsr);


}

}

  • Hello Mahavir

    1. The LED will not flicker as the value computed in bit is either 0 or 1. The GPIO Pin write works by writing the value of the Pin when using the pin. The function must be modified as

    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1,(bit << 1));

    2. What is the garbage value that is coming and how do you expect it proceed. Then compute the same manually and what does that show up,

    Regards

    Amit

  • Thanks Sir,Though I have got the flickering on the led but still i am getting a garbage value on the serial monitor,can u throw some light on that.i am attaching the image of serial monitor below .

    With regards

    Mahavir

  • Hi mahavir

    Without analyzing to much of the code, that seems to be that the monitor settings don't correspond to the UART configuration. What least that's the kind of thing that appears for me when that's the problem

  • Thanks Luis ,for your response

    Luis I have used the same settings for monitor and I have got the desired result for other codes.What I think  the problem may be with this code is the data types of variables.

    With regards

    Mahavir

  • That screen shot appears to be showing individual bytes (that's good) but w/ms (most significant) "bit" set.  Most terminal programs produce best "human readable" output when only the lowest 7 (ASCII) bits are employed.

    You may wish to "strip out" that msb prior to placing that data into the MCU's UART.