Output won't respond to button input (Microchip DV164132 evaluation board & PIC16LF1937)

Discussion in 'Embedded Systems and Microcontrollers' started by k1mzz, Jul 31, 2016.

  1. k1mzz

    Thread Starter New Member

    Jul 31, 2016
    7
    0
    Hi,

    I am new to PIC and C and have purchased a Microchip DV164132 evaluation kit (http://www.microchip.com/DevelopmentTools/ProductDetails.aspx?PartNO=dv164132). This is based on the PIC16LF1937 microcontroller.

    I am trying to get an output to respond to an input - when a button is pressed, I would like an LED to illuminate.

    To sum up the schematic: The switch is connected to RD2, which (from my interpretation) remains high unless the switch is pressed (where it goes low (GND) for the duration of the press).

    So what I want to do can be summed up as:

    do forever
    if RD2 = low
    RE0 -> 1
    else
    RE0 -> 0
    end

    So I have written the following:

    Code (C):
    1. #include <xc.h>
    2. // CONFIG1
    3. #pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
    4. #pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
    5. #pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
    6. #pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
    7. #pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
    8. #pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
    9. #pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
    10. #pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
    11. #pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
    12. #pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
    13.  
    14. // CONFIG2
    15. #pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
    16. #pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
    17. #pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
    18. #pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
    19. #pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
    20.  
    21. // #pragma config statements should precede project file includes.
    22. // Use project enums instead of #define for ON and OFF.
    23.  
    24. void main(void) {
    25.     //Set direction of ports using the TRIS register
    26.     TRISDbits.TRISD2=1;
    27.    
    28.     //PORTE - all output
    29.     TRISE = 0b00000000;
    30.    
    31.     //Set all bits on PORTE to 0
    32.     LATE = 0;
    33.    
    34.        
    35.     for(;;)
    36.     {
    37.         PORTEbits.RE0 = !PORTDbits.RD2;
    38.     }
    39.     return;
    40. }


    It compiles correctly, but the LED connected to RE0 remains illuminated irrespective of whether the button on RD2 is pressed or not.

    What am I doing wrong?

    Many thanks,
    Kim

    Mod edit: code tags
     
    Last edited by a moderator: Jul 31, 2016
  2. k1mzz

    Thread Starter New Member

    Jul 31, 2016
    7
    0
    Also, to add, I'm not concerned about debounce at the moment - I just want to see the button on an input do something!
     
  3. k1mzz

    Thread Starter New Member

    Jul 31, 2016
    7
    0
    Apologies for not using the code tag - forgot to save the edit within 10 minutes!
     
  4. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    You need to initialize your pins:

    Code (C):
    1. // make D2 digital input
    2. TRISDbits.TRISD2 = 1;
    3. ANSELDbits.ANSD2 = 0;
    4.  
    5. // make E0 an output
    6. TRISEbits.TRISE0 = 0;
     
  5. k1mzz

    Thread Starter New Member

    Jul 31, 2016
    7
    0
    Thanks for that; the line ANSELDbits.ANSD2 = 0 sorted this.

    Why would this be needed? I thought all inputs were digital by default?

    Cheers :)
     
  6. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    Usually not. This is because digital inputs waste power if they're not connected to anything.
     
  7. dannyf

    Well-Known Member

    Sep 13, 2015
    1,780
    360
    Reading the gpio section of the data sheet will answer that question.
     
    k1mzz likes this.
Loading...