Ok, sorry I missed the title!
So just to be clear, you have a button on RA4, which is the default location of T1G. This button is active high, so you have a pull-down on this to make it 0 when no button is pressed. You set RA5 as an output, and this should indicate the state of T1G, but reading the T1GCON register and looking at its state.
When you say the code 'is working', that implies it is doing what you want it to do. I'll admit the timer-gate module of timer1 looks very confusing, but you only enter your toggling routine if T1GCONbits.T1GGO_nDONE is 0 here:
if (T1GCONbits.T1GGO_nDONE = 0) { //Resets bit, if done
T1GCONbits.T1GGO_nDONE = 1;
This bit is cleared by hardware, and must be set in software - as you are doing. However, the hardware only clears this bit on the falling of the timer gate pin (datasheet, page 149). Therefore your will only change the state of the output pin when the timer gate is 0. And because it reads the state of the gate, and sets the output pin to the same state, that will also always be 0.
It does this for 'single pulse mode' so the T1GGO_nDONE bit can indicate when an input pulse has ended - so you can check the contents of TMR1 to measure the pulse width.
In order to do what you wish to do, you would either have to fiddle about switching the polarity of T1G (so it inverts the input and see's a rising edge from your button, as a falling edge) OR... more simply:
In your loop, just constantly read the state of T1G. You could disable 'single pulse mode' as you're not actually measuring the input pulse width. But if you wish to keep it, you would have to set it to one, only when you read T1GVAL as 0 (falling edge on the gate, causes it to be cleared in hardware, and when its 0, T1GVAL will not change, even if the input does).
TMR1ON = 1; //Timer1 on - only need to do this once, outside of the main loop
while (1) {
if (T1GVAL == 1) { //Timer gate value high
LATAbits.LATA5 = 1;
}
else { //Timer gate value low
LATAbits.LATA5 = 0;
// if you've got single pulse mode enabled, set the T1GGO bit here, or it will ignore subsequent input changes.
T1GCONbits.T1GGO_nDONE = 1;
}
}
return;
long story short, its a complicated bit of hardware, but makes sense when you realize what it is designed for - to capture input pulses and measure their width, without subsequent pulses causing timer1 to increment before you have had time to read the TMR1 value. You can set the 'polarity' of the pulse (measuring rising to falling, or falling to rising), and 'arm' it by setting the T1GGO_nDONE bit, and once it has the pulse, it fires an interrupt, so you can read the contents of timer1, and 're-arm' it.