Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 30, 2020, 07:41:57 07:41


Login with username, password and session length


Pages: [1]
Print
Author Topic: Changes Led On And Off  (Read 829 times)
0 Members and 1 Guest are viewing this topic.
galee
Newbie
*
 Muted
Offline Offline

Posts: 11

Thank You
-Given: 0
-Receive: 0


« 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.
Logged
Manuel
Active Member
***
Offline Offline

Posts: 195

Thank You
-Given: 296
-Receive: 105


« Reply #1 on: April 16, 2020, 08:34:59 08:34 »

Can you let us know what PIC are you using ?

X!
Logged

-> An Apple a Day does not Let U become a Macintosh!
Sideshow Bob
Cracking Team
Hero Member
****
Offline Offline

Posts: 722

Thank You
-Given: 205
-Receive: 712



« Reply #2 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
Logged

I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum
Wilksey
Cracking Team
Senior Member
****
Offline Offline

Posts: 352

Thank You
-Given: 143
-Receive: 2470


« Reply #3 on: April 16, 2020, 11:27:21 11:27 »

If using  a PIC that supports it use PORT for reading and LAT for writing
Logged
galee
Newbie
*
 Muted
Offline Offline

Posts: 11

Thank You
-Given: 0
-Receive: 0


« Reply #4 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.



 
Logged
ajith
Inactive

Offline Offline

Posts: 3

Thank You
-Given: 9
-Receive: 2


« Reply #5 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);
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC