Need help with a code. Beginner.

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
agnivesh.adhikari
Posts: 23
Joined: 12 Mar 2012 16:22
Location: Kolkata, India
Contact:

Need help with a code. Beginner.

#1 Post by agnivesh.adhikari » 02 Apr 2012 08:40

I want my code to increase a variable at a fast rate, 500 times a second or so, only when a push button is pressed. The maximum value for the variable should be 5 (i.e. the variable can have any value from 0 to 5). Then i want PORTB = the variable. Push button may be connected to PORTC.F1 (doesnt matter where it is connected). Please suggest a simple code.

Thanking you in anticipation
Best Regards,

Agnivesh Adhikari
India

Sy
Posts: 708
Joined: 10 Dec 2009 13:41
Location: UK

Re: Need help with a code. Beginner.

#2 Post by Sy » 02 Apr 2012 08:46

Hi,

There are several ways you could do this, 500 times a second isn't fast. You could use an interrupt on a timer and set the timer for 500Hz.

In your interrupt increment your counter, make sure you declare your counter as volatile. Are you writing some debounce logic?
Kind Regards,
Sy

agnivesh.adhikari
Posts: 23
Joined: 12 Mar 2012 16:22
Location: Kolkata, India
Contact:

Re: Need help with a code. Beginner.

#3 Post by agnivesh.adhikari » 02 Apr 2012 11:00

Actually, I wanted to make a dice. in order to randomize the output, 500Hz would be enough i guess. You must have guessed that switch bounce isnt a factor. I'm an absolute beginner, so i dont know much about interrupts and counters. Please explain a little elaborately. Sorry if i sound silly.
Best Regards,

Agnivesh Adhikari
India

Sy
Posts: 708
Joined: 10 Dec 2009 13:41
Location: UK

Re: Need help with a code. Beginner.

#4 Post by Sy » 02 Apr 2012 12:09

I'm at work at the moment if no one else replies, I'll post something tonight. 12:09 GMT here now.
Kind Regards,
Sy

agnivesh.adhikari
Posts: 23
Joined: 12 Mar 2012 16:22
Location: Kolkata, India
Contact:

Re: Need help with a code. Beginner.

#5 Post by agnivesh.adhikari » 02 Apr 2012 14:29

Thank you very much!!!
Best Regards,

Agnivesh Adhikari
India

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

Re: Need help with a code. Beginner.

#6 Post by drdoug » 03 Apr 2012 02:21

Have you looked at the examples? You can make slight modifications and get the results you are looking for and learn different programming strategies in the process. It is how I learned.
Break it up into individual parts. Button, timer, display, done.

agnivesh.adhikari
Posts: 23
Joined: 12 Mar 2012 16:22
Location: Kolkata, India
Contact:

Re: Need help with a code. Beginner.

#7 Post by agnivesh.adhikari » 03 Apr 2012 05:42

I have figured it out. Thanks all of you for your help!!! For anyone curious about the code:

Code: Select all

void main()
 {
 TRISB = 0x00;
 TRISC = 0x01;
 PORTB = 0x00;
      while(1)
       {
            while(PORTC.F0 == 1)
             {
                  if(PORTB < 6)
                   {
                   PORTB = PORTB + 1;
                   }
                  else
                   {
                   PORTB = 0x01;
                   }
             }
       }
 }
Best Regards,

Agnivesh Adhikari
India

Post Reply

Return to “mikroC PRO for PIC General”