{
return AD1CON1bits.DONE;
//Wait for conversion to complete
}
#define AN0_SetHigh() LATAbits.LATA0 = 1)
#define AN0_SetLow() (LATAbits.LATA0 = 0)
#define AN0_Toggle() (LATAbits.LATA0 ^= 1)
#define AN0_GetValue() PORTAbits.RA0
#define AN0_SetDigitalInput()
(TRISAbits.TRISA0 = 1)
#define AN0_SetDigitalOutput()
(TRISAbits.TRISA0 = 0)
#define BUTTON_SetHigh() (LATBbits.LATB12 = 1)
#define BUTTON_SetLow() (LATBbits.LATB12 = 0)
#define BUTTON_Toggle() (LATBbits.LATB12 ^= 1)
#define BUT TON_GetValue() PORTBbits.RB12
#define BUTTON_SetDigitalInput()
(TRISBbits.TRISB12 = 1)
#define BUTTON_SetDigitalOutput()
(TRISBbits.TRISB12 = 0)
#define LED_SetHigh() (LATBbits.LATB13 = 1)
#define LED_SetLow() (LATBbits.LATB13 = 0)
#define LED_Toggle() (LATBbits.LATB13 ^= 1)
#define LED_GetValue() PORTBbits.RB13
#define LED_SetDigitalInput()
(TRISBbits.TRISB13 = 1)
#define LED_SetDigitalOutput()
(TRISBbits.TRISB13 = 0)
The MPLAB Code Configurator is an equal
opportunity code generator. So, what it does for
peripherals, it also does for pins. Here is the driver code
for pins AN0, LED, and BUTTON:
Up to this point, we have produced
PIC32MM0064GLP028 start-up code and implemented a
UART and ADC. Thus far, we have not written a single line
of code.
GPIO Configuration Using the MPLAB
Code Configurator
Our original schematic is rather bare. So far, we
added a UART interface on pins RB14 and RB15. We
have also parked an analog input on RA0. What if we
wanted to hang an LED on RB13 and a pushbutton input
on RB12? If we use the Code Configurator to perform the
LED and button GPIO assignments, we won’t even break
a sweat.
Let’s start with the LED. The story is told in
Screenshot 5. We selected a free pin (in this case, RB13)
in the pin manager window. Note that the pin can be
selected as an input or output. Since we’re driving an LED,
the choice is output pin. Once the pin is selected, we can
finish the pin’s configuration in the pin module window.
As you can see in Screenshot 5, we’ve named the pin
LED, which is also reflected in the PIC32MM0064GLP028
graphic.
The pin driver definitions are obvious to the most
casual observer. However, the Code Configurator still
generates examples in the pin_manager.h file just as it did
for the ADC and UART peripherals. Here’s an example of
how to obtain the BUTTON pin’s logical status:
Configuring the pushbutton input pin follows a similar
process. Instead of selecting RB12 as an output in the pin
manager, we click on the input lock. You can see that I’ve
done just that in Screenshot 6.
uint16_t portValue;
// Read RB12
postValue = BUTTON_GetValue();
Timing is Everything
No matter how many
PIC32MM0064GLP028 peripherals or GPIO
pins we configure, sooner or later our
application will require the services of a timer.
Normally, the timer is used as a periodic
interrupt time base. Configuring a timer to
overflow and trigger an interrupt on a periodic
basis is a walk in the park when we include the
MPLAB Code Configurator in the process. The
interrupt period specified in Screenshot 7 is 10
mS.
■ SCREENSHOT 5. This is how it works. Pick out a free
PIC32MM0064GLP028 pin, declare it output or input in the Pin
Manager window, set the pin properties — including the pin name —
in the pin module window and click the Generate button. The Code
Configurator generates all of the drivers for the selected pin
automatically and places the code in your project.
When using the Code Configurator, no
manual computations are necessary to get the
correct timer overflow value for a 10 mS
periodic interrupt. Selecting a prescaler value
that allows the timer period limits to fall into
your interrupt period requirements is all that you
need to do. As you can see in Screenshot 7, a
prescaler value of 8 will allow a 10 mS timer
period. The overflow value you see in the Period
Count window is automatically calculated by the
Code Configurator.
54 September 2016