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 series Keil-software problem

Other Parts Discussed in Thread: EK-TM4C123GXL

I am not goot at programing texas tiva c series in Keil. But I want to make that If switch is pressed(PF4=0), PF2 toggle(flip bit from 0 to 1, or from 1 to 0), If the switch is not pressed(PF4=1), set PF2(LED is on)

I have already written something. But it does not work.

#include "TExaS.h"
#define PF4 (*((volatile unsigned long *)0x40025040))
#define PF2 (*((volatile unsigned long *)0x40025010))
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_AFSEL_R (*((volatile unsigned long *)0x40025420))
#define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025510))
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C))
#define GPIO_PORTF_AMSEL_R (*((volatile unsigned long *)0x40025528))
#define GPIO_PORTF_PCTL_R (*((volatile unsigned long *)0x4002552C))
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
#define SYSCTL_RCGC2_GPIOF 0x00000020 // port F Clock Gating Control
unsigned long In;
unsigned long Out;

// basic functions defined at end of startup.s
void DisableInterrupts(void); // Disable interrupts
void EnableInterrupts(void); // Enable interrupts
void delay(void){unsigned long volatile time;
unsigned long i;
while(time > 0){
i = 1333333;
while(i > 0) {
i = i-1;
}
time = time-1;
}
}

int main(void){

TExaS_Init(SW_PIN_PF4, LED_PIN_PF2); // activate grader and set system clock to 80 MHz
SYSCTL_RCGC2_R |= 0x00000020; // activate clock for Port F
GPIO_PORTF_AMSEL_R = 0x00; //disable analog on PF
GPIO_PORTF_PCTL_R = 0x00000000; //PCTL GPIO on PF4-0
GPIO_PORTF_DIR_R = 0x04;
GPIO_PORTF_AFSEL_R = 0x00;
GPIO_PORTF_DEN_R = 0x14;
GPIO_PORTF_PUR_R = 0x10;
GPIO_PORTF_DATA_R|=0x04;
EnableInterrupts(); // enable interrupts for the grader

while(1)
{
delay();
In=GPIO_PORTF_DATA_R&0x10;
if ( In ==0){
while(In==0)
{
Out=GPIO_PORTF_DATA_R=(~0x04);
Out=Out^0x04;
GPIO_PORTF_DATA_R=Out;
delay();
}
}
else {
GPIO_PORTF_DATA_R=(0x04);
}
}
}




  • You did not mention which part or board you are using. From your description of GPIO PF2 and PF4, I think you are using the EK-TM4C123GXL Launchpad. Is that correct?

    I suggest that you not use #define and direct register writes. Download the TivaWare library if you have not done so, and look at the example in:
    C:\ti\TivaWare_C_Series-2.1.4.178\examples\boards\ek-tm4c123gxl\blinky

    This example will show you how to use the driver library functions to enable the GPIO port, configure the pin type and manipulate the GPIO. Learning to use the TivaWare driver library will help you avoid trouble.
  • Agree w/vendor that Direct Register code - as you present - adds MUCH extra time/effort - BOTH to your code effort - and to those here!    How can that be good?

    To your issue:   this code block (below) - once entered - proves "inescapable" (whenever In is 0) - does it not?    (you read "In" two lines above that  "while loop" - thus "In" cannot be read again - and you are "trapped" w/in that while loop - don't you agree?)

    while(In==0)
    {
    Out=GPIO_PORTF_DATA_R=(~0x04);
    Out=Out^0x04;
    GPIO_PORTF_DATA_R=Out;
    delay();
    }

    placing both:

    In=GPIO_PORTF_DATA_R&0x10;
    if ( In ==0){

    within the (above) while loop enables the "escape" from that loop when In is non-zero.

    Note I've made NO/ZERO Effort to confirm all earlier DRM style code which (attempted) to configure Port F.