Trouble checking status of a button

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Anilsone.ca
Posts: 37
Joined: 08 Nov 2010 02:50

Trouble checking status of a button

#1 Post by Anilsone.ca » 02 Dec 2010 10:20

Hi,
I have this code, to check the status of a button.
The problem is that when I press the button it does not work. does not change state.

Code: Select all

/*
Author:  Anilson Cardoso
Project: Button function
Date:    December 1, 2010
*/


void main() {
  ANSEL = 0x00;          // Configure AN pins as digital
  PORTA = 0x00;          // all pins in PORTA are OFF
  TRISA = 0x01;          // All pins in PORTA are output except RA0
                         // binary order 0000 0001

  do {

     if(RA0_bit)         //
     {RA1_bit = 0;}      // if bit RA0 is true then RA1 turns off
     else
     {RA1_bit = 1;}      // if bit RA0 is false then RA1 turns On
     
      } while(1);
}
RA1 is connected to a 470 ohm, then to LED and then to GND.
RA0 is connected to a N.O. (Normally Open) Swicth and then to GND.

Why is that i can not detect the button?

I even tried to run some version

Code: Select all

/*
Author:  Anilson Cardoso
Project: Button function
Date:    December 1, 2010
*/


void main() {
  ANSEL = 0x00;          // Configure AN pins as digital
  PORTA = 0x00;          // all pins in PORTA are OFF
  TRISA = 0x01;          // All pins in PORTA are output except RA0
                         // binary order 0000 0001
                         
  PORTB=0x00;
  TRISB=0xFF;

  do {

     if(RA0_bit)         //
     {RB1_bit = 0;}      // if bit RA0 is true then RB1 turns off
     else
     {RB1_bit = 1;}      // if bit RA0 is false then RB1 turns On
     
      } while(1);
}

but this version I couldnt compile. I got this error message.

0 1 mikroCPIC1618.exe -MSF -DBG -pP16F688 -DL -O11111114 -fo8 -N"C:\Users\Anilson Cardoso\Documents\Electrical Circuit\Pic_Projects\Button.mcppi" -SP"C:\Program Files (x86)\Mikroelektronika\mikroC PRO for PIC\defs\" -SP"C:\Program Files (x86)\Mikroelektronika\mikroC PRO for PIC\Uses\P16\" -SP"C:\Users\Anilson Cardoso\Documents\Electrical Circuit\Pic_Projects\" "Button.c" "__Lib_Math.mcl" "__Lib_MathDouble.mcl" "__Lib_System.mcl" "__Lib_Delays.mcl" "__Lib_CType.mcl" "__Lib_CString.mcl" "__Lib_CStdlib.mcl" "__Lib_CMath.mcl" "__Lib_Conversions.mcl" "__Lib_Sprinti.mcl" "__Lib_Sprintl.mcl" "__Lib_Time.mcl" "__Lib_Trigonometry.mcl" "__Lib_Button.mcl" "__Lib_Manchester.mcl" "__Lib_OneWire.mcl" "__Lib_PS2.mcl" "__Lib_Sound.mcl" "__Lib_SoftI2C.mcl" "__Lib_SoftSPI.mcl" "__Lib_SoftUART.mcl" "__Lib_ADC_A_B.mcl" "__Lib_EEPROM.mcl" "__Lib_FLASH_R.mcl" "__Lib_UART_c45.mcl" "__Lib_LcdConsts.mcl" "__Lib_Lcd.mcl" "__Lib_RS485.mcl"
0 1138 Available RAM: 240 [bytes], Available ROM: 4096 [bytes]
0 126 All files Preprocessed in 47 ms
0 122 Compilation Started Button.c
14 324 Undeclared identifier 'PORTB' in expression Button.c
15 324 Undeclared identifier 'TRISB' in expression Button.c
20 324 Undeclared identifier 'RB1_bit' in expression Button.c
22 324 Undeclared identifier 'RB1_bit' in expression Button.c
0 102 Finished (with errors): 02 Dec 2010, 04:13:46 Button.mcppi
Can I have any help??

Anilsone.ca
Posts: 37
Joined: 08 Nov 2010 02:50

Re: Trouble checking status of a button

#2 Post by Anilsone.ca » 02 Dec 2010 11:38

Hi again,
the microchip I am using is PIC16F88.

User avatar
ranko.rankovic
Posts: 433
Joined: 11 Jun 2010 09:22

Re: Trouble checking status of a button

#3 Post by ranko.rankovic » 02 Dec 2010 12:58

Hello Anilsone.ca,

First program code is OK. But you'll need to pull up or down Button pin over resistor to Vcc or GND. Here is how to do Pull-Down to GND.
pin.PNG
pin.PNG (2.45 KiB) Viewed 1373 times
For Pull-Up, just change positions of Resistor and Switch.

In second code, for PORTB to be output, you'll need to put TRISB = 0x00; instead 0xFF.

Best regards
Ranko Rankovic
mikroElektronika [Support Department]

Post Reply

Return to “mikroC PRO for PIC General”