carmembeddedgpio

How to use GPIO Port Control (GPIOPCTL) in ARM Cortex M4 TM4C123G microcontroller?


I am trying to interface an ultrasonic sensor with ARM Cortex M4 mcu. Since I am using and edge-triggered timer that calculates the time between the rising and falling edge of the echo of the sensor, What is the alternate function that I should be assigning to the echo input pin ? I have found the following configuration on http://cortex-m.com/tm4c-lessons/:

void Timer0_init(void)
{
SYSCTL->RCGCTIMER |=(1U<<0); 
SYSCTL->RCGCGPIO |=(1U<<1); 
GPIOB->DIR &=~ECHO;
GPIOB->DEN |=ECHO;
GPIOB->AFSEL |=ECHO;
GPIOB->PCTL &=~0x0F000000;
GPIOB->PCTL |= 0x07000000;
TIMER0->CTL &=~1;
TIMER0->CFG =4;
TIMER0->TAMR = 0x17;
TIMER0->CTL |=0x0C;
TIMER0->CTL |=1;
}

I used the data sheet to understand every line but for the lines I don't understand :

GPIOB->PCTL &=~0x0F000000;
GPIOB->PCTL |= 0x07000000;

I guess the first line is just a reset, and the second line selects the peripheral function, but I can't understand or find what does setting the pin he used for input with 7 in the PCTL register?


Solution

  • As described in the datasheet on page 688f the used bits 27:24 of this register are used to set the alternate function to be used on pin 6. According to GPIOB->PCTL the configured pin is PB6.

    Now you can see on page 1351 Table 23-5 which alternate function is set. In this case it is T0CCP0 which is either a pwm, a capture or a compare funtcion based on the pin direction and peripheral configuration.

    GPIOB->PCTL &=~0x0F000000; is used to reset the pin 6 function, without touching the other pins.

    GPIOB->PCTL |= 0x07000000; sets the pin 6 function, without changing the others.