Simple Flashing led program

General discussion on mikroC.
Post Reply
Author
Message
crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Simple Flashing led program

#1 Post by crocu » 14 Nov 2009 08:38

Hello,


I would like to have a permanent LED flashing program that will flash a led like this :

|||||__|||||__|||||__

5 quick flashes, nothing done during 1 second and 5 quick flashes again ... etc ...

The delay between flashes and hold time has not be very occurate.

Can someone tell me how to do that with a C code ?

Many thanks for your help,

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

#2 Post by Sobrietytest » 15 Nov 2009 06:26

Code: Select all


void main() {


TRISB = 0x00; // PORTB as ouput
PORTB = 0x00; // Set port output as all 0's

while(1){
for (i=0, i < 5, i++){ //repeat 5 times
    PORTB.F0 = 1;   //switch on LED at PORTB pin 0
    delay_ms(150);  //short delay
    PORTB.F0 = 0;   //switch off LED
    delay_ms(150);
    }

Delay_ms(1000);// 1 second delay
}//endless loop

}


crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

#3 Post by crocu » 16 Nov 2009 07:28

Thank you very much Sobrietytest,

Your code example is fine but i can't delay and have the main loop in hold every 1 second because i need to have other things to be run from there.

If there a way to have led flashing holding 1 second after 5 flashes while the rest of the code is NOT interrupted ?

If so can you show me how to do that please ?

Many thanks,

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#4 Post by drdoug » 16 Nov 2009 14:44

I would set up an interrupt timer and for 100 ms (or whatever time) and then count how many times it has been activated. Activate it 5 times to turn on/off and then leave it oftf for 10 counts and then repeat. You just have to keep track of where you are in the count.

crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

#5 Post by crocu » 16 Nov 2009 17:45

Can you show me how to do this please ?

I'm novice this C programming and an example would be much appreciated.

Many thanks,

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#6 Post by drdoug » 16 Nov 2009 19:35

I'll try to put something together for you but in the meantime take a look at the mE examples (Flashing LED with Timer 0).

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#7 Post by drdoug » 16 Nov 2009 20:05

I put this together while eating my lunch. I did not test or compile so there may be errors and there are more efficient ways of doing things but this should help some.

I modified the mE Timer 0 example. You should be able to do other things in the program but if all depends on what you are doing.

Code: Select all

unsigned cnt, myTick;

void interrupt() {
  if (TMR0IF_bit) {
    cnt++;                 // increment counter
    TMR0IF_bit = 0;        // clear TMR0IF
    TMR0   = 96;
  }
}

void main() {
  OPTION_REG = 0x84;       // Assign prescaler to TMR0
  ANSEL  = 0;              // Configure AN pins as digital
  ANSELH = 0;
  C1ON_bit = 0;            // Disable comparators
  C2ON_bit = 0;
  TRISB = 0;               // PORTB is output
  PORTB = 0xFF;            // Initialize PORTB
  TMR0  = 96;              // Timer0 initial value
  INTCON = 0xA0;           // Enable TMRO interrupt
  cnt = 0;                 // Initialize cnt
  myTick = 0;
  do {
    if (cnt >= 40) {      //  100 ms delay I think
      myTick++;
      cnt = 0;             // Reset cnt
      if (myTick < 10) {     // Flash light 5 times
         PORTB.F0 = ~PORTB.F0
       } // end if (myTick < 10)
      if (myTick >= 10) {    // Leave off for 1 second
         PORTB.F0 = 0;
       if (myTick >= 20) myTick = 0;   // reset myTick to begin cylce again
       }// end if (myTick >= 10)
    }

    // You should be able to add in your other code
    // items here with a minimal impact.
    
  } while(1);
}

Post Reply

Return to “mikroC General”