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

Thread Starter

k1mzz

Joined Jul 31, 2016
16
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:

C:
#include <xc.h>
// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

void main(void) {
    //Set direction of ports using the TRIS register
    TRISDbits.TRISD2=1;
   
    //PORTE - all output
    TRISE = 0b00000000;
   
    //Set all bits on PORTE to 0
    LATE = 0;
   
       
    for(;;)
    {
        PORTEbits.RE0 = !PORTDbits.RD2;
    }
    return;
}
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
 

Attachments

Last edited by a moderator:

Thread Starter

k1mzz

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

NorthGuy

Joined Jun 28, 2014
611
You need to initialize your pins:

C:
// make D2 digital input
TRISDbits.TRISD2 = 1;
ANSELDbits.ANSD2 = 0;

// make E0 an output
TRISEbits.TRISE0 = 0;
 

Thread Starter

k1mzz

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

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

Cheers :)
 
Top