1

As a C beginner, I struggle alot to solve this problem of mine :

I am working on a project where I basicly have to program a PIC (microchip) in C using the mikroC platform.

What I'm trying to achieve is with only one switch/button, I'll have to switch from three different "modules" (as I call them "modules", they correspond to different lighting effects created by LEDs) depending on the time spent pressing the switch/button. In my case, after 500 ms module 1 is up, after 1500 ms module 2 goes up and after 3500 ms module 3 is up (and the whole thing has to be in an infinitely repeating loop since I have to be able to change a module at ANYTIME during the operation).

My only problem is getting the timer/clock to start runnning at the beginning of the program and keep counting time until it reaches a stop signal (like the end of a loop or something).

This may not be appropriate to ask to this community but here I am nonetheless.

I am concious that this is more of a 'algorithmics/logic' problem than anything but I have been trying for the last week without any clue on how to get past this problem...

No results since the code isn't ready at all.

glue
  • 85
  • 6
  • Even if the code is not ready yet, it is difficult to give a reasonable answer without seeing your code. So, please, [edit] your question and add your unfinished code. Format it as a code block, e.g. using the `{}` tool of the editor field. – Bodo Apr 18 '19 at 15:21
  • It seems that you are trying to ask which register / function in mikroC on the PIC you need to read / call to get the current time in milliseconds. https://circuitdigest.com/microcontroller-projects/pic-microcontroller-timer-tutorial. Of course this can't be implemented just using delays (if what you mean by delay is sit in a busy wait loop for a certain amount of time). You need to time how long the button is pushed, perform switch de-bouncing, etcetera. – fdk1342 Apr 18 '19 at 16:42

2 Answers2

1

There is a mikroC library for handling button presses. The following example (from the link.) provides a skeletal example of detecting a button push...

bit oldstate;                                    // Old state flag

void main() {

  ANSEL  = 0;                                    // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                                  // Disable comparators
  C2ON_bit = 0;

  TRISB0_bit = 1;                                // set RB0 pin as input

  TRISC = 0x00;                                  // Configure PORTC as output
  PORTC = 0xAA;                                  // Initial PORTC value
  oldstate = 0;

  do {
    if (Button(&PORTB, 0, 1, 1)) {               // Detect logical one
      oldstate = 1;                              // Update flag
    }
    if (oldstate && Button(&PORTB, 0, 1, 0)) {   // Detect one-to-zero transition
      PORTC = ~PORTC;                            // Invert PORTC
      oldstate = 0;                              // Update flag
    }
  } while(1);                                    // Endless loop
}

There is also a collection of MicroE Examples which include timer examples such as these, and this one. Each of these provide code base examples that might be adapted to create a function that can be wrapped around sections in the button press code to obtain time durations.

I hope this helps.

ryyker
  • 22,849
  • 3
  • 43
  • 87
1

When the button gets pressed, you need to initialize timer module of respective microcontroller check the interrupt flag, and put it in a loop. increment the value until the button is released, and after that compare the resultant value with the specified limits, this was you can decide for how long a button is pressed, and consecutively you will be able to switch your output. This is the only way if you want to precisely monitor the delay without interrupting the current execution of the program.