Sonsivri

Electronics => Pic C Languages => Topic started by: galee on April 15, 2020, 06:40:01 18:40



Title: Changes Led On And Off
Post by: galee on April 15, 2020, 06:40:01 18:40
All member here,
I have get a stuck in my code, that code has working well in real hardware but a little get stuck to configure on and off when latching position in LED1.

Here are my code :
Code:
 /*******************************************************************************/
bit oldstate0;
//oldstate1;

sbit LED0 at PORTB.B0;     //Led indicator T
sbit LED1 at PORTB.B4;     //Led indicator R
sbit LED2 at PORTB.B5;     //Led blink
sbit LED3 at PORTB.B6;     //Led power up


/********* Mode button **********/

sbit LED_Latching at PORTB.B1;      //Led latch
sbit LED_Momentary at PORTB.B2;     //Led moment

sbit SELECTOR at PORTB.B3;  // Make it selected betwen latch and moment

/*************End Mode button *************/

/*************Delay for blinking few second************/
void blink(){
char i;
for (i=0;i<25;i++)    //After counting then
{
LED2 =~LED2;          //On and Off
Delay_ms(80);        //Delay it before start
//LED2 = 0;
}
}

void main()
{
  CMCON        = 0x07;  // turn off the comparators
  TRISA        = 0xF7;  // 0x03 set RA0 and RA1 as input pins   // Except RA3 Output
  TRISB        = 0x08;  // PORTB3 Input, Others Is Output
  PORTA        = 0x00;  // turn off all the pins on port A
  PORTB        = 0x00;  // turn off all the pins on port B

   blink();            //Count few second to blink (LED2)
       
     Delay_ms(100);   //Then delay it
     LED3 = 1;        // Once finish blink then turn it off, then LED2 bring it high

  do
  {
   if(SELECTOR == 1)       // Selector switch momentary is on pos
        {
    LED_Momentary = 1;      //Led UP
    LED_Latching = 0;       // Led DOWN

     {
    if (Button(&PORTA, 0, 10, 1))  // you can set the debouncing delay directy in this function (10ms)

    {
      oldstate0 = 1;  //Put it high once release
    }

    if (oldstate0 && Button(&PORTA, 0, 10, 0))
    {
      LED0 = ~LED0;

      oldstate0 = 0;
   }
   }
   }
   
     else

      {

    if(SELECTOR == 0)        // Selector switch latch is on pos

         LED_Latching = 1;       //Led UP
         LED_Momentary = 0;     //Led DOWN

        if (Button(&PORTA, 0, 10, 1))  // you can set the debouncing delay directy in this function (10ms)
    {
       oldstate0 = 1;
       LED0   = 0;          // Once selected led Off
       LED1 = 1;            // Led up
    }

    if (oldstate0 && Button(&PORTA, 0, 10, 0))
    {
      oldstate0 = 0;
      LED0 = 1;
      LED1 = 0;

    }
    }
 }  while (1);
}

That the problem is, my " sbit LED1 at PORTB.B4;     //Led indicator R " when latch doesn't going to low state when button on RA0 or PORTA0 toggle.

Hope someone here, help me to find what is happen.


Galee.


Title: Re: Changes Led On And Off
Post by: Manuel on April 16, 2020, 08:34:59 08:34
Can you let us know what PIC are you using ?

X!


Title: Re: Changes Led On And Off
Post by: Sideshow Bob on April 16, 2020, 10:18:06 10:18
Try this google search https://www.google.com/search?q=bit+twidling Also take a look here https://www.microchip.com/forums/m945043.aspx it will help you


Title: Re: Changes Led On And Off
Post by: Wilksey on April 16, 2020, 11:27:21 11:27
If using  a PIC that supports it use PORT for reading and LAT for writing


Title: Re: Changes Led On And Off
Post by: galee on April 16, 2020, 01:24:20 13:24
That processor i used is a PIC16F628A, it mean no need to add LAT function.

That code are working very well, there are 2 mode function, latch and momentary, for latch function that working well, momentary also working well too for only run 1 LED, but if I run for 2 LED, that problem the other LED can't follow when toggle is pressed.

Well, let me detail the code of problem, here are :
Code:
 do
  {
   if(SELECTOR == 1)       // Selector switch momentary is on pos
        {
    LED_Momentary = 1;      //Led UP
    LED_Latching = 0;       // Led DOWN

     {
    if (Button(&PORTA, 0, 10, 1))  // you can set the debouncing delay directy in this function (10ms)

    {
      oldstate0 = 1;  //Put it high once release
    }

    if (oldstate0 && Button(&PORTA, 0, 10, 0))
    {
      LED0 = ~LED0;

      oldstate0 = 0;
   }
   }
   }
   
     else

      { 

If only 1 LED0 that function is okay, but I wish to add LED1, it mean when the button is toggle then LED0 is off and LED1 is on then one more toggle or release LED0 is on and LED1 is Off.

Below code :
Code:
 
    if(SELECTOR == 0)        // Selector switch latch is on pos

         LED_Latching = 1;       //Led UP
         LED_Momentary = 0;     //Led DOWN

        if (Button(&PORTA, 0, 10, 1))  // you can set the debouncing delay directy in this function (10ms)
    {
       oldstate0 = 1;
       LED0   = 0;          // Once selected led Off
       LED1 = 1;            // Led up
    }

    if (oldstate0 && Button(&PORTA, 0, 10, 0))
    {
      oldstate0 = 0;
      LED0 = 1;
      LED1 = 0;

    }
    }
 }  while (1);
}

Are fine, no problem the function is working well.

Galee.



 


Title: Re: Changes Led On And Off
Post by: ajith on July 12, 2020, 08:21:56 08:21
Note sure if this is already solved. However, reading the code, it looks like the blocks are mixed up with probably missing some braces. Here is a pretty printed version of the do-while loop. Perhaps this makes it easier for you to spot if your intent got translated into code.

Code:
do
  {
   if(SELECTOR == 1)       // Selector switch momentary is on pos
   {
    LED_Momentary = 1;      //Led UP
    LED_Latching = 0;       // Led DOWN
    {
     if (Button(&PORTA, 0, 10, 1))  // you can set the debouncing delay directy in this function (10ms)
     {
      oldstate0 = 1;  //Put it high once release
     } // end if (Button(&PORTA, 0, 10, 1))
     if (oldstate0 && Button(&PORTA, 0, 10, 0))
     {
      LED0 = ~LED0;
      oldstate0 = 0;
     } // end if (oldstate0 && Button(&PORTA, 0, 10, 0))
    }
   } // end if(SELECTOR == 1)
   else
   {
    if(SELECTOR == 0)        // Selector switch latch is on pos
     LED_Latching = 1;       //Led UP

    LED_Momentary = 0;     //Led DOWN

    if (Button(&PORTA, 0, 10, 1))  // you can set the debouncing delay directy in this function (10ms)
    {
     oldstate0 = 1;
     LED0   = 0;          // Once selected led Off
     LED1 = 1;            // Led up
    } // end if (Button(&PORTA, 0, 10, 1))
    if (oldstate0 && Button(&PORTA, 0, 10, 0))
    {
      oldstate0 = 0;
      LED0 = 1;
      LED1 = 0;
    } // end if (oldstate0 && Button(&PORTA, 0, 10, 0))
   } // end else
 }  while (1);