Reputation: 195
I'm using MPLABX XC16 on a PIC24F16KA102 and attempting to migrate from a PIC16 and mpasm.
A simple loop which sequences some LEDs should test a switch between each LED on/off and then branch out to GoSleep
if the switch is not closed.
As far as I can see the SwitchTest
function is never called.
void main(void) {
AD1PCFG = 0xFFFF; // All ports are digital
TRISA = 0x0041; // PORTA bit 0 and 6 input (port 0 = Select Button, port 6 = Wake INT)
__builtin_nop();
TRISB = 0x0000; // PORTB bit all outputs
__builtin_nop();
while (1) {
SwitchTest(); // if the switch is down cycle the LED's
led_step = 0b11000001; // and leave the last one selected on for 1 sec
Display = led_step; // mimic the selection in binary on LED's 5-7
LED1 = LED_ON; // turn on LED1
__delay_ms(2000);
LED1 = LED_OFF; // turn off LED1
SwitchTest();
led_step = 0b10100010;
Display = led_step;
LED2 = LED_ON; // turn on LED2
__delay_ms(2000);
LED2 = LED_OFF; // turn off LED2
}
}
void SwitchTest(void)
{
if (PORTAbits.RA0 == 0)
{ // Is the Status Button pressed (low = 0)
__delay_ms(25);
if (PORTAbits.RA0 == 0)
{
return; // Yes, resume main loop
}
}
else // No, Sleep
{
GoSleep();
}
}
External pullup resistor on RA0 and pin correctly toggles when switch is pushed/released.
Attempts to debug with correctly connected PICKit failed (states bits not set correctly but doesn't show which bits) and despite running the code (in MPLABX) this PIC doesn't retain the code when the PICKit/MPLABX is disconnected and external power is applied.
Upvotes: 0
Views: 35
Reputation: 195
I have just read an Errata for that PIC and it seems that there are major issues with RA0 and RB0 and other functions. Have just tested with RA1 and it seems to work. Serves me right for trying to migrate to more modern code and PICs.
Upvotes: 1