Button and UART

General discussion on mikroC PRO for PIC32.
Post Reply
Author
Message
Dorokun
Posts: 12
Joined: 09 Jul 2019 02:58

Button and UART

#1 Post by Dorokun » 09 Jul 2019 03:03

Hi guys,

I'm trying to implement a code that when a button is pressed, it will trigger to send a message via uart.
Unfortunately, my code always detect the button as "always pressed" resulting to continuous send of message. I only want it to send everytime I press the button.

This is my code below:

Code: Select all

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;

    }
   }
}



hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Button and UART

#2 Post by hexreader » 09 Jul 2019 12:43

Change:

Code: Select all

  TRISA = 1;                    //setting PORTA bit 0 as input, rest outputs
To:

Code: Select all

  TRISA = 0xffff;                    //setting All PORTA as inputs
Start every day with a smile...... (get it over with) :)

Dorokun
Posts: 12
Joined: 09 Jul 2019 02:58

Re: Button and UART

#3 Post by Dorokun » 10 Jul 2019 00:28

hexreader wrote:Change:

Code: Select all

 TRISA = 1; //setting PORTA bit 0 as input, rest outputs
To:

Code: Select all

 TRISA = 0xffff; //setting All PORTA as inputs
This is really helpful! Thank you very much!
I'll try to learn the difference of using HEX instead of just using "1".

Post Reply

Return to “mikroC PRO for PIC32 General”