Author Topic: MikroC Pro Push Button sense program  (Read 1846 times)

0 Members and 1 Guest are viewing this topic.

Offline asadulhuqTopic starter

  • Contributor
  • Posts: 36
  • Country: bd
MikroC Pro Push Button sense program
« on: August 22, 2020, 07:27:36 am »
Dear All,
I want to understand following button sense program, specifically the meaning of the following part of the program-
(Button(&PORTB, 0, 1, 1)). Please consider that I am new to PIC programming. Thanks in advance.

///////////////////////////////////////////////////////////////////////////
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);
//////////////////////////////////////////////////////////////////

Source of the code: https://download.mikroe.com/documents/compilers/mikroc/pic/help/button_library.htm
« Last Edit: August 22, 2020, 08:12:47 am by asadulhuq »
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12864
Re: MikroC Pro Push Button sense program
« Reply #1 on: August 22, 2020, 08:05:38 am »
From the context, Button(&PORTB, 0, 1, 1) obviously returns something to do with the state of a button switch attached to a Port B pin.

Mikroeletronika's MikroC libraries are closed source.  Short of disassembling your code and studying what the Button() function compiles to (difficult and confusing even for an expert), there is no way to gain insight into its internals.

The best you can do is look it up in the manual,  check the permitted parameter values and their meanings and treat it as a black box function with a 'magic' invocation.
 
The following users thanked this post: asadulhuq

Offline ggchab

  • Frequent Contributor
  • **
  • Posts: 276
  • Country: be
Re: MikroC Pro Push Button sense program
« Reply #2 on: August 22, 2020, 08:57:22 am »
I found this
Code: [Select]
unsigned short Button(unsigned short *port, unsigned short pin, unsigned short time, unsigned short active_state);on this page https://download.mikroe.com/documents/compilers/mikroc/pic/help/button_library.htm

So, in your code, the button seems to be connected on RB0 (2 first parameters: &PORTB = port address ; 0 = line of the port).
The third parameter is a debounce period in ms .
The last parameter is a button state. If the current state is that one, the function returns 255, otherwise, 0
 
The following users thanked this post: asadulhuq

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12864
Re: MikroC Pro Push Button sense program
« Reply #3 on: August 22, 2020, 09:16:35 am »
That seems quite horrible.  Does that mean that every time you invoke Button() it waits around for the debounce period to expire, or, *worse* does it extend the period it waits by the specified debounce period every time it detects a transition?  If so, good luck maintaining a responsive main loop if you are polling a lot of buttons and need longer debounce periods!

IMHO, as soon as you have gained enough skills to move on to writing serious programs, throw out that library function and write your own multi-button debouncer that runs in the background off a timer interrupt.  Debouncing can be far from simple.  See  Jack Ganssle's article A Guide to Debouncing.  Part one defines the problem, and part two concentrates on hardware and software solutions.
 
The following users thanked this post: asadulhuq


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf