0

"I'm trying to use the button that when it is pressed, it triggers to send a string message via UART, but upon running the code, it always detect the button to be "always pressed" which is not ideal. I need help fixing my code. Thank you very much.

I'm using EasyPIC fusion v7. The device is P32MX795F512L @80MHz. I'm using the MikroC pro for PIC32 as my IDE."

"Button and UART library is used for this code."

char read;
unsigned int oldstate;


void main() {
  AD1PCFG = 0XFFFF;             //SETTING AN INPUTS AS DIGITAL I/O
  JTAGEN_bit = 0;               //disable JTAG


  UART2_Init(9600);
  Delay_ms(1000);
  TRISA = 1;                    //setting All portA as inputs

  UART_Set_Active(&UART2_Read, &UART2_Write, &UART2_Data_Ready, &UART2_Tx_Idle);    //Sets UART2 as active
  UART_Write_Text("UART is now ready.");
  UART_Write(13);
  UART_Write(10);

   while(1)
   {
    if (Button(&PORTA, 15, 1 ,1))            //detect if button is pressed
    {
        UART_Write_Text("Button is pressed.");
        UART_Write(13);
        UART_Write(10);
        oldstate = 1;

    }

    if (oldstate && Button(&PORTA, 15, 1, 0)) //detect from logic 1 to 0
    {
        UART_Write_Text("Button is pressed again.");
        UART_Write(13);
        UART_Write(10);
        oldstate = 0;

    }
   }
}

"I've only learned how UART works and how the Button library works.

I expect the output to be 'Button is pressed' when the button is pressed first, then "Button is pressed again" when I press the same button again.

The output for the code is always 'Button is pressed' and prints continuously"

Dorokun192
  • 63
  • 7
  • Does it stop when the button is pressed? Could be the type of button. – Deanie Jul 09 '19 at 04:06
  • Can you provide a link to the Button and Uart librarys used – Rishikesh Raje Jul 09 '19 at 04:31
  • Are you sure you are not simply missing the (single) iteration printing the "again" line among all the others? You reset `oldstate` in the block printing "again". – Hulk Jul 09 '19 at 04:56
  • it does not stop. My solution for now is to specify which port is the input (RA15). I'll try your suggestions tomorrow. Thank you very much – Dorokun192 Jul 09 '19 at 09:12

2 Answers2

0

The main problem with your code is that this line:

UART_Write_Text("Button is pressed again.");

should in fact read:

UART_Write_Text("Button is RELEASED.");

Other than that you might be having a hardware issue if you're not tying your RA15 pin to GND through a pull-down resistor. You could also use the internal pull-ups on your PIC.

Marcos G.
  • 3,371
  • 2
  • 8
  • 16
  • The solution that I've found for now is setting a specific port (RA15) as an input. But tomorrow I'll try setting the internal pull-up resistors. Thank you very much. – Dorokun192 Jul 09 '19 at 09:10
  • Now that you mention it, I think your `TRISA=1` should be `TRISA = 0b11111111` or `TRISA = 0xFF`, right? – Marcos G. Jul 09 '19 at 09:16
  • Yes, I have learned the difference between using "1" and using "0xFFFF". It seems that I have to use "1" only when specifying the Port that I'm going to use. Thank you very much for your support! – Dorokun192 Jul 09 '19 at 23:32
0

The problem is here:

TRISA = 1;

This is simular to TRISA = 0x0001; and will only make the Port A0 an input. You had to write:

TRISA = 0xFFFF;`
Mike
  • 4,041
  • 6
  • 20
  • 37
  • 1
    Yes this is what I've learned for now. I think the proper way of using "1" is to specify the port that I'll use (TRISA15_bit = 1). Thank you very much for this answer. I'm still learning and I think I have a long way to go – Dorokun192 Jul 09 '19 at 23:30