I dont understand why button input doesn't work on PIC16F628

General discussion on mikroC.
Post Reply
Author
Message
tol87
Posts: 16
Joined: 08 Jul 2011 17:36

I dont understand why button input doesn't work on PIC16F628

#1 Post by tol87 » 13 Jul 2011 01:46

I have a circuit with 4 LEDs connected to PORTB via 4.7K resistors and a push button wired between the ground and pin17 (RA0) of PIC16F628. There is a pull-up resistor between pin17 and the 5v supply.

I run this program on the device and expect the LED patterns to change, indicating that a switch was pressed.
When I compile, write and test, PORTB always appears to be 0b00010000. Nothing seems to change weather the switch is on or off. Why? When I debug, the default state of RA0 or PORTA.F0 is 0 not 1 as described in many examples.

It may be that I accidentally powered this PIC device with 12 volts but there was no program written on it at that time.
I am trying to understand if this is hardware or software problem.

Code: Select all

void main()
{

TRISA = 1;        // port a as input
TRISB = 0;        // port b as output
PORTB = 0;        // Turn OFF all LEDs

for(;;)        // Endless loop
{
    if(PORTA.F0 == 0)  // Is switch pressed ?
    {
    PORTB = 0b00010000; // turns on pins - port B
    }
    else if (PORTA.F0 == 1)
    {
    PORTB = 0b10110000; // turns on pins - port B
    }
}
}

Thank you.

PS:

My new declaration for this program looks like this:

Code: Select all

INTCON = 0b00000000;
PORTA = 1;
TRISA = 1;        // port a as input
CMCON = 0b111;
CMCON = 1;
TRISB = 0;
PORTB = 0;        // Turn OFF all LEDs
but it didn't solve the problem.


-------------------------------------------------------------------------------------------------------------------
I am all set.
I am ashamed to tell what was the problem.
It was me copying my program folder to desktop instead of creating a shortcut. Therefore I kept uploading the same malfunctioning .hex into my microcontroller.
Not a language or a hardware issue.
Thank you for your time.

tol87
Posts: 16
Joined: 08 Jul 2011 17:36

Re: I dont understand why button input doesn't work on PIC16

#2 Post by tol87 » 14 Jul 2011 04:14

Code: Select all

/*
Pick the pair of IF statements that you like and see how the PORTB
is toggled on and off by the button attached to RA0 pin of the PORTA
of PIC16F628 microcontroller.

The hardware circuitry for the button may cause instability and therefore is a
ubject for research.

The value of the pullup resistor determines the stability.
The delay is necessary to supress the contact bounce effect or to 'debounce' the button.

The software part is here and it is tested to work in 3 combinations of IF statements.

enjoy

Created by Vladimir Tolskiy*/


bit oldstate;
void main()
{
short oldstate=0;
CMCON = 0x07;                   // Disable comparators
TRISA.F0 = 1;                     //pin RA0 as input

TRISB = 0x00;                     // PORTB as output
PORTB = 0x00;                    // PORTB pins are all "off"

do {
    //if (Button(&PORTA, 0, 1, 1))     // Detect logical one
    //if(PORTA.F0 == 0)
    if ((PORTA & 0b00000001) == 0b00000000)
    {
    Delay_ms(10); // "debouncing" mechanical contacts
      oldstate = 1;                          // Update flag
    }
    //if (oldstate && Button(&PORTA, 0, 1, 0))         // Detect one to zero change
    //if (oldstate && PORTA.F0 == 1)
    if (oldstate && (PORTA & 0b00000001) == 0b00000001)
    {
    
    Delay_ms(10); // "debouncing" mechanical contacts
      PORTB = ~PORTB;                   // Invert PORTB
      oldstate = 0;                          // Update flag
    }
  } while(1);                                  // Endless loop
}

Post Reply

Return to “mikroC General”