PIC18f programming using mikroc

Thread Starter

pr123

Joined Mar 13, 2015
30
Hi,
I want to program PIC18f such that i can switch on/off LED for different timings. Example if push button1 pressed the LED is on for 1 sec and off for 2 and this continues unless another button pressed which changes the time example 2 sec on and 4 sec off. I am doing this using delays however, the problem is that pic doesnt recognize when i press second button while first routine is on. that is during delay time. Please tell me where i'm going wrong. Thank you
 

R!f@@

Joined Apr 2, 2009
9,918
Welcome to AAC.

During delay time μC will not respond to inputs.
I do not know yet how to go around tht but I know there are others here who can do tht.
You have to wait a while for them to find your thread and post the code you already wrote.
 

MCU88

Joined Mar 12, 2015
358
Hi,
I want to program PIC18f such that i can switch on/off LED for different timings. Example if push button1 pressed the LED is on for 1 sec and off for 2 and this continues unless another button pressed which changes the time example 2 sec on and 4 sec off. I am doing this using delays however, the problem is that pic doesnt recognize when i press second button while first routine is on. that is during delay time. Please tell me where i'm going wrong. Thank you
Hello....

Try having your delay broken down in an loop.

Code:
   // Delay
   for (z = 0; z < 100; z++)
   {
      Delay_ms(1);
      checkInputsButons();
   }
The delay is 100mS + the time taken for the for next loop to execute, but stuck (non-responsive) -- to other functions for only 1mS at a time.
 

MCU88

Joined Mar 12, 2015
358
I have an project here that is basically just four buttons and four LEDs.

What can be done with just four buttons and four LEDs?




Have a read through some code here with button de-bouncing:

Code:
// 16 in 1 LEDs 'n Buttons FUN

unsigned short nStart = 0;
unsigned short dir = 0;
unsigned short keyUp = 0;
unsigned short keyDown = 0;
unsigned short keyPress = 0;
unsigned int randomNo = 0;

unsigned short i = 0;
unsigned short j = 0;
unsigned short k = 0;
unsigned short e = 0;
unsigned short s = 0;
unsigned short simon[32]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                           0,0,0,0,0,0,0,0,0,0,0,0,0,0};


#define dSW1 PORTA.F2
#define dSW2 PORTA.F3
#define dSW3 PORTA.F0
#define dSW4 PORTA.F1

#define sw01 PORTB.F3
#define sw02 PORTB.F2
#define sw03 PORTB.F1
#define sw04 PORTB.F0

#define ld01 PORTB.F4
#define ld02 PORTB.F5
#define ld03 PORTB.F6
#define ld04 PORTB.F7

progressiveLed(int dir)
{
   i++;

   if (dir == 0)
   {
      switch (i)
      {
         case 1: ld01 = 1; break;
         case 2: ld02 = 1; break;
         case 3: ld03 = 1; break;
         case 4: ld04 = 1; break;
      }
   }
   else
   {
      switch (i)
      {
         case 1: ld04 = 1; break;
         case 2: ld03 = 1; break;
         case 3: ld02 = 1; break;
         case 4: ld01 = 1; break;
      }
   }

   if (i == 5)
   {
      i = 0;
      ld01 = 0;
      ld02 = 0;
      ld03 = 0;
      ld04 = 0;
   }

   Delay_ms(500);
}

void timer(int period)
{
   do
   { // Wait for user to press a key
   } while (sw01 == 1 && sw02 == 1 && sw03 == 1 && sw04 == 1);

   // All leds on
   ld01 = 1;
   ld02 = 1;
   ld03 = 1;
   ld04 = 1;
   
   // Timer interval
   if (period == 0)
   {
      Delay_ms(10000); // 1 min
   }
   else
   {
      delay_ms(30000); // 3 min
   }
   
   // All leds off
   ld01 = 0;
   ld02 = 0;
   ld03 = 0;
   ld04 = 0;
}

void randomLed(int decision)
{
   randomNo = rand();

   if (decision == 1)
   {
      do
      {
      } while (sw01 == 1 && sw02 == 1 && sw03 == 1 && sw04 == 1);
     
      for (j = 0; j < 10; j++)
      {
         for (i = 0; i < 4; i++)
         {
            ld01 = 0;
            ld02 = 0;
            ld03 = 0;
            ld04 = 0;

            Delay_ms(40);
         
            switch (i)
            {
               case 0: ld01 = 1; break;
               case 1: ld02 = 1; break;
               case 2: ld03 = 1; break;
               case 3: ld04 = 1; break;
            }
         
            Delay_ms(40);
         }
      }
   }

   ld01 = 0;
   ld02 = 0;
   ld03 = 0;
   ld04 = 0;

   if (randomNo <  8000)
   {
      ld01 = 1;
   }
   else if (randomNo < 16000)
   {
      ld02 = 1;
   }
   else if (randomNo < 24000)
   {
      ld03 = 1;
   }
   else
   {
      ld04 = 1;
   }
   
   if (decision == 0)
   {
      Delay_ms(500);
   }
}

