help ;why my program not running

LED Blinking, Walking with Cytron Servo, Displaying RFID, Multi-function Mobile Robot......

help ;why my program not running

Postby captenusop » Wed Oct 13, 2010 4:28 pm

#include<pic.h>
//#include"16f877a.h"
__CONFIG(0x3F32);

void blinked1(void);
void blinked2(void);
void init_io(void);
void delay(unsigned int DlyCnt);

void delay(unsigned int DlyCnt)
{
unsigned int MyCnt,i;
MyCnt=DlyCnt;
while(MyCnt){
i=20000; //ubah nilai i dari 1-65535
while (i) i--;
MyCnt--;
}
}

void init_io(void)
{
TRISB=0b00000000;
PORTB=0b00000000;
TRISD=0b11111111;
PORTD=0b00000000;
}

/*main program*/
void main (void)
{
unsigned char Cnt;
init_io();
Cnt=0;
while(1){
if (Cnt<=5){blinked1();Cnt++;} // subroutine blinked1 akan jalan sebanyak 5 kali
if (6<=Cnt<=11) {blinked2();Cnt++;} //subroutine blinked2 akan jalan sebanyak 5 kali
if (Cnt==12) {PORTB=0b00000000;}//led akan stop
}
}
//***********Subrautine*****
void blinked1(void)
{
PORTB=0b11110000;
delay(5);
PORTB=0b00001111;
delay(2);
}

void blinked2(void)
{
PORTB=0b00000000;
delay(1);
PORTB=0b11111111;
delay(1);
}
captenusop
Novice
 
Posts: 20
Joined: Fri Jul 16, 2010 11:24 am
Location: perlis

Re: help ;why my program not running

Postby kl84 » Wed Oct 13, 2010 11:58 pm

Because of this:
CODE: SELECT_ALL_CODE
i=20000; //ubah nilai i dari 1-65535
while (i) i--;


The argument in while() must be 1 or 0, not other values. An appropriate way to do what you wish to carry out here is:
CODE: SELECT_ALL_CODE
i=20000; //ubah nilai i dari 1-65535
while(i>0) i--;
User avatar
kl84
Amateur
 
Posts: 166
Joined: Thu Jul 08, 2010 12:14 pm

Re: help ;why my program not running

Postby captenusop » Thu Oct 14, 2010 2:18 pm

In my project i must create counter.
the subroutine delay, blinked1 and blinked2 are working.
The problem is when

if (Cnt=12) PORTB=0b00000000;//

all the led suppose to stop blinking here but there still blinking.
captenusop
Novice
 
Posts: 20
Joined: Fri Jul 16, 2010 11:24 am
Location: perlis

Re: help ;why my program not running

Postby ober » Thu Oct 14, 2010 5:26 pm

kl84 WROTE:Because of this:
CODE: SELECT_ALL_CODE
i=20000; //ubah nilai i dari 1-65535
while (i) i--;


Actually C allow this. As long as the value inside ( ) is not zero, it will return true. Therefore,
while(i) is the same as while (i != 0).

captenusop WROTE:In my project i must create counter.
the subroutine delay, blinked1 and blinked2 are working.
The problem is when

if (Cnt=12) PORTB=0b00000000;//

all the led suppose to stop blinking here but there still blinking.


What is the blinking pattern after 1st loop? pattern 1 or pattern 2? Or it loop both pattern?
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: help ;why my program not running

Postby kl84 » Thu Oct 14, 2010 5:31 pm

Ooo...thanks for the info!

what about this:
CODE: SELECT_ALL_CODE
if (6<=Cnt<=11)

Is it allowed?
User avatar
kl84
Amateur
 
Posts: 166
Joined: Thu Jul 08, 2010 12:14 pm

Re: help ;why my program not running

Postby captenusop » Fri Oct 15, 2010 9:16 am

if (Cnt==12) PORTB=0b00000000;// the program will loop patern 2

if (Cnt<=12) PORTB=0b00000000;// the program will loop patern 1 and 2
captenusop
Novice
 
Posts: 20
Joined: Fri Jul 16, 2010 11:24 am
Location: perlis

Re: help ;why my program not running

Postby shahrul » Fri Oct 15, 2010 2:50 pm

I have 1 example LED Blinking here.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: help ;why my program not running

Postby captenusop » Fri Oct 15, 2010 4:48 pm

led is blinking in 2 pattern. but my counter(variable) is not working.
captenusop
Novice
 
Posts: 20
Joined: Fri Jul 16, 2010 11:24 am
Location: perlis

Re: help ;why my program not running

Postby shahrul » Fri Oct 15, 2010 5:22 pm

captenusop WROTE:led is blinking in 2 pattern. but my counter(variable) is not working.

If all your function is working, maybe you can try this way.
CODE: SELECT_ALL_CODE
if (Cnt<=5){blinked1();Cnt++;} // subroutine blinked1 akan jalan sebanyak 5 kali
else if (Cnt<=11) {blinked2();Cnt++;} //subroutine blinked2 akan jalan sebanyak 5 kali
else if (Cnt==12) {PORTB=0b00000000;}//led akan stop
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: help ;why my program not running

Postby captenusop » Sat Oct 16, 2010 8:16 am

:lol: thanks,, its working
captenusop
Novice
 
Posts: 20
Joined: Fri Jul 16, 2010 11:24 am
Location: perlis

Next

Return to DIY Project Set

Who is online

Users browsing this forum: No registered users and 8 guests