Hi,
I am working Microchip DV164132 evaluation kit (http://www.microchip.com/DevelopmentTools/ProductDetails.aspx?PartNO=dv164132). This is based on the PIC16LF1937 microcontroller.
I am trying to perform a reaction time test - when an LED lights, the timer starts. When the button is pressed, the TMR0 overflow flag is checked; if the timer hasn't overflowed, you have been successful and a series of LEDs should light up indicating this.
My code is as follows (unsure as to how to define language - it's C!):
This works, however, the success output is only observed on one LED (STATUS1 (PORTDbits.PORTD1 / LED D1)). The other two (friendly names STATUS2 and STATUS3 briefly flash - not enough to be observed properly!).
It seems to me that PORTE, bits 1 and 2 are clearing themselves! Not sure why this is happening - I have set the direction of the ports up correctly and am sure the timer routine is configured correctly as PORTD, bit 1 is functioning correctly.
I am able to light all of the LEDs at once so I don't think it's a power supply issue.
Has anyone experienced anything similar? Thanks in advance.
I am working Microchip DV164132 evaluation kit (http://www.microchip.com/DevelopmentTools/ProductDetails.aspx?PartNO=dv164132). This is based on the PIC16LF1937 microcontroller.
I am trying to perform a reaction time test - when an LED lights, the timer starts. When the button is pressed, the TMR0 overflow flag is checked; if the timer hasn't overflowed, you have been successful and a series of LEDs should light up indicating this.
My code is as follows (unsure as to how to define language - it's C!):
Code:
#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)
//definition of internal oscillator frequency for use with delay function
#define _XTAL_FREQ 500000
//definition of friendly names for pins
#define STATUS1 PORTDbits.RD1 //LED D1
#define STATUS2 PORTEbits.RE2 //LED D2
#define STATUS3 PORTEbits.RE1 //LED D3
#define START PORTEbits.RE0 //LED D4
#define BUTTON PORTDbits.RD2 //BUTTON
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
void main(void) {
//Configure oscillator
OSCCONbits.SCS1 = 1;
OSCCONbits.IRCF = 0b0111;
//Configure timer
OPTION_REGbits.TMR0CS = 0; //Timer mode to instruction clock
OPTION_REGbits.PSA = 0; //Enable timer prescaler
OPTION_REGbits.PS = 111; //Timer prescaler to 1:256
//Set direction of RD2 to input
TRISDbits.TRISD2 = 1;
//Make RD2 a digital input
ANSELDbits.ANSD2 = 0;
//Set direction of RD1 to output
TRISDbits.TRISD1 = 0;
//Set direction of PORTE (all output)
TRISE = 0b00000000;
for(;;)
{
//Zero outputs
STATUS1 = 0;
STATUS2 = 0;
STATUS3 = 0;
START = 0;
//Delay before start
__delay_ms(2000);
if(BUTTON == 1) //if button is NOT pressed
{
START = 1; //indicate ready to start
TMR0 = 0; //reset timer0 register
INTCONbits.TMR0IF = 0; //reset timer0 overflow flag
while(BUTTON == 1); //wait until button low (button press)
if(INTCONbits.TMR0IF == 0)
{//indicate success if timer didn't overflow (within reaction time)
STATUS1 = 1;
STATUS2 = 1;
STATUS3 = 1;
}
START = 0;
}
//1sec delay so success can be observed
__delay_ms(1000);
//wait for button release
while(BUTTON == 0);
}
return;
}
It seems to me that PORTE, bits 1 and 2 are clearing themselves! Not sure why this is happening - I have set the direction of the ports up correctly and am sure the timer routine is configured correctly as PORTD, bit 1 is functioning correctly.
I am able to light all of the LEDs at once so I don't think it's a power supply issue.
Has anyone experienced anything similar? Thanks in advance.