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,
Simple Flashing led program
-
- Posts: 619
- Joined: 05 Jul 2008 06:05
- Location: Thailand
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
}
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,
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,
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.
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);
}