pushbutton code does not work ........AVR DISCUSSION

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sir as earlier i asked you for the problem what i am facing...

here is the code i have made 

ISR(TIMER0_OVF_vect)
{
    x++;

        if (v==0)
        {
            print_1(b);
            v=1;
        }
        else if (v==1)
        {
            print_2(c);
            v=2;
        }
        else if (v==2)
        {
            print_3(d);
            v=3;
        }
        else if (v==3)
        {
            print_4(e);
            v=0;
        }

}

int main()
{
    DDRD=0xf0;
    DDRA=0xff;
    DDRB=0xff;
    DDRC=0xfd;
    DDRD &= ~(1<<DDD3);
    DDRC &= ~(1<<DDC1);
    TIMSK=0x01;
    TCCR0 = (1<<CS01)|(1<<CS00);
    TCNT0 = 0xB2;
    sei();
    
        while(1)
        {
            
            if (!(PIND&(1<<PIND3)))
            {
                if(press_out1==1)
                {
                    press_out1=0;
                    if (x>=1000)
                    {
                        x=0;
                        if (count==0)
                        {
                            n=1;
                            count=1;
                            count2=1;
                            //a=0;
                        }
                        else if (count==1)
                        {
                            n=0;
                            count=0;
                            count2=0;
                            //a=0;
                        }
                    }
                    
                }
                
            }
            else
            {
                press_out1=1;
            }
            
            
            if(n==1)
            {
                //z=1;
                C0_HIGH;
                ascii(1234);
                
            }
            else if (n==0)
            {
                C0_LOW;
                print_1(0);
                print_2(1);
                print_3(2);
                //z=0;
            }

Rjchoudhary

Last Edited: Sun. Mar 22, 2020 - 05:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

this code works but only for 1 or 2 minutes only and after the duration of 2 min if i will press the buttom it does not response. i think the problem is the variable x that is increasing in isr...but when i made a code if(x>1000){x=0;}

it starts no response from starting.

Rjchoudhary

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Show us your variable declarations /definitions. My guess is that x needs to be volatile.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Also, what speed is your ISR running at? It might be, that with printf() in there it is taking too long and missing further interrupts.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes i have made x as volatile int x=0

Rjchoudhary

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please give your threads meaningful titles - this is a dedicated AVR Discussion forum, so calling your thread "AVR Discussion" is really uninformative!

 

Especially as you've used it before: https://www.avrfreaks.net/forum/...

 

Please see Tip #1 in my signature, below, for how to properly post source code.

 

Also Tip #5 needs to be applied to your other threads.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Atomicity!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Rajat choudhary wrote:
Yes i have made x as volatile int x=0

 

It is so much easier for people to help you if you post ALL your code EXACTLY as it is in your tool chain. That means using the 'Add Code' button on the toolbar. The one that looks like "<>".

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have no idea of what the problem in your code is except that it is not written for Arduino, which should be used by all students before "normal" C or C++.

 

However, I reformatted your code to make it easier to understand.  I suggest this:

 //  Always_start_with_the_source's_filename.c   Add one sentence of description.  Then your name and date.
 //    Are you Rajat choudhary the cricketter? Do you feed your not-working code to the crocodiles in Chambal Garden?
 

ISR(TIMER0_OVF_vect) {
  x++;

       if (v==0) { print_1(b); v=1; }
  else if (v==1) { print_2(c); v=2; }
  else if (v==2) { print_3(d); v=3; }
  else if (v==3) { print_4(e); v=0; }
}


int main()
{
  DDRA=0xff; DDRB=0xff; DDRC=0xfd; DDRD=0xf0;   
  DDRD &= ~(1<<DDD3); 
  DDRC &= ~(1<<DDC1);
  TIMSK = 0x01;
  TCCR0 = (1<<CS01)|(1<<CS00);
  TCNT0 = 0xB2;
  
  sei();
  
  while(1) {
      // this must be a pushswitch on PortD3
      if (!(PIND&(1<<PIND3))) {
        if(press_out1==TRUE)   {  //   TRUE = 1; FALSE = 0;
          press_out1 = FALSE;   //  should be TRUE/FALSE  for boolean flag values
          if (x >= 1000)  {   // x is incremented by T0ovf ISR
            x = 0;
            if (count==FALSE)   { 
                n=TRUE; count=TRUE; count2=TRUE;  
            }
            else if (count==TRUE) {
                n=FALSE; count=FALSE; count2=FALSE;
      }  }  }  }
      else {  press_out1=TRUE;  }
          
      if(n==TRUE)  {  C0_HIGH; ascii(1234);  }
      else if (n==FALSE)  {
          C0_LOW;
          print_1(0);
          print_2(1);
          print_3(2);
      }  
  }  
}  

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Atomicity!

But that fault would "fix itself" the next time the ISR fires. The major fault leads to a permanently failed state.

 

Simonetta wrote:
However, I reformatted your code to make it easier to understand

Simply reformatting hasn't worked, it's going to need a major rewrite to knock this into shape.

 

Rajat could get a few useful runs on the scoreboard by renaming his variables more descriptively. (x, v, count1, count2 are too obscure)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

STOP CREATING RIDICULOUSLY TITLED THREADS!

 

This thread is what should have been added to here:

 

https://www.avrfreaks.net/forum/...

 

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user