Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pic16f877 , intrruption lighting a led ...

Status
Not open for further replies.

crazy-igzp

Member level 1
Joined
Feb 3, 2014
Messages
34
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
Settat, Morocco
Activity points
255
hi !
I'm trying to to make a led light on when i push the button !
here is the situation :
- when I push it for the first time, the led lights and switch off after a delay ! but if I push it the second time and she's still lights ( in the delay ) it should turn off
I made this prog ! but it didn't work ! I forgot to mention I'm working on pic16f877
Code:
[syntax=c]int b,a=0;
void interrupt() {
  if (INTCON.INTF) {


      portc.b0=0;
      b=5;
      
      INTCON.INTF=0;
  }
}
void main()
{
             trisb=0xFF;
             trisC=0x02;
             INTCON=0x90;
    aa :
         a=0;
         b=0;
         portc.b0=0;
         //-----------------------
         for(;;)
         {
              if(portb.B1==0)
              {
                 portc.b0=1;
                 for(;;)
                 {
                     delay_ms(20);
                     a++;
                     if(b==5)
                      {
                         goto aa;
                      }
                     if(a==100)
                      {
                          goto aa;
                      }
                  }
               }

          }
}[/syntax]


Screenshot (9).png

help ! were is the problem ?
 

1. you need a resistor in series with the LED.
2. you should rewrite the code with some structure to it. Get rid of those 'goto' instructions and jumps from within one loop to another.

I would suggest you do it like this:

Code:
char ButtonPushed=0;

void interrupt() 
{
  if (INTCON.INTF) 
  {
    ButtonPushed = 1;
    INTCON.INTF=0;
  }
}

void main()
{
  trisb=0xFF;
  trisC=0x02;
  INTCON=0x90;
  
  portc.b0=0;
  
   //-----------------------
   for(;;)
   {
     if(ButtonPushed)
     {
       ButtonPushed = 0;
       if(portc.b0 == 0) portc.b0 = 1;
       else portc.b0 = 0;
       delay_ms(20);
      }
    }
}

I have not tried this code but I think it will work. Tell me which compiler you are using if it doesn't.

Brian.
 

I use mikroC PRO for PIC ! but that code, it just lights and turn off the led !
but what I'm loking for is ,
when I push the buton for the first time, the led lights and switch off after a delay ! but if I push it the second time and the led still on (still on from the 1st push (did not switch off after the delay, (in the delay) ) ) it should turn off
 

Ok, try this:
Code:
char ButtonPushed=0;
char LedState = 0;
volatile unsigned int DelayTimer = 0;

void interrupt() 
{
  if(INTCON.INTF) 
  {
    ButtonPushed = 1;
    INTCON.INTF=0;
  }

  if(PIR1.TMR1IF)
  {
    TMR1H = 0x3C; // these values are for 100mS @ 8MHz clock
    TMR1L = 0x80;
    if(DelayTimer > 0) DelayTimer--;
    PIR1.TMR1IF = 0;
  }
}


void main()
{
  trisb=0xFF;
  trisC=0x02;
  TMR1H = 0x3C;
  TMR1L = 0x80;
  T1CON = 0x31; 
  INTCON=0xD0;
  
  portc.b0=0;
  
   //-----------------------

  for(;;)
   {
     if(ButtonPushed)
     {
       ButtonPushed = 0;

       if(LedState == 0)
       {
         LedState = 1;
         DelayTimer = 20;  // <- Number of 1/10th seconds delay you want
       }
       else 
       {
         LedState = 0;
         DelayTimer = 0;
       }
     }
     
     if(DelayTimer == 0) LedState = 0;
     portc.b0 = LedState?1:0;  
   }
}

Again, I have not tested it. I do not use MikroC.
I made some assumptions, first that you are using an 8MHz system clock, second that the delay is 2 seconds. If you are using a different clock speed you can adjust the values loaded into TMR1 and if necessary the prescaler ratio (in T1CON) to set the 'tick' to 0.1 seconds. For different delays before the LED turns off by itself you can adjust the value loaded into 'DelayTimer'. The range is 0.1 seconds to ~109 minutes.

You will see that I changed the code so it doesn't read the port to see whether the LED is on or not, it now uses a variable 'LedState' instead. This is safer because it avoids what is called "RMW" (Read Modify Write) problems.

Brian.
 

    V

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top