illusion.00
#pragma config FOSC = ECH // Oscillator Selection (ECH, External Clock, High Power Mode (4-32 MHz): device clock supplied to CLKIN pin)
...
OSCCON = 0xF0; // Internal oscillator 8MHz and PLL enabled
Do you see why those two statements are incompatible?
illusion.00
#pragma config WDTE = ON // Watchdog Timer Enable (WDT enabled)
Turn off the watchdog timer until you are experienced enough to know why you want to use it. And then you still probably don't want to use it.
illusion.00
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
Do you have an external pullup resistor on the MCLR pin?
illusion.00
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
If you're not actually using Low Voltage Programming you should probably turn it off.
illusion.00
#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)
...
#define _XTAL_FREQ 8000000 // SET frequency as 8000000 bit because of the 8 MHz clock
...
OSCCON = 0xF0; // Internal oscillator 8MHz and PLL enabled
8MHz X 4xPLL = ? (hint: not 8MHz)
illusion.00
ADCON0 = 0; // Disable ADC module
ANSELAbits.ANSA2 = 0; // SET LED PIN to Digital
ANSELAbits.ANSA4 = 0; // SET Button pin to Digital
If you're just turning on an LED, turn off analog functions altogether:
ANSELA = 0;
ADCON0 = 0;
ADCON1 = 0;
CM1CON0 = 0; // these two lines aren't actually necessary
CM1CON1 = 0;
illusion.00
TRISAbits.TRISA2 = 0; // LED bit is output
TRISAbits.TRISA4 = 1; // Button bit is input
Don't leave inputs floating. Best to set everything to output and turn on the inputs you need:
TRISA = 0; // all output
TRISAbits.TRISA4 = 1; // Button bit is input
illusion.00
PORTAbits.RA4 = 0; // Button bit low initial
PORTAbits.RA2 = 0; // LED bit low initial
Search for the "Read-Modify-Write" problem. PIC's with data latches should use them.
Hence: Write to LATA, read from PORTA.
illusion.00
PORTAbits.RA4 = 0; // Button bit low initial
Why write to a pin that is set as INPUT?
post edited by upand_at_them - 2021/03/03 14:33:03