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.

Launchpad TM4C123GH6P.. LED blinking too fast

Hello, 

I'm just starting out embedded programming and chose this launchpad to be my first. I'm using Keil to blink a LED within a 1 second interval (ON for 1 second, OFF for 1 second) by using the Systick timer (Main clock, no PLL). However, it seems like the LED is blinking in 0.5 second intervals. 

Also, I tested in simulation mode using the logic analyzer in Keil, and according to the logic analyzer, the LED is blinking within 1 second intervals. I honestly have no idea what is going on.

Below is my code



#include "tm4c123gh6pm.h" // Port F LED #define LED_RED (1U << 1); #define LED_BLUE (1U << 2); #define LED_GREEN (1U << 3); void PortF_Init(void); void PLL_Init(void); void SysTick_Init(void); void Systick_delay(void); int main(){ PortF_Init(); SysTick_Init(); for(;;) { // Turn on RED LED GPIOF->DATA ^= LED_RED; Systick_delay(); } return 0; } // // Initializes Port F to allow SW1 as input, and RED, BLUE and GREEN LED as outut void PortF_Init(void) { SYSCTL->RCGCGPIO = (1U << 5); // Enable PortF Clock while( (SYSCTL->RCGCGPIO) == 0x00 ); // Wait for Clock to stabilize // Configure PF4 (SW1) as Input; PF1, PF2, PF3 as output GPIOF->DIR &= 0xFFFFFFEF; // PF4 As Input 0b 1110 1111 GPIOF->DIR |= 0x0000000E; // PF3, PF2, PF1 as output 0b 0000 1110 // Clear Alternate Functions to set GPIO pins as GPIO GPIOF->AFSEL = 0x00000000; // Enable Pull up Resistor on SW1 GPIOF->PUR |= (1U << 4); // Disable Analog Mode and Enable Digital Mode for GPIOF pins GPIOF->AMSEL &= 0x00000000; GPIOF->DEN |= 0x0000001E; // Digital Enable Port F pins 1 - 4 (bit[4:1]) } // // Initializes Systick timer void SysTick_Init(void) { // 0. Disable Systick SysTick->CTRL &= 0x00; // 1. Program the value in the STRELOAD register. SysTick->LOAD = 16000000U; // 2. Clear the STCURRENT register by writing to it with any value. SysTick->VAL = 0U; // 3. Configure the STCTRL register for the required operation. SysTick->CTRL |= (1U << 2); // Use System Clock (16 MHz) SysTick->CTRL |= (1U << 0); // Enable Systick } // // Delay Timer Using Systick // Will delay for 1 second void Systick_delay(void) { // Write to VAL to reset Counter SysTick->VAL = 16000000U; // Wait while count flag is still 0 while( (SysTick->CTRL & 0x00010000) == 0x00 ); }

If someone can give me some advice, it would be greatly appreciated. Thanks

  • Hello William,

    Your code is fine. The LED will blink at the rate of 1 sec ON and 1 sec OFF. Can you connect the LED Pin to a LA or scope and monitor the same. Also it would be rather easier to use the SysTick Interrupt Handler than using the delay loop.

    Regards
    Amit
  • Hi Amit,

    I figured out what went wrong. The startup code called SystemInit() which in turn configured the system clock. I'm not too sure what configuration it set the clock to be, but I believe it was set to 80MHz
  • Hello William,

    I believe instead of hardcoding the SysTick Value it would be wiser to get the system clock frequency and then use the same. Or better still configure the SysTick timer to switch to PIOSC as the run time clock making it system clock independent.

    Regards
    Amit
  • Amit Ashara said:
    configure the SysTick timer to switch to PIOSC as the run time clock making it system clock independent.  

    Bravo - so wise - may humble outsiders suggest a, "Raise?"    Surely well earned - you've not only answered - yours was an (inspired) answer...  (which our merry band will hereby "borrow/appropriate"...claim as our own...)    (note that "investors" are pleased to provide such - especially as financial performance "beats" the market...)