Program sequence

Thread Starter

Moelsayed

Joined Dec 12, 2017
16
Hello everybody,

I am pretty new in pic microcontroller, I am creating a simple program by using micro-c
The is program consist of two LEDs

LED1 flashing on by 1sec.and off for 1sec
LED2 will be on directly if one button is pushed

But I got LED2 will be on after the delay time elapsed even( not directly)

I don’t know how can I overcome that

The code as in below:
define LED portb.f1
#define LED2 portb.f2
#define PB portb.f4
#define on 1
#define off 0
#define d1 delay_ms(1000)
#define d2 delay_ms(2000)

void main()
{

trisb.F1 = 0;
trisb.f2 = 0;
trisb.f4 = 1;
LED = off;
LED2 = off;

for(;

{
//push button program
if(PB == 1)
{
LED2 = on;
}
else
{
LED2 = off;
}

// Flash program
LED = on;
d1;
LED = off;

}

}


Thanks in advance
Moelsayed
 

spinnaker

Joined Oct 29, 2009
7,830
Can you try to be more clear? I have no idea what you are asking.

And please use code tags when posting code.

You can also improve on your macro definitions..

Instead of

#define d1 delay_ms(1000)

do it like this

#define DELAY1() delay_ms(1000)

then in your code you would do something like

DELAY1();

All caps usually means the code is a macro. You should define it as such. Naming it for what is will also make your code more clear. Instead of PB just just PUSH_BUTTON
 

spinnaker

Joined Oct 29, 2009
7,830
And you need a delay after LED = off if you want it off by 1 second. Otherwise it will be off just for the time it takes to execute program instructions ehich is very fast.

Plus you have a blocking algorithm. While your code is in one of the delays, push buttons are being missed. And most of the time is spent in delays. It would be better to use a timer or an external interrupt or interrupt on change for the push button.
 

Thread Starter

Moelsayed

Joined Dec 12, 2017
16
And you need a delay after LED = off if you want it off by 1 second. Otherwise it will be off just for the time it takes to execute program instructions ehich is very fast.

Plus you have a blocking algorithm. While your code is in one of the delays, push buttons are being missed. And most of the time is spent in delays. It would be better to use a timer or an external interrupt or interrupt on change for the push button.

Thanks Spinnaker for your fast reply.
the previous reply this what I meant another words ( i want to get an action from push button even if delay time not elapsed )
because as you said the delay function will delay execution of all instruction in the program.

but i don't know how can i use timer or how can i do external interrupt ?

Thanks for your information

Moelsayed
 

Parth786

Joined Jun 19, 2017
642
Hello everybody,
I am pretty new in pic microcontroller, I am creating a simple program by using micro-c
Can you understand this flow chart? Can you convert it to a program? I think you should try this program first


You still didn't tell whether you have bought PIC micro-controller or you are working on a simulator

If you want to post the program, then you should use the code tag. So that your program looks nice and others can easily understand it. I have put a code tags in your program. You should always remember that whenever you post a program, you should use the code tags

C:
 define LED portb.f1
#define LED2 portb.f2
#define PB portb.f4
#define on 1
#define off 0
#define d1 delay_ms(1000)
#define d2 delay_ms(2000)

void main()
{

       trisb.F1 = 0;
       trisb.f2 = 0;
       trisb.f4 = 1;
       LED = off;
       LED2 = off;

   for(;
     {
         //push button program
           if(PB == 1)
             {
                   LED2 = on;
             }
         else
           {
                 LED2 = off;
           }

// Flash program
    LED = on;
    d1;
   LED = off;

    }

}
 
Last edited:

Thread Starter

Moelsayed

Joined Dec 12, 2017
16
Can you understand this flow chart? Can you convert it to a program? I think you should try this program first


You still didn't tell whether you have bought PIC micro-controller or you are working on a simulator

If you want to post the program, then you should use the code tag. So that your program looks nice and others can easily understand it. I have put a code tags in your program. You should always remember that whenever you post a program, you should use the code tags

C:
 define LED portb.f1
#define LED2 portb.f2
#define PB portb.f4
#define on 1
#define off 0
#define d1 delay_ms(1000)
#define d2 delay_ms(2000)

void main()
{

       trisb.F1 = 0;
       trisb.f2 = 0;
       trisb.f4 = 1;
       LED = off;
       LED2 = off;

   for(;
     {
         //push button program
           if(PB == 1)
             {
                   LED2 = on;
             }
         else
           {
                 LED2 = off;
           }

// Flash program
    LED = on;
    d1;
   LED = off;

    }

}
Thanks parth for your information

I understand this flow chart I am using protus to test my code
 

AlbertHall

Joined Jun 4, 2014
12,626
To get this to work you can use interrupts for either the timer or the button (or both) but you can also do it by polling.
http://www.microcontrollerboard.com/pic-timer0-tutorial.html
This site has code for a one second delay by polling timer 0. It includes the following line:
while(!T0IF);
This line waits for timer 0 to overflow. You can extend this like this:
Code:
while(!T0IF)
{
    [poll the button and set its LED - code as above]
}
This code will spend most of its time in that loop waiting for either the timer or the button and will respond quickly to either event.
 

spinnaker

Joined Oct 29, 2009
7,830
Thanks Spinnaker for your fast reply.
the previous reply this what I meant another words ( i want to get an action from push button even if delay time not elapsed )
because as you said the delay function will delay execution of all instruction in the program.

but i don't know how can i use timer or how can i do external interrupt ?

Thanks for your information

Moelsayed

As I said, that is happening is the delay is hanging up your program. The delay is basically a loop. While the delay is in the loop any push buttons are being ignored.

Any information on interrupts or timers will be found in your data sheet. Mikro C used to have some pretty good lessons on how to use these features. My link no longer works so maybe you want to search around on their site.
 

spinnaker

Joined Oct 29, 2009
7,830
To get this to work you can use interrupts for either the timer or the button (or both) but you can also do it by polling.
http://www.microcontrollerboard.com/pic-timer0-tutorial.html
This site has code for a one second delay by polling timer 0. It includes the following line:
while(!T0IF);
This line waits for timer 0 to overflow. You can extend this like this:
Code:
while(!T0IF)
{
    [poll the button and set its LED - code as above]
}
This code will spend most of its time in that loop waiting for either the timer or the button and will respond quickly to either event.
There is one other way (and it is ugly) where it can be done with no interrupts or timers. Have a counter that gets incremented every loop. You would need to keep track if the LED was on or off. Once the counter reached its limit toggle the LED and the flag you use to keep track of its status. Again ugly and confusing. It might be easier to learn to use timers.

In fact I would not even do timer polling. Once you learn how to do timers it is not that much harder to learn how to do timer interrupts.

Mikro has a very nice timer calculator BTW. It will write all of the timer code for you.
 
Last edited:

Thread Starter

Moelsayed

Joined Dec 12, 2017
16
There is one other way (and it is ugly) where it can be done with no interrupts or timers. Have a counter that gets implemented every loop. You would need to keep track if the LED was on or off. Once the counter reached its limit toggle the LED and the flag you use to keep track of its status. Again ugly and confusing. It might be easier to learn to use timers.

In fact I would not even do timer polling. Once you learn how to do timers it is not that much harder to learn how to do timer interrupts.

Mikro has a very nice timer calculator BTW. It will write all of the timer code for you.
Dear spinnaker,

Really thank you for your rich information and your guidance

It’s really appreciated
I will start learning timer interrupt.

Thanks again
Moelsayed
 
Top