/*********************************************************************
*
* PIC32MX PORT API Example Code
*
*********************************************************************
* FileName: ports_basic.c
* Dependencies: plib.h, attribs.h
*
* Processor: PIC32MX
*
* Complier: MPLAB C32
* MPLAB IDE
* Company: Microchip Technology Inc.
*
* Software License Agreement
*
* The software supplied herewith by Microchip Technology Incorporated
* (the “Company”) for its PIC32MX Microcontroller is intended
* and supplied to you, the Company’s customer, for use solely and
* exclusively on Microchip Microcontroller products.
* The software is owned by the Company and/or its supplier, and is
* protected under applicable copyright laws. All rights are reserved.
* Any use in violation of the foregoing restrictions may subject the
* user to criminal sanctions under applicable laws, as well as to
* civil liability for the breach of the terms and conditions of this
* license.
*
* THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*
*********************************************************************
* $Id: ports_basic.c 9390 2008-06-16 23:43:04Z rajbhartin $
*
* $Name: $
*********************************************************************
* The purpose of this example code is to demonstrate the PIC32MX
* peripheral library macros and functions supporting IOPORT
* module and its various features.
*
* Platform: PIC32MX Starter Kit
*
* Features demonstrated:
* - Port pin configurations
* - Output Pin toggle
* - Interrupt on Change Notice
* - Weak pullups
* - Interrupt configuration
* - ISR syntax
*
* Description:
* This example blinks LED D1 (PORTD.RD0) when SW1 (PORTD.RD6)
* is pressed, causing a Change Notice interrupt.
* occurs.
*
* Oscillator Configuration Bit Settings:
* - Oscillator Selection Bits = Primary Osc w/PLL (XT+HS+EC+PLL)
* - Primary Oscillator Config = HS osc mode
* - PLL Input Divider = 2x Divider
* - PLL Multiplier = 18x Multiplier
*
* Notes:
* - To clear a mis-match condition, always read the port first,
* then clear CNIF interrupt flag.
*
* - PIC32MX Starter Kit Led and Sw assignments:
* LED1 is on PORTD.RD0
* LED2 is on PORTD.RD1
* LED3 is on PORTD.RD2
* SW1 is on PORTD.RD6, Change Notice (CN15)
* SW2 is on PORTD.RD7, Change Notice (CN16)
* SW2 is on PORTD.RD13, Change Notice (CN19)
*
* - No resistor pullups are provided for SW1..SW3. PORTD internal
* pullups should be enabled.
*
********************************************************************/
#include <plib.h>
// Configuration Bit settings
// SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 40 MHz
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
//
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
#define CONFIG (CN_ON | CN_IDLE_CON)
#define PINS (CN15_ENABLE)
#define PULLUPS (CN15_PULLUP_ENABLE)
#define INTERRUPT (CHANGE_INT_ON | CHANGE_INT_PRI_2)
#define SYS_FREQ (80000000L)
int main(void)
{
unsigned int temp;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//STEP 1. Configure cache, wait states and peripheral bus clock
// Configure the device for maximum performance but do not change the PBDIV
// Given the options, this function will change the flash wait states, RAM
// wait state and enable prefetch cache but will not change the PBDIV.
// The PBDIV value is already set via the pragma FPBDIV option above..
SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STEP 2. configure the port registers
PORTSetPinsDigitalOut(IOPORT_D, BIT_0);
PORTSetPinsDigitalIn(IOPORT_D, BIT_6);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STEP 3. initialize the port pin states = outputs low
mPORTDClearBits(BIT_0);
// Note: It is recommended to disable vector interrupts prior to
// configuring the change notice module, if they are enabled.
// The user must read one or more IOPORTs to clear the IO pin
// change notice mismatch condition, then clear the change notice
// interrupt flag before re-enabling the vector interrupts.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STEP 4. enable change notice, enable discrete pins and weak pullups
mCNOpen(CONFIG, PINS, PULLUPS);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STEP 5. read port(s) to clear mismatch on change notice pins
temp = mPORTDRead();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STEP 6. clear change notice interrupt flag
ConfigIntCN(INTERRUPT);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STEP 7. enable multi-vector interrupts
INTEnableSystemMultiVectoredInt();
while(1)
{
// .. add things to do
};
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STEP 8. configure the CN interrupt handler
void __ISR(_CHANGE_NOTICE_VECTOR, ipl2) ChangeNotice_Handler(void)
{
unsigned int temp;
// clear the mismatch condition
temp = mPORTDRead();
// clear the interrupt flag
mCNClearIntFlag();
// .. things to do .. toggle the led
mPORTDToggleBits(BIT_0);
}