Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

simple program for Blinking LED

Status
Not open for further replies.

sahu

Member
Dear All
Plz guied me I need a simple program for LED1 to LED8.

task_1
when S1 unpressed ;

{LED=11111110;}
(HOLD IT for 2sec)

{LED=11111101;}
(HOLD IT for 2sec)

{LED=11111011;}
(HOLD IT for 2sec)

{LED=11110111;}
(HOLD IT for 2sec)


{LED=11101111;}
(HOLD IT for 2sec)
Repeat this function every time till S1 unpressed .

task_2
but when S1 pressed for 1time within 1sec

{LED=11111110;}
(HOLD IT for 20sec)
& ofter 20 sec exit form task_2 & Repeat this function task_1

but when S1 pressed for 2time within 1sec

{LED=11111101;}
(HOLD IT for 20sec)
& ofter 20 sec exit form task_2 & Repeat this function task_1


but when S1 pressed for 3time within 1sec

{LED=11111011;}
(HOLD IT for 20sec)
& ofter 20 sec exit form task_2 & Repeat this function task_1


but when S1 pressed for 4time within 1sec

{LED=11101111;}
(HOLD IT for 20sec)
& ofter 20 sec exit form task_2 & Repeat this function task_1
 
Nigel's tutorials have almost all you need..... If you need it in C.... well you know where my tutorials are..
its ok sir ?
PHP:
if(sw_pressed)
{
   count++;
  switch(count)
  {   case 1:   LED=11111110;
                  delay(20sec);
      break ;
    
      case 2:   LED=11111101;
                  delay(20sec);
      break ; 
      case 3:   LED=11111011;
                  delay(20sec);
      break ;
     case 4:   LED=11110111;
                  delay(20sec);
                  count=0;
      break ;
     
}
else
{
     {LED=11111110;}
delay(2sec);
{LED=11111101;}
delay(2sec);
{LED=11111011;}
delay(2sec);
{LED=11110111;}
delay(2sec);
{LED=11101111;}
delay(2sec);
}
 
Looks ok... Does it work?

Why are the last few LED statements in curly brackets???
yes sir it work fine with few changes .
but have some eshus.
when press sw mcu inter count mode & hold there till delay time .if agen i press sw during hold time mcu not goes next counter mode . it goes when delay time complete there .
whats wrong with me .
 
Last edited:
it have some some problem .
when press sw mcu inter count mode & hold there till delay time .if agen i press sw during hold time mcu not goes next counter mode . it goes when delay time complete there .
whats wrong with me .
PHP:
// Global includes
#include <xc.h>
#include <delay.h>
__CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF);

#define LED PORTC

int count;
#define sw  RA0

#define _XTAL_FREQ   4000000 //Internal clock is 4MHz//
/* THE main source code Start here*/
void main(void)
{unsigned int count_flag = 0;
       
    CMCON=0x7;          // disable the comparator
    ANSEL=0x00;         // all pin are Digital
    TRISA=0b00001001;   //only RA3 & RA0 is input
    TRISC=0b00000000;   //
    PORTC=0b11111111;
    while (1) {  //loop forever
    if(sw==1)//(sw_pressed)
{count_flag=1;}
if(count_flag==1){
   count++;
  switch(count)
  {   case 1:   {LED=0b00000001;}
                   delay_ms(5000);
      break ;
  
      case 2:   {LED=0b00000010;}
                   delay_ms(5000);
      break ;
      case 3:   {LED=0b00000100;}
                   delay_ms(5000);
      break ;
     case 4:   {LED=0b00001000;}
                   delay_ms(5000);
      break ;
     case 5:   {LED=0b00010000;}
                   delay_ms(5000);
       break ;
     case 6:   {LED=0b00100000;}
                   delay_ms(5000);
                  count=0;
      break ;
        default :    
                    break;   
}
}
//else
if(sw==0)//(sw_unpressed)
{{count_flag=0;}
if(count_flag==0)
{LED=0b00000001;}
delay_ms(200);
{LED=0b00000010;}
delay_ms(200);
{LED=0b00000100;}
delay_ms(200);
{LED=0b00001000;}
delay_ms(200);
{LED=0b00010000;}
delay_ms(200);
{LED=0b00100000;}
delay_ms(200);
}  
}  
}
}
 
When you get in a delay all you do is count if you want to change when switch is pressed you need to have the switch read in a interrupt.
 
When you get in a delay all you do is count if you want to change when switch is pressed you need to have the switch read in a interrupt.
i has not understand interrupt fundamental .
so pl requested to u give me an example .pl
 
No need for interrupts.... If you break your delays up into manageable functions
lets say SW is your switch.... SWbuff is your buffer..

C:
int DelayMs(int x)
   {
   while(x--)
     {
     if(SW != SWbuff)
         {
          SW = SWbuff;
         return 1;
         }
      __delay_us(800);
     }
     return 0;
   }

This way your switch condition will be monitored while in the delay ( well at least every mS ) you will need to tweak the us delay as I'm not sure of the "if" overhead...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top