void upBCDCounter()
{
   i++;
   j = (i * 16);
   PORTB = j;

   if (i > 15)
   {
      i = 0;
   }

   Delay_ms(500);
}

void dnBCDCounter()
{
   i++;
   j = (240 - (i * 16));
   PORTB = j;

   if (i > 15)
   {
      i = 0;
   }

   Delay_ms(500);
}

void onOffButtons()
{

   if (keyDown == 0)
   {
      if (sw01 == 0)
      {
         ld01 = ! ld01;
         keyDown = 1;
      }
      if (sw02 == 0)
      {
         ld02 = ! ld02;
         keyDown = 1;
      }
      if (sw03 == 0)
      {
         ld03 = ! ld03;
         keyDown = 1;
      }
      if (sw04 == 0)
      {
         ld04 = ! ld04;
         keyDown = 1;
      }
   }

   if (sw01 == 1)
   {
      if (sw02 == 1)
      {
         if (sw03 == 1)
         {
            if (sw04 == 1)
            {
               keyDown = 0;
            }
          }
       }
    }

   // Debounce period
   Delay_ms(50);
}

void momentaryButtons()
{
   Delay_ms(50); // Debounce period
   ld01 =! sw01;
   ld02 =! sw02;
   ld03 =! sw03;
   ld04 =! sw04;
}

void flasher()
{
   Delay_ms(500);
   
   ld01 = 0;
   ld02 = 0;
   ld03 = 0;
   ld04 = 0;

   Delay_ms(500);

   ld01 = 1;
   ld02 = 1;
   ld03 = 1;
   ld04 = 1;
}

void alternateFlasher()
{
   Delay_ms(500);

   ld01 = 1;
   ld02 = 1;
   ld03 = 0;
   ld04 = 0;

   Delay_ms(500);

   ld01 = 0;
   ld02 = 0;
   ld03 = 1;
   ld04 = 1;
}


void ledChaser(int dir)
{
   if (i == 0)
   {
      if (dir == 0)
      {
         j = 16;
      }
      else
      {
         j = 128;
      }
   }
   else
   {
      if (dir == 0)
      {
         j = j << 1;
      }
      else
      {
         j = j >> 1;
      }
   }

   i++;
   PORTB = j;

   if (i == 4)
   {
      i = 0;
   }

   Delay_ms(500);
}

void ledScanner()
{
   if (i == 3)
   {
      i = 0;
      dir++;

      if (dir == 2)
      {
         dir = 0;
      }
   }

   if (nStart == 0)
   {
      j = 16;
      nStart = 1;
   }

   PORTB = j;

   if (dir == 0)
   {
      j = j << 1;
   }
   else
   {
      j = j >> 1;
   }

   Delay_ms(250);
   i++;
}

void simonSays()
{

   ld01 = 0;
   ld02 = 0;
   ld03 = 0;
   ld04 = 0;

   for (e = 0; e < k; e++)
   {
      randomNo = rand();

      if (randomNo <  8000)
      {
         ld01 = 1;
         simon[e] = 1;
      }
      else if (randomNo < 16000)
      {
         ld02 = 1;
         simon[e] = 2;
      }
      else if (randomNo < 24000)
      {
         ld03 = 1;
         simon[e] = 3;
      }
      else
      {
         ld04 = 1;
         simon[e] = 4;
      }

      Delay_ms(1000);

      ld01 = 0;
      ld02 = 0;
      ld03 = 0;
      ld04 = 0;

      Delay_ms(1000);

   }

   // User to repeat
   for (e = 0; e < k; e++)
   {
      do
      {
         if (keyDown == 0)
         {
            if (sw01 == 0)
            {
               j = 1;
               ld01 = 1;
               keyDown = 1;
            }
            if (sw02 == 0)
            {
               j = 2;
               ld02 = 1;
               keyDown = 1;
            }
            if (sw03 == 0)
            {
               j = 3;
               ld03 = 1;
               keyDown = 1;
            }
            if (sw04 == 0)
            {
               j = 4;
               ld04 = 1;
               keyDown = 1;
            }
         }

         if (keyDown == 1)
         {
            if (sw01 == 1)
            {
               if (sw02 == 1)
               {
                  if (sw03 == 1)
                  {
                     if (sw04 == 1)
                     {
                        keyDown = 0;
                        keyPress = 1;

                        ld01 = 0;
                        ld02 = 0;
                        ld03 = 0;
                        ld04 = 0;
                     }
                  }
               }
            }
         }
      } while (keyPress == 0);

      keyPress = 0;
      Delay_ms(200);
     
      if (simon[e] != j)
      {
         if (j != 0)
         {
            for (s = 0; s < 5; s++)
            {
               Delay_ms(500);
               ld01 = 1;
               ld02 = 1;
               ld03 = 1;
               ld04 = 1;

               Delay_ms(500);
               ld01 = 0;
               ld02 = 0;
               ld03 = 0;
               ld04 = 0;
            }
         }
         k = 0;
         break;
      }
      j = 0;
   }
   k++;
}

