Basic Delay Operation

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Basic Delay Operation

Postby silveroblado » Mon Sep 12, 2011 10:11 am

I tried using very basic delay code in SK40C with PIC 16f877a but it did not work!!According to C logic it should work but in real life it wasnt working!!can you help me know why??Its just a very simple Program!!not much detailed!!

SW1=RBO
LED1=RB6
unsigned int count;

Void main(void)
{
while(1)
{
If(SW1==0)
{
count++;
while(count<20)
{
LED1=1;
delay(4000);
LED1=0;
}
}
}
}

When I write this code the LED never blinks even if use the DelayMS(20); why??Also If I write the code in this way, the easiest one!!it still doesnt work:

If(SW1==0)
{
LED1=1;
delay(4000);
LED1=0;
}

According to it when SW1 is pressed once on SK40C the led should be on for delay value then off!!bt in real life it remains on for ever why??How to rectify this problem!!I dont' want to use interrupt or timers as it is going to make my program really confusing!!So plz tell me the way without using interrupts and timers!!

Also if possible plz tell how to count in PIC,I mean when I press SW1 the count increases by 1 only, Then when i press SW1 again the count increase by 1 until it reaches 20!!then led lights up!!I tried many combinations!!bt as soon as I press my SW1 once the LED is on after sometime without reaching count 20!!

Plz help!!this might seem easy!!bt once u try it doesnt work!!I tried a lot bt cant reach any where!!actually I need to control my servo position using the digital sensor!!

If IR==1
then servoON();
DelayMs(20);
servoOFF();

I tried it bt its not working!!using of timer module is not possible now!!as it seems really confusing!!and i dont have time to put in my code!!If possible plz explain using basic delay!!Thanks a lot!!Help would be really appreciated!!

I never thought such a small thing can create such a big ptoblem!
silveroblado
Apprentice
 
Posts: 32
Joined: Mon Aug 15, 2011 2:30 am
Location: Kuala Lumpur

Re: Basic Delay Operation

Postby hyng » Mon Sep 12, 2011 10:24 am

Hi,obviously you didn't refer to SK40C sample code. :twisted: you only delay for LED1=1, how bout LED1=0? The result for your code should be LED1 always ON right? Therefore, please add in a delay after LED1=0.

Add while(SW1==0); after If (SW1==0) will help you to count. Please refer to the sample code in SK40C page. :lol:

Hope to hear from you.
User avatar
hyng
Moderator
 
Posts: 292
Joined: Thu Apr 16, 2009 11:35 am

Re: Basic Delay Operation

Postby hyng » Mon Sep 12, 2011 10:43 am

As for the servo sample code, please download at http://www.cytron.com.my/viewProduct.php?pcode=C40R&name=RC%20Servo%20Motor

There is basic explanation inside the code.

Yeah... sometimes a simple problem may drag a lot of time to find out the solution. As long as you are determined, nothing will stop you. :mrgreen:
User avatar
hyng
Moderator
 
Posts: 292
Joined: Thu Apr 16, 2009 11:35 am

Re: Basic Delay Operation

Postby silveroblado » Mon Sep 12, 2011 11:12 am

what i expected for the led was,

When I press SW1==0, then the led should be on for sometime and then off!!not be on always!!just on once and then off after a delay of 1000!!

If I used delay after LED1=0, then my LED will keep on blinking when I have my finger pressing the switch!!If i use while it will always just blink!!
silveroblado
Apprentice
 
Posts: 32
Joined: Mon Aug 15, 2011 2:30 am
Location: Kuala Lumpur

Re: Basic Delay Operation

Postby hyng » Mon Sep 12, 2011 11:52 am

If(SW1==0)
{
LED1=1;
delay(4000);
LED1=0;
}


it is working for me. Have you try in actual hardware? If yes, then it should be your setting problem.
User avatar
hyng
Moderator
 
Posts: 292
Joined: Thu Apr 16, 2009 11:35 am

Re: Basic Delay Operation

Postby silveroblado » Mon Sep 12, 2011 12:00 pm

Yes I tried for the whole night!!Made 15-20 different combinations!!I gss its setting problem then!!need to sit and find the problem!!Thanks for the help bro!
silveroblado
Apprentice
 
Posts: 32
Joined: Mon Aug 15, 2011 2:30 am
Location: Kuala Lumpur

Re: Basic Delay Operation

Postby hyng » Mon Sep 12, 2011 12:25 pm

Post your setting code here... remember to use code display. curious to know what's the problem.
User avatar
hyng
Moderator
 
Posts: 292
Joined: Thu Apr 16, 2009 11:35 am

Re: Basic Delay Operation

Postby silveroblado » Mon Sep 12, 2011 1:49 pm

What is code display??

My code is same as cytrons quadrature encoder sample code!!Instead of hall effect sensors A and B!!I was using the switch 1 and switch 2 on SK40C!!

Here, it is what I was discussing!!

viewtopic.php?f=9&t=11112
silveroblado
Apprentice
 
Posts: 32
Joined: Mon Aug 15, 2011 2:30 am
Location: Kuala Lumpur

Re: Basic Delay Operation

Postby shiyan » Tue Sep 13, 2011 11:09 pm

hahah.....
silveroblado WROTE:I never thought such a small thing can create such a big ptoblem!


is not the small thing create big problem, is because you don understand the small thing 8-)

As hyng suggested, share your code here using the proper method.
CODE: SELECT_ALL_CODE
Using this, you should be able to see the icon "Code" when you reply post


Anyway don expect all of us will study your code and problem as we are joining this forum voluntarily.
User avatar
shiyan
Amateur
 
Posts: 189
Joined: Wed Jun 09, 2010 10:59 am

Re: Basic Delay Operation

Postby silveroblado » Wed Sep 14, 2011 2:09 am

I never said all should study my code!!As I am stuck in the code somewhere!!Other members are far more experienced than me!!they can just take a look!!and maybe can help me by telling what i can improve!!I m not forcing anyone to reply!!So chill!!This is a forum!!So that ppl can discuss!!And how can I discuss without showing them my code!
silveroblado
Apprentice
 
Posts: 32
Joined: Mon Aug 15, 2011 2:30 am
Location: Kuala Lumpur

Next

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 2 guests

cron