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.

Tiva C precise delay



Hello, my name is Karol and I'm a brazilian computer engineering student. I'm not fluent, so I'm sorry about my english.

Following the Valvano's book as support, I decided to use SysTick to get accurate delays. I used an LED connected to PORT A2 to view the delay.
I chose to use the internal oscillator (16Mhz) with its frequency divided by 4 for my clock, it results in a 4 MHz frequency. Knowing this, my register NVIC_ST_CURRENT_R goes from value 4000000 to 0 so that they spend 1 second and the flag in bit 16 of the register NVIC_ST_CTRL_R become one.

Then comes the problem: during debug, as there are locking in breakpoints, I see my LED turning on and off. But in the Run Mode it is just on or off, as if the delay was not working. I could not detect the error in the code or logic. A help it would be possible? Here is my code:

#include <inttypes.h>
#include <stdint.h>
#include "tm4c123gh6pm.h"	//Header file to registers address masks
/*
 * Karoline Tyemi Fujimoto
 * Using SysTick and Clock Control to get a precise delay
 * The page numbers are datasheet references
 */
void setClock();
void setPortA();
void initSysTick();
void delay(unsigned long sec, unsigned long freq);
int main(void) {
	unsigned long portA = 0;
	setClock();
	setPortA();
	initSysTick();
	while(1) {
		portA = GPIO_PORTA_DATA_R&0x04;
		GPIO_PORTA_DATA_R = (!portA) << 2;
		delay(1,4000000);
	}
	return 0;
}
void setClock() {
	SYSCTL_RCC_R = SYSCTL_RCC_MOSCDIS;		//Disable main oscillator (external)
	SYSCTL_RCC_R |= SYSCTL_RCC_OSCSRC_INT4;	//Select the internal 16Mhz oscillator / 4 = 4Mhz
	SYSCTL_RCC_R |= SYSCTL_RCC_BYPASS; 		//Select OSC like clock source
	SYSCTL_RCC_R |= SYSCTL_RCC_PWRDN;		//Turn off PLL
}
void setPortA() {
	//System Control GPIO Run Mode Gating Control - Enable the GPIO PORTA's clock - Pag. 340
	SYSCTL_RCGCGPIO_R |= 0x01;
	//GPIO PORTA Lock Register - Unlock PA0 - Pag. 684
	GPIO_PORTA_LOCK_R = 0x4C4F434B;
	//GPIO PORTA Commit Register - Allow changes in PA2 - Pag. 685
	GPIO_PORTA_CR_R = 0x04;
	//GPIO PORTA Analog Mode Register - Turn off Analog Mode - Pag. 687
	GPIO_PORTA_AMSEL_R = 0;
	//GPIO PORTA Port Control - Set GPIO Funcion (AFSEL Complement) - Pag. 688
	GPIO_PORTA_PCTL_R = 0;
	//GPIO PORTA Data Direction - Set Pins 2 to output - Pag. 26
	GPIO_PORTA_DIR_R |= 0x04;
	//GPIO PORTF Alternate Function Select - Set the default pin operation - Pag. 602
	GPIO_PORTA_AFSEL_R = 0;
	//General Purpose PORTF Digital Enable - Pag. 682
	GPIO_PORTA_DEN_R |= 0x04;
}
void initSysTick() {
	//SysTick Control and Status Register - Enable SysTick with PIOSC/4 - Pag. 138
	NVIC_ST_CTRL_R = 0x00;
}
void delay(unsigned long sec, unsigned long freq) {
	unsigned long i, j = 0;
	//SysTick Set Reload - Reload when have been passed 1 sec - Pag. 140
	NVIC_ST_RELOAD_R = freq - 1;
	//
	for(i = 0;i < sec;i++) {
		//SysTick Current - Reset the count register - Pag. 141
		NVIC_ST_CURRENT_R = 0;
		//Verify SysTick Control Register Flag - Pag. 138
		while((NVIC_ST_CTRL_R&0x00010000)==0){
			j = NVIC_ST_CURRENT_R;
		}
	}
}

  • Greetings,

    You've not been here long enough to know this - the "Direct Register Macro" (DRM) coding method you've employed is NOT favored by vendor rep here nor by "outsiders" such as myself.    My small (yet growing) tech firm (only - rarely) employs "DRM or ASM" feeling instead that this vendor's API and modern, C Compilers produce very serviceable code - at ten times the code output (or higher) than that achievable via DRM/ASM.  

    In small, tech business - "Getting to market properly, and EARLY" means survival!    We could NOT survive being bound to DRM/ASM.    Why those are emphasized in school programs is disturbing!    (some say - "Job security" for teaching staff - I'm simply reporting...)   We do NOT find that "Adding Complexity, Program Delay and Error is an effective means to, "build a tech business!"   The API - rejected by your teacher(s) is well known to be, "Tried, True, Expansive & Effective" - use of DRM is "always" an adventure!   (i.e. Untried, untrue, microscopic, and time/effort wasting!)   Did I say that my firm - and many others - do NOT allow its use - unless under very, very special conditions!

    Specifically - in your case - might your "Optimizations w/in your IDE be set too high?"   That often causes the IDE to "reject delays" as it judges these to be non-essential to the program's operation.

    Know too that your English is well understood - surely overwhelms the Portuguese seen here - but for (famed) University poster, Luis...

  • Thank you, Luis!