void main()
{

  // Configuration of ports etc ...

  CMCON      = 7;       // Disable analog comparators
  TRISA      = 0x0F;    // 4 inputs 4 outputs
  TRISB      = 0x0F;    // 4 inputs 4 outputs
  PORTA      = 0x00;    // Init port, all pins low
  PORTB      = 0x00;    // Init port, all pins low

  while(1)
  {
     if (dSW1 == 1 && dSW2 == 1 && dSW3 == 1 && dSW4 == 1)
     {
        momentaryButtons();
     }
     else if (dSW1 == 0 && dSW2 == 1 && dSW3 == 1 && dSW4 == 1)
     {
        ledChaser(0);
     }
     else if (dSW1 == 1 && dSW2 == 0 && dSW3 == 1 && dSW4 == 1)
     {
        ledChaser(1);
     }
     else if (dSW1 == 0 && dSW2 == 0 && dSW3 == 1 && dSW4 == 1)
     {
        ledScanner();
     }
     else if (dSW1 == 1 && dSW2 == 1 && dSW3 == 0 && dSW4 == 1)
     {
        onOffButtons();
     }
     else if (dSW1 == 0 && dSW2 == 1 && dSW3 == 0 && dSW4 == 1)
     {
        upBCDCounter();
     }
     else if (dSW1 == 1 && dSW2 == 0 && dSW3 == 0 && dSW4 == 1)
     {
        dnBCDCounter();
     }
     else if (dSW1 == 0 && dSW2 == 0 && dSW3 == 0 && dSW4 == 1)
     {
        randomLed(0);
     }
     else if (dSW1 == 1 && dSW2 == 1 && dSW3 == 1 && dSW4 == 0)
     {
        progressiveLed(0);
     }
     else if (dSW1 == 0 && dSW2 == 1 && dSW3 == 1 && dSW4 == 0)
     {
        progressiveLed(1);
     }
     else if (dSW1 == 1 && dSW2 == 0 && dSW3 == 1 && dSW4 == 0)
     {
        simonSays();
     }
     else if (dSW1 == 0 && dSW2 == 0 && dSW3 == 1 && dSW4 == 0)
     {
        randomLed(1); // Decision Maker
     }
     else if (dSW1 == 1 && dSW2 == 1 && dSW3 == 0 && dSW4 == 0)
     {
        timer(0);
     }
     else if (dSW1 == 0 && dSW2 == 1 && dSW3 == 0 && dSW4 == 0)
     {
        timer(1);
     }
     else if (dSW1 == 1 && dSW2 == 0 && dSW3 == 0 && dSW4 == 0)
     {
        flasher();
     }
     else if (dSW1 == 0 && dSW2 == 0 && dSW3 == 0 && dSW4 == 0)
     {
        alternateFlasher();
     }
  }
}
 

Attachments

ErnieM

Joined Apr 24, 2011
8,377
What can be done with just four buttons and four LEDs?
With just four buttons and four LEDs I could, dare I say, RULE THE WORLD.

Or just hijack someone else's thread. Please begin your own topic to discuss your project board.

And welcome to the forums!
 

MCU88

Joined Mar 12, 2015
358
With just four buttons and four LEDs I could, dare I say, RULE THE WORLD.

Or just hijack someone else's thread. Please begin your own topic to discuss your project board.

And welcome to the forums!
Arh it is applicable to topic and I think the OP will appreciate the code. Probably trying to achieve the same thing. But perhaps not on the same line of thought as in 'what the bloody hell can I make with just 33 one-cent parts'

Thanks for the welcome! ;)
 
Top