# How to toggle a pin in Harmony framework for a pic?

I'm new to pic's and the harmony framework, and i'm trying to toggle a pin from high to low and vice versa in the interrupt callback of the Timer.

I configured my oscillator to operate at 32 MHz

or at least is what appears here, so i suppose it's right:

Then i configured a Timer Driver, with prescale 256, so each tick is 8us:

Now my question, i want an interrupt to happen at each 8us, and i want to toggle a pin (RG0) at each interrupt. How can that be done?

I see what seems the interrupt code at the file system_interrupt.c:

But if inside that function i put the following line:

LATGINV = 0x00000001; nothing happens, i go check on the oscilloscope the signal and there is no toggle.

If i put the same line on main.c it works, the problems is that it doesn't seem to be accessible in system_interrupt.c

• You have to write code in callback function. It is not generated by Harmony as it is your application code. Jun 12 '19 at 10:11
• Hi @Swanand you mean the IntHandlerDrvTmrInstance0 function right? Jun 12 '19 at 10:15
• Yes! IntHandlerDrvTmrInstance0 Jun 12 '19 at 10:29
• @Swanand but is it a good solution to put code inside that function? It is code generated, every time i generate code i need to check what i need to keep inside, but maybe it's the only way. Jun 12 '19 at 10:32
• Why you want to generate code multiple times? You configure the Harmony, Generate basic code and then start writing your application. Jun 12 '19 at 11:26

There are two ways of configuring timers in harmony v2.

i.e. Static or Dynamic

For static timers you need to add your handling code into the system-interrupt.c file timer handler. See here

For dynamic timers you can specify a custom handler in your application code. See here. See 'Creating the ISR' and 'Registering the ISR' sections.

I would use the harmony timer system service, see here, which is a bit simpler implementation and similar to dynamic implementation but might result in less accurate timing due to extra software handling.

LATGINV is not typical usage.

There is pin toggle example code in this example.

To toggle your LATG 0 pin with harmony you may do the following

Initialization

PLIB_PORTS_PinDirectionOutputSet(PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_0);


Toggling

PLIB_PORTS_PinToggle(PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_0);


The microchip developer help is quite in depth and I'd recommend reading

• Hi @pm101 thanks for the tip. Still in system_interrupt.c it doesn't seem to work. the invert of the pin. Jun 12 '19 at 14:29
• LATGINV will not set a pin. Did you see PLIB_PORTS_PinDirectionOutputSet(PORTS_ID_0, PORT_CHANNEL_A, PORTS_BIT_POS_0); and PLIB_PORTS_PinToggle(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_0); in the timer system service example? Jun 12 '19 at 15:32
• i already tried using that functions but i will check again, about the LATGINV = 0x00000001 it works when i run in main, it seems valid. Maybe the problem is more than that, the interrupt probably is never called i need to understand why. Thanks. Jun 12 '19 at 15:41
• You had to learn to use the debugger, then check if the ISR is called!
– Mike
Jun 13 '19 at 5:36