RGD led + 2 buttons code

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

RGD led + 2 buttons code

1,183 Views
akshayhv
Contributor II

I got a problem again. i am trying to write a code for rgb led with 2 buttons the colors must get switch according to my switch press.....here is a code for default it is high so it is entering into both button high statement but when i press any switch i

#include <MKL25Z4.H>

volatile uint32_t msTicks;

void SysTick_Handler(void)

{

  msTicks++;                        /* increment counter necessary in Delay() */

}

void Delay (uint32_t dlyTicks)

{

  uint32_t curTicks;

  curTicks = msTicks;

  while ((msTicks - curTicks) < dlyTicks);

}

static void LED_Config(void)

{

  SIM->SCGC5    |= (1 << 10) | (1  <<  12);      /* Enable Clock to Port B & D */

  PORTB->PCR[18] = (1 <<  8);                      /* Pin PTB18 is GPIO */

  PORTB->PCR[19] = (1 <<  8);                      /* Pin PTB19 is GPIO */

  PORTB->PCR[0]  = (1 <<  8);                    

  PORTB->PCR[1]  = (1 <<  8);

  PORTD->PCR[1]  = (1 <<  8);                      /* Pin PTD1  is GPIO */

  FPTB->PDDR|=(1 << 18|1 << 19);

  //FPTB->PDDR|=(0 << 0 | 0 << 1);

  FPTD->PDDR|=(1 << 1);

}

int main(void)

{

  SystemCoreClockUpdate();            

  SysTick_Config(SystemCoreClock/1000);

  LED_Config();

while(1)

{

  if(FPTB->PDIR & (1 << 0) && FPTB->PDIR & (1 << 1) )

  {

  FPTB->PSOR = 1<< 18;

  FPTB->PSOR = 1<<19;

  FPTD->PSOR = 1<<1;

  Delay(500);

  }

  if(FPTB->PDIR & (0 << 0) )//|| FPTB->PDIR & (1 << 1))

  {

  FPTB->PSOR = 1<<19;

  FPTD->PSOR = 1<<1;

  FPTB->PCOR = 1<<18;

  Delay(500);

  }

  if(FPTB->PDIR  & (0 << 1))//  ||  FPTB->PDIR & (0 << 1))

  {

  FPTB->PSOR = 1<< 18;

  FPTD->PSOR = 1<<1;

  FPTB->PCOR = 1<<19;

  Delay(500);

  }

  if(FPTB->PDIR & (0 << 0) && FPTB->PDIR & (0 << 1))

  {

  FPTB->PSOR = 1<< 18;

  FPTB->PSOR = 1<<19;

  FPTD->PCOR = 1<<1;

  Delay(500);

  }

}

}

Tags (1)
0 Kudos
3 Replies

522 Views
jeremyzhou
NXP Employee
NXP Employee

As you mentioned, default voltage of switch is high, when press happen, the voltage will change to low. So you can try add PORTB->PCR[0]  |= 0X03;PORTB->PCR[1]|=0x03; in LED_Config();

Can you show me your switch design then can be more easy for me to help you?

0 Kudos

522 Views
ndavies
Contributor V

I wouldn't spend too much time looking for a hardware issue. There's logic issues with this code. The last three of the four if statements will never be entered. 0<<0 resolves to 0. and anything with 0, it will resolve to a false. The logic needs to be changed so that it returns a true when the pin level is 0.

try

( ! FPTB->PDIR & (1 << 0))

instead of

(FPTB->PDIR & (0 << 0))

Also this code would be terrible in a shipping product. Push buttons chatter when they are pressed. They don't go instantly from 0 to 1 or 1 to  0. They bounce between the two states and then finally get to the end state. In a real product you would need some sort of Debounce circuit or software debounce routine.

522 Views
apanecatl
Senior Contributor II

Sorry I had to edit the title of your post but there were some problems when trying to reply, quick advice: keep the title short including keywords, in the description field you will have more space explaining your problem, could you please update the description with the full issue? it was cut off when you tried to type it in the title :smileysilly:

0 Kudos