كود:
void main() {
int x;
TRISB=0XFF; // initialisation du port B en entrée
TRISC=0; //initialisation du port C en sortie
ADCON1=0X06;
PORTC=0;
while(1){
if (RB4_bit==0)
{ delay_ms (10);
x=x+1;
if (RB4_bit==1)
{
if (x=1)
{ portc= ~portc; delay_ms (100);portc= ~portc; }
if (x=2 )
{ portc= ~portc; delay_ms (300); portc= ~portc; }
if (x=3 )
{ portc= ~portc; delay_ms (500); portc= ~portc; }
}
}
}
}
ملف المحاكاة
http://www.gulfup.com/?SGDPip
|
السلام عليكم
تفضل اخي هذا أحد الحلول
كود:
char x ;
unsigned int k;
void check_switch()
{
delay_ms(10);
while( RB4_bit==0 );
delay_ms(10);
x++;
if(x==4)x=1;
}
void main()
{
TRISB=0XFF; // initialisation du port B en entrée
TRISC=0; //initialisation du port C en sortie
ADCON1=0X06;
PORTC=0;
while(1){
if(x==0 && RB4_bit==0 )check_switch();// start value
else if (x==1)
{
portc=~portc;
for(k=0;k<100;k++) // 1 second
{
delay_ms(10);
if(RB4_bit==0)check_switch();
}
}
else if (x==2)
{
portc=~portc;
for(k=0;k<300;k++) // 3 second
{
delay_ms(10);
if(RB4_bit==0)check_switch();
}
}
else if (x==3)
{
portc=~portc;
for(k=0;k<500;k++) // 5 second
{
delay_ms(10);
if(RB4_bit==0)check_switch();
}
}
}
}
لماذا لم يعمل معاك ؟
اولا يوجد خطأ في كتابه الشرط if لازم تكتب علامه مساواه == وليس =
2- طالما يوجد زمن تأخيري كبير فمايكرو سيكون مشغول بتفيذ هذا الزمن يجب علينا أختبار شرط سويج ضمن فتره زمنيه لكي يقوم بالتنفيذ اوامر بشكل صحيح وهذا يعني كل 10 ملي ثانيه بختبر سويج هل تم الضغط عليه ام لا