Thanks for your support always. I am learning c coding and facing some problems while using delay function.
I am using PIC 6 board, I want to use delay function (2000 MS) to switch ON LED(RB0). When I will press RD0 Switch again it must go off as soon as I press RD0. So while switch on LED RB0 I need 2000 MS delay.
can some one guide me ?
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Code: Select all
bit oldstate0;
bit oldstate1; // Old state flag
bit oldstate2; // Old state flag
bit oldstate3; // Old state flag
bit oldstate4; // Old state flag
bit oldstate5; // Old state flag
bit oldstate6; // Old state flag
bit oldstate7; // Old state flag
void main() {
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
TRISB = 0xff; // set portB as input
PORTB = 0xff; // Initial PORTC value
TRISD = 0xff; // set portD as output
PORTD = 0xff; // Initial PORTD value
TRISC = 0x00;
PORTC = 0x00;
oldstate0 = 0;
oldstate1 = 0;
oldstate2 = 0;
oldstate3 = 0;
oldstate4 = 0;
oldstate5 = 0;
oldstate6 = 0;
oldstate7 = 0;
delay_ms (2000);
TRISB0_bit = 0;
[color=#FF4000]do {
if (Button(&PORTD, 0, 1, 1)) {
oldstate0 = 1;
}
if (oldstate0 & Button(&PORTD, 0, 1, 0)) {
delay_ms (2000);
TRISB0_bit = ~TRISB0_bit;
oldstate0 = 0;
}[/color]
if (Button(&PORTD, 1, 1, 1)) {
oldstate1 = 1;
}
if (oldstate1 && Button(&PORTD, 1, 1, 0)) {
TRISB1_bit = ~TRISB1_bit;
oldstate1 = 0;
}
if (Button(&PORTD, 2, 1, 1)) {
oldstate2 = 1;
}
if (oldstate2 && Button(&PORTD, 2, 1, 0)) {
TRISB2_bit = ~TRISB2_bit;
oldstate2 = 0;
}
if (Button(&PORTD, 3, 1, 1)) {
oldstate3 = 1;
}
if (oldstate3 && Button(&PORTD, 3, 1, 0)) {
TRISB3_bit = ~TRISB3_bit;
oldstate3 = 0;
}
if (Button(&PORTD, 4, 1, 1)) {
oldstate4 = 1;
}
if (oldstate4 && Button(&PORTD, 4, 1, 0)) {
TRISB4_bit = ~TRISB4_bit;
oldstate4 = 0;
}
if (Button(&PORTD, 5, 1, 1)) {
oldstate5 = 1;
}
if (oldstate5 && Button(&PORTD, 5, 1, 0)) {
TRISB5_bit = ~TRISB5_bit;
oldstate5 = 0;
}
if (Button(&PORTD, 6, 1, 1)) {
oldstate6 = 1;
}
if (oldstate6 && Button(&PORTD, 6, 1, 0)) {
TRISB6_bit = ~TRISB6_bit;
oldstate6 = 0;
}
if (Button(&PORTD, 7, 1, 1)) {
oldstate7 = 1;
}
if (oldstate7 && Button(&PORTD, 7, 1, 0)) {
TRISB7_bit = ~TRISB7_bit;
oldstate7 = 0;
}
} while(1);
}
Best Regards,
DJ