Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

5 Led nd 5 Buttons

Status
Not open for further replies.

mierdogan

New Member
Hi to all

I need all of your help!

I am still learning to use PIC (newbie user) and am using CCS C

I will share one code but that code not for me.

I wrote some codes but i dont want use this code i mean i cant completed

I want the prepare simple circuit with pic 16F877A and when i was press to one button, first led starts blinking but when i was press to another button for another led, first led never stop and second led never starts to blinking. How can reach this problem? If possible, could you help me ?

Thank you very much for everythink

Not: My english not very well

Code:
Code:
#include <16f877A.h>      
#use delay(CLOCK=4000000)
#use fast_io(b)
#use fast_io(a)
#fuses XT,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP,NOWDT
int i=0;
void main()
{  set_tris_b(0xf8);
   set_tris_a(0x00);
   output_b(0x00);
   output_a(0x00);
   while(true)
   {  if(input(pin_b7) && i==0)
      {  i=1;
         output_high(pin_b2);
         delay_ms(6000);
         output_low(pin_b2);
      }
      if(input(pin_b6) && i==0)
      {  i=1;
         output_high(pin_b1);
         delay_ms(3000);
         output_low(pin_b1);
      }
      if(input(pin_b5) && i==0)
      {  i=1;
         output_high(pin_b0);
         delay_ms(1500);
         output_low(pin_b0);
      }
      if(input(pin_b4) && i==0)
      {  i=1;
         output_high(pin_a1);
         delay_ms(3000);
         output_low(pin_a1);
      }
      if(input(pin_b3) && i==0)
      {  i=1;
         output_high(pin_a0);
         delay_ms(6000);
         output_low(pin_a0);
      }
      if(!input(pin_b3)&& !input(pin_b4)&& !input(pin_b5)&& !input(pin_b6)&& !input(pin_b7))
      {  i=0;  }
   }
}
 
Last edited:
why you are using the last if condition ... ? and also checking the i==0 in every if clause ? . just remove these two . and also a small delay after the output_low() in every condition.
 
When i was try remove i==0 and last if condition, CCS C compiler gives to me error 58 for every clause

Actualy i need simple code for simple and little circuits with 16f877a

examples

action:

When you press button one, first led has to start blinking continuisly
then

when you press button two second led hast start blinling continuisly

....
..
.

how can i write this little code? i confused :S
 
Code:
......
#define SW1_PRESSED() (SW1==0)
#define SW2_PRESSED() (SW2==0)
#define SW3_PRESSED() (SW3==0)
.......
while(1)
{
     if(SW1_PRESSED())
     {   // blink LED1
          while((!SW2_PRESSED()) && (!SW3_PRESSED()))
          {
                LED1 = 1;
                delay(100);
                LED1 = 0;
                delay(100);
           }
      }
       if(SW2_PRESSED())
       {   // blink LED1
            while((!SW1_PRESSED()) && (!SW3_PRESSED()))
            {
                LED2 = 1;
                delay(100);
                LED2 = 0;
                delay(100);
            }
        }
        if(SW3_PRESSED())
         {   // blink LED1
             while((!SW1_PRESSED()) && (!SW2_PRESSED()))
             {
                LED3 = 1;
                delay(100);
                LED3 = 0;
                delay(100);
             }
        }
}

if you are using some big delays, then you need to either use a flag to hold the key press or you need to press the switch until it exits from the delay.(Also its possible to keep the Keypress flag in delay, but not recommended) .

If you are using a timer based delay , it will be become simple..
 
Code:
......
#define SW1_PRESSED() (SW1==0)
#define SW2_PRESSED() (SW2==0)
#define SW3_PRESSED() (SW3==0)
.......
while(1)
{
     if(SW1_PRESSED())
     {   // blink LED1
          while((!SW2_PRESSED()) && (!SW3_PRESSED()))
          {
                LED1 = 1;
                delay(100);
                LED1 = 0;
                delay(100);
           }
      }
       if(SW2_PRESSED())
       {   // blink LED1
            while((!SW1_PRESSED()) && (!SW3_PRESSED()))
            {
                LED2 = 1;
                delay(100);
                LED2 = 0;
                delay(100);
            }
        }
        if(SW3_PRESSED())
         {   // blink LED1
             while((!SW1_PRESSED()) && (!SW2_PRESSED()))
             {
                LED3 = 1;
                delay(100);
                LED3 = 0;
                delay(100);
             }
        }
}

if you are using some big delays, then you need to either use a flag to hold the key press or you need to press the switch until it exits from the delay.(Also its possible to keep the Keypress flag in delay, but not recommended) .

If you are using a timer based delay , it will be become simple..



Hi!

Thank you very much my friend you are gentleman

I will try with these codes and i will tel you results

regards
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top