Menu

all about electronic and microcontrollers

Sunday, October 17, 2010

Lesson nr.06-Push Button

Hardware setup:
In this experiment we will make a demonstration with button library. The PORTB is used for this experiment (as output, for a leds) and RA6 (as imput, for button S2).
As usualy, button S1 and (pull-up) resistance with value of 4.7 K will provide the manual reset mode to the microcontroller. This arangement provides a standard schematic when we whant to include a button as input, current mode is with pull-up resistance, but also exist and with pull-down resistance which will not be used in our experiments, at least for the moment.
ICSP connector provides the connection between the programmer (PicKit 2) and microcontroller (PIC16F628A).



Circuit Diagram:
For those who want to build it on their own breadboard or other platform, here is the electronic scheme built in Eagle Cad, free version:


Software:
Here's a page from Button Library that specify buttons routines:


And as usualy, here is the C program written for MikroC PRO for PIC 2010 (version v4.15).
/*
'*******************************************************************************
'  Lesson nr.06:
'          Push Button
'  Written by:
'          Aureliu Raducu Macovei, 2010.
'  Description:
'          In this experiment we will make a demonstration with button library
'          The PORTB is used for this experiment (as output, for a leds)
'          and RA6 (as imput, for button).
'  Test configuration:
'    MCU:                        PIC16F628A
'    Test.Board:                 WB-106 Breadboard 2420 dots
'    SW:                         MikroC PRO for PIC 2010 (version v4.15)
'  Configuration Word
'    Oscillator:                 INTOSC:I/O on RA.6, I/O on RA.7
'    Watchdog Timer:             OFF
'    Power up Timer:             Disabled
'    Master Clear Enable:        Enabled
'    Browun Out Detect:          Enabled
'    Low Voltage Program:        Disabled
'    Data EE Read Protect:       Disabled
'    Code Protect:               OFF
'*******************************************************************************
*/
unsigned short oldstate;
void main()
{                                     // Main;
 TRISB = 0;                           // Set PORTB direction to be output;
 PORTB = 0xFF;                        // Turn OFF LEDs on PORTA;

 TRISA6_bit = 1;                      // Set PORT RB1 input for button;
 oldstate = 0;                        // Define value of oldstate;

do {
    if (Button(&PORTA,6,1,1)) oldstate = 1;
    Delay_ms(100);                                              //delay for prevent crash
    if (oldstate &&Button(&PORTA,6,1,0)) {
                                          PORTB = ~PORTB;       //invert state of PORTB
                                          oldstate = 0;
                                          }
    } while(1);                                                 //Infinite loop.
}

5 comments:

  1. Hi.
    how the treatment of Button. if (Button(&PORTA, 3, 10, 1)) ??
    I do not get in mikroC. buildingg
    I look at asm/

    ReplyDelete
  2. Hi Erik,
    According to mikroc button library, we have this manner:
    Button(&PORTX, pin number, time, activ state)) .
    Parameter port specifies the location of the button; parameter pin is the pin number on designated port and goes from 0..7; parameter time is a debounce period in milliseconds; parameter active_state can be either 0 or 1, and it determines if the button is active upon logical zero or logical one.

    For example:
    Button(&PORTB, 0, 1, 1) = Detect logical one
    Button(&PORTB, 0, 1, 0) = Detect one-to-zero transition

    I hope that helps you this info.

    ReplyDelete
  3. i want to use the long press method for a task currently using single and double press of push button now i want to press a button for given interval and turn the LED on kindly help me through as i am stuck tried to use debounce time and timer but cant get it please help
    regards
    yousaf

    ReplyDelete
    Replies
    1. sorry Can you help me to understand more about parameters zero,one,one or zero,one,zero

      Delete
  4. I want to understand more about parameters zer0,one,one or zero,one,zero

    ReplyDelete

If you do not understand something, or if you make some aplication helped by this blog, let me know.