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.

Switch

Hi, i tried to using the switch on tiva to control an external air pump. The code below is to create a 100ms delay for the air pump (port D pin 3). Can anyone help to check the code please? Thank you so much.  

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"


bool bSw1_Level;
//uint32_t INT_PORTD;

int main(void){
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);//configure system clock
SysCtlDelay(2000000);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);//enable port F
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);//enable port D

GPIOPinTypeGPIOInput(GPIO_PORTF_BASE,GPIO_PIN_4);//configure PortF pin 4 as input
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_3);//configure portD pin3 as output
// GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3,0x08);
// GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3,0x00);
// bSw1_Level = GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_0);
GPIOPadConfigSet(GPIO_PORTF_BASE,GPIO_PIN_4,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
//GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4 , GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
//GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3,0x08);
//SysCtlDelay(1000000);


bSw1_Level = GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_0);

while(1)
{ if (bSw1_Level==1)

{
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3,0x00);
}else
{
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3,0x08);
SysCtlDelay(1000000);}
}
}

  • Hello Jia,

    PF0 is a locked pin and requires special handling. Please see the following post for details

    http://e2e.ti.com/support/microcontrollers/tiva_arm/f/908/t/374640

    Secondly, the delay loop at 40Mhz would result in 75ms of delay. Each delay count takes 3 clock cycles. Thus total time is 1000000*3/40MHz.

    Regards
    Amit
  • Hi Amiit,

    Sorry for troubling you again. Thanks for your help. I have unlocked the PF0 and modified the code. Please advice. Thank you so much.

    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "inc/hw_types.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/gpio.h"
    #include "driverlib/interrupt.h"

    bool bSw1_Level;

    int main(void){
    SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);//configure system clock
    SysCtlDelay(2000000);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);//enable port F
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);//enable port D

    // Enable pin PF0/4 for GPIOInput
    //First open the lock and select the bits we want to modify in the GPIO commit register.
    HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
    HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x1;

    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE,GPIO_PIN_0|GPIO_PIN_4); //configure PortF pin 4 as input
    GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_3); //configure portD pin3 as output

    GPIOPadConfigSet(GPIO_PORTF_BASE,GPIO_PIN_0|GPIO_PIN_4,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);

    //************************ delay loop*****************************

    bSw1_Level = GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_0);

    while(1)
    { if (bSw1_Level==1){
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3,0x00);
    }else
    {
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3,0x08);
    SysCtlDelay((1000000 * 3) / 40);}
    }
    }
  • Hello Jia,

    The delay loop computation is wrong. For every SysCtlDelay you would be using 3 clock cycles. So you have to back compute the 100ms delay using the equation 100ms*40Mhz/3 = delay count

    Also what is still the issue now?

    Regards
    Amit
  • Hi Amit,

    So the delay count = 1333333?

    and the Loop will be:

    bSw1_Level = GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_0);

    while(1)
    { if (bSw1_Level==1){
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3,0x00);
    }else
    {
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3,0x08);

    SysCtlDelay((SysCtlClockGet()*1000000) / 3);}
    }
  • Hello Jia

    Yes, the delay count is correct. But the Delay loop is not. The SysCtlClockGet will return the value 40000000. So replace the multiplier and divide it by 30.

    Regards
    Amit
  • Hi,

    There is a warning on line #38. I want to write a 1 to PE3 in a while loop. Can help?

    Thanks 

  • Hello Jia,

    The expression does not evaluate to a condition. It should be

    while(GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_0) == 0x0)

    Regards
    Amit
  • what if i want to write PD3 = 1 &PD3 =0?
  • Hello Jia,

    The condition will evaluate as follows in C where cond1 and cond2 are what you want to compare it with

    while(cond1 && cond2)

    Regards
    Amit
  • Hi Amit,

    What if I want to write PD3=0? And write PD3=1?

    Sorry....

  • Hello Jia

    In that case you would need to first configure the GPIO as an output using GPIOPinTypeGPIOOutput and then use GPIOPinWrite API to write the value

    Regards
    Amit
  • Hi Amit, 

    Is it correct? 

    Thank you.

  • Hi,
    You could have your problem half solved by using the example file(s) button.c(.h)and qs-rgb.c.
    First of all,after correct configuration of your input pin, since you need to trigger
    an event by pressing this button, you need to debounce it, as it is in qs-rgb example
    (this means use the SysTick interrupt routine). That detects the correct state of your
    button.

    At this stage, you must add your code: once detected, set port D pin high and start a 32-bit timer
    configured as single shot, for 100 ms. Prepare its interrupt routine and there clear the port D pin
    and set others as you wish (this seems a little bit easier at this stage, see below other solution).

    Note: you can use the SysTick interrupt also for timing and count down ten ticks(if I remember well
    the SysTick is set for 10 ms, check for your clock setting), when you should clear the port D pin (and/or set/clear others).

    The main problem with your application is what you expect/need to do if the user press again
    the button inside the 100ms timing interval - you have the choice to either block the
    action of pin to start a new count or effectively make the interval longer. But you must write
    your code by yourself. Try it, step by step,...

  • Hello Jia,

    To have a simple delay debounce, a solution would be to

    while(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0) == 0x0);
    SysCtlDelay(10)
    if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0) == GPIO_PIN_0)
    {
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3,0x08) ;
    SysCtlDelay(1333333); // Please make sure that this is corrected.
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3,0x00) ;
    }

    Regards
    Amit