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
Need help with a code. Beginner.
-
- Posts: 23
- Joined: 12 Mar 2012 16:22
- Location: Kolkata, India
- Contact:
Need help with a code. Beginner.
Best Regards,
Agnivesh Adhikari
India
Agnivesh Adhikari
India
Re: Need help with a code. Beginner.
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?
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
Sy
-
- Posts: 23
- Joined: 12 Mar 2012 16:22
- Location: Kolkata, India
- Contact:
Re: Need help with a code. Beginner.
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
Agnivesh Adhikari
India
Re: Need help with a code. Beginner.
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
Sy
-
- Posts: 23
- Joined: 12 Mar 2012 16:22
- Location: Kolkata, India
- Contact:
Re: Need help with a code. Beginner.
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.
Break it up into individual parts. Button, timer, display, done.
-
- Posts: 23
- Joined: 12 Mar 2012 16:22
- Location: Kolkata, India
- Contact:
Re: Need help with a code. Beginner.
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
Agnivesh Adhikari